1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CONN_BR_CONNECTION_H 17 #define CONN_BR_CONNECTION_H 18 19 #include "message_handler.h" 20 #include "softbus_conn_interface.h" 21 #include "softbus_json_utils.h" 22 #include "wrapper_br_interface.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 #define INVALID_SOCKET_HANDLE (-1) 29 #define MAX_BR_READ_BUFFER_CAPACITY (40 * 1000) 30 #define MAX_BR_MTU_SIZE (3 * 1024) 31 32 #define WAIT_BR_NEGOTIATION_CLOSING_TIMEOUT_MILLIS (3 * 1000) 33 #define RETRY_NOTIFY_REFERENCE_DELAY_MILLIS (1 * 1000) 34 35 #define MAX_RETRY_COUNT (2) 36 37 enum ConnBrConnectionState { 38 BR_CONNECTION_STATE_CONNECTING = 0, 39 BR_CONNECTION_STATE_CONNECTED, 40 BR_CONNECTION_STATE_EXCEPTION, 41 BR_CONNECTION_STATE_NEGOTIATION_CLOSING, 42 BR_CONNECTION_STATE_CLOSING, 43 BR_CONNECTION_STATE_CLOSED, 44 BR_CONNECTION_STATE_INVALID, 45 }; 46 47 typedef struct { 48 ListNode node; 49 uint32_t connectionId; 50 ConnSideType side; 51 char addr[BT_MAC_LEN]; 52 uint32_t mtu; 53 54 uint32_t retryCount; 55 // protect variable access below 56 SoftBusMutex lock; 57 int32_t socketHandle; 58 enum ConnBrConnectionState state; 59 // reference counter that record times required by buziness 60 int32_t connectionRc; 61 // reference counter that record times for memory management 62 int32_t objectRc; 63 64 bool isOccupied; 65 // congestion control 66 int32_t window; 67 int64_t sequence; 68 int64_t waitSequence; 69 int32_t ackTimeoutCount; 70 // connect process status 71 SoftBusList *connectProcessStatus; 72 } ConnBrConnection; 73 74 typedef struct { 75 void (*onServerAccepted)(uint32_t connectionId); 76 void (*onClientConnected)(uint32_t connectionId); 77 void (*onClientConnectFailed)(uint32_t connectionId, int32_t error); 78 void (*onDataReceived)(uint32_t connectionId, uint8_t *data, uint32_t dataLen); 79 void (*onConnectionException)(uint32_t connectionId, int32_t error); 80 void (*onConnectionResume)(uint32_t connectionId); 81 } ConnBrEventListener; 82 83 ConnBrConnection *ConnBrCreateConnection(const char *addr, ConnSideType side, int32_t socketHandle); 84 void ConnBrFreeConnection(ConnBrConnection *connection); 85 86 int32_t ConnBrUpdateConnectionRc(ConnBrConnection *connection, int32_t delta); 87 int32_t ConnBrOnReferenceRequest(ConnBrConnection *connection, const cJSON *json); 88 int32_t ConnBrOnReferenceResponse(ConnBrConnection *connection, const cJSON *json); 89 int32_t ConnBrConnect(ConnBrConnection *connection); 90 int32_t ConnBrDisconnectNow(ConnBrConnection *connection); 91 int32_t ConnBrStartServer(void); 92 int32_t ConnBrStopServer(void); 93 94 void ConnBrOccupy(ConnBrConnection *connection); 95 96 int32_t ConnBrConnectionMuduleInit(SoftBusLooper *looper, SppSocketDriver *sppDriver, ConnBrEventListener *listener); 97 98 #ifdef __cplusplus 99 } 100 #endif /* __clpusplus */ 101 #endif /* CONN_BR_CONNECTION_H */ 102