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 29 extern "C" { 30 #endif 31 32 enum { 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 58 enum DeviceState { 59 IDEL, 60 ACTIVE, 61 LEAVE 62 }; 63 64 typedef enum { 65 NSTACKX_EVENT_JOIN, 66 NSTACKX_EVENT_UPDATE, 67 NSTACKX_EVENT_LEAVE, 68 } NSTACKX_Event; 69 70 enum 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 79 typedef 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 87 typedef 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 93 typedef struct { 94 char name[INTERFACE_NAME_POSSIBLE][NSTACKX_MAX_INTERFACE_NAME_LEN]; 95 } NetworkInterfacePrefiexPossible; 96 97 typedef struct { 98 struct in_addr ip; 99 uint8_t state; 100 } WifiApChannelInfo; 101 102 typedef struct { 103 WifiApChannelInfo wifiApInfo; 104 } NetChannelInfo; 105 106 typedef 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 112 typedef struct SeqAll { 113 uint8_t dealBcast; 114 uint16_t seqBcast; 115 uint16_t seqUcast; 116 } SeqAll; 117 118 typedef 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 char version[NSTACKX_MAX_HICOM_VERSION]; 131 uint8_t mode; 132 uint8_t discoveryType; 133 char deviceHash[DEVICE_HASH_LEN]; 134 char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN]; 135 uint8_t businessType; 136 BusinessDataAll businessData; 137 #ifndef DFINDER_USE_MINI_NSTACKX 138 char extendServiceData[NSTACKX_MAX_EXTEND_SERVICE_DATA_LEN]; 139 #endif 140 char networkName[NSTACKX_MAX_INTERFACE_NAME_LEN]; 141 SeqAll seq; 142 char notification[NSTACKX_MAX_NOTIFICATION_DATA_LEN]; 143 } DeviceInfo; 144 145 int32_t DeviceModuleInit(EpollDesc epollfd, uint32_t maxDeviceNum); 146 void DeviceModuleClean(void); 147 148 #ifdef DFINDER_SAVE_DEVICE_LIST 149 int32_t UpdateDeviceDb(const CoapCtxType *coapCtx, const DeviceInfo *deviceInfo, uint8_t forceUpdate, 150 uint8_t receiveBcast); 151 #endif 152 153 int32_t DeviceInfoNotify(const DeviceInfo *deviceInfo); 154 int32_t ReportDiscoveredDevice(const CoapCtxType *coapCtx, const DeviceInfo *deviceInfo, 155 uint8_t forceUpdate, uint8_t receiveBcast); 156 157 void SetModeInfo(uint8_t mode); 158 uint8_t GetModeInfo(void); 159 160 uint32_t GetNotifyTimeoutMs(void); 161 162 int32_t ConfigureDiscoverySettings(const NSTACKX_DiscoverySettings *discoverySettings); 163 int32_t DiscConfigInner(const DFinderDiscConfig *discConfig); 164 165 #ifndef DFINDER_USE_MINI_NSTACKX 166 void UpdateAllNetworkInterfaceNameIfNeed(const NetworkInterfaceInfo *interfaceInfo); 167 #endif /* END OF DFINDER_USE_MINI_NSTACKX */ 168 169 void SetMaxDeviceNum(uint32_t maxDeviceNum); 170 uint32_t GetMaxDeviceNum(void); 171 uint32_t *GetFilterCapability(uint32_t *capabilityBitmapNum); 172 void IncreaseSequenceNumber(uint8_t sendBcast); 173 uint16_t GetSequenceNumber(uint8_t sendBcast); 174 void ResetSequenceNumber(void); 175 176 int32_t RegisterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]); 177 int32_t SetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]); 178 bool MatchDeviceFilter(const DeviceInfo *deviceInfo); 179 int32_t RegisterServiceData(const char *serviceData); 180 void ResetDeviceTaskCount(uint8_t isBusy); 181 182 uint8_t GetIfaceType(const char *ifname); 183 int32_t SetReservedInfoFromDeviceInfo(NSTACKX_DeviceInfo *deviceList, const DeviceInfo *deviceInfo); 184 int32_t GetNotifyDeviceInfo(NSTACKX_DeviceInfo *notifyDevice, const DeviceInfo *deviceInfo); 185 186 #ifdef __cplusplus 187 } 188 #endif 189 190 #endif /* #ifndef NSTACKX_DEVICE_H */ 191