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

OpenHarmony輕量系統(tǒng)開發(fā)WiFi之STA模式連接熱點(diǎn)

原創(chuàng)
系統(tǒng) OpenHarmony
本文簡(jiǎn)單介紹Hi3861WiFi操作,怎么連接到熱點(diǎn),查看IP,ping服務(wù)器等.

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

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

??https://harmonyos.51cto.com??

摘要:本文簡(jiǎn)單介紹Hi3861WiFi操作,怎么連接到熱點(diǎn),查看IP,ping服務(wù)器等

適合群體:適用于潤(rùn)和Hi3861開發(fā)板

文中所有代碼倉庫:https://gitee.com/qidiyun/hihope-3861-smart-home-kit

9.1AT指令操作WiFi

我們可以使用AT指令進(jìn)行Hi3861 WiFi操作,連接熱點(diǎn)、ping服務(wù)器等。

但是很多時(shí)候,我們需要實(shí)現(xiàn)開機(jī)后自動(dòng)連接到某個(gè)熱點(diǎn),光靠AT指令不行。

Hi3861 為我們提供了WiFi操作的相關(guān)API,方便我們編寫代碼,實(shí)現(xiàn)熱點(diǎn)連接。

9.2 代碼實(shí)現(xiàn)

先直接上代碼和操作演示。

跟我們最早的hello world代碼一樣,在app下新增業(yè)務(wù)demo_wifi_sta,其中demo_wifi_sta.c為業(yè)務(wù)代碼,BUILD.gn為編譯腳本,具體規(guī)劃目錄結(jié)構(gòu)如下:

其中BUILD.gn文件內(nèi)容如下:

static_library("demo_wifi_sta") {
sources = [
"demo_wifi_sta.c"
]

include_dirs = [
"http://utils/native/lite/include",
"http://kernel/liteos_m/components/cmsis/2.0",
"http://base/iot_hardware/peripheral/interfaces/kits",
"http://device/soc/hisilicon/hi3861v100/hi3861_adapter/hals/communication/wifi_lite/wifiservice",
"http://device/soc/hisilicon/hi3861v100/hi3861_adapter/kal",
"http://device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/lwip_sack/include",
]
}

hi_wifi_start_sta函數(shù):設(shè)置WiFi參數(shù)、掃描熱點(diǎn)

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;

//這里不需要重復(fù)進(jìn)行WiFi init,因?yàn)橄到y(tǒng)啟動(dòng)后就自己會(huì)做WiFi init
#if 0
printf("_______>>>>>>>>>> %s %d \r\n", __FILE__, __LINE__);
ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
if (ret != HISI_OK) {
return -1;
}
#endif
ret = hi_wifi_sta_start(ifname, &len);
if (ret != HISI_OK) {
return -1;
}

/* register call back function to receive wifi event, etc scan results event,
* connected event, disconnected event.
*/
ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
if (ret != HISI_OK) {
printf("register wifi event callback failed\n");
}

/* acquire netif for IP operation */
g_lwip_netif = netifapi_netif_find(ifname);
if (g_lwip_netif == NULL) {
printf("%s: get netif failed\n", __FUNCTION__);
return -1;
}

/* 開始掃描附件的WiFi熱點(diǎn) */
ret = hi_wifi_sta_scan();
if (ret != HISI_OK) {
return -1;
}

sleep(5); /* sleep 5s, waiting for scan result. */

hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
if (pst_results == NULL) {
return -1;
}

//把掃描到的熱點(diǎn)結(jié)果存儲(chǔ)起來
ret = hi_wifi_sta_scan_results(pst_results, &num);
if (ret != HISI_OK) {
free(pst_results);
return -1;
}

//打印掃描到的所有熱點(diǎn)
for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
printf("SSID: %s\n", pst_results[loop].ssid);
}
free(pst_results);

/* 開始接入熱點(diǎn) */
ret = hi_wifi_start_connect();
if (ret != 0) {
return -1;
}
return 0;
}

連接熱點(diǎn):

int hi_wifi_start_connect(void)
{
int ret;
errno_t rc;
hi_wifi_assoc_request assoc_req = {0};

/* copy SSID to assoc_req */
rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "RedmiK40", 8); /* 9:ssid length */
if (rc != EOK) {
return -1;
}

//熱點(diǎn)加密方式
assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

/* 熱點(diǎn)密碼 */
memcpy(assoc_req.key, "07686582488", 11);

ret = hi_wifi_sta_connect(&assoc_req);
if (ret != HISI_OK) {
return -1;
}

return 0;
}

