1 /*
2  * Copyright (c) 2021-2024 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 LNN_NODE_INFO_H
17 #define LNN_NODE_INFO_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 
22 #include "lnn_connect_info.h"
23 #include "lnn_device_info.h"
24 #include "lnn_net_capability.h"
25 #include "softbus_def.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 #define WIFI_SSID_LEN 32
31 #define WIFI_MAC_LEN 6
32 #define OFFLINE_CODE_LEN 32
33 #define OFFLINE_CODE_BYTE_SIZE 4
34 #define EXTDATA_LEN 8
35 #define PTK_DEFAULT_LEN 32
36 #define STATIC_CAP_LEN 100
37 #define STATIC_CAP_STR_LEN 201
38 #define PTK_ENCODE_LEN 45
39 
40 #define LNN_RELATION_MASK 0x03
41 #define WIFI_CFG_INFO_MAX_LEN 512
42 #define CHANNEL_LIST_STR_LEN  256
43 
44 #define SESSION_KEY_STR_LEN 65
45 #define PTK_STR_LEN 65
46 
47 #define BROADCAST_IV_LEN 16
48 #define BROADCAST_IV_STR_LEN 33
49 
50 #define LFINDER_UDID_HASH_LEN 32
51 #define LFINDER_IRK_LEN 16
52 #define LFINDER_IRK_STR_LEN 33
53 #define LFINDER_MAC_ADDR_LEN 6
54 #define LFINDER_MAC_ADDR_STR_LEN 13
55 
56 typedef enum {
57     AUTH_AS_CLIENT_SIDE = 0,
58     AUTH_AS_SERVER_SIDE,
59     AUTH_SIDE_MAX,
60 } AuthSide;
61 
62 typedef enum {
63     ROLE_UNKNOWN = 0,
64     ROLE_CONTROLLER,
65     ROLE_LEAF,
66 } ConnectRole;
67 
68 typedef enum {
69     STATUS_OFFLINE = 0,
70     STATUS_ONLINE,
71 } ConnectStatus;
72 
73 typedef enum {
74     DISCOVERY_TYPE_UNKNOWN = 0,
75     DISCOVERY_TYPE_WIFI,
76     DISCOVERY_TYPE_BLE,
77     DISCOVERY_TYPE_BR,
78     DISCOVERY_TYPE_P2P,
79     DISCOVERY_TYPE_LSA,
80     DISCOVERY_TYPE_COUNT,
81 } DiscoveryType;
82 
83 typedef struct {
84     char ssid[WIFI_SSID_LEN + 1];
85     unsigned char targetBssid[WIFI_MAC_LEN];
86 } BssTransInfo;
87 
88 typedef struct {
89     int32_t p2pRole;
90     char wifiCfg[WIFI_CFG_INFO_MAX_LEN];
91     char chanList5g[CHANNEL_LIST_STR_LEN];
92     int32_t staFrequency;
93     char p2pMac[MAC_LEN]; // the mac of local p2p interface
94     char goMac[MAC_LEN]; // the mac of p2p Go device, while local device as Gc role.
95     char p2pIp[IP_LEN];
96 } P2pInfo;
97 
98 typedef struct {
99     bool isMetaNode;
100     uint32_t metaDiscType;
101 } MetaInfo;
102 
103 typedef struct {
104     bool isCharging;
105     int32_t batteryLevel;
106 } BatteryInfo;
107 
108 typedef enum {
109     BIT_SUPPORT_EXCHANGE_NETWORKID = 0,
110     BIT_SUPPORT_NORMALIZED_LINK = 1,
111     BIT_SUPPORT_NEGOTIATION_AUTH = 2,
112     BIT_SUPPORT_BR_DUP_BLE = 3,
113 } AuthCapability;
114 
115 typedef struct {
116     int32_t keylen;
117     unsigned char key[SESSION_KEY_LENGTH];
118     unsigned char iv[BROADCAST_IV_LEN];
119 } BroadcastCipherInfo;
120 
121 typedef struct {
122     uint8_t peerIrk[LFINDER_IRK_LEN];
123     unsigned char publicAddress[LFINDER_MAC_ADDR_LEN];
124     unsigned char peerUdidHash[LFINDER_UDID_HASH_LEN];
125 } RpaInfo;
126 
127 typedef struct {
128     char softBusVersion[VERSION_MAX_LEN];
129     char versionType[VERSION_MAX_LEN]; // compatible nearby
130     char pkgVersion[VERSION_MAX_LEN];
131     char uuid[UUID_BUF_LEN]; // compatible nearby
132     char lastNetworkId[NETWORK_ID_BUF_LEN];
133     char networkId[NETWORK_ID_BUF_LEN];
134     char publicId[ID_MAX_LEN];
135     char parentId[ID_MAX_LEN];
136     char masterUdid[UDID_BUF_LEN];
137     char nodeAddress[SHORT_ADDRESS_MAX_LEN];
138     char extData[EXTDATA_LEN];
139     int32_t masterWeight;
140     ConnectRole role;
141     ConnectStatus status;
142     uint32_t netCapacity;
143     uint32_t authCapacity;
144     uint32_t heartbeatCapacity;
145     uint32_t discoveryType;
146     uint64_t bleDirectTimeStamp;
147     DeviceBasicInfo deviceInfo;
148     ConnectInfo connectInfo;
149     BssTransInfo bssTransInfo;
150     bool isBleP2p; // true: this device support connect p2p via ble connection
151     P2pInfo p2pInfo;
152     char wifiDirectAddr[MAC_LEN];
153     uint64_t supportedProtocols;
154     char accountHash[SHA_256_HASH_LEN];
155     int64_t accountId;
156     unsigned char offlineCode[OFFLINE_CODE_BYTE_SIZE];
157     int64_t authSeq[DISCOVERY_TYPE_COUNT];
158     MetaInfo metaInfo;
159     uint32_t AuthTypeValue;
160     uint16_t dataChangeFlag;
161     uint16_t dataDynamicLevel;
162     uint16_t dataStaticLevel;
163     uint32_t dataSwitchLevel;
164     uint16_t dataSwitchLength;
165     BatteryInfo batteryInfo;
166     bool isScreenOn;
167     int64_t wifiVersion;
168     int64_t bleVersion;
169     uint64_t feature;
170     uint64_t connSubFeature;
171     int64_t bleStartTimestamp;
172     int32_t wifiBuffSize;
173     int32_t brBuffSize;
174     int32_t stateVersion;
175     int32_t localStateVersion;
176     uint32_t groupType;
177     bool initPreventFlag;
178     int64_t networkIdTimestamp;
179     RpaInfo rpaInfo;
180     BroadcastCipherInfo cipherInfo;
181     int32_t bleMacRefreshSwitch;
182     int32_t bleConnCloseDelayTime;
183     uint8_t staticCapability[STATIC_CAP_LEN];
184     int32_t staticCapLen;
185     char remotePtk[PTK_DEFAULT_LEN];
186     char remoteMetaPtk[PTK_DEFAULT_LEN];
187     int32_t deviceSecurityLevel;
188     uint8_t relation[CONNECTION_ADDR_MAX];
189     int64_t authSeqNum;
190     int32_t authChannelId[CONNECTION_ADDR_MAX][AUTH_SIDE_MAX];
191     uint64_t heartbeatTimeStamp;
192     uint64_t onlinetTimestamp;
193     uint64_t updateTimestamp;
194     bool isAuthExchangeUdid;
195     bool isSupportIpv6;
196     uint32_t stateVersionReason;
197     int64_t lastAuthSeq;
198 } NodeInfo;
199 
200 const char *LnnGetDeviceUdid(const NodeInfo *info);
201 int32_t LnnSetDeviceUdid(NodeInfo *info, const char *udid);
202 const char *LnnGetDeviceUuid(const NodeInfo *info);
203 bool LnnHasDiscoveryType(const NodeInfo *info, DiscoveryType type);
204 int32_t LnnSetDiscoveryType(NodeInfo *info, DiscoveryType type);
205 int32_t LnnClearDiscoveryType(NodeInfo *info, DiscoveryType type);
206 bool LnnIsNodeOnline(const NodeInfo *info);
207 void LnnSetNodeConnStatus(NodeInfo *info, ConnectStatus status);
208 const char *LnnGetBtMac(const NodeInfo *info);
209 void LnnSetBtMac(NodeInfo *info, const char *mac);
210 const char *LnnGetBleMac(const NodeInfo *info);
211 void LnnSetBleMac(NodeInfo *info, const char *mac);
212 const char *LnnGetWiFiIp(const NodeInfo *info);
213 void LnnSetWiFiIp(NodeInfo *info, const char *ip);
214 const char *LnnGetNetIfName(const NodeInfo *info);
215 void LnnSetNetIfName(NodeInfo *info, const char *netIfName);
216 const char *LnnGetMasterUdid(const NodeInfo *info);
217 int32_t LnnSetMasterUdid(NodeInfo *info, const char *udid);
218 int32_t LnnGetAuthPort(const NodeInfo *info);
219 int32_t LnnSetAuthPort(NodeInfo *info, int32_t port);
220 int32_t LnnGetSessionPort(const NodeInfo *info);
221 int32_t LnnSetSessionPort(NodeInfo *info, int32_t port);
222 int32_t LnnGetProxyPort(const NodeInfo *info);
223 int32_t LnnSetProxyPort(NodeInfo *info, int32_t port);
224 int32_t LnnSetP2pRole(NodeInfo *info, int32_t role);
225 int32_t LnnGetP2pRole(const NodeInfo *info);
226 int32_t LnnSetWifiCfg(NodeInfo *info, const char *wifiCfg);
227 const char *LnnGetWifiCfg(const NodeInfo *info);
228 int32_t LnnSetChanList5g(NodeInfo *info, const char *chanList5g);
229 const char *LnnGetChanList5g(const NodeInfo *info);
230 int32_t LnnSetStaFrequency(NodeInfo *info, int32_t staFrequency);
231 int32_t LnnGetStaFrequency(const NodeInfo *info);
232 int32_t LnnSetP2pMac(NodeInfo *info, const char *p2pMac);
233 uint16_t LnnGetDataChangeFlag(const NodeInfo *info);
234 int32_t LnnSetDataChangeFlag(NodeInfo *info, uint16_t dataChangeFlag);
235 uint16_t LnnGetDataDynamicLevel(const NodeInfo *info);
236 int32_t LnnSetDataDynamicLevel(NodeInfo *info, uint16_t dataDynamicLevel);
237 uint16_t LnnGetDataStaticLevel(const NodeInfo *info);
238 int32_t LnnSetDataStaticLevel(NodeInfo *info, uint16_t dataStaticLvel);
239 uint32_t LnnGetDataSwitchLevel(const NodeInfo *info);
240 int32_t LnnSetDataSwitchLevel(NodeInfo *info, uint32_t dataSwitchLevel);
241 uint16_t LnnGetDataSwitchLength(const NodeInfo *info);
242 int32_t LnnSetDataSwitchLength(NodeInfo *info, uint16_t dataSwitchLevel);
243 const char *LnnGetP2pMac(const NodeInfo *info);
244 int32_t LnnSetP2pGoMac(NodeInfo *info, const char *goMac);
245 const char *LnnGetP2pGoMac(const NodeInfo *info);
246 uint64_t LnnGetSupportedProtocols(const NodeInfo *info);
247 int32_t LnnSetSupportedProtocols(NodeInfo *info, uint64_t protocols);
248 int32_t LnnSetWifiDirectAddr(NodeInfo *info, const char *wifiDirectAddr);
249 const char *LnnGetWifiDirectAddr(const NodeInfo *info);
250 int32_t LnnSetStaticCapability(NodeInfo *info, uint8_t *cap, uint32_t len);
251 int32_t LnnGetStaticCapability(NodeInfo *info, uint8_t *cap, uint32_t len);
252 int32_t LnnSetPtk(NodeInfo *info, const char *remotePtk);
253 void LnnDumpRemotePtk(const char *oldPtk, const char *newPtk, const char *log);
254 void LnnDumpNodeInfo(const NodeInfo *deviceInfo, const char *log);
255 int32_t LnnSetScreenStatus(NodeInfo *info, bool isScreenOn);
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 #endif // LNN_NODE_INFO_H
261