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 CONN_BR_MANAGER_H
17 #define CONN_BR_MANAGER_H
18 
19 #include <stdint.h>
20 
21 #include "softbus_conn_br_connection.h"
22 #include "softbus_conn_interface.h"
23 #include "softbus_conn_manager.h"
24 #include "softbus_def.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 BR_CONNECT_TIMEOUT_MIN_MILLIS                    (10 * 1000)
33 #define BR_CONNECT_TIMEOUT_MAX_MILLIS                    (20 * 1000)
34 
35 #define BR_CONNECTION_PEND_TIMEOUT_MAX_MILLIS            (10 * 1000)
36 #define BR_CONNECTION_ACL_RETRY_CONNECT_COLLISION_MILLIS (3 * 1000)
37 #define BR_CONNECTION_ACL_CONNECT_COLLISION_MILLIS       (6 * 1000)
38 #define BR_WAIT_BLE_DISCONNECTED_PEND_MILLIS             (10 * 1000)
39 #define BR_NIP_SEQ                                       (0xeaddeaddeaddeadd)
40 
41 #define BR_CONNECT_WAIT_CALLBACK_TIMEOUT_MAX_MILLIS      (500)
42 
43 enum ConnBrDeviceState {
44     BR_DEVICE_STATE_INIT,
45     BR_DEVICE_STATE_WAIT_EVENT,
46     BR_DEVICE_STATE_PENDING,
47     BR_DEVICE_STATE_WAIT_SCHEDULE,
48     BR_DEVICE_STATE_SCHEDULING,
49 };
50 
51 typedef struct {
52     ListNode node;
53     char addr[BT_MAC_LEN];
54     enum ConnBrDeviceState state;
55     ListNode requests;
56     uint32_t connectionId;
57     struct {
58         uint32_t keepAliveBleRequestId;
59         uint32_t keepAliveBleConnectionId;
60     } bleKeepAliveInfo;
61     uint32_t waitTimeoutDelay;
62 } ConnBrDevice;
63 
64 typedef struct {
65     ListNode node;
66     uint32_t requestId;
67     ConnectResult result;
68     ConnectStatistics statistics;
69 } ConnBrRequest;
70 
71 typedef struct {
72     uint32_t requestId;
73     uint32_t connectionId;
74     char addr[BT_MAC_LEN];
75     ConnectResult result;
76     ConnectStatistics statistics;
77     uint32_t waitTimeoutDelay;
78 } ConnBrConnectRequestContext;
79 
80 typedef struct {
81     uint32_t connectionId;
82     uint8_t *data;
83     uint32_t dataLen;
84 } ConnBrDataReceivedContext;
85 
86 typedef struct {
87     char addr[BT_MAC_LEN];
88     uint64_t firstStartTimestamp;
89     uint32_t firstDuration;
90     uint64_t startTimestamp;
91     uint32_t duration;
92 } ConnBrPendInfo;
93 
94 typedef struct {
95     char *(*name)(void);
96     void (*enter)(void);
97     void (*exit)(void);
98     void (*connectRequest)(const ConnBrConnectRequestContext *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)(ConnBrDataReceivedContext *ctx);
105     void (*connectionException)(uint32_t connectionId, int32_t error);
106     void (*connectionResume)(uint32_t connectionId);
107     void (*disconnectRequest)(uint32_t connectionId);
108     void (*unpend)(const char *addr);
109     void (*reset)(int32_t reason);
110 } ConnBrState;
111 
112 int32_t ConnBrSaveConnection(ConnBrConnection *connection);
113 void ConnBrRemoveConnection(ConnBrConnection *connection);
114 ConnBrConnection *ConnBrGetConnectionByAddr(const char *addr, ConnSideType side);
115 ConnBrConnection *ConnBrGetConnectionById(uint32_t connectionId);
116 void ConnBrReturnConnection(ConnBrConnection **connection);
117 
118 ConnectFuncInterface *ConnInitBr(const ConnectCallback *callback);
119 
120 #ifdef __cplusplus
121 }
122 #endif /* __clpusplus */
123 #endif /* CONN_BR_MANAGER_H */
124