• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2023 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 "nstackx_inet.h"
26 #include "coap_discover.h"
27 #include "coap_app.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 enum {
34     IFACE_TYPE_ETH,
35     IFACE_TYPE_WLAN,
36     IFACE_TYPE_USB_SCALE,
37     IFACE_TYPE_P2P,
38     IFACE_TYPE_USB,
39     IFACE_TYPE_UNKNOWN,
40     IFACE_TYPE_MAX,
41 };
42 
43 #define MAX_ADDRESS_LEN 64
44 #define MAX_MAC_ADDRESS_LENGTH 6
45 #define MAX_IPV4_ADDRESS_LEN 4
46 #define INTERFACE_NAME_POSSIBLE 3
47 
48 /*
49  * 1st discover interval: 100ms
50  * 2nd ~ 3rd discover interval: 200ms
51  * Remaining discover interval (9 times): 500ms
52  */
53 #define COAP_DEFAULT_DISCOVER_COUNT 12
54 #define COAP_FIRST_DISCOVER_COUNT_RANGE 1
55 #define COAP_SECOND_DISCOVER_COUNT_RANGE 3
56 #define COAP_FIRST_DISCOVER_INTERVAL 100
57 #define COAP_SECOND_DISCOVER_INTERVAL 200
58 #define COAP_LAST_DISCOVER_INTERVAL 500
59 
60 enum DeviceState {
61     IDEL,
62     ACTIVE,
63     LEAVE
64 };
65 
66 typedef enum {
67     NSTACKX_EVENT_JOIN,
68     NSTACKX_EVENT_UPDATE,
69     NSTACKX_EVENT_LEAVE,
70 } NSTACKX_Event;
71 
72 enum NetChannelState {
73     NET_CHANNEL_STATE_START,
74     NET_CHANNEL_STATE_DISABLED,
75     NET_CHANNEL_STATE_DISCONNECT,
76     NET_CHANNEL_STATE_CONNETING,
77     NET_CHANNEL_STATE_CONNETED,
78     NET_CHANNEL_STATE_END,
79 };
80 
81 typedef enum  {
82     DFINDER_UPDATE_STATE_NULL,
83     DFINDER_UPDATE_STATE_BROADCAST,
84     DFINDER_UPDATE_STATE_UNICAST,
85     DFINDER_UPDATE_STATE_ALL,
86     DFINDER_UPDATE_STATE_END,
87 } UpdateState;
88 
89 typedef struct {
90     char name[NSTACKX_MAX_INTERFACE_NAME_LEN];
91     char alias[NSTACKX_MAX_INTERFACE_NAME_LEN];
92     struct in_addr ip;
93 } NetworkInterfaceInfo;
94 
95 typedef struct {
96     char name[INTERFACE_NAME_POSSIBLE][NSTACKX_MAX_INTERFACE_NAME_LEN];
97 } NetworkInterfacePrefiexPossible;
98 
99 typedef struct {
100     union InetAddr addr;
101     uint8_t af;
102     uint8_t state;
103 } WifiApChannelInfo;
104 
105 typedef struct {
106     WifiApChannelInfo wifiApInfo;
107 } NetChannelInfo;
108 
109 typedef struct BusinessDataAll {
110     uint8_t isBroadcast; /* Used only to process received packets */
111     char businessDataBroadcast[NSTACKX_MAX_BUSINESS_DATA_LEN];
112     char businessDataUnicast[NSTACKX_MAX_BUSINESS_DATA_LEN];
113 } BusinessDataAll;
114 
115 typedef enum {
116     DFINDER_SEQ_TYPE_NONE,
117     DFINDER_SEQ_TYPE_BCAST,
118     DFINDER_SEQ_TYPE_UNICAST,
119 } SeqType;
120 
121 typedef struct SeqAll {
122     /* Is there a sequence number in the payload */
123     uint8_t seqType;
124     uint16_t seqBcast;
125     uint16_t seqUcast;
126 } SeqAll;
127 
128 typedef struct DeviceInfo {
129     char deviceId[NSTACKX_MAX_DEVICE_ID_LEN];
130     char deviceName[NSTACKX_MAX_DEVICE_NAME_LEN];
131 #ifdef DFINDER_SAVE_DEVICE_LIST
132     int8_t update : 1;
133     uint8_t reserved : 7;
134 #endif
135     uint32_t deviceType;
136     NetChannelInfo netChannelInfo;
137     /* Capability data */
138     uint32_t capabilityBitmapNum;
139     uint32_t capabilityBitmap[NSTACKX_MAX_CAPABILITY_NUM];
140     uint8_t mode;
141     uint8_t discoveryType;
142     char deviceHash[DEVICE_HASH_LEN];
143     char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN];
144     uint8_t businessType;
145     BusinessDataAll businessData;
146 #ifndef DFINDER_USE_MINI_NSTACKX
147     char extendServiceData[NSTACKX_MAX_EXTEND_SERVICE_DATA_LEN];
148 #endif
149     char networkName[NSTACKX_MAX_INTERFACE_NAME_LEN];
150     SeqAll seq;
151     char notification[NSTACKX_MAX_NOTIFICATION_DATA_LEN];
152 } DeviceInfo;
153 
154 int32_t DeviceModuleInit(EpollDesc epollfd, uint32_t maxDeviceNum);
155 void DeviceModuleClean(void);
156 
157 #ifdef DFINDER_SAVE_DEVICE_LIST
158 int32_t UpdateDeviceDb(const CoapCtxType *coapCtx, const DeviceInfo *deviceInfo, uint8_t forceUpdate,
159     uint8_t receiveBcast);
160 #endif
161 
162 int32_t DeviceInfoNotify(const DeviceInfo *deviceInfo);
163 int32_t ReportDiscoveredDevice(const CoapCtxType *coapCtx, const DeviceInfo *deviceInfo,
164     uint8_t forceUpdate, uint8_t receiveBcast);
165 
166 void SetModeInfo(uint8_t mode);
167 uint8_t GetModeInfo(void);
168 
169 uint32_t GetNotifyTimeoutMs(void);
170 
171 int32_t ConfigureDiscoverySettings(const NSTACKX_DiscoverySettings *discoverySettings);
172 int32_t DiscConfigInner(const DFinderDiscConfig *discConfig);
173 
174 #ifndef DFINDER_USE_MINI_NSTACKX
175 void UpdateAllNetworkInterfaceNameIfNeed(const NetworkInterfaceInfo *interfaceInfo);
176 #endif /* END OF DFINDER_USE_MINI_NSTACKX */
177 
178 void SetMaxDeviceNum(uint32_t maxDeviceNum);
179 uint32_t GetMaxDeviceNum(void);
180 uint32_t *GetFilterCapability(uint32_t *capabilityBitmapNum);
181 
182 void IncreaseUcastSequenceNumber(uint8_t af);
183 void IncreaseBcastSequenceNumber(void);
184 uint16_t GetSequenceNumber(uint8_t af, uint8_t sendBcast);
185 void ResetSequenceNumber(void);
186 
187 int32_t RegisterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]);
188 int32_t SetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]);
189 bool MatchDeviceFilter(const DeviceInfo *deviceInfo);
190 int32_t RegisterServiceData(const char *serviceData);
191 void ResetDeviceTaskCount(uint8_t isBusy);
192 
193 uint8_t GetIfaceType(const char *ifname);
194 int32_t SetReservedInfoFromDeviceInfo(NSTACKX_DeviceInfo *deviceList, const DeviceInfo *deviceInfo);
195 int32_t GetNotifyDeviceInfo(NSTACKX_DeviceInfo *notifyDevice, const DeviceInfo *deviceInfo);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif /* #ifndef NSTACKX_DEVICE_H */
202