【開發(fā)板試用報(bào)告】HarmonyOS之HelloWorld,WIFI連接,Socket tcp
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
開發(fā)環(huán)境都準(zhǔn)備就緒以后,就迫不及待的想來(lái)一個(gè)入門篇-HelloWorld,
HelloWorld 1.首先在..\applications\sample\wifi-iot\app目錄下創(chuàng)建my_first_app目錄 2 創(chuàng)建hello_world.c文件 并寫入代.3 創(chuàng)建BUILD.gn文件 4 進(jìn)入代碼根目錄python build.py wifiiot 編譯 5 使用VS_Code中的的插件DevEco Device Tool就行燒錄
 
- static_library("myapp") {
 - sources = [
 - "hello_world.c"
 - ]
 - include_dirs = [
 - "//utils/native/lite/include"
 - ]
 - }
 
- #include <stdio.h>
 - #include "ohos_init.h"
 - #include "ohos_types.h"
 - void HelloWorld(void)
 - {
 - printf("[xm] Hello world.\n");
 - }
 - SYS_RUN(HelloWorld);
 
WIFI連接及Socket tcp測(cè)試 1 首先在..\applications\sample\wifi-iot\app目錄下創(chuàng)建sta_demo目錄 2 編寫代碼 3 創(chuàng)建BUILD.gn文件 4 編譯 5燒錄(注意如果想多個(gè)功能一起使用,必須在app下的BUILD.gn中進(jìn)行配置。

- #include <stdio.h>
 - #include <unistd.h>
 - #include "ohos_init.h"
 - #include "cmsis_os2.h"
 - #include <unistd.h>
 - #include "hi_wifi_api.h"
 - //#include "wifi_sta.h"
 - #include "lwip/ip_addr.h"
 - #include "lwip/netifapi.h"
 - static struct netif *g_lwip_netif = NULL;
 - /* 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;
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - 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);
 - }
 - 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;
 - }
 - }
 - int hi_wifi_start_connect(void)
 - {
 - int ret;
 - errno_t rc;
 - hi_wifi_assoc_request assoc_req = {0};
 - /* copy SSID to assoc_req */
 - //熱點(diǎn)名稱
 - rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "CU_K22k", 7); /* 9:ssid length */
 - if (rc != EOK)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - return -1;
 - }
 - /*
 - * OPEN mode
 - * for WPA2-PSK mode:
 - * set assoc_req.auth as HI_WIFI_SECURITY_WPA2PSK,
 - * then memcpy(assoc_req.key, "12345678", 8).
 - */
 - //熱點(diǎn)加密方式
 - assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
 - /* 熱點(diǎn)密碼 */
 - memcpy(assoc_req.key, "tkhbx8ec", 8);
 - ret = hi_wifi_sta_connect(&assoc_req);
 - if (ret != HISI_OK)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - return -1;
 - }
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - hi_wifi_sta_get_ap_rssi
 - return 0;
 - }
 - void sta_demo(void)
 - {
 - int ret;
 - char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
 - int len = sizeof(ifname);
 - unsigned int num = WIFI_SCAN_AP_LIMIT;
 - ret = hi_wifi_sta_start(ifname, &len);
 - if (ret != HISI_OK)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - return;
 - }
 - /* 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;
 - }
 - /* start scan, scan results event will be received soon */
 - ret = hi_wifi_sta_scan();
 - if (ret != HISI_OK)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - return;
 - }
 - 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)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - return;
 - }
 - ret = hi_wifi_sta_scan_results(pst_results, &num);
 - if (ret != HISI_OK)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - free(pst_results);
 - return;
 - }
 - for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++)
 - {
 - printf("SSID: %s\n", pst_results[loop].ssid);
 - }
 - free(pst_results);
 - /* if received scan results, select one SSID to connect */
 - ret = hi_wifi_start_connect();
 - if (ret != 0)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - return;
 - }
 - return;
 - }
 - #include "lwip/sockets.h"
 - #define SERVER_PORT_TCP 8888
 - #define TCP_BACKLOG 10
 - int sock_fd, new_fd;
 - char recvbuf[512];
 - char *buf = "hello I'm Server Your ?";
 - int tcp_demo(void)
 - {
 - //自己的地址信息
 - struct sockaddr_in my_addr;
 - //連接者的地址信息
 - struct sockaddr_in their_addr;
 - int sin_size;
 - struct sockaddr_in *cli_addr;
 - /* 創(chuàng)建socket */
 - if ((sock_fd = socket(AF_INET, SOCK_STREAM,0)) == -1)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - perror("socket is error \r\n");
 - exit(1);
 - }
 - /*主機(jī)字節(jié)順序*/
 - /*協(xié)議*/
 - my_addr.sin_family = AF_INET;
 - my_addr.sin_port = htons(8888);
 - /*當(dāng)前ip地址寫入*/
 - my_addr.sin_addr.s_addr = INADDR_ANY;
 - /*將結(jié)構(gòu)體其余的都清零*/
 - bzero(&(my_addr.sin_zero), 8);
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - if (bind(sock_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - perror("bind is error \r\n");
 - exit(1);
 - }
 - /*開始監(jiān)聽(tīng)*/
 - if (listen(sock_fd, TCP_BACKLOG) == -1)
 - {
 - perror("listen is error \r\n");
 - exit(1);
 - }
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - printf("START ACCEPT -----------");
 - while (1)
 - {
 - sin_size = sizeof(struct sockaddr_in);
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - if ((new_fd = accept(sock_fd, (struct sockaddr *)&their_addr, (socklen_t *)&sin_size)) == -1)
 - {
 - perror("accept");
 - continue;
 - }
 - cli_addr = malloc(sizeof(struct sockaddr));
 - printf("accept addr \r\n");
 - if (cli_addr != NULL)
 - {
 - memcpy(cli_addr, &their_addr, sizeof(struct sockaddr));
 - }
 - //處理目標(biāo)
 - ssize_t ret;
 - while (1)
 - {
 - printf("%s %d \r\n", __FILE__, __LINE__);
 - if ((ret = recv(new_fd, recvbuf, sizeof(recvbuf), 0)) == -1)
 - {
 - printf("recv error \r\n");
 - return -1;
 - }
 - printf("recv: \r\n");
 - printf("%s", recvbuf);
 - printf(" \r\n");
 - sleep(2);
 - if ((ret = send(new_fd, buf, strlen(buf) + 1, 0)) == -1)
 - {
 - perror("send :error");
 - }
 - sleep(2);
 - }
 - close(new_fd);
 - return 0;
 - }
 - }
 - void tcp_entry(void)
 - {
 - sta_demo();
 - tcp_demo();
 - }
 - SYS_RUN(tcp_entry);
 - 
 
- # Copyright (c) 2020 Huawei Device 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.
 - static_library("sta_demo") {
 - sources = [
 - "sta_demo.c"
 - ]
 - include_dirs = [
 - "//utils/native/lite/include",
 - "//kernel/liteos_m/components/cmsis/2.0",
 - "//base/iot_hardware/interfaces/kits/wifiiot_lite",
 - "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include",
 - "//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
 - ]
 - }
 
運(yùn)行結(jié)果
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz


















 
 
 





 
 
 
 