1 /*
2  * Copyright (C) 2022 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 PAN_DEFINES_H
17 #define PAN_DEFINES_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "gatt_data.h"
23 #include "log.h"
24 #include "l2cap_if.h"
25 #include "packet.h"
26 
27 namespace OHOS {
28 namespace bluetooth {
29 static constexpr int PAN_SUCCESS = 0;
30 static constexpr int PAN_FAILURE = 1;
31 
32 static constexpr int PAN_STATE_DISCONNECTED = 0;
33 static constexpr int PAN_STATE_CONNECTING = 1;
34 static constexpr int PAN_STATE_DISCONNECTING = 2;
35 static constexpr int PAN_STATE_CONNECTED = 3;
36 
37 static constexpr int PAN_SERVICE_STARTUP_EVT = 1;
38 static constexpr int PAN_SERVICE_SHUTDOWN_EVT = 2;
39 static constexpr int PAN_API_CLOSE_EVT = 3;
40 static constexpr int PAN_INT_OPEN_EVT = 4;
41 static constexpr int PAN_INT_CLOSE_EVT = 5;
42 static constexpr int PAN_OPEN_CMPL_EVT = 6;
43 static constexpr int PAN_API_WRITE_DATA_EVT = 7;
44 static constexpr int PAN_INT_DATA_EVT = 8;
45 static constexpr int PAN_CONNECTION_TIMEOUT_EVT = 9;
46 static constexpr int PAN_DISCONNECTION_TIMEOUT_EVT = 10;
47 
48 static constexpr int PAN_REMOVE_STATE_MACHINE_EVT = 11;
49 
50 static constexpr int BNEP_L2CAP_START_EVT = 100;
51 static constexpr int BNEP_L2CAP_CONNECT_REQ_EVT = 101;
52 static constexpr int BNEP_L2CAP_CONNECT_RSP_EVT = 102;
53 static constexpr int BNEP_L2CAP_CONFIG_REQ_EVT = 103;
54 static constexpr int BNEP_L2CAP_CONFIG_RSP_EVT = 104;
55 static constexpr int BNEP_L2CAP_DISCONNECT_REQ_EVT = 105;
56 static constexpr int BNEP_L2CAP_DISCONNECT_RSP_EVT = 106;
57 static constexpr int BNEP_L2CAP_DISCONNECT_ABNORMAL_EVT = 107;
58 static constexpr int BNEP_L2CAP_DATA_EVT = 108;
59 static constexpr int BNEP_L2CAP_REMOTE_BUSY_EVT = 109;
60 static constexpr int BNEP_L2CAP_SECURITY_RESULT_EVT = 110;
61 static constexpr int BNEP_L2CAP_IND_SECURITY_RESULT_EVT = 111;
62 static constexpr int BNEP_L2CAP_END_EVT = 112;
63 static const std::string PAN_SERVICE_NAME = "Personal Area Network NAP";
64 static const std::string PAN_SERVICE_DESCRIPTION = "NAP";
65 static constexpr uint16_t BT_BNEP_PSM = 0x000F;
66 
67 static constexpr int BNEP_CONN_STATE_UNUSED = 0;
68 static constexpr int BNEP_CONN_STATE_CONNECTING = 1;
69 static constexpr int BNEP_CONN_STATE_CONFIG = 2;
70 static constexpr int BNEP_CONN_STATE_CONNECTED = 3;
71 static constexpr int BNEP_CONN_STATE_DISCONNECTING = 4;
72 static constexpr int BNEP_CONN_STATE_SECURITY = 5;
73 static constexpr int BNEP_CONN_STATE_SETUP_CONNECTION = 6;
74 
75 static constexpr uint8_t BNEP_CONN_FLAGS_IS_ORIG = 0x01;
76 static constexpr uint8_t BNEP_CONN_FLAGS_HIS_CFG_DONE = 0x02;
77 static constexpr uint8_t BNEP_CONN_FLAGS_MY_CFG_DONE = 0x04;
78 static constexpr uint8_t BNEP_CONN_FLAGS_ALL_CONFIGURED = 0x06;
79 
80 static constexpr int BNEP_MTU_SIZE = 1691;
81 static constexpr int BNEP_UINT16_SIZE = 2;
82 
83 static constexpr uint8_t BNEP_CONTROL_COMMAND_NOT_UNDERSTOOD_LENGTH = 3;
84 static constexpr uint8_t BNEP_CONTROL_RESPONSE_LENGTH = 4;
85 static constexpr uint8_t BNEP_GENERAL_ETHERNET_HEAD_LENGTH = 15;
86 static constexpr uint8_t BNEP_COMPRESSED_ETHERNET_HEAD_LENGTH = 3;
87 static constexpr uint8_t BNEP_COMPRESSED_ETHERNET_SRC_ONLY_HEAD_LENGTH = 9;
88 static constexpr uint8_t BNEP_COMPRESSED_ETHERNET_DEST_ONLY_HEAD_LENGTH = 9;
89 
90 static constexpr uint8_t BNEP_MAX_PROTOCOL_FILTER = 5;
91 static constexpr uint8_t BNEP_MAX_MULTYCAST_FILTER = 5;
92 static constexpr uint8_t BNEP_PROTOCOL_FILTER_RANGE_LENGTH = 4;
93 static constexpr uint8_t BNEP_MULTYCAST_FILTER_RANGE_LENGTH = 12;
94 
95 static constexpr uint8_t BNEP_GENERAL_ETHERNET = 0;
96 static constexpr uint8_t BNEP_CONTROL = 1;
97 static constexpr uint8_t BNEP_COMPRESSED_ETHERNET = 2;
98 static constexpr uint8_t BNEP_COMPRESSED_ETHERNET_SRC_ONLY = 3;
99 static constexpr uint8_t BNEP_COMPRESSED_ETHERNET_DEST_ONLY = 4;
100 
101 static constexpr uint8_t BNEP_CONTROL_COMMAND_NOT_UNDERSTOOD = 0;
102 static constexpr uint8_t BNEP_SETUP_CONNECTION_REQUEST_MSG = 1;
103 static constexpr uint8_t BNEP_SETUP_CONNECTION_RESPONSE_MSG = 2;
104 static constexpr uint8_t BNEP_FILTER_NET_TYPE_SET_MSG = 3;
105 static constexpr uint8_t BNEP_FILTER_NET_TYPE_RESPONSE_MSG = 4;
106 static constexpr uint8_t BNEP_FILTER_MULTI_ADDR_SET_MSG = 5;
107 static constexpr uint8_t BNEP_FILTER_MULTI_ADDR_RESPONSE_MSG = 6;
108 
109 static constexpr uint8_t BNEP_EXTENTION_CONTROL = 0;
110 
111 static constexpr uint16_t BNEP_SETUP_SUCCESSFULL = 0x0000;
112 static constexpr uint16_t BNEP_SETUP_FAILED_INVALID_DEST_UUID = 0x0001;
113 static constexpr uint16_t BNEP_SETUP_FAILED_INVALID_SRC_UUID = 0x0002;
114 static constexpr uint16_t BNEP_SETUP_FAILED_INVALID_UUID_SIZE = 0x0003;
115 static constexpr uint16_t BNEP_SETUP_FAILED_CONN_NOT_ALLOWED = 0x0004;
116 
117 static constexpr uint16_t BNEP_FILTER_SUCCESSFULL = 0x0000;
118 static constexpr uint16_t BNEP_FILTER_UNSUPPORTED_REQUEST = 0x0001;
119 static constexpr uint16_t BNEP_FILTER_FAILED_INVALID_NET_TYPE_RANGE = 0x0002;
120 static constexpr uint16_t BNEP_FILTER_FAILED_TOO_MANY_FILTER = 0x0003;
121 static constexpr uint16_t BNEP_SETUP_FAILED_SECURITY_ERROR = 0x0004;
122 
123 static constexpr uint16_t BNEP_UUID_PANU = 0x1115;
124 static constexpr uint16_t BNEP_UUID_NAP = 0x1116;
125 
126 static constexpr int BT_ADDRESS_LENGTH = 6;
127 static constexpr int BNEP_MAX_WAITING_SEND_DATA_LIST_NUMBER = 20;
128 
129 static const char PAN_DEVICE_PATH[] = { "/dev/tun" };
130 static const char PAN_NETWORK_NAME[] = { "bt-pan" };
131 static const char PAN_NETWORK_IPV4_ADDRESS[] = { "192.168.44.1" };
132 static constexpr int PAN_NETWORK_IPV4_PREFIX_LENGTH = 24;
133 static constexpr int MAX_MASK_LENGTH = 32;
134 static constexpr int PAN_MAX_NETWORK_PACKET_SIZE = 1600;
135 
136 struct PanL2capConnectionInfo {
137     uint16_t lpsm = 0;
138     uint16_t lcid = 0;
139     int result = 0;
140     uint8_t id = 0;
141     L2capConfigInfo cfg;
142     uint8_t reason = 0;
143     uint8_t isBusy = 0;
144 };
145 
146 struct EthernetHeader {
147     uint8_t destAddr[BT_ADDRESS_LENGTH];
148     uint8_t srcAddr[BT_ADDRESS_LENGTH];
149     uint16_t protocol;
150 };
151 
152 struct BnepProtocolFilterRange {
153     uint16_t start;
154     uint16_t end;
155 };
156 
157 struct BnepMultycastFilterRange {
158     uint8_t start[BT_ADDRESS_LENGTH];
159     uint8_t end[BT_ADDRESS_LENGTH];
160 };
161 }  // namespace bluetooth
162 }  // namespace OHOS
163 #endif  // PAN_DEFINES_H
164