• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MANAGER_STRUCT_H
17 #define SOFTBUS_CONN_BLE_MANAGER_STRUCT_H
18 
19 #include <semaphore.h>
20 
21 #include "common_list.h"
22 #include "softbus_common.h"
23 #include "softbus_conn_interface_struct.h"
24 #include "legacy/softbus_hisysevt_connreporter.h"
25 #include "softbus_protocol_def.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define BLE_CONNECT_TIMEOUT_MILLIS (10 * 1000)
32 #define BLE_CONNECT_KEEP_ALIVE_TIMEOUT_MILLIS (10 * 1000)
33 #define BLE_GATT_CONNECT_MAX_RETRY_COUNT (2)
34 
35 enum ConnBleDeviceState {
36     BLE_DEVICE_STATE_INIT,
37     BLE_DEVICE_STATE_WAIT_EVENT,
38     BLE_DEVICE_STATE_WAIT_SCHEDULE,
39     BLE_DEVICE_STATE_SCHEDULING,
40 };
41 
42 typedef struct {
43     ListNode node;
44     // SHOULD diff requests by protocol type
45     BleProtocolType protocol;
46     char addr[BT_MAC_LEN];
47     // ble address will change over time and different advertisement, so devive is identified by udid first
48     char udid[UDID_BUF_LEN];
49     bool fastestConnectEnable;
50     uint32_t psm;
51     bool isSupportNetworkIdExchange;
52     enum ConnBleDeviceState state;
53     ListNode requests;
54     // The ble connection failed to scan the broadcast due to a timeout of 3.1 seconds and was retried
55     uint32_t retryCount;
56 } ConnBleDevice;
57 
58 typedef struct {
59     ListNode node;
60     // SHOULD diff requests by protocol type
61     BleProtocolType protocol;
62     uint32_t requestId;
63     uint16_t challengeCode; /* for ble direct */
64     ConnectResult result;
65     ConnectStatistics statistics;
66 } ConnBleRequest;
67 
68 typedef struct {
69     char addr[BT_MAC_LEN];
70     char udid[UDID_BUF_LEN];
71     bool fastestConnectEnable;
72     BleProtocolType protocol;
73     uint32_t requestId;
74     uint32_t psm;
75     uint16_t challengeCode; /* for ble direct */
76     ConnectResult result;
77     ConnectStatistics statistics;
78 } ConnBleConnectRequestContext;
79 
80 typedef struct {
81     uint32_t connectionId;
82     bool isConnCharacteristic;
83     uint8_t *data;
84     uint32_t dataLen;
85 } ConnBleDataReceivedContext;
86 
87 typedef struct {
88     char addr[BT_MAC_LEN];
89     char udid[UDID_BUF_LEN];
90     uint32_t requestId;
91     ProtocolType protocol;
92 } ConnBleReuseConnectionContext;
93 
94 typedef struct {
95     char *(*name)(void);
96     void (*enter)(void);
97     void (*exit)(void);
98     void (*connectRequest)(const ConnBleConnectRequestContext *ctx);
99     void (*handlePendingRequest)(void);
100     void (*serverAccepted)(uint32_t connectionId);
101     void (*clientConnected)(uint32_t connectionId);
102     void (*clientConnectFailed)(uint32_t connectionId, int32_t error);
103     void (*clientConnectTimeout)(uint32_t connectionId, const char *address);
104     void (*dataReceived)(ConnBleDataReceivedContext *ctx);
105     void (*connectionClosed)(uint32_t connectionId, int32_t error);
106     void (*connectionResume)(uint32_t connectionId);
107     void (*disconnectRequest)(uint32_t connectionId);
108     int32_t (*reuseConnectionRequest)(const ConnBleReuseConnectionContext *ctx);
109     void (*preventTimeout)(const char *udid);
110     void (*reset)(int32_t reason);
111     void (*keepAliveTimeout)(uint32_t connectionId, uint32_t requestId);
112 } ConnBleState;
113 
114 #ifdef __cplusplus
115 }
116 #endif /* __cplusplus */
117 #endif /* SOFTBUS_CONN_BLE_MANAGER_STRUCT_H */
118