Hi3516如何连接WiFi(三)

系统
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[391922]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

小伙伴们大家好,上一篇(Hi3516如何连接Wifi(二))介绍了用程序启动wap_supplicant,也就是Wifi的Daemon。下一步就是如何向Daemon发信息,我们可以参考//applications/sample/camera/communication/wpa_cli,他实现了连接Daemon、扫描热点、连接热点等功能。

我们打开wpa_cli_sample.c文件,核心功能在于这几个函数:

  • InitControlInterface:初始化
  • TestScan:扫描周围热点
  • TestNetworkConfig:连接到指定热点

首先看InitControlInterface函数,他先调用了wpa_ctrl_open函数,获取了用于发送命令的控制接口g_ctrlConn,是一个类型为struct wpa_ctrl的结构体。又调用一遍wpa_ctrl_open打开了一个用于事件监控的控制接口g_monitorConn。然后启动了事件监控的线程执行MonitorTask函数,这个监控线程不是必须,可以省略。

  1. int InitControlInterface() 
  2.     g_ctrlConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for send cmd 
  3.     g_monitorConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for event monitor 
  4.     if (!g_ctrlConn || !g_monitorConn) { 
  5.         SAMPLE_ERROR("open wpa control interface failed."); 
  6.         return -1; 
  7.     } 
  8.     if (wpa_ctrl_attach(g_monitorConn) == 0) { // start monitor 
  9.         pthread_create(&g_wpaThreadId, NULL, MonitorTask, NULL); // create thread for read event 
  10.         return 0; 
  11.     } 
  12.     return -1; 

然后我们来着重分析一下TestNetworkConfig函数。实际上就是一系列SendCtrlCommand向Daemon发送指令。

  1. static void TestNetworkConfig(void) 
  2.     char networkId[20] = {0}; 
  3.     size_t networkIdLen = sizeof(networkId); 
  4.     int ret = SendCtrlCommand("DISCONNECT", networkId, &networkIdLen); 
  5.     ret += SendCtrlCommand("ADD_NETWORK", networkId, &networkIdLen); 
  6.     if (ret != 0) { 
  7.         SAMPLE_ERROR("add network failed."); 
  8.         return
  9.     } 
  10.     SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, networkId); 
  11.     char reply[100] = {0}; 
  12.     size_t replyLen = sizeof(reply); 
  13.     char cmd[200] = {0}; 
  14.     sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"example\"", networkIdLen, networkId); 
  15.     ret += SendCtrlCommand(cmd, reply, &replyLen); 
  16.     replyLen = sizeof(reply); 
  17.     sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"012345678\"", networkIdLen, networkId); 
  18.     ret += SendCtrlCommand(cmd, reply, &replyLen); 
  19.     replyLen = sizeof(reply); 
  20.     sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, networkId); 
  21.     ret += SendCtrlCommand(cmd, reply, &replyLen); 
  22.     replyLen = sizeof(reply); 
  23.     ret += SendCtrlCommand("RECONNECT", reply, &replyLen); 
  24.     replyLen = sizeof(reply); 
  25.     if (ret == 0) { 
  26.         SAMPLE_INFO("network config success."); 
  27.         return
  28.     } 
  29.     sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, networkId); 
  30.     SendCtrlCommand(cmd, reply, &replyLen); 
  31.     SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, networkId); 

wpa_supplicant定义了许多命令,常见的有:

  • PING:心跳检测命令。客户端用它判断WPAS是否工作正常。WPAS收到”PING”命令后需要回复“PONG”。
  • MIB:客户端用该命令获取设备的MIB信息。
  • STATUS:客户端用该命令来获取WPAS的工作状态。
  • ADD_NETWORK:为WPAS添加一个新的无线网络。它将返回此新无线网络的id(从0开始)。注意:此network id非常重要,客户端后续将通过它来指明自己想操作的无线网络。
  • SET_NETWORK :network id是无线网络的id。此命令用于设置指定无线网络的信息。其中variable为参数名,value为参数的值。
  • ENABLE_NETWORK:使能某个无线网络。此命令最终将促使WPAS发起一系列操作以加入该无线网络。
  • SCAN: 扫描附近AP
  • SCAN_RESULT:列出最近一次扫描的结果
  • LIST_NETWORKS: 列出添加的所有AP

看完上边就很好理解了,先是DISCONNECT断开已有连接,ADD_NETWORK添加一个新的无线网络,SET_NETWORK设置ssid和psk,ENABLE_NETWORK使能这个无线网络,最后一个RECONNECT重新连接有点迷,去掉应该也没关系,不过本人没有尝试。

