最开始使用Hi3861的时候,是使用AT指令去控制wifi的状态。不过在实际使用中,如果使用AT指令需要额外使用一个芯片去控制,很不方便。
Hi3861 为我们提供了很多wifi操作的API,可以通过调用这些API完成对wifi的扫描连接等操作。
主要代码如下:
- #include <stdio.h>
- #include <unistd.h>
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include <unistd.h>
- #include "hi_wifi_api.h"
- #include "lwip/ip_addr.h"
- #include "lwip/netifapi.h"
- #define APP_INIT_VAP_NUM 2
- #define APP_INIT_USR_NUM 2
- #define SSID_NAME 需要连接的wifi的名称
- #define SSID_PASSWORD 需要连接的wifi的密码
- static struct netif *g_lwip_netif = NULL;
- /* 清除netif的ip、网关和网络掩码 */
- void hi_sta_reset_addr(struct netif *pst_lwip_netif)
- {
- ip4_addr_t st_gw;
- ip4_addr_t st_ipaddr;
- ip4_addr_t st_netmask;
- printf("%s %d rn", __FILE__, __LINE__);
- if (pst_lwip_netif == NULL) {
- printf("hisi_reset_addr::Null param of netdevrn");
- return;
- }
- IP4_ADDR(&st_gw, 0, 0, 0, 0);
- IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
- IP4_ADDR(&st_netmask, 0, 0, 0, 0);
- netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
- }
- void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
- {
- if (hisi_event == NULL)
- return;
- switch (hisi_event->event) {
- case HI_WIFI_EVT_SCAN_DONE:
- printf("WiFi: Scan results availablen");
- break;
- case HI_WIFI_EVT_CONNECTED:
- printf("WiFi: Connectedn");
- netifapi_dhcp_start(g_lwip_netif);
- break;
- case HI_WIFI_EVT_DISCONNECTED:
- printf("WiFi: Disconnectedn");
- netifapi_dhcp_stop(g_lwip_netif);
- hi_sta_reset_addr(g_lwip_netif);
- break;
- case HI_WIFI_EVT_WPS_TIMEOUT:
- printf("WiFi: wps is timeoutn");
- break;
- default:
- break;
- }
- }
- int hi_wifi_start_connect(void)
- {
- int ret;
- errno_t rc;
- hi_wifi_assoc_request assoc_req = {0};
- /* 将SSID复制到assoc_req */
- rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, SSID_NAME, strlen(SSID_NAME));
- if (rc != EOK) {
- printf("%s %d rn", __FILE__, __LINE__);
- return -1;
- }
- //热点加密方式
- assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
- /* 热点密码 */
- memcpy(assoc_req.key, SSID_PASSWORD, strlen(SSID_PASSWORD));
- ret = hi_wifi_sta_connect(&assoc_req);
- if (ret != HISI_OK) {
- printf("%s %d rn", __FILE__, __LINE__);
- return -1;
- }
- printf("%s %d rn", __FILE__, __LINE__);
- return 0;
- }
- int hi_wifi_start_sta(void)
- {
- int ret;
- char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
- int len = sizeof(ifname);
- const unsigned char wifi_vap_res_num = APP_INIT_VAP_NUM;
- const unsigned char wifi_user_res_num = APP_INIT_USR_NUM;
- unsigned int num = WIFI_SCAN_AP_LIMIT;
- printf("%s %d rn", __FILE__, __LINE__);
- ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
- if (ret != HISI_OK) {
- printf("%s %d rn", __FILE__, __LINE__);
- //return -1;
- }
- printf("%s %d rn", __FILE__, __LINE__);
- ret = hi_wifi_sta_start(ifname, &len);
- if (ret != HISI_OK) {
- printf("%s %d rn", __FILE__, __LINE__);
- return -1;
- }
- /* 注册回拨功能,接收wifi事件、etc扫描结果事件、已连接事件、已断开连接事件 */
- ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
- if (ret != HISI_OK) {
- printf("register wifi event callback faiLEDn");
- }
- /* 获取用于IP操作的netif */
- g_lwip_netif = netifapi_netif_find(ifname);
- if (g_lwip_netif == NULL) {
- printf("%s: get netif failedn", __FUNCTION__);
- return -1;
- }
- /* 开始扫描,扫描结果事件将很快收到 */
- ret = hi_wifi_sta_scan();
- if (ret != HISI_OK) {
- printf("%s %d rn", __FILE__, __LINE__);
- return -1;
- }
- sleep(5); /* 睡眠5秒,等待扫描结果 */
- hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
- if (pst_results == NULL) {
- printf("%s %d rn", __FILE__, __LINE__);
- return -1;
- }
- ret = hi_wifi_sta_scan_results(pst_results, &num);
- if (ret != HISI_OK) {
- printf("%s %d rn", __FILE__, __LINE__);
- free(pst_results);
- return -1;
- }
- for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
- printf("SSID: %sn", pst_results[loop].ssid);
- }
- free(pst_results);
- /* 如果收到扫描结果,请选择一个要连接的SSID */
- ret = hi_wifi_start_connect();
- if (ret != 0) {
- printf("%s %d rn", __FILE__, __LINE__);
- return -1;
- }
- return 0;
- }
- void hi_wifi_stop_sta(void)
- {
- int ret;
- ret = hi_wifi_sta_stop();
- if (ret != HISI_OK) {
- printf("failed to stop stan");
- }
- ret = hi_wifi_deinit();
- if (ret != HISI_OK) {
- printf("failed to deinit wifin");
- }
- g_lwip_netif = NULL;
-
- }
- void WiFiExampleEntry(void)
- {
- hi_wifi_start_sta();
- }
- SYS_RUN(WiFiExampleEntry);
复制代码运行之后log如下
- ready to OS start
- sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
- formatting spiffs...
- FileSystem mount ok.
- wifi init success!
- ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 111
- ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 115
- ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 119
- +NOTICE:SCANFINISH
- WiFi: Scan results available
- SSID: ChinaNet-9qDC
- SSID: SuperSpeedTurbo
- SSID:
- SSID:
- SSID: 浜嶱HICOMM_F1
- SSID: CMCC-TnpK
- SSID: Lenovo Wi-Fi AP R3200
- SSID: st
- SSID: ChinaNet-3MFk
- SSID:
- SSID: ChinaNet-yLWz
- SSID: CU_HAfG
- SSID: ChinaNet-bwEU
- SSID: Tenda_445420
- SSID: CU_9JXc
- SSID: FAST_4EAC
- SSID: @PHICOMM_35
- SSID: 瀛欐垚鎴愭槸瀚╃埞
- SSID: 绫冲
- ../../applications/sample/wifi-iot/app/wifi_demo/wifi_demo.c 98
- 00 00:00:00 0 196 D 0/HIVIEW: hilog init success.
- 00 00:00:00 0 196 D 0/HIVIEW: log limit init success.
- 00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3).
- 00 00:00:00 0 196 I 1/SAMGR: Init service:0x4aea5c TaskPool:0xfc3e8
- 00 00:00:00 0 196 I 1/SAMGR: Init service:0x4aea80 TaskPool:0xfc408
- 00 00+NOTICE:CONNECTED
- :00:00 0 196 I 1/SAMGR: Init service:0x4aeb90 TaskPool:0xfc7bc
- 00 00:00:00 0 228 I 1/SAMGR: Init service 0x4aea80 <time: 5570ms> success!
- 00 00:00:00 0 28 I 1/SAMGR: Init service 0x4aea5c <time: 5570ms> success!
- 00 00:00:00 0 72 D 0/HIVIEW: hiview init success.
- 00 00:00:00 0 72 I 1/SAMGR: Init service 0x4aeb90 <time: 5570ms> success!
- 00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services!
- 00 00:00:00 0 28 I 1/SAMGR: Bootstrap system and application services(count:0).
- 00 00:00:00 0 28 I 1/SAMGR: Initialized all system and application services!
- 00 00:00:00 0 28 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
- WiFi: Connected
复制代码其中最后打印的WiFi: Connected代表已经成功连接到wifi