1 /* 2 * Copyright (c) 2023 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_MANAGER_H 17 #define SOFTBUS_CONN_BLE_MANAGER_H 18 19 #include <semaphore.h> 20 21 #include "common_list.h" 22 #include "softbus_conn_ble_connection.h" 23 #include "softbus_conn_interface.h" 24 #include "softbus_conn_manager.h" 25 #include "softbus_error_code.h" 26 #include "softbus_hisysevt_connreporter.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 #define BLE_CONNECT_TIMEOUT_MILLIS (10 * 1000) 33 #define BLE_CONNECT_KEEP_ALIVE_TIMEOUT_MILLIS (10 * 1000) 34 #define BLE_GATT_CONNECT_MAX_RETRY_COUNT (2) 35 36 enum ConnBleDeviceState { 37 BLE_DEVICE_STATE_INIT, 38 BLE_DEVICE_STATE_WAIT_EVENT, 39 BLE_DEVICE_STATE_WAIT_SCHEDULE, 40 BLE_DEVICE_STATE_SCHEDULING, 41 }; 42 43 typedef struct { 44 ListNode node; 45 // SHOULD diff requests by protocol type 46 BleProtocolType protocol; 47 char addr[BT_MAC_LEN]; 48 // ble address will change over time and different advertisement, so devive is identified by udid first 49 char udid[UDID_BUF_LEN]; 50 bool fastestConnectEnable; 51 uint32_t psm; 52 bool isSupportNetworkIdExchange; 53 enum ConnBleDeviceState state; 54 ListNode requests; 55 // The ble connection failed to scan the broadcast due to a timeout of 3.1 seconds and was retried 56 uint32_t retryCount; 57 } ConnBleDevice; 58 59 typedef struct { 60 ListNode node; 61 // SHOULD diff requests by protocol type 62 BleProtocolType protocol; 63 uint32_t requestId; 64 uint16_t challengeCode; /* for ble direct */ 65 ConnectResult result; 66 ConnectStatistics statistics; 67 } ConnBleRequest; 68 69 typedef struct { 70 BleProtocolType protocol; 71 uint32_t requestId; 72 char addr[BT_MAC_LEN]; 73 char udid[UDID_BUF_LEN]; 74 bool fastestConnectEnable; 75 uint32_t psm; 76 uint16_t challengeCode; /* for ble direct */ 77 ConnectResult result; 78 ConnectStatistics statistics; 79 } ConnBleConnectRequestContext; 80 81 typedef struct { 82 uint32_t connectionId; 83 bool isConnCharacteristic; 84 uint8_t *data; 85 uint32_t dataLen; 86 } ConnBleDataReceivedContext; 87 88 typedef struct { 89 char addr[BT_MAC_LEN]; 90 char udid[UDID_BUF_LEN]; 91 uint32_t requestId; 92 ProtocolType protocol; 93 } ConnBleReuseConnectionContext; 94 95 typedef struct { 96 char *(*name)(void); 97 void (*enter)(void); 98 void (*exit)(void); 99 void (*connectRequest)(const ConnBleConnectRequestContext *ctx); 100 void (*handlePendingRequest)(void); 101 void (*serverAccepted)(uint32_t connectionId); 102 void (*clientConnected)(uint32_t connectionId); 103 void (*clientConnectFailed)(uint32_t connectionId, int32_t error); 104 void (*clientConnectTimeout)(uint32_t connectionId, const char *address); 105 void (*dataReceived)(ConnBleDataReceivedContext *ctx); 106 void (*connectionClosed)(uint32_t connectionId, int32_t error); 107 void (*connectionResume)(uint32_t connectionId); 108 void (*disconnectRequest)(uint32_t connectionId); 109 int32_t (*reuseConnectionRequest)(const ConnBleReuseConnectionContext *ctx); 110 void (*preventTimeout)(const char *udid); 111 void (*reset)(int32_t reason); 112 void (*keepAliveTimeout)(uint32_t connectionId, uint32_t requestId); 113 } ConnBleState; 114 115 int32_t ConnBleSaveConnection(ConnBleConnection *connection); 116 void ConnBleRemoveConnection(ConnBleConnection *connection); 117 ConnBleConnection *ConnBleGetConnectionByAddr(const char *addr, ConnSideType side, BleProtocolType protocol); 118 ConnBleConnection *ConnBleGetConnectionById(uint32_t connectionId); 119 ConnBleConnection *ConnBleGetConnectionByHandle(int32_t underlayerHandle, ConnSideType side, BleProtocolType protocol); 120 // get connection with different address and same udid 121 ConnBleConnection *ConnBleGetConnectionByUdid(const char *addr, const char *udid, BleProtocolType protocol); 122 // get connection with same udid and client side 123 ConnBleConnection *ConnBleGetClientConnectionByUdid(const char *udid, BleProtocolType protocol); 124 void ConnBleReturnConnection(ConnBleConnection **connection); 125 void NotifyReusedConnected(uint32_t connectionId, uint16_t challengeCode); 126 int32_t ConnBleKeepAlive(uint32_t connectionId, uint32_t requestId, uint32_t time); 127 int32_t ConnBleRemoveKeepAlive(uint32_t connectionId, uint32_t requestId); 128 129 ConnectFuncInterface *ConnInitBle(const ConnectCallback *callback); 130 131 #ifdef __cplusplus 132 } 133 #endif /* __cplusplus */ 134 #endif /* SOFTBUS_CONN_BLE_MANAGER_H */