• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NSTACKX_DEVICE_H
17 #define NSTACKX_DEVICE_H
18 
19 #ifndef _WIN32
20 #include <arpa/inet.h>
21 #endif
22 #include <stdbool.h>
23 
24 #include "nstackx.h"
25 #include "coap_discover.h"
26 
27 #define MAX_ADDRESS_LEN 64
28 #define MAX_MAC_ADDRESS_LENGTH 6
29 #define MAX_IPV4_ADDRESS_LEN 4
30 #define INTERFACE_NAME_POSSIBLE 3
31 
32 /*
33  * 1st discover interval: 100ms
34  * 2nd ~ 3rd discover interval: 200ms
35  * Remaining discover interval (9 times): 500ms
36  */
37 #define COAP_DEFAULT_DISCOVER_COUNT 12
38 #define COAP_FIRST_DISCOVER_COUNT_RANGE 1
39 #define COAP_SECOND_DISCOVER_COUNT_RANGE 3
40 #define COAP_FIRST_DISCOVER_INTERVAL 100
41 #define COAP_SECOND_DISCOVER_INTERVAL 200
42 #define COAP_LAST_DISCOVER_INTERVAL 500
43 #define REDUNDANCY_NET_CHANNEL_NUM 2
44 #define NSTACKX_MAX_NET_CHANNEL_NUM (NSTACKX_MAX_LISTENED_NIF_NUM + REDUNDANCY_NET_CHANNEL_NUM)
45 enum DeviceState {
46     IDEL,
47     ACTIVE,
48     LEAVE
49 };
50 
51 typedef enum {
52     NSTACKX_EVENT_JOIN,
53     NSTACKX_EVENT_UPDATE,
54     NSTACKX_EVENT_LEAVE,
55 } NSTACKX_Event;
56 
57 enum NetChannelState {
58     NET_CHANNEL_STATE_START,
59     NET_CHANNEL_STATE_DISABLED,
60     NET_CHANNEL_STATE_DISCONNECT,
61     NET_CHANNEL_STATE_CONNETING,
62     NET_CHANNEL_STATE_CONNETED,
63     NET_CHANNEL_STATE_END,
64 };
65 
66 typedef enum  {
67     DFINDER_UPDATE_STATE_NULL,
68     DFINDER_UPDATE_STATE_BROADCAST,
69     DFINDER_UPDATE_STATE_UNICAST,
70     DFINDER_UPDATE_STATE_ALL,
71     DFINDER_UPDATE_STATE_END,
72 } UpdateState;
73 
74 typedef struct {
75     char name[NSTACKX_MAX_INTERFACE_NAME_LEN];
76     char alias[NSTACKX_MAX_INTERFACE_NAME_LEN];
77     struct in_addr ip;
78 } NetworkInterfaceInfo;
79 
80 typedef struct {
81     char name[INTERFACE_NAME_POSSIBLE][NSTACKX_MAX_INTERFACE_NAME_LEN];
82 } NetworkInterfacePrefiexPossible;
83 
84 typedef struct {
85     struct in_addr ip;
86     uint8_t state;
87     /* AP information? */
88 } WifiApChannelInfo;
89 
90 typedef struct {
91     WifiApChannelInfo wifiApInfo;
92 } NetChannelInfo;
93 
94 typedef struct BusinessDataAll {
95     uint8_t isBroadcast; /* Used only to process received packets */
96     char businessDataBroadcast[NSTACKX_MAX_BUSINESS_DATA_LEN];
97     char businessDataUnicast[NSTACKX_MAX_BUSINESS_DATA_LEN];
98 } BusinessDataAll;
99 
100 typedef struct DeviceRemoteChannelInfo {
101     BusinessDataAll businessDataAll;
102     NetChannelInfo remoteChannelInfo;
103     struct timespec lastRecvTime;
104     UpdateState updateState;
105 } DeviceRemoteChannelInfo;
106 
107 typedef struct LocalIfInfoAll {
108     NSTACKX_InterfaceInfo localIfInfo;
109     DeviceRemoteChannelInfo deviceRemoteChannelInfo[NSTACKX_MAX_NET_CHANNEL_NUM];
110     uint8_t nextRemoteIdx;
111 } LocalIfInfoAll;
112 
113 typedef struct DeviceInfo {
114     char deviceName[NSTACKX_MAX_DEVICE_NAME_LEN];
115     char deviceId[NSTACKX_MAX_DEVICE_ID_LEN];
116 #ifdef DFINDER_SAVE_DEVICE_LIST
117     uint8_t update : 1;
118     uint8_t reserved : 7;
119 #ifndef DFINDER_SUPPORT_MULTI_NIF
120     UpdateState updateState;
121 #endif
122 #endif
123     uint8_t deviceType;
124     uint16_t portNumber;
125 #ifdef DFINDER_SUPPORT_MULTI_NIF
126     LocalIfInfoAll localIfInfoAll[NSTACKX_MAX_LISTENED_NIF_NUM];
127     uint8_t ifState[NSTACKX_MAX_LISTENED_NIF_NUM];
128     uint8_t ifNums;
129     uint8_t nextNifIdx;
130 #endif
131     NetChannelInfo netChannelInfo;
132     /* Capability data */
133     uint32_t capabilityBitmapNum;
134     uint32_t capabilityBitmap[NSTACKX_MAX_CAPABILITY_NUM];
135     char version[NSTACKX_MAX_HICOM_VERSION];
136     uint8_t mode;
137     uint8_t discoveryType;
138     char deviceHash[DEVICE_HASH_LEN];
139     char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN];
140     uint8_t businessType;
141     BusinessDataAll businessData;
142 #ifndef DFINDER_USE_MINI_NSTACKX
143     char extendServiceData[NSTACKX_MAX_EXTEND_SERVICE_DATA_LEN];
144 #endif
145     char networkName[NSTACKX_MAX_INTERFACE_NAME_LEN];
146 } DeviceInfo;
147 
148 int32_t DeviceModuleInit(EpollDesc epollfd, uint32_t maxDeviceNum);
149 #ifndef DFINDER_USE_MINI_NSTACKX
150 int32_t P2pUsbTimerInit(EpollDesc epollfd);
151 void DestroyP2pUsbServerInitRetryTimer(void);
152 #endif /* END OF DFINDER_USE_MINI_NSTACKX */
153 void DeviceModuleClean(void);
154 void PushPublishInfo(DeviceInfo *deviceInfo, NSTACKX_DeviceInfo *deviceList, uint32_t deviceNum);
155 
156 #ifdef DFINDER_SUPPORT_MULTI_NIF
157 int32_t UpdateDeviceDbWithIdx(const DeviceInfo *deviceInfo, uint8_t forceUpdate, uint8_t idx);
158 #endif
159 
160 #ifdef DFINDER_SAVE_DEVICE_LIST
161 int32_t UpdateDeviceDb(const DeviceInfo *deviceInfo, uint8_t forceUpdate);
162 uint8_t ClearDevices(void *deviceList);
163 int32_t BackupDeviceDB(void);
164 void *GetDeviceDB(void);
165 void *GetDeviceDBBackup(void);
166 DeviceInfo *GetDeviceInfoById(const char *deviceId, const void *db);
167 void GetDeviceListWrapper(NSTACKX_DeviceInfo *deviceList, uint32_t *deviceCountPtr, bool doFilter);
168 #else
169 int32_t DeviceInfoNotify(const DeviceInfo *deviceInfo, uint8_t forceUpdate);
170 #endif
171 
172 void SetModeInfo(uint8_t mode);
173 uint8_t GetModeInfo(void);
174 void SetDeviceHash(uint64_t deviceHash);
175 
176 int32_t ConfigureLocalDeviceInfo(const NSTACKX_LocalDeviceInfo *localDeviceInfo);
177 void ConfigureLocalDeviceName(const char *localDeviceName);
178 void ConfigureDiscoverySettings(const NSTACKX_DiscoverySettings *discoverySettings);
179 
180 #ifdef DFINDER_SUPPORT_MULTI_NIF
181 int32_t UpdateLocalNetworkInterface(void);
182 #else
183 int32_t UpdateLocalNetworkInterface(const NetworkInterfaceInfo *interfaceInfo);
184 #endif
185 
186 #ifndef DFINDER_USE_MINI_NSTACKX
187 void UpdateAllNetworkInterfaceNameIfNeed(const NetworkInterfaceInfo *interfaceInfo);
188 int32_t UpdateLocalNetworkInterfaceP2pMode(const NetworkInterfaceInfo *interfaceInfo, uint16_t nlmsgType);
189 int32_t UpdateLocalNetworkInterfaceUsbMode(const NetworkInterfaceInfo *interfaceInfo, uint16_t nlmsgType);
190 uint8_t FilterNetworkInterface(const char *ifName);
191 #ifdef _WIN32
192 uint8_t IsWlanIpAddr(const struct in_addr *ifAddr);
193 uint8_t IsEthIpAddr(const struct in_addr *ifAddr);
194 uint8_t IsP2pIpAddr(const struct in_addr *ifAddr);
195 uint8_t IsUsbIpAddr(const struct in_addr *ifAddr);
196 #else
197 uint8_t IsWlanIpAddr(const char *ifName);
198 uint8_t IsEthIpAddr(const char *ifName);
199 uint8_t IsP2pIpAddr(const char *ifName);
200 uint8_t IsUsbIpAddr(const char *ifName);
201 #endif
202 #endif /* END OF DFINDER_USE_MINI_NSTACKX */
203 
204 const DeviceInfo *GetLocalDeviceInfoPtr(void);
205 
206 #ifdef DFINDER_SUPPORT_MULTI_NIF
207 uint8_t IsApConnected(void);
208 uint8_t IsApConnectedWithIdx(uint32_t idx);
209 char *GetLocalNifNameWithIdx(uint32_t idx);
210 #else
211 uint8_t IsWifiApConnected(void);
212 #endif
213 
214 #ifdef DFINDER_SUPPORT_MULTI_NIF
215 int32_t GetLocalIpStringWithIdx(char *ipString, size_t length, uint32_t idx);
216 int32_t GetLocalInterfaceNameWithIdx(char *ifName, size_t ifNameLen, uint32_t idx);
217 #endif
218 
219 int32_t GetLocalIpString(char *ipString, size_t length);
220 int32_t GetLocalInterfaceName(char *ifName, size_t ifNameLength);
221 int32_t GetNetworkName(char *name, int32_t len);
222 
223 int32_t RegisterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]);
224 int32_t SetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]);
225 int32_t RegisterServiceData(const char *serviceData);
226 void ResetDeviceTaskCount(uint8_t isBusy);
227 void GetLocalIp(struct in_addr *ip);
228 #ifndef DFINDER_USE_MINI_NSTACKX
229 int32_t RegisterExtendServiceData(const char *extendServiceData);
230 void GetLocalNetworkInterface(void *arg);
231 void SetP2pIp(const struct in_addr *ip);
232 void SetUsbIp(const struct in_addr *ip);
233 int32_t GetP2pIpString(char *ipString, size_t length);
234 int32_t GetUsbIpString(char *ipString, size_t length);
235 #endif /* END OF DFINDER_USE_MINI_NSTACKX */
236 int32_t SetLocalDeviceBusinessDataUnicast(const char* businessData, uint32_t length);
237 #endif /* #ifndef NSTACKX_DEVICE_H */
238