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 SOFTBUS_CONN_BLE_CONNECTION_H 17 #define SOFTBUS_CONN_BLE_CONNECTION_H 18 19 #include "common_list.h" 20 #include "message_handler.h" 21 #include "softbus_adapter_thread.h" 22 #include "softbus_conn_ble_trans.h" 23 #include "softbus_conn_interface.h" 24 #include "softbus_conn_manager.h" 25 #include "softbus_error_code.h" 26 #include "softbus_json_utils.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 #define SOFTBUS_SERVICE_UUID "11C8B310-80E4-4276-AFC0-F81590B2177F" 33 #define SOFTBUS_CHARA_BLENET_UUID "00002B00-0000-1000-8000-00805F9B34FB" 34 #define SOFTBUS_CHARA_BLECONN_UUID "00002B01-0000-1000-8000-00805F9B34FB" 35 #define SOFTBUS_DESCRIPTOR_CONFIGURE_UUID "00002902-0000-1000-8000-00805F9B34FB" 36 37 #define INVALID_UNDERLAY_HANDLE (-1) 38 #define NET_CTRL_MSG_TYPE_HEADER_SIZE 4 39 #define BLE_CLIENT_MAX_RETRY_SEARCH_SERVICE_TIMES 1 40 41 #define RETRY_SERVER_STATE_CONSISTENT_MILLIS (3 * 1000) 42 #define BASIC_INFO_EXCHANGE_TIMEOUT (5 * 1000) 43 #define UNDERLAY_CONNECTION_DISCONNECT_TIMEOUT (5 * 1000) 44 #define WAIT_NEGOTIATION_CLOSING_TIMEOUT_MILLIS (2 * 1000) 45 #define CONNECTION_IDLE_DISCONNECT_TIMEOUT_MILLIS (60 * 1000) 46 #define CLOSING_TIMEOUT_MILLIS 200 47 #define DEFAULT_MTU_SIZE 512 48 49 enum BleNetCtrlMsgType { 50 NET_CTRL_MSG_TYPE_UNKNOW = -1, 51 NET_CTRL_MSG_TYPE_AUTH = 0, 52 NET_CTRL_MSG_TYPE_BASIC_INFO = 1, 53 NET_CTRL_MSG_TYPE_DEV_INFO = 2, 54 }; 55 56 enum ConnBleConnectionState { 57 BLE_CONNECTION_STATE_CONNECTING = 0, // client connection init state 58 BLE_CONNECTION_STATE_CONNECTED, // server connection init state 59 BLE_CONNECTION_STATE_SERVICE_SEARCHING, 60 BLE_CONNECTION_STATE_SERVICE_SEARCHED, 61 BLE_CONNECTION_STATE_CONN_NOTIFICATING, 62 BLE_CONNECTION_STATE_CONN_NOTIFICATED, 63 BLE_CONNECTION_STATE_NET_NOTIFICATING, 64 BLE_CONNECTION_STATE_NET_NOTIFICATED, 65 BLE_CONNECTION_STATE_MTU_SETTING, 66 BLE_CONNECTION_STATE_MTU_SETTED, 67 BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO, 68 BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO, 69 BLE_CONNECTION_STATE_NEGOTIATION_CLOSING, 70 BLE_CONNECTION_STATE_CLOSING, 71 BLE_CONNECTION_STATE_CLOSED, 72 BLE_CONNECTION_STATE_INVALID, 73 }; 74 75 enum ConnBleDisconnectReason { 76 BLE_DISCONNECT_REASON_CONNECT_TIMEOUT, 77 BLE_DISCONNECT_REASON_INTERNAL_ERROR, 78 BLE_DISCONNECT_REASON_NO_REFERENCE, 79 BLE_DISCONNECT_REASON_NEGOTIATION_NO_REFERENCE, 80 BLE_DISCONNECT_REASON_NEGOTIATION_WAIT_TIMEOUT, 81 BLE_DISCONNECT_REASON_IDLE_WAIT_TIMEOUT, 82 BLE_DISCONNECT_REASON_CONFLICT, 83 BLE_DISCONNECT_REASON_FORCELY, 84 BLE_DISCONNECT_REASON_POST_BYTES_FAILED, 85 BLE_DISCONNECT_REASON_RESET, 86 }; 87 88 enum ConnBleFeatureCapability { 89 BLE_FEATURE_SUPPORT_REMOTE_DISCONNECT = 1, 90 BLE_FEATURE_SUPPORT_DISCONNECT_BY_DEVICEID, 91 BLE_FEATURE_SUPPORT_SUPPORT_NETWORKID_BASICINFO_EXCAHNGE, 92 }; 93 typedef uint32_t ConnBleFeatureBitSet; 94 95 typedef struct { 96 ListNode node; 97 BleProtocolType protocol; 98 uint32_t connectionId; 99 ConnSideType side; 100 bool fastestConnectEnable; 101 char addr[BT_MAC_LEN]; 102 union { 103 uint32_t psm; 104 }; 105 // sequence is only read and modify in send thread, no need lock 106 uint32_t sequence; 107 // ble connection may be devide the data to several packet, so we should assemble them together 108 ConnBleReadBuffer buffer; 109 110 // protect variable access below 111 SoftBusMutex lock; 112 enum ConnBleConnectionState state; 113 int32_t underlayerHandle; 114 uint32_t mtu; 115 char udid[UDID_BUF_LEN]; 116 char networkId[NETWORK_ID_BUF_LEN]; 117 ConnBleFeatureBitSet featureBitSet; 118 // reference counter that record times required by buziness 119 int32_t connectionRc; 120 // reference counter that record times for memory management 121 int32_t objectRc; 122 123 // NOTICE: fields below are inner ones for helping connect progress, they are invalid after connection established 124 int32_t retrySearchServiceCnt; 125 SoftBusList *connectStatus; 126 127 // ble Quick connection fails due to scan failures 128 bool underlayerFastConnectFailedScanFailure; 129 130 bool isOccupied; 131 } ConnBleConnection; 132 133 typedef struct { 134 ListNode node; 135 int32_t result; 136 int32_t status; 137 } BleUnderlayerStatus; 138 139 typedef struct { 140 void (*onServerAccepted)(uint32_t connectionId); 141 void (*onConnected)(uint32_t connectionId); 142 void (*onConnectFailed)(uint32_t connectionId, int32_t error); 143 void (*onDataReceived)(uint32_t connectionId, bool isConnCharacteristic, uint8_t *data, uint32_t dataLen); 144 void (*onConnectionClosed)(uint32_t connectionId, int32_t status); 145 void (*onConnectionResume)(uint32_t connectionId); 146 } ConnBleConnectionEventListener; 147 148 // client unify listener 149 typedef struct { 150 void (*onClientConnected)(uint32_t connectionId); 151 void (*onClientFailed)(uint32_t connectionId, int32_t error); 152 void (*onClientDataReceived)(uint32_t connectionId, bool isConnCharacteristic, uint8_t *data, uint32_t dataLen); 153 void (*onClientConnectionClosed)(uint32_t connectionId, int32_t status); 154 } ConnBleClientEventListener; 155 156 // server unify listener 157 typedef struct { 158 void (*onServerStarted)(BleProtocolType protocol, int32_t status); 159 void (*onServerClosed)(BleProtocolType protocol, int32_t status); 160 void (*onServerAccepted)(uint32_t connectionId); 161 void (*onServerDataReceived)(uint32_t connectionId, bool isConnCharacteristic, uint8_t *data, uint32_t dataLen); 162 void (*onServerConnectionClosed)(uint32_t connectionId, int32_t status); 163 } ConnBleServerEventListener; 164 165 // gatt and coc SHOULD implement 166 typedef struct { 167 int32_t (*bleClientConnect)(ConnBleConnection *connection); 168 int32_t (*bleClientDisconnect)(ConnBleConnection *connection, bool grace, bool refreshGatt); 169 int32_t (*bleClientSend)(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module); 170 int32_t (*bleClientUpdatePriority)(ConnBleConnection *connection, ConnectBlePriority priority); 171 int32_t (*bleServerStartService)(void); 172 int32_t (*bleServerStopService)(void); 173 int32_t (*bleServerSend)(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module); 174 int32_t (*bleServerConnect)(ConnBleConnection *connection); 175 int32_t (*bleServerDisconnect)(ConnBleConnection *connection); 176 int32_t (*bleClientInitModule)(SoftBusLooper *looper, const ConnBleClientEventListener *listener); 177 int32_t (*bleServerInitModule)(SoftBusLooper *looper, const ConnBleServerEventListener *listener); 178 } BleUnifyInterface; 179 180 ConnBleConnection *ConnBleCreateConnection( 181 const char *addr, BleProtocolType protocol, ConnSideType side, int32_t underlayerHandle, bool fastestConnectEnable); 182 void ConnBleFreeConnection(ConnBleConnection *connection); 183 int32_t ConnBleStartServer(void); 184 int32_t ConnBleStopServer(void); 185 int32_t ConnBleConnect(ConnBleConnection *connection); 186 int32_t ConnBleDisconnectNow(ConnBleConnection *connection, enum ConnBleDisconnectReason reason); 187 int32_t ConnBleUpdateConnectionRc(ConnBleConnection *connection, uint16_t challengeCode, int32_t delta); 188 int32_t ConnBleOnReferenceRequest(ConnBleConnection *connection, const cJSON *json); 189 int32_t ConnBleUpdateConnectionPriority(ConnBleConnection *connection, ConnectBlePriority priority); 190 int32_t ConnBleSend(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module); 191 // connection will be disconnected forcely when idle more than CONNECTION_IDLE_DISCONNECT_TIMEOUT_MILLIS 192 void ConnBleRefreshIdleTimeout(ConnBleConnection *connection); 193 void ConnBleOccupy(ConnBleConnection *connection); 194 195 // complement connection device id 196 // NOTICE: MUST ONLY used in ble connection inner module 197 void ConnBleInnerComplementDeviceId(ConnBleConnection *connection); 198 199 int32_t ConnBleInitConnectionMudule(SoftBusLooper *looper, ConnBleConnectionEventListener *listener); 200 201 #ifdef __cplusplus 202 } 203 #endif /* __cplusplus */ 204 #endif /* SOFTBUS_CONN_BLE_CONNECTION_H */