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 #include <arpa/inet.h> 20 #include <stdbool.h> 21 22 #include "nstackx.h" 23 #include "coap_discover.h" 24 25 #define MAX_ADDRESS_LEN 64 26 #define MAX_MAC_ADDRESS_LENGTH 6 27 #define MAX_IPV4_ADDRESS_LEN 4 28 29 enum DeviceState { 30 IDEL, 31 ACTIVE, 32 LEAVE 33 }; 34 35 typedef enum { 36 NSTACKX_EVENT_JOIN, 37 NSTACKX_EVENT_UPDATE, 38 NSTACKX_EVENT_LEAVE, 39 } NSTACKX_Event; 40 41 enum NetChannelState { 42 NET_CHANNEL_STATE_START, 43 NET_CHANNEL_STATE_DISABLED, 44 NET_CHANNEL_STATE_DISCONNECT, 45 NET_CHANNEL_STATE_CONNETING, 46 NET_CHANNEL_STATE_CONNETED, 47 NET_CHANNEL_STATE_END, 48 }; 49 50 typedef struct { 51 char name[NSTACKX_MAX_INTERFACE_NAME_LEN]; 52 char alias[NSTACKX_MAX_INTERFACE_NAME_LEN]; 53 struct in_addr ip; 54 } NetworkInterfaceInfo; 55 56 typedef struct { 57 struct in_addr ip; 58 uint8_t state; 59 /* AP information? */ 60 } WifiApChannelInfo; 61 62 typedef struct { 63 WifiApChannelInfo wifiApInfo; 64 } NetChannelInfo; 65 66 typedef struct DeviceInfo { 67 char deviceName[NSTACKX_MAX_DEVICE_NAME_LEN]; 68 char deviceId[NSTACKX_MAX_DEVICE_ID_LEN]; 69 uint8_t update : 1; 70 uint8_t reserved : 7; 71 uint8_t deviceType; 72 uint16_t portNumber; 73 NetChannelInfo netChannelInfo; 74 /* Capability data */ 75 uint32_t capabilityBitmapNum; 76 uint32_t capabilityBitmap[NSTACKX_MAX_CAPABILITY_NUM]; 77 char version[NSTACKX_MAX_HICOM_VERSION]; 78 uint8_t mode; 79 char deviceHash[DEVICE_HASH_LEN]; 80 char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN]; 81 } DeviceInfo; 82 83 int32_t DeviceModuleInit(EpollDesc epollfd); 84 85 void DeviceModuleClean(void); 86 void PushPublishInfo(const DeviceInfo *deviceInfo, NSTACKX_DeviceInfo *deviceList, uint32_t deviceNum); 87 88 int32_t UpdateDeviceDb(const DeviceInfo *deviceInfo, uint8_t forceUpdate); 89 uint8_t ClearDevices(void *deviceList); 90 int32_t BackupDeviceDB(void); 91 void *GetDeviceDB(void); 92 void *GetDeviceDBBackup(void); 93 94 DeviceInfo *GetDeviceInfoById(const char *deviceId, const void *db); 95 96 void GetDeviceList(NSTACKX_DeviceInfo *deviceList, uint32_t *deviceCountPtr, bool doFilter); 97 void SetModeInfo(uint8_t mode); 98 uint8_t GetModeInfo(void); 99 void SetDeviceHash(uint64_t deviceHash); 100 101 int32_t ConfigureLocalDeviceInfo(const NSTACKX_LocalDeviceInfo *localDeviceInfo); 102 103 const DeviceInfo *GetLocalDeviceInfoPtr(void); 104 uint8_t IsWifiApConnected(void); 105 int32_t GetLocalIpString(char *ipString, size_t length); 106 int32_t GetLocalInterfaceName(char *ifName, size_t ifNameLength); 107 108 int32_t RegisterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]); 109 int32_t SetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]); 110 int32_t RegisterServiceData(const char *serviceData); 111 void ResetDeviceTaskCount(uint8_t isBusy); 112 #endif /* #ifndef NSTACKX_DEVICE_H */ 113