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_TRANS_H
17 #define SOFTBUS_CONN_BLE_TRANS_H
18 
19 #include <stdint.h>
20 
21 #include "common_list.h"
22 #include "softbus_error_code.h"
23 #include "softbus_conn_common.h"
24 #include "softbus_conn_manager.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define CTRL_MSG_KEY_METHOD            "KEY_METHOD"
31 #define CTRL_MSG_KEY_DELTA             "KEY_DELTA"
32 #define CTRL_MSG_KEY_REF_NUM           "KEY_REF_NUM"
33 #define CTRL_MSG_KEY_CHALLENGE         "KEY_CHALLENGE"
34 #define CTRL_MSG_METHOD_NOTIFY_REQUEST 1
35 
36 typedef struct {
37     SoftBusMutex lock;
38     bool messagePosted;
39     bool sendTaskRunning;
40 } StartBleSendLPInfo;
41 
42 typedef struct {
43     uint32_t seq;
44     uint32_t size;
45     uint32_t offset;
46     uint32_t total;
47 } BleTransHeader;
48 
49 typedef struct {
50     ListNode node;
51     BleTransHeader header;
52     uint8_t *data;
53 } ConnBlePacket;
54 
55 typedef struct {
56     uint32_t seq;
57     uint32_t received;
58     uint32_t total;
59     ListNode packets;
60 } ConnBleReadBuffer;
61 
62 enum BleCtlMessageMethod {
63     METHOD_NOTIFY_REQUEST = 1,
64 };
65 
66 typedef struct {
67     uint32_t connectionId;
68     int32_t flag;
69     enum BleCtlMessageMethod method;
70     union {
71         struct {
72             int32_t delta;
73             int32_t referenceNumber;
74         } referenceRequest;
75     };
76     uint16_t challengeCode;
77 } BleCtlMessageSerializationContext;
78 
79 typedef struct {
80     void (*onPostBytesFinished)(
81         uint32_t connectionId, uint32_t len, int32_t pid, int32_t flag, int32_t module, int64_t seq, int32_t error);
82 } ConnBleTransEventListener;
83 
84 typedef void (*PostBytesFinishAction)(uint32_t connectionId, int32_t error);
85 int32_t ConnBlePostBytesInner(
86     uint32_t connectionId, uint8_t *data, uint32_t len, int32_t pid, int32_t flag, int32_t module, int64_t seq,
87     PostBytesFinishAction postBytesFinishAction);
88 uint8_t *ConnGattTransRecv(
89     uint32_t connectionId, uint8_t *data, uint32_t dataLen, ConnBleReadBuffer *buffer, uint32_t *outLen);
90 uint8_t *ConnCocTransRecv(
91     uint32_t connectionId, LimitedBuffer *buffer, int32_t *outLen);
92 int64_t ConnBlePackCtlMessage(BleCtlMessageSerializationContext ctx, uint8_t **outData, uint32_t *outLen);
93 int32_t ConnBleTransConfigPostLimit(const LimitConfiguration *configuration);
94 
95 int32_t ConnBleInitTransModule(ConnBleTransEventListener *listener);
96 
97 #ifdef __cplusplus
98 }
99 #endif /* __cplusplus */
100 #endif /* SOFTBUS_CONN_BLE_TRANS_H */