TestScan函数就留给读者自行分析。

了解了这些,我们就可以在自己的代码中去连接WIFI热点了,我是放在了ACE模块IDE自建module中,这样就可以在JS中调用了。还可以传递ssid和psk参数,连接指定的热点。如果需要,也可以增加扫描热点并获得热点列表的功能。

在移植的过程中,需要注意的是,BUILD.gn文件需要增加相关配置,头文件目录增加:

  1. "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/"

依赖项deps增加:

  1. "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant"
  2.  
  3. ldflags选项增加"-lwpa_client"。 

 这样应该就可以正常编译了。

运行后观察日志输出,有点多:

  1. 01-01 00:01:32.597 11 60 D 03B00/JS-3RD-APP: [Console Debug] Connecting to ap: huaweim20 
  2. 01-01 00:01:32.597 11 60 I 03900/ACE: ConnectToWifi invoked! 
  3. 01-01 00:01:32.597 11 60 I 03900/ACE: ssid: huaweim20 
  4.  WpaCliSample(DumpString:584):  SendCtrlCommand raw return dump start. 
  5. OK 
  6.  
  7.  WpaCliSample(DumpString:589):  SendCtrlCommand raw return dump end
  8. 01-01 00:01:32.597 11 60 I 03900/ACE: psk: huaweim20 
  9.  WpaCliSample(DumpString:584):  SendCtrlCommand raw return dump start. 
  10.  
  11.  WpaCliSample(DumpString:589):  SendCtrlCommand raw return dump end
  12.  WpaCliSample(ConnectToWifiInner:717):  add network success, network id [1 
  13. 01-01 00:01:32.597 11 60 I 03900/ACE: InitControlInterface 
  14.  WpaCliSample(DumpString:584):  SendCtrlCommand raw return dump start. 
  15. OK 
  16.  
  17.  WpaCliSample(DumpString:589):  SendCtrlCommand raw return dump end
  18. 01-01 00:01:32.623 11 60 I 03900/ACE: ConnectToWifiInner 
  19.  WpaCliSample(DumpString:584):  SendCtrlCommand raw return dump start. 
  20. OK 
  21.  
  22.  WpaCliSample(DumpString:589):  SendCtrlCommand raw return dump end
  23.  WpaCliSample(DumpString:584):  SendCtrlCommand raw return dump start. 
  24. OK 
  25.  
  26.  WpaCliSample(DumpString:589):  SendCtrlCommand raw return dump end
  27.  WpaCliSample(DumpString:584):  SendCtrlCommand raw return dump start. 
  28. OK 
  29.  
  30.  WpaCliSample(DumpString:589):  SendCtrlCommand raw return dump end
  31.  WpaCliSample(ConnectToWifiInner:733):  network config success. 
  32. WIFI: Scan : (null) SSID : 0 
  33.  
  34. [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=15, ret=0 
  35. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=5 
  36. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=5 
  37. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=5 
  38. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=5 
  39. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=5 
  40. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=4 
  41. WifiWpaDriverEventProcess event=5 
  42. WifiWpaEventScanResultProcess: ie_len=248, beacon_ie_len=248 
  43. WifiWpaEventScanResultProcess done 
  44. WifiWpaDriverEventProcess event=5 
  45. WifiWpaEventScanResultProcess: ie_len=310, beacon_ie_len=310 
  46. WifiWpaEventScanResultProcess done 
  47. WifiWpaDriverEventProcess event=5 
  48. WifiWpaEventScanResultProcess: ie_len=226, beacon_ie_len=226 
  49. WifiWpaEventScanResultProcess done 
  50. WifiWpaDriverEventProcess event=5 
  51. WifiWpaEventScanResultProcess: ie_len=243, beacon_ie_len=243 
  52. WifiWpaEventScanResultProcess done 
  53. WifiWpaDriverEventProcess event=5 
  54. WifiWpaEventScanResultProcess: ie_len=198, beacon_ie_len=198 
  55. WifiWpaEventScanResultProcess done 
  56. WifiWpaDriverEventProcess event=4 
  57. WifiWpaGetScanResults2 done 
  58. WifiWpaEventScanDoneProcess done 
  59. wlan0: Trying to associate with bc:e2:65:3c:19:70 (SSID='huaweim20' freq=2462 MHz) 
  60. [97854][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  61. [E]oal_exception_submit, g_pst_exception_info is null 
  62. [97859][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  63. [E]oal_exception_submit, g_pst_exception_info is null 
  64. [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=17, ret=0 
  65. WifiWpaAssociate done ret=0 
  66. [97876][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  67. [E]oal_exception_submit, g_pst_exception_info is null 
  68. [97887][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  69. [E]oal_exception_submit, g_pst_exception_info is null 
  70. [97897][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  71. [E]oal_exception_submit, g_pst_exception_info is null 
  72. [97907][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  73. [E]oal_exception_submit, g_pst_exception_info is null 
  74. [97917][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  75. [E]oal_exception_submit, g_pst_exception_info is null 
  76. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=13 
  77. [97931][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  78. WifiWpaDriverEventProcess event=13[E]oal_exception_submit, g_pst_exception_info is null 
  79.  
  80. [97944][E:1786]{oal_sdio_transfer_scatt::write failed=-84} 
  81. WifiWpaDriverEventEapolRecvProcess call[E]oal_exception_submit, g_pst_exception_info is null 
  82.  
  83. [ERR] 
  84.  l2_packet_receive1 
  85.  [HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=6 
  86.  
  87.  l2_packet_receive2 
  88.  [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=6, ret=0 
  89.  
  90.  l2_packet_receive3 
  91.  
  92.  rx_callback 
  93.  WifiWpaReceiveEapol done 
  94. WifiWpaDriverEventProcess event=6 
  95. wlan0: Associated with bc:e2:65:3c:19:70 
  96. [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=5, ret=0 
  97. WifiWpaWpaSendEapol done ret=0 
  98. wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0 
  99. WifiWpaEventConnectResultProcess done 
  100. [ERR][HDF:E/HDF_LOG_TAG]HdfWifiSendEvent event=13 
  101. WifiWpaDriverEventProcess event=13 
  102. WifiWpaDriverEventEapolRecvProcess call 
  103.  
  104.  l2_packet_receive1 
  105.  
  106.  l2_packet_receive2 
  107.  [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=6, ret=0 
  108.  
  109.  l2_packet_receive3 
  110.  
  111.  rx_callback 
  112.  [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=5, ret=0 
  113. WifiWpaWpaSendEapol done ret=0 
  114. [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=1, ret=0 
  115. [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=3, ret=0 
  116. [HDF:I/HDF_LOG_TAG]WifiWpaCmdBlockSyncSend: cmd=1, ret=0 
  117. wlan0: WPA: Key negotiation completed with bc:e2:65:3c:19:70 [PTK=CCMP GTK=CCMP] 
  118. wlan0: CTRL-EVENT-CONNECTED - Connection to bc:e2:65:3c:19:70 completed [id=1 id_str=] 
  119. WifiWpaReceiveEapol done 
  120. [ERR][HDF:E/NetDeviceLite]LiteNetDhcpIsBound fail, ret = -5! 
  121. [ERR][HDF:E/NetDeviceLite]LiteNetDhcpIsBound fail, ret = -5! 

能清楚的看到SendCtrlCommand的过程。最后看到wlan0: CTRL-EVENT-CONNECTED,就是连接成功了。同时手机热点已连接设备数显示为1。

稍后,我会做一个带有界面的视频Demo,展示效果。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-03-29 15:36:46

鸿蒙HarmonyOS应用

2021-03-16 09:49:16

鸿蒙HarmonyOS应用

2021-07-09 14:20:23

鸿蒙HarmonyOS应用

2021-05-25 14:47:43

鸿蒙HarmonyOS应用

2021-11-09 15:28:41

鸿蒙HarmonyOS应用

2022-04-15 14:45:49

Hi3516系统类型烧录鸿蒙

2021-12-03 09:50:39

鸿蒙HarmonyOS应用

2021-06-25 09:28:46

鸿蒙HarmonyOS应用

2021-09-24 10:20:42

鸿蒙HarmonyOS应用

2021-07-21 09:58:50

鸿蒙HarmonyOS应用

2021-03-02 14:30:20

鸿蒙HarmonyOS应用

2021-10-09 10:12:39

鸿蒙HarmonyOS应用

2021-07-19 15:34:05

鸿蒙HarmonyOS应用

2020-10-16 09:50:37

Hi3861WiFi热点

2022-02-16 16:01:02

Hi3516开发板鸿蒙

2021-07-07 09:45:20

鸿蒙HarmonyOS应用

2021-08-06 15:09:22

鸿蒙HarmonyOS应用

2021-07-08 16:16:59

鸿蒙HarmonyOS应用

2022-03-14 15:26:59

Hi3516Ark子系统鸿蒙

2021-07-05 09:35:36

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号