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 <stdint.h> 20 21 #include "message_handler.h" 22 #include "softbus_conn_interface.h" 23 #include "softbus_error_code.h" 24 #include "softbus_json_utils.h" 25 #include "wrapper_br_interface.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #define INVALID_SOCKET_HANDLE (-1) 32 #define MAX_BR_READ_BUFFER_CAPACITY (40 * 1000) 33 #define MAX_BR_MTU_SIZE (3 * 1024) 34 35 #define WAIT_BR_NEGOTIATION_CLOSING_TIMEOUT_MILLIS (3 * 1000) 36 #define RETRY_NOTIFY_REFERENCE_DELAY_MILLIS (1 * 1000) 37 38 #define MAX_RETRY_COUNT (2) 39 40 enum ConnBrConnectionState { 41 BR_CONNECTION_STATE_CONNECTING = 0, 42 BR_CONNECTION_STATE_CONNECTED, 43 BR_CONNECTION_STATE_EXCEPTION, 44 BR_CONNECTION_STATE_NEGOTIATION_CLOSING, 45 BR_CONNECTION_STATE_CLOSING, 46 BR_CONNECTION_STATE_CLOSED, 47 BR_CONNECTION_STATE_INVALID, 48 }; 49 50 typedef struct { 51 ListNode node; 52 uint32_t connectionId; 53 ConnSideType side; 54 char addr[BT_MAC_LEN]; 55 uint32_t mtu; 56 57 uint32_t retryCount; 58 // protect variable access below 59 SoftBusMutex lock; 60 int32_t socketHandle; 61 enum ConnBrConnectionState state; 62 // reference counter that record times required by buziness 63 int32_t connectionRc; 64 // reference counter that record times for memory management 65 int32_t objectRc; 66 67 bool isOccupied; 68 // congestion control 69 int32_t window; 70 int64_t sequence; 71 int64_t waitSequence; 72 int32_t ackTimeoutCount; 73 // connect process status 74 SoftBusList *connectProcessStatus; 75 } ConnBrConnection; 76 77 typedef struct { 78 void (*onServerAccepted)(uint32_t connectionId); 79 void (*onClientConnected)(uint32_t connectionId); 80 void (*onClientConnectFailed)(uint32_t connectionId, int32_t error); 81 void (*onDataReceived)(uint32_t connectionId, uint8_t *data, uint32_t dataLen); 82 void (*onConnectionException)(uint32_t connectionId, int32_t error); 83 void (*onConnectionResume)(uint32_t connectionId); 84 } ConnBrEventListener; 85 86 ConnBrConnection *ConnBrCreateConnection(const char *addr, ConnSideType side, int32_t socketHandle); 87 void ConnBrFreeConnection(ConnBrConnection *connection); 88 89 int32_t ConnBrUpdateConnectionRc(ConnBrConnection *connection, int32_t delta); 90 int32_t ConnBrOnReferenceRequest(ConnBrConnection *connection, const cJSON *json); 91 int32_t ConnBrOnReferenceResponse(ConnBrConnection *connection, const cJSON *json); 92 int32_t ConnBrConnect(ConnBrConnection *connection); 93 int32_t ConnBrDisconnectNow(ConnBrConnection *connection); 94 int32_t ConnBrStartServer(void); 95 int32_t ConnBrStopServer(void); 96 97 void ConnBrOccupy(ConnBrConnection *connection); 98 int32_t ConnBrConnectionMuduleInit(SoftBusLooper *looper, SppSocketDriver *sppDriver, ConnBrEventListener *listener); 99 100 #ifdef __cplusplus 101 } 102 #endif /* __clpusplus */ 103 #endif /* CONN_BR_CONNECTION_H */ 104