熱點(diǎn)連接結(jié)果回調(diào)函數(shù)

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 available\n");
break;
case HI_WIFI_EVT_CONNECTED:
printf("WiFi: Connected\n");
netifapi_dhcp_start(g_lwip_netif);
break;
case HI_WIFI_EVT_DISCONNECTED:
printf("WiFi: Disconnected\n");
netifapi_dhcp_stop(g_lwip_netif);
hi_sta_reset_addr(g_lwip_netif);
break;
case HI_WIFI_EVT_WPS_TIMEOUT:
printf("WiFi: wps is timeout\n");
break;
default:
break;
}
}

hi_sta_reset_addr:重新復(fù)位sta的地址、網(wǎng)關(guān)等參數(shù)。

/* clear netif's ip, gateway and netmask */
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;

if (pst_lwip_netif == NULL) {
printf("hisi_reset_addr::Null param of netdev\r\n");
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);
}

9.3 WiFi相關(guān)API

Hi3861 提供了非常多的wifi相關(guān)API,主要文件是 hi_wifi_api.h

我們這里只列舉最重要的幾個(gè)API

(1)開啟STA

int hi_wifi_sta_start(char *ifname, int *len);

(2)停止STA

int hi_wifi_sta_stop(void);

(3)掃描附件的熱點(diǎn)

int hi_wifi_sta_scan(void);

(4)連接熱點(diǎn)

int hi_wifi_sta_connect(hi_wifi_assoc_request *req);

其中hi_wifi_assoc_request *req 結(jié)構(gòu)的定義如下:

typedef struct {
char ssid[HI_WIFI_MAX_SSID_LEN + 1]; /**< SSID. CNcomment: SSID 只支持ASCII字符.CNend */
hi_wifi_auth_mode auth; /**< Authentication mode. CNcomment: 認(rèn)證類型.CNend */
char key[HI_WIFI_MAX_KEY_LEN + 1]; /**< Secret key. CNcomment: 秘鑰.CNend */
unsigned char bssid[HI_WIFI_MAC_LEN]; /**< BSSID. CNcomment: BSSID.CNend */
hi_wifi_pairwise pairwise; /**< Encryption type. CNcomment: 加密方式,不需指定時(shí)置0.CNend */
} hi_wifi_assoc_request;

這里需要注意的是,通常加密方式是:HI_WIFI_SECURITY_WPA2PSK

例如我家的熱點(diǎn)的連接方式的代碼實(shí)現(xiàn)如下:

int hi_wifi_start_connect(void)
{
int ret;
errno_t rc;
hi_wifi_assoc_request assoc_req = {0};

/* copy SSID to assoc_req */
rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "RedmiK40", 8); /* 9:ssid length */
if (rc != EOK) {
return -1;
}

//熱點(diǎn)加密方式
assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

/* 熱點(diǎn)密碼 */
memcpy(assoc_req.key, "07686582488", 11);

ret = hi_wifi_sta_connect(&assoc_req);
if (ret != HISI_OK) {
return -1;
}

return 0;
}

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

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

??https://harmonyos.51cto.com??

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

2022-01-21 21:22:24

OpenHarmon操作系統(tǒng)鴻蒙

2022-02-09 19:45:41

MQTTOpenHarmon鴻蒙

2022-02-10 15:07:10

云平臺(tái)OpenHarmon系統(tǒng)開發(fā)

2023-04-03 15:39:31

2022-02-08 15:21:59

Hi3861開發(fā)鴻蒙

2023-03-24 14:39:17

鴻蒙系統(tǒng)開發(fā)

2022-01-24 18:35:56

OpenHarmon鴻蒙操作系統(tǒng)

2022-01-24 18:43:20

OpenHarmon操作系統(tǒng)鴻蒙

2022-05-24 15:46:51

Wi-FiSTA模式

2022-02-09 19:31:41

Hi3861OpenHarmon鴻蒙

2022-04-15 14:31:02

鴻蒙操作系統(tǒng)

2024-01-05 15:32:47

鴻蒙SNTP智慧時(shí)鐘

2022-04-15 11:46:09

輕量系統(tǒng)解耦鴻蒙操作系統(tǒng)

2022-09-06 15:25:22

Wifi設(shè)備開發(fā)

2020-11-02 11:56:57

鴻蒙 WiFi操作

2021-03-16 09:49:16

鴻蒙HarmonyOS應(yīng)用

2023-07-28 15:32:26

鴻蒙操作系統(tǒng)

2022-08-12 19:13:07

etswifi連接操作

2020-10-16 09:50:37

Hi3861WiFi熱點(diǎn)

2023-06-13 14:55:04

點(diǎn)贊
收藏

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