1 /* 2 * Copyright (c) 2025 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_STRUCT_H 17 #define SOFTBUS_CONN_BLE_CONNECTION_STRUCT_H 18 19 #include "common_list.h" 20 #include "message_handler.h" 21 #include "softbus_adapter_thread.h" 22 #include "softbus_common.h" 23 #include "softbus_conn_interface_struct.h" 24 #include "softbus_error_code.h" 25 #include "softbus_json_utils.h" 26 #include "softbus_conn_ble_trans_struct.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 #define WAIT_NEGOTIATION_CLOSING_TIMEOUT_MILLIS 5500 41 42 #define RETRY_SERVER_STATE_CONSISTENT_MILLIS (3 * 1000) 43 #define BASIC_INFO_EXCHANGE_TIMEOUT (5 * 1000) 44 #define UNDERLAY_CONNECTION_DISCONNECT_TIMEOUT (5 * 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 isNeedSetIdleTimeout; 131 bool isOccupied; 132 } ConnBleConnection; 133 134 typedef struct { 135 ListNode node; 136 int32_t result; 137 int32_t status; 138 } BleUnderlayerStatus; 139 140 typedef struct { 141 void (*onServerAccepted)(uint32_t connectionId); 142 void (*onConnected)(uint32_t connectionId); 143 void (*onConnectFailed)(uint32_t connectionId, int32_t error); 144 void (*onDataReceived)(uint32_t connectionId, bool isConnCharacteristic, uint8_t *data, uint32_t dataLen); 145 void (*onConnectionClosed)(uint32_t connectionId, int32_t status); 146 void (*onConnectionResume)(uint32_t connectionId); 147 } ConnBleConnectionEventListener; 148 149 // client unify listener 150 typedef struct { 151 void (*onClientConnected)(uint32_t connectionId); 152 void (*onClientFailed)(uint32_t connectionId, int32_t error); 153 void (*onClientDataReceived)(uint32_t connectionId, bool isConnCharacteristic, uint8_t *data, uint32_t dataLen); 154 void (*onClientConnectionClosed)(uint32_t connectionId, int32_t status); 155 } ConnBleClientEventListener; 156 157 // server unify listener 158 typedef struct { 159 void (*onServerStarted)(BleProtocolType protocol, int32_t status); 160 void (*onServerClosed)(BleProtocolType protocol, int32_t status); 161 void (*onServerAccepted)(uint32_t connectionId); 162 void (*onServerDataReceived)(uint32_t connectionId, bool isConnCharacteristic, uint8_t *data, uint32_t dataLen); 163 void (*onServerConnectionClosed)(uint32_t connectionId, int32_t status); 164 } ConnBleServerEventListener; 165 166 // gatt and coc SHOULD implement 167 typedef struct { 168 int32_t (*bleClientConnect)(ConnBleConnection *connection); 169 int32_t (*bleClientDisconnect)(ConnBleConnection *connection, bool grace, bool refreshGatt); 170 int32_t (*bleClientSend)(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module); 171 int32_t (*bleClientUpdatePriority)(ConnBleConnection *connection, ConnectBlePriority priority); 172 int32_t (*bleServerStartService)(void); 173 int32_t (*bleServerStopService)(void); 174 int32_t (*bleServerSend)(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module); 175 int32_t (*bleServerConnect)(ConnBleConnection *connection); 176 int32_t (*bleServerDisconnect)(ConnBleConnection *connection); 177 int32_t (*bleClientInitModule)(SoftBusLooper *looper, const ConnBleClientEventListener *listener); 178 int32_t (*bleServerInitModule)(SoftBusLooper *looper, const ConnBleServerEventListener *listener); 179 } BleUnifyInterface; 180 181 #ifdef __cplusplus 182 } 183 #endif /* __cplusplus */ 184 #endif /* SOFTBUS_CONN_BLE_CONNECTION_STRUCT_H */