From 96c49ca9d224589bcb9020ee1bea526d008551a1 Mon Sep 17 00:00:00 2001 From: liufeng24015 Date: Tue, 29 Mar 2022 20:15:40 +0800 Subject: [PATCH] delete error files Signed-off-by: liufeng24015 --- hwiotlink_example/README_zh.md | 487 ----------------------------- hwiotlink_example/deviceauth-zh.md | 151 --------- hwiotlink_example/wifi_connect.c | 226 ------------- 3 files changed, 864 deletions(-) delete mode 100755 hwiotlink_example/README_zh.md delete mode 100755 hwiotlink_example/deviceauth-zh.md delete mode 100755 hwiotlink_example/wifi_connect.c diff --git a/hwiotlink_example/README_zh.md b/hwiotlink_example/README_zh.md deleted file mode 100755 index 6608a92..0000000 --- a/hwiotlink_example/README_zh.md +++ /dev/null @@ -1,487 +0,0 @@ -# TWLink组件使用说明 - -# 简述 - -TWLink是模组统一连接拓维IoT平台的组件,该组件是一个全局统一的MQTTClient库,使用OpenHarmony SA机制作为原子化共性启动服务,整个系统中只存在一个,对上层应用提供统一的LPC通讯方式与拓维IoT平台进行交互,上层应用只要关注业务实现,不再关注与平台对接细节。 - -TWLink组件依deviceauth组件,twlink服务由deviceauth组件拉起。 - -# TWLink组件配置说明 - -niobeu4的TWLink组件可通过内核配置进行选配,操作如下: - -1. 切换到kernel/liteos_m/目录 - - ``` - cd kernel/liteos_m/ - ``` - -2. 输入make menuconfig 打开内核配置界面 - - ``` - make menuconfig - ``` - - 弹出配置界面,选择Platform下的select board niobeu4。 - - ![image-20220216150938794](figures/1.png) - -3. 选配上红色框内的组件,选配了该组件系统才会开启设备鉴权功能。 - - ![image-20220304170529872](figures/2.png) - -TWLink组件需要依赖设备鉴权组件来提供三元组信息,因此选择TWLink组件时,设备鉴权组件也会自动选配上。 - -## 组件接口说明 - -SetTWClientConfig - -功能:在应用层配置连接参数 - -``` -typedef struct -{ - const char Host[20]; // 服务器IP - const int Port; // 服务器连接端口 - const char UserName[32]; // 用户名 - const char PassWord[32]; // 连接密码 - const char ClientId[32]; // 连接客户端标示 - const char IoTProductKey[32]; //平台三元组--产品型号 - const char IoTDeviceName[32]; //平台三元组 -- 设备名称 - const char IoTDeviceSecret[32]; //平台三元组 设备连接秘钥 -} TWLinkParam; - -``` - -测例: - -``` -static void *MainTask(const char *arg) -{ - (void)arg; - WifiConnect("aaa", "talkweb1996"); - - TWLinkParam param = { - .Host = "116.63.137.223", - .Port = 1883, - .UserName = "", - .PassWord = "", - .ClientId = "talkweb_test", - .IoTDeviceName = "niobeu4", - .IoTDeviceSecret = "5348de753199dded", - .IoTProductKey = "L0rzgyzyy"}; - - SetTWClientConfig(¶m); -} -``` - -ConnectionedNotification - -功能:重新连接服务器回调接口 - -``` -/* - Description: - 重新连接服务器回调事件, 可能是客户端自己没有网络,也可能是服务器端关闭了 - - Parameter: - - Return Value: - NA -*/ -typedef void (*ConnectionedNotificationCallback)(); - -/* - Description: - 注册联网处理回调. - - Parameter: - ConnectionedNotificationCallback -- onConnectioned Server callback - - Return Value: -*/ -void ConnectionedNotification(ConnectionedNotificationCallback callback); - -``` - -测例: - -``` -static void OnConnectionedCallback() -{ - printf("[Application][TaskID:%p][OnConnectionedCallback]!\n", - osThreadGetId()); -} -// 注册联网回调 -ConnectionedNotification(OnConnectionedCallback); - -``` - -DisConnectionNotification - -功能:客户端断网或者服务器连接不上、服务器重启的时候触发 - -``` -/* - Description: - 离线回调事件, 可能是客户端自己没有网络,也可能是服务器端关闭了 - - Parameter: - - Return Value: - NA -*/ -typedef void (*DisConnectionNotificationCallback)(); - -/* - Description: - 注册离线处理回调. - - Parameter: - DisConnectionNotificationCallback -- onDisConnection Server callback - Return Value: -*/ -void DisConnectionNotification(DisConnectionNotificationCallback callback); -``` - -测例: - -``` -static void OnDisConnectionLineCallback() -{ - printf("[Application][TaskID:%p][OnDisConnectionLineCallback]!\n", - osThreadGetId()); -} -// 注册离线回调 -DisConnectionNotification(OnDisConnectionLineCallback); -``` - -SubscribeMQTTTopic - -功能:订阅主题 - -``` -/* - Description: - 订阅主题. - - Parameter: - sub_topic -- subscribe topic name of MQTT - MQTT_MESSAGE_QOS --mqtt qos - PMQTTMessageCallback -- message callback - - Return Value: - 0 -- Success - other -- Failure -*/ -int32 SubscribeMQTTTopicQOS(const char *sub_topic,MQTT_MESSAGE_QOS qos,PMQTTMessageCallback callback); -``` - -测例: - -``` -static void UserTopicCallback(char *ptopic, char *pdata) -{ - const char *rtopic = (const char *)ptopic; - const char *rdata = (const char *)pdata; - printf("[Application][TaskID:%p][UserTopicCallback][Topic:%s] : %s !\n", - osThreadGetId(), rtopic, pdata); -} - -SubscribeMQTTTopic("aaa",QOS2, UserTopicCallback); -``` - -UnSubscribeMQTTTopic - -功能:取消订阅主题 - -``` -/* - Description: - 取消订阅主题. - - Parameter: - topic -- unsubscribe topic name of MQTT - Return Value: - 0 -- Success - other -- Failure -*/ -int32 UnSubscribeMQTTTopic(const char *topic); -``` - -测例: - -``` -UnSubscribeMQTTTopic("aaa"); // 取消订阅主题aaa -``` - -PushMQTTMessage - -功能: 发送MQTT消息, - -``` -typedef enum -{ - QOS0 = 0, - QOS1 = 1, - QOS2 = 2, -} MQTT_MESSAGE_QOS; - -/* - Description: - 向某个主题发送信息. - - Parameter: - topic -- subscribe topic name of MQTT - MQTT_MESSAGE_QOS qos - topic -- MQTT message , The Message Max Size 2048 bytes - Return Value: - 0 -- Success - other -- Failure -*/ -int32 PushMQTTMessageQOS(char *topic, const char *message, MQTT_MESSAGE_QOS qos); -``` - -测例 : - -``` -PushMQTTMessage("aaa", "3333333"); -``` - -TwlinkIsConnected - -功能:读取twlink链接状态 - -## 测试 - -niobeu4的TWLink的测试可以通过内核核配置打开,操作如下: - -1. 切换到kernel/liteos_m/目录 - - ``` - cd kernel/liteos_m/ - ``` - -2. 输入make menuconfig 打开内核配置界面 - - ``` - make menuconfig - ``` - - 弹出配置界面,选择Platform下的select board niobeu4。 - - ![image-20220216150938794](figures/1.png) - -3. 选配application中的twlink example。 - - ![image-20220304164423511](figures/3.png) - -测例: - -``` -/* - * Copyright (c) 2021-2022 Talkweb Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "ohos_init.h" -#include "cmsis_os2.h" -#include "mqtt_proxy.h" -#include "string.h" -#include "ohos_run.h" -#include "wifi_device.h" -#include "wifi.h" - -static uint32 pubCount = 0; -static uint32 subCount = 0; -// 自定义主题回调处理 -static void UserTopicCallback(char *ptopic, char *pdata) -{ - const char *rtopic = (const char *)ptopic; - const char *rdata = (const char *)pdata; - - subCount++; - printf("[Application]: %d, %s %s !\n", - strlen(rdata), rtopic, pdata); -} - - -static void OnConnectionedCallback() -{ - // 注册主题, 注意: 平台限制主题注册格式, - SubscribeMQTTTopicQOS("tlink/${productKey}/${deviceName}/event/propertyReport/post", QOS2,UserTopicCallback); - SubscribeMQTTTopicQOS("tlink/${productKey}/${deviceName}/command/setProperty/exec",QOS2, UserTopicCallback); - SubscribeMQTTTopicQOS("tlink/${productKey}/${deviceName}/ota/device/upgrade", QOS0,UserTopicCallback); - printf("OnConnectionedCallback test\n"); -} - -// 系统离线回调 -static void OnDisConnectionCallback() -{ - printf("OnDisConnectionCallback test\n"); -} - -static void *MainTask(const char *arg) -{ - (void)arg; - - DeviceAutoInit(); /*设备鉴权,获取三元组*/ - printf("twlink test!!!!\r\n"); - /*等待twlink连接云*/ - while(!TwlinkIsConnected()) - { - LOS_TaskDelay(100) ; - } - - ConnectionedNotification(OnConnectionedCallback); - // 注册离线回调 - DisConnectionNotification(OnDisConnectionCallback); - OnConnectionedCallback(); - - int step = 0; - int ret = -1, - ret1 = -1; - // 不让程序退出 - while (1) - { - LOS_TaskDelay(100); - ret = PushMQTTMessageQOS("tlink/${productKey}/${deviceName}/event/propertyReport/post","talkweb_twlinksdk test111>>>>>> !!!", QOS2); - if(ret == 0) - pubCount++; - ret1 = PushMQTTMessageQOS("tlink/${productKey}/${deviceName}/command/setProperty/exec","talkweb_twlinksdk test2222>>>>>> !!!", QOS2); - if(ret1 == 0 ) - pubCount++; - printf("subCount:%ld pubCount:%ld \r\n",subCount,pubCount); - } -} -static int testflag = 0; -static void MainEntry(void) -{ - osThreadAttr_t attr; - attr.name = "MainEntry"; - attr.attr_bits = 0U; - attr.cb_mem = NULL; - attr.cb_size = 0U; - attr.stack_mem = NULL; - attr.stack_size = 4096 * 3; - attr.priority = osPriorityNormal; - - if (osThreadNew((osThreadFunc_t)MainTask, NULL, &attr) == NULL) - { - printf("[MainTask] Falied to create MainTask!\n"); - } - -} - -void twlink_example(void) -{ - //连接Wifi - WifiErrorCode error = EnableWifi(); - printf("EnableWifi errCode: %d\r\n", error); - if (error != WIFI_SUCCESS) { - return; - } - - error = Scan(); - printf("ScanWifi errCode: %d\r\n", error); - if (error != WIFI_SUCCESS) { - return; - } - OnWifiConnectDevice("HUAWEIP40","houpengfei8"); - printf("GetWifiConnectStatus: %d\r\n", GetWifiConnectStatus()); - - while(!GetWifiConnectStatus()) - { - LOS_TaskDelay(1000); - } - - MainEntry(); - -} -OHOS_APP_RUN(twlink_example); - -``` - -BUILD.gn - -``` -import("//kernel/liteos_m/liteos.gni") - -module_name = get_path_info(rebase_path("."), "name") -kernel_module(module_name) { - sources = [ - "twlinktest.c", - ] - - include_dirs = [ - "//device/board/talkweb/components/tlinksdk/interfaces/kits", - "//foundation/communication/wifi_lite/interfaces/wifiservice", - "//device/soc/espressif/esp32/liteos_m/drivers/wifi", - ] -} -``` - -TWLink测试运行结果如下: - -设备鉴权成功: - -``` -device auth success !,ret is 0 -``` - -twlink - -``` -[TWMQTTInit]: MQTT_Init is 1073220968 -121.37.143.77 10003 -mqtt_connection_cb: Successfully connected -RegisterOfflineNotificationHandler -RegisterDisConnectionNotificationHandler -OnConnectionedCallback test -Subscribe result: 0 -Subscribe result: 0 -Subscribe result: 0 -subCount:0 pubCount:2 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:2 pubCount:4 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:4 pubCount:6 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:6 pubCount:8 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:8 pubCount:10 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:10 pubCount:12 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:12 pubCount:14 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:14 pubCount:16 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:16 pubCount:18 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -subCount:18 pubCount:20 -[Application]: 36, tlink/k0bvhXvUX/T13o0poxp/event/propertyReport/post talkweb_twlinksdk test111>>>>>> !!! ! -[Application]: 37, tlink/k0bvhXvUX/T13o0poxp/command/setProperty/exec talkweb_twlinksdk test2222>>>>>> !!! ! -``` - diff --git a/hwiotlink_example/deviceauth-zh.md b/hwiotlink_example/deviceauth-zh.md deleted file mode 100755 index baf4fe3..0000000 --- a/hwiotlink_example/deviceauth-zh.md +++ /dev/null @@ -1,151 +0,0 @@ -# 设备鉴权模块使用说明 - -# 简述 - -设备鉴权组件实现设备与云平台的一型一密认证。 组件工作的前提条件是设备必须连上可用网络。当设备连接到网络之后,鉴权服务通过http动态从云平台获取设备三元组信息(ProductKey、DeviceName、DeviceSecret),存储三元组信息到设备flash,拉起Tlink服务。 - -# 设备鉴权模块使用说明 - -niobeu4的设备鉴权组件可通过内核配置进行选配,操作如下: - -1. 切换到kernel/liteos_m/目录 - - ``` - cd kernel/liteos_m/ - ``` - -2. 输入make menuconfig 打开内核配置界面 - - ``` - make menuconfig - ``` - - 弹出配置界面,选择Platform下的select board niobeu4。 - -![image-20220215170753727](figures/1.png) - -选配上红色框内的组件,选配了该组件系统才会开启设备鉴权功能。 - -设备鉴权组件获取三元组信息需要使用到设备的ProductKey和ProductSecret,可以在vendor\talkweb\niobeu4\hals\utils\token\hal_token.c中修改这两个参数: - -``` -/*productkey adn ProductSecret*/ -static char ProductSecret[] = {"ec76e4a427446224"}; -static char ProductKey[] = {"k0bvhXvUX"}; // ProductKey by OEM. -``` - -这两个参数由云平台提供。 - -![](figures/4.png) - -## API - -``` -/* - Description: - 启动DeviceAuth服务. - - Parameter: - - Return Value: -*/ -void DeviceAutoInit(); /*启动设备鉴权*/ -``` - -## 组件功能说明 - -设备鉴权组件实现以下功能: - -1. 从云平台动态获取三元组信息,获取到的元组信息存储在nvs里 -2. 设备鉴权成功拉起twlink服务。 - -## 测试结果 - -选配设备鉴权组件后,选配twlink的测试代码,通过http自动获取三元组,测试如下: - -``` -httpclient_conn() result:0, client:0x2001c86c -request header is POST /gateway/twiot-device-server/device/registerAndExtras HTTP/1.1 -User-Agent: OneOS-HTTP-Client/1.0 -Cache-Control: no-cache -Host: iot.devcloud.talkweb.com.cn - -Trying to write 219 bytes http header:POST /gateway/twiot-device-server/device/registerAndExtras HTTP/1.1 -User-Agent: OneOS-HTTP-Client/1.0 -Cache-Control: no-cache -Host: iot.devcloud.talkweb.com.cn -Content-Length: 149 -Content-Type: application/json - - -http tcp send wrapper buffer is POST /gateway/twiot-device-server/device/registerAndExtras HTTP/1.1 -User-Agent: OneOS-HTTP-Client/1.0 -Cache-Control: no-cache -Host: iot.devcloud.talkweb.com.cn -Content-Length: 149 -Content-Type: application/json - - -Written 219 bytes, socket = 10 -client_data->post_buf:{ - "productKey": "k0bvhXvUX", - "deviceName": "d234f86a137e", - "timestamp": 0, - "sign": "94b08aa21897d2f85d9605e33bb92682", - "signMethod": "hmacmd5" -} -Written 149 bytes -httpclient_send() result:0, client:0x2001c86c -select return 0 may disconnected -reclen:450, buf:HTTP/1.1 200 OK -Date: Thu, 17 Feb 2022 06:38:33 GMT -Content-Type: application/json;charset=UTF-8 -Transfer-Encoding: chunked -Connection: keep-alive -Vary: Accept-Encoding -bodyLength: 199 -Server: elb - -e8 -{"code":{"code":"0","msg":"success","ok":true},"result":{"isPreRegister":"1","deviceSecret":"1ee8385ff16c419b","mqttIp":"121.37.143.77","productTanentId":"T0001","productKey":"k0bvhXvUX","deviceName":"U1yK0RKtR","mqttPort":"10003"}} -0 - - -Read header : Date: Thu, 17 Feb 2022 06:38:33 GMT -Read header : Content-Type: application/json;charset=UTF-8 -Read header : Transfer-Encoding: chunked -Read header : Connection: keep-alive -Read header : Vary: Accept-Encoding -Read header : bodyLength: 199 -Read header : Server: elb -in loop http_client_retrieve_content 644 len 243 ret 0 -Retrieving 232 bytes, len:239 -readLen 232, len:239 -memmove 232 239 0 -in loop http_client_retrieve_content 644 len 5 ret 0 -no more (last chunk) -httpclient_recv_data() result:0, client:0x2001c86c -No form data info found -httpclient_close() client:0x2001c86c -TWWriteLinkParam ready to write twlink.cfg -device auth success !,ret is 0 - -``` - -出现device auth success !,表示设备鉴权成功 - -获取三元组成功后,再次上电将直接使用上次保持的三元组信息,不再通过http获取。 - -``` -device auth success !,ret is 0 -[TWMQTTInit]: MQTT_Init is 536929676 -121.37.143.77 10003 -mqtt_connection_cb: Successfully connected -RegisterOfflineNotificationHandler -RegisterDisConnectionNotificationHandler -OnConnectionedCallback test -Subscribe result: 0 -Subscribe result: 0 -Subscribe result: 0 -``` - diff --git a/hwiotlink_example/wifi_connect.c b/hwiotlink_example/wifi_connect.c deleted file mode 100755 index a127446..0000000 --- a/hwiotlink_example/wifi_connect.c +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (c) 2021-2022 Talkweb Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include -#include -#include "ohos_init.h" -#include "cmsis_os2.h" -#include "lwip/netif.h" -#include "lwip/netifapi.h" -#include "lwip/ip4_addr.h" -#include "wifi_device.h" - -#define DEF_TIMEOUT 15 -#define ONE_SECOND 1 - -#define SELECT_WLAN_PORT "wlan0" -#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK - -static int g_staScanSuccess = 0; -static int g_ConnectSuccess = 0; -static int ssid_count = 0; - -WifiErrorCode error; -WifiEvent g_wifiEventHandler = {0}; - -int sock_fd; -int addr_length; - -#define STD_TIMEZONE_OFFSET +8 -const int timeZone = 8; - - -static void WiFiInit(void); -static void WaitSacnResult(void); -static int WaitConnectResult(void); -static void OnWifiScanStateChangedHandler(int state, int size); -static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info); -static void OnHotspotStaJoinHandler(StationInfo *info); -static void OnHotspotStateChangedHandler(int state); -static void OnHotspotStaLeaveHandler(StationInfo *info); - - -void DisableWIFI(void){ - DisableWifi(); -} - -static void OnHotspotStaJoinHandler(StationInfo *info) -{ - (void)info; - printf("STA join AP\n"); - return; -} - -static void OnHotspotStaLeaveHandler(StationInfo *info) -{ - (void)info; - printf("HotspotStaLeave:info is null.\n"); - return; -} - -static void OnHotspotStateChangedHandler(int state) -{ - printf("HotspotStateChanged:state is %d.\n", state); - return; -} - -static void WiFiInit(void) -{ - printf("<--Wifi Init-->\r\n"); - g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler; - g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler; - g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; - g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; - g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; - error = RegisterWifiEvent(&g_wifiEventHandler); - if (error != WIFI_SUCCESS) - { - printf("register wifi event fail!\r\n"); - } - else - { - printf("register wifi event succeed!\r\n"); - } -} - -static void OnWifiScanStateChangedHandler(int state, int size) -{ - (void)state; - if (size > 0) - { - ssid_count = size; - g_staScanSuccess = 1; - } - return; -} - -static int result; -int WifiConnect(const char *ssid, const char *psk) -{ - - WifiScanInfo *info = NULL; - unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT; - static struct netif *g_lwip_netif = NULL; - WifiDeviceConfig select_ap_config = {0}; - - osDelay(200); - printf("<--System Init-->\r\n"); - - //wifi init - WiFiInit(); - - if (EnableWifi() != WIFI_SUCCESS) - { - printf("EnableWifi failed, error = %d\n", error); - return -1; - } - - //check wifi - if (IsWifiActive() == 0) - { - printf("Wifi station is not actived.\n"); - return -1; - } - - //malloc mem,store wifiscaninfo - info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT); - if (info == NULL) - { - printf("faild to create wifiscanInfo.\n"); - return -1; - } - - do - { - ssid_count = 0; - g_staScanSuccess = 0; - //start scan - Scan(); - WaitSacnResult(); - //get scan result - error = GetScanInfoList(info, &size); - } while (g_staScanSuccess != 1); - - strcpy(select_ap_config.ssid, ssid); - strcpy(select_ap_config.preSharedKey, psk); - printf("%s %s \r\n",select_ap_config.ssid,select_ap_config.preSharedKey); - select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE; - - if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS){ - if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1){ - printf("WiFi connect succeed!\r\n"); - } - } - osDelay(100); - - return 0; -} - -static int WaitConnectResult(void) -{ - int ConnectTimeout = DEF_TIMEOUT; - while (ConnectTimeout > 0) - { - sleep(1); - ConnectTimeout--; - if (g_ConnectSuccess == 1) - { - printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout)); - break; - } - } - if (ConnectTimeout <= 0) - { - printf("WaitConnectResult:timeout!\n"); - return 0; - } - - return 1; -} - -static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info) -{ - (void)info; - if (state > 0) - { - g_ConnectSuccess = 1; - printf("callback function for wifi connect\r\n"); - } - else - { - printf("connect error,please check password,state:%d,tyr connect again\r\n",state); - esp_wifi_connect(); - } - return; -} - -static void WaitSacnResult(void) -{ - int scanTimeout = DEF_TIMEOUT; - while (scanTimeout > 0) - { - sleep(ONE_SECOND); - scanTimeout--; - if (g_staScanSuccess == 1) - { - printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout)); - break; - } - } - if (scanTimeout <= 0) - { - printf("WaitSacnResult:timeout!\n"); - } -} \ No newline at end of file -- Gitee