自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hi3516如何連接WiFi(三)

系統(tǒng)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com

[[391922]]

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

小伙伴們大家好,上一篇(Hi3516如何連接Wifi(二))介紹了用程序啟動(dòng)wap_supplicant,也就是Wifi的Daemon。下一步就是如何向Daemon發(fā)信息,我們可以參考//applications/sample/camera/communication/wpa_cli,他實(shí)現(xiàn)了連接Daemon、掃描熱點(diǎn)、連接熱點(diǎn)等功能。

我們打開wpa_cli_sample.c文件,核心功能在于這幾個(gè)函數(shù):

  • InitControlInterface:初始化
  • TestScan:掃描周圍熱點(diǎn)
  • TestNetworkConfig:連接到指定熱點(diǎn)

首先看InitControlInterface函數(shù),他先調(diào)用了wpa_ctrl_open函數(shù),獲取了用于發(fā)送命令的控制接口g_ctrlConn,是一個(gè)類型為struct wpa_ctrl的結(jié)構(gòu)體。又調(diào)用一遍wpa_ctrl_open打開了一個(gè)用于事件監(jiān)控的控制接口g_monitorConn。然后啟動(dòng)了事件監(jiān)控的線程執(zhí)行MonitorTask函數(shù),這個(gè)監(jiān)控線程不是必須,可以省略。

  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函數(shù)。實(shí)際上就是一系列SendCtrlCommand向Daemon發(fā)送指令。

  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:心跳檢測命令??蛻舳擞盟袛郬PAS是否工作正常。WPAS收到”PING”命令后需要回復(fù)“PONG”。
  • MIB:客戶端用該命令獲取設(shè)備的MIB信息。
  • STATUS:客戶端用該命令來獲取WPAS的工作狀態(tài)。
  • ADD_NETWORK:為WPAS添加一個(gè)新的無線網(wǎng)絡(luò)。它將返回此新無線網(wǎng)絡(luò)的id(從0開始)。注意:此network id非常重要,客戶端后續(xù)將通過它來指明自己想操作的無線網(wǎng)絡(luò)。
  • SET_NETWORK :network id是無線網(wǎng)絡(luò)的id。此命令用于設(shè)置指定無線網(wǎng)絡(luò)的信息。其中variable為參數(shù)名,value為參數(shù)的值。
  • ENABLE_NETWORK:使能某個(gè)無線網(wǎng)絡(luò)。此命令最終將促使WPAS發(fā)起一系列操作以加入該無線網(wǎng)絡(luò)。
  • SCAN: 掃描附近AP
  • SCAN_RESULT:列出最近一次掃描的結(jié)果
  • LIST_NETWORKS: 列出添加的所有AP

看完上邊就很好理解了,先是DISCONNECT斷開已有連接,ADD_NETWORK添加一個(gè)新的無線網(wǎng)絡(luò),SET_NETWORK設(shè)置ssid和psk,ENABLE_NETWORK使能這個(gè)無線網(wǎng)絡(luò),最后一個(gè)RECONNECT重新連接有點(diǎn)迷,去掉應(yīng)該也沒關(guān)系,不過本人沒有嘗試。

TestScan函數(shù)就留給讀者自行分析。

了解了這些,我們就可以在自己的代碼中去連接WIFI熱點(diǎn)了,我是放在了ACE模塊IDE自建module中,這樣就可以在JS中調(diào)用了。還可以傳遞ssid和psk參數(shù),連接指定的熱點(diǎn)。如果需要,也可以增加掃描熱點(diǎn)并獲得熱點(diǎn)列表的功能。

在移植的過程中,需要注意的是,BUILD.gn文件需要增加相關(guān)配置,頭文件目錄增加:

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

依賴項(xiàng)deps增加:

  1. "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant"
  2.  
  3. ldflags選項(xiàng)增加"-lwpa_client"。 

 這樣應(yīng)該就可以正常編譯了。

運(yùn)行后觀察日志輸出,有點(diǎn)多:

  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,就是連接成功了。同時(shí)手機(jī)熱點(diǎn)已連接設(shè)備數(shù)顯示為1。

稍后,我會做一個(gè)帶有界面的視頻Demo,展示效果。

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-03-29 15:36:46

鴻蒙HarmonyOS應(yīng)用

2021-03-16 09:49:16

鴻蒙HarmonyOS應(yīng)用

2021-07-09 14:20:23

鴻蒙HarmonyOS應(yīng)用

2022-04-15 14:45:49

Hi3516系統(tǒng)類型燒錄鴻蒙

2021-05-25 14:47:43

鴻蒙HarmonyOS應(yīng)用

2021-11-09 15:28:41

鴻蒙HarmonyOS應(yīng)用

2021-12-03 09:50:39

鴻蒙HarmonyOS應(yīng)用

2021-06-25 09:28:46

鴻蒙HarmonyOS應(yīng)用

2021-09-24 10:20:42

鴻蒙HarmonyOS應(yīng)用

2021-07-21 09:58:50

鴻蒙HarmonyOS應(yīng)用

2021-07-19 15:34:05

鴻蒙HarmonyOS應(yīng)用

2021-10-09 10:12:39

鴻蒙HarmonyOS應(yīng)用

2021-03-02 14:30:20

鴻蒙HarmonyOS應(yīng)用

2022-02-16 16:01:02

Hi3516開發(fā)板鴻蒙

2020-10-16 09:50:37

Hi3861WiFi熱點(diǎn)

2021-08-06 15:09:22

鴻蒙HarmonyOS應(yīng)用

2021-07-07 09:45:20

鴻蒙HarmonyOS應(yīng)用

2022-03-14 15:26:59

Hi3516Ark子系統(tǒng)鴻蒙

2021-07-05 09:35:36

鴻蒙HarmonyOS應(yīng)用

2021-07-08 16:16:59

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號