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