• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_bt_common.h"
22 #include "softbus_adapter_thread.h"
23 #include "softbus_conn_ble_trans.h"
24 #include "softbus_conn_interface.h"
25 #include "softbus_conn_manager.h"
26 #include "softbus_error_code.h"
27 #include "softbus_json_utils.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define SOFTBUS_SERVICE_UUID              "11C8B310-80E4-4276-AFC0-F81590B2177F"
34 #define SOFTBUS_CHARA_BLENET_UUID         "00002B00-0000-1000-8000-00805F9B34FB"
35 #define SOFTBUS_CHARA_BLECONN_UUID        "00002B01-0000-1000-8000-00805F9B34FB"
36 #define SOFTBUS_DESCRIPTOR_CONFIGURE_UUID "00002902-0000-1000-8000-00805F9B34FB"
37 
38 #define INVALID_UNDERLAY_HANDLE                   (-1)
39 #define NET_CTRL_MSG_TYPE_HEADER_SIZE             4
40 #define BLE_CLIENT_MAX_RETRY_SEARCH_SERVICE_TIMES 1
41 #define DEFAULT_MTU_SIZE                          512
42 
43 #define RETRY_SERVER_STATE_CONSISTENT_MILLIS      (3 * 1000)
44 #define BASIC_INFO_EXCHANGE_TIMEOUT               (5 * 1000)
45 #define UNDERLAY_CONNECTION_DISCONNECT_TIMEOUT    (5 * 1000)
46 #define WAIT_NEGOTIATION_CLOSING_TIMEOUT_MILLIS   (2 * 1000)
47 #define CONNECTION_IDLE_DISCONNECT_TIMEOUT_MILLIS (60 * 1000)
48 #define CLOSING_TIMEOUT_MILLIS                     200
49 
50 enum BleNetCtrlMsgType {
51     NET_CTRL_MSG_TYPE_UNKNOW = -1,
52     NET_CTRL_MSG_TYPE_AUTH = 0,
53     NET_CTRL_MSG_TYPE_BASIC_INFO = 1,
54     NET_CTRL_MSG_TYPE_DEV_INFO = 2,
55 };
56 
57 enum ConnBleConnectionState {
58     BLE_CONNECTION_STATE_CONNECTING = 0, // client connection init state
59     BLE_CONNECTION_STATE_CONNECTED,      // server connection init state
60     BLE_CONNECTION_STATE_SERVICE_SEARCHING,
61     BLE_CONNECTION_STATE_SERVICE_SEARCHED,
62     BLE_CONNECTION_STATE_CONN_NOTIFICATING,
63     BLE_CONNECTION_STATE_CONN_NOTIFICATED,
64     BLE_CONNECTION_STATE_NET_NOTIFICATING,
65     BLE_CONNECTION_STATE_NET_NOTIFICATED,
66     BLE_CONNECTION_STATE_MTU_SETTING,
67     BLE_CONNECTION_STATE_MTU_SETTED,
68     BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO,
69     BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO,
70     BLE_CONNECTION_STATE_NEGOTIATION_CLOSING,
71     BLE_CONNECTION_STATE_CLOSING,
72     BLE_CONNECTION_STATE_CLOSED,
73     BLE_CONNECTION_STATE_INVALID,
74 };
75 
76 enum ConnBleDisconnectReason {
77     BLE_DISCONNECT_REASON_CONNECT_TIMEOUT,
78     BLE_DISCONNECT_REASON_INTERNAL_ERROR,
79     BLE_DISCONNECT_REASON_NO_REFERENCE,
80     BLE_DISCONNECT_REASON_NEGOTIATION_NO_REFERENCE,
81     BLE_DISCONNECT_REASON_NEGOTIATION_WAIT_TIMEOUT,
82     BLE_DISCONNECT_REASON_IDLE_WAIT_TIMEOUT,
83     BLE_DISCONNECT_REASON_CONFLICT,
84     BLE_DISCONNECT_REASON_FORCELY,
85     BLE_DISCONNECT_REASON_POST_BYTES_FAILED,
86     BLE_DISCONNECT_REASON_RESET,
87 };
88 
89 enum ConnBleFeatureCapability {
90     BLE_FEATURE_SUPPORT_REMOTE_DISCONNECT = 1,
91     BLE_FEATURE_SUPPORT_DISCONNECT_BY_DEVICEID,
92     BLE_FEATURE_SUPPORT_SUPPORT_NETWORKID_BASICINFO_EXCAHNGE,
93 };
94 typedef uint32_t ConnBleFeatureBitSet;
95 
96 typedef struct {
97     SoftBusBtUuid serviceUuid;
98     SoftBusBtUuid connCharacteristicUuid;
99     SoftBusBtUuid netUuid;
100     SoftBusBtUuid descriptorUuid;
101 } GattService;
102 
103 typedef struct {
104     ListNode node;
105     BleProtocolType protocol;
106     uint32_t connectionId;
107     ConnSideType side;
108     bool fastestConnectEnable;
109     char addr[BT_MAC_LEN];
110     union {
111         int32_t psm;
112     };
113     // sequence is only read and modify in send thread, no need lock
114     uint32_t sequence;
115     // ble connection may be devide the data to several packet, so we should assemble them together
116     ConnBleReadBuffer buffer;
117 
118     // protect variable access below
119     SoftBusMutex lock;
120     enum ConnBleConnectionState state;
121     int32_t underlayerHandle;
122     uint32_t mtu;
123     char udid[UDID_BUF_LEN];
124     char networkId[NETWORK_ID_BUF_LEN];
125     ConnBleFeatureBitSet featureBitSet;
126     // reference counter that record times required by buziness
127     int32_t connectionRc;
128     // reference counter that record times for memory management
129     int32_t objectRc;
130 
131     // NOTICE: fields below are inner ones for helping connect progress, they are invalid after connection established
132     int32_t retrySearchServiceCnt;
133 
134     GattServiceType serviceId;
135     GattService gattService;
136     uint32_t expectedMtuSize;
137 } ConnBleConnection;
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)(GattService *servce, GattServiceType serviceId);
172     int32_t (*bleServerStopService)(GattServiceType serviceId);
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)(
177         SoftBusLooper *looper, const ConnBleClientEventListener *listener, GattServiceType serviceId);
178     int32_t (*bleServerInitModule)(
179         SoftBusLooper *looper, const ConnBleServerEventListener *listener, GattServiceType serviceId);
180 } BleUnifyInterface;
181 
182 ConnBleConnection *ConnBleCreateConnection(
183     const char *addr, BleProtocolType protocol, ConnSideType side, int32_t underlayerHandle, bool fastestConnectEnable);
184 void ConnBleFreeConnection(ConnBleConnection *connection);
185 int32_t ConnBleStartServer(void);
186 int32_t ConnBleStopServer(void);
187 int32_t ConnBleConnect(ConnBleConnection *connection);
188 int32_t ConnBleDisconnectNow(ConnBleConnection *connection, enum ConnBleDisconnectReason reason);
189 int32_t ConnBleUpdateConnectionRc(ConnBleConnection *connection, uint16_t challengeCode, int32_t delta);
190 int32_t ConnBleOnReferenceRequest(ConnBleConnection *connection, const cJSON *json);
191 int32_t ConnBleUpdateConnectionPriority(ConnBleConnection *connection, ConnectBlePriority priority);
192 int32_t ConnBleSend(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module);
193 // connection will be disconnected forcely when idle more than CONNECTION_IDLE_DISCONNECT_TIMEOUT_MILLIS
194 void ConnBleRefreshIdleTimeout(ConnBleConnection *connection);
195 
196 // complement connection device id
197 // NOTICE: MUST ONLY used in ble connection inner module
198 void ConnBleInnerComplementDeviceId(ConnBleConnection *connection);
199 
200 int32_t ConnBleInitConnectionMudule(SoftBusLooper *looper, ConnBleConnectionEventListener *listener);
201 
202 void ReturnConnection(GattServiceType serviceId, ConnBleConnection *connection);
203 #ifdef __cplusplus
204 }
205 #endif /* __cplusplus */
206 #endif /* SOFTBUS_CONN_BLE_CONNECTION_H */