【开发板试用报告】HarmonyOS之HelloWorld,WIFI连接,Socket tcp

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

[[353334]]

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

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

https://harmonyos.51cto.com/#zz

 开发环境都准备就绪以后,就迫不及待的想来一个入门篇-HelloWorld,

HelloWorld 1.首先在..\applications\sample\wifi-iot\app目录下创建my_first_app目录 2 创建hello_world.c文件 并写入代.3 创建BUILD.gn文件 4 进入代码根目录python build.py wifiiot 编译 5 使用VS_Code中的的插件DevEco Device Tool就行烧录

 
  1. static_library("myapp") { 
  2.     sources = [ 
  3.         "hello_world.c" 
  4.     ] 
  5.     include_dirs = [ 
  6.         "//utils/native/lite/include" 
  7.     ] 

  1. #include <stdio.h> 
  2. #include "ohos_init.h" 
  3. #include "ohos_types.h" 
  4.   
  5. void HelloWorld(void) 
  6.     printf("[xm] Hello world.\n"); 
  7. SYS_RUN(HelloWorld);​ 

 WIFI连接及Socket tcp测试 1 首先在..\applications\sample\wifi-iot\app目录下创建sta_demo目录 2 编写代码 3 创建BUILD.gn文件 4 编译 5烧录(注意如果想多个功能一起使用,必须在app下的BUILD.gn中进行配置。 


  1. #include <stdio.h> 
  2.  
  3. #include <unistd.h> 
  4.  
  5. #include "ohos_init.h" 
  6. #include "cmsis_os2.h" 
  7.  
  8. #include <unistd.h> 
  9. #include "hi_wifi_api.h" 
  10. //#include "wifi_sta.h" 
  11. #include "lwip/ip_addr.h" 
  12. #include "lwip/netifapi.h" 
  13.  
  14. static struct netif *g_lwip_netif = NULL
  15.  
  16. /* clear netif's ip, gateway and netmask */ 
  17. void hi_sta_reset_addr(struct netif *pst_lwip_netif) 
  18.     ip4_addr_t st_gw; 
  19.     ip4_addr_t st_ipaddr; 
  20.     ip4_addr_t st_netmask; 
  21.     printf("%s %d \r\n", __FILE__, __LINE__); 
  22.     if (pst_lwip_netif == NULL
  23.     { 
  24.         printf("hisi_reset_addr::Null param of netdev\r\n"); 
  25.         return
  26.     } 
  27.  
  28.     IP4_ADDR(&st_gw, 0, 0, 0, 0); 
  29.     IP4_ADDR(&st_ipaddr, 0, 0, 0, 0); 
  30.     IP4_ADDR(&st_netmask, 0, 0, 0, 0); 
  31.  
  32.     netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw); 
  33.  
  34. void wifi_wpa_event_cb(const hi_wifi_event *hisi_event) 
  35.     if (hisi_event == NULL
  36.         return
  37.  
  38.     switch (hisi_event->event) 
  39.     { 
  40.     case HI_WIFI_EVT_SCAN_DONE: 
  41.         printf("WiFi: Scan results available\n"); 
  42.         break; 
  43.     case HI_WIFI_EVT_CONNECTED: 
  44.         printf("WiFi: Connected\n"); 
  45.         netifapi_dhcp_start(g_lwip_netif); 
  46.         break; 
  47.     case HI_WIFI_EVT_DISCONNECTED: 
  48.         printf("WiFi: Disconnected\n"); 
  49.         netifapi_dhcp_stop(g_lwip_netif); 
  50.         hi_sta_reset_addr(g_lwip_netif); 
  51.         break; 
  52.     case HI_WIFI_EVT_WPS_TIMEOUT: 
  53.         printf("WiFi: wps is timeout\n"); 
  54.         break; 
  55.     default
  56.         break; 
  57.     } 
  58.  
  59. int hi_wifi_start_connect(void) 
  60.     int ret; 
  61.     errno_t rc; 
  62.     hi_wifi_assoc_request assoc_req = {0}; 
  63.  
  64.     /* copy SSID to assoc_req */ 
  65.     //热点名称 
  66.     rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "CU_K22k", 7); /* 9:ssid length */ 
  67.     if (rc != EOK) 
  68.     { 
  69.         printf("%s %d \r\n", __FILE__, __LINE__); 
  70.         return -1; 
  71.     } 
  72.  
  73.     /* 
  74.      * OPEN mode 
  75.      * for WPA2-PSK mode: 
  76.      * set assoc_req.auth as HI_WIFI_SECURITY_WPA2PSK, 
  77.      * then memcpy(assoc_req.key"12345678", 8). 
  78.      */ 
  79.     //热点加密方式 
  80.     assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK; 
  81.  
  82.     /* 热点密码 */ 
  83.     memcpy(assoc_req.key"tkhbx8ec", 8); 
  84.  
  85.     ret = hi_wifi_sta_connect(&assoc_req); 
  86.     if (ret != HISI_OK) 
  87.     { 
  88.         printf("%s %d \r\n", __FILE__, __LINE__); 
  89.         return -1; 
  90.     } 
  91.     printf("%s %d \r\n", __FILE__, __LINE__); 
  92.     hi_wifi_sta_get_ap_rssi 
  93.     return 0; 
  94.  
  95. void sta_demo(void) 
  96.     int ret; 
  97.     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0}; 
  98.     int len = sizeof(ifname); 
  99.     unsigned int num = WIFI_SCAN_AP_LIMIT; 
  100.  
  101.     ret = hi_wifi_sta_start(ifname, &len); 
  102.     if (ret != HISI_OK) 
  103.     { 
  104.         printf("%s %d \r\n", __FILE__, __LINE__); 
  105.         return
  106.     } 
  107.  
  108.     /* register call back function to receive wifi event, etc scan results event, 
  109.      * connected event, disconnected event. 
  110.      */ 
  111.     ret = hi_wifi_register_event_callback(wifi_wpa_event_cb); 
  112.     if (ret != HISI_OK) 
  113.     { 
  114.         printf("register wifi event callback failed\n"); 
  115.     } 
  116.  
  117.     /* acquire netif for IP operation */ 
  118.     g_lwip_netif = netifapi_netif_find(ifname); 
  119.     if (g_lwip_netif == NULL
  120.     { 
  121.         printf("%s: get netif failed\n", __FUNCTION__); 
  122.         return
  123.     } 
  124.  
  125.     /* start scan, scan results event will be received soon */ 
  126.     ret = hi_wifi_sta_scan(); 
  127.     if (ret != HISI_OK) 
  128.     { 
  129.         printf("%s %d \r\n", __FILE__, __LINE__); 
  130.         return
  131.     } 
  132.  
  133.     sleep(5); /* sleep 5s, waiting for scan result. */ 
  134.  
  135.     hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT); 
  136.     if (pst_results == NULL
  137.     { 
  138.         printf("%s %d \r\n", __FILE__, __LINE__); 
  139.         return
  140.     } 
  141.  
  142.     ret = hi_wifi_sta_scan_results(pst_results, &num); 
  143.     if (ret != HISI_OK) 
  144.     { 
  145.         printf("%s %d \r\n", __FILE__, __LINE__); 
  146.         free(pst_results); 
  147.         return
  148.     } 
  149.  
  150.     for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) 
  151.     { 
  152.         printf("SSID: %s\n", pst_results[loop].ssid); 
  153.     } 
  154.     free(pst_results); 
  155.  
  156.     /* if received scan results, select one SSID to connect */ 
  157.     ret = hi_wifi_start_connect(); 
  158.     if (ret != 0) 
  159.     { 
  160.         printf("%s %d \r\n", __FILE__, __LINE__); 
  161.         return
  162.     } 
  163.  
  164.     return
  165. #include "lwip/sockets.h" 
  166. #define SERVER_PORT_TCP 8888 
  167. #define TCP_BACKLOG 10 
  168. int sock_fd, new_fd; 
  169. char recvbuf[512]; 
  170. char *buf = "hello I'm Server Your ?"
  171.  
  172. int tcp_demo(void) 
  173.  
  174.     //自己的地址信息 
  175.     struct sockaddr_in my_addr; 
  176.     //连接者的地址信息 
  177.     struct sockaddr_in their_addr; 
  178.     int sin_size; 
  179.     struct sockaddr_in *cli_addr; 
  180.  
  181.     /* 创建socket */ 
  182.     if ((sock_fd = socket(AF_INET, SOCK_STREAM,0)) == -1) 
  183.     { 
  184.         printf("%s %d \r\n", __FILE__, __LINE__); 
  185.         perror("socket is error \r\n"); 
  186.         exit(1); 
  187.     } 
  188.     /*主机字节顺序*/ 
  189.     /*协议*/ 
  190.     my_addr.sin_family = AF_INET; 
  191.     my_addr.sin_port = htons(8888); 
  192.     /*当前ip地址写入*/ 
  193.     my_addr.sin_addr.s_addr = INADDR_ANY; 
  194.  
  195.     /*将结构体其余的都清零*/ 
  196.     bzero(&(my_addr.sin_zero), 8); 
  197.  
  198.     printf("%s %d \r\n", __FILE__, __LINE__); 
  199.  
  200.     if (bind(sock_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) 
  201.     { 
  202.         printf("%s %d \r\n", __FILE__, __LINE__); 
  203.         perror("bind is error \r\n"); 
  204.         exit(1); 
  205.     } 
  206.     /*开始监听*/ 
  207.     if (listen(sock_fd, TCP_BACKLOG) == -1) 
  208.     { 
  209.         perror("listen is error \r\n"); 
  210.         exit(1); 
  211.     } 
  212.     printf("%s %d \r\n", __FILE__, __LINE__); 
  213.     printf("START ACCEPT -----------"); 
  214.  
  215.     while (1) 
  216.     { 
  217.         sin_size = sizeof(struct sockaddr_in); 
  218.         printf("%s %d \r\n", __FILE__, __LINE__); 
  219.         if ((new_fd = accept(sock_fd, (struct sockaddr *)&their_addr, (socklen_t *)&sin_size)) == -1) 
  220.         { 
  221.             perror("accept"); 
  222.             continue
  223.         } 
  224.         cli_addr = malloc(sizeof(struct sockaddr)); 
  225.  
  226.         printf("accept addr \r\n"); 
  227.         if (cli_addr != NULL
  228.         { 
  229.             memcpy(cli_addr, &their_addr, sizeof(struct sockaddr)); 
  230.         } 
  231.         //处理目标 
  232.         ssize_t ret; 
  233.  
  234.         while (1) 
  235.         { 
  236.             printf("%s %d \r\n", __FILE__, __LINE__); 
  237.             if ((ret = recv(new_fd, recvbuf, sizeof(recvbuf), 0)) == -1) 
  238.             { 
  239.                 printf("recv error \r\n"); 
  240.                 return -1; 
  241.             } 
  242.             printf("recv: \r\n"); 
  243.             printf("%s", recvbuf); 
  244.             printf(" \r\n"); 
  245.             sleep(2); 
  246.             if ((ret = send(new_fd, buf, strlen(buf) + 1, 0)) == -1) 
  247.             { 
  248.                 perror("send :error"); 
  249.             } 
  250.             sleep(2); 
  251.         } 
  252.         close(new_fd); 
  253.         return 0; 
  254.     } 
  255.  
  256. void tcp_entry(void) 
  257.  
  258.     sta_demo(); 
  259.     tcp_demo(); 
  260.  
  261. SYS_RUN(tcp_entry); 
  262. ​ 

  1. # Copyright (c) 2020 Huawei Device Co., Ltd. 
  2. # Licensed under the Apache License, Version 2.0 (the "License"); 
  3. # you may not use this file except in compliance with the License. 
  4. # You may obtain a copy of the License at 
  5. #     http://www.apache.org/licenses/LICENSE-2.0 
  6. # Unless required by applicable law or agreed to in writing, software 
  7. # distributed under the License is distributed on an "AS IS" BASIS, 
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  9. # See the License for the specific language governing permissions and 
  10. # limitations under the License. 
  11.  
  12. static_library("sta_demo") { 
  13.     sources = [ 
  14.         "sta_demo.c" 
  15.     ] 
  16.  
  17.     include_dirs = [ 
  18.         "//utils/native/lite/include"
  19.         "//kernel/liteos_m/components/cmsis/2.0"
  20.         "//base/iot_hardware/interfaces/kits/wifiiot_lite"
  21.         "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include"
  22.         "//foundation/communication/interfaces/kits/wifi_lite/wifiservice"
  23.          
  24.     ] 

 

 运行结果

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

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

https://harmonyos.51cto.com/#zz

 

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

2020-11-17 12:15:36

MQTT开发

2020-11-03 09:53:04

体验官方固件程序

2020-10-21 10:01:47

HiSpark Wi-智能家居套件

2020-10-20 09:32:43

HiSparkWi-FiIoT

2020-11-26 12:02:07

OneNet平台

2020-11-27 11:52:40

OneNet

2020-10-23 09:50:15

HiSpark Wi-开发套件

2020-11-25 11:55:47

FlappyBird

2020-10-29 09:53:06

Hi3861硬件开发板

2020-12-09 09:44:29

Hi3861硬件介绍鸿蒙开发板

2020-12-31 12:22:15

鸿蒙Hi3861应用开发

2020-10-30 17:42:36

鸿蒙 OS Hi386

2020-10-26 10:21:39

Wi-Fi IoT智能家居套件

2021-01-28 14:46:29

鸿蒙HarmonyOS应用开发

2020-11-11 10:06:15

鸿蒙

2020-11-04 13:20:14

HiSpark

2021-12-15 15:28:18

鸿蒙HarmonyOS应用

2009-02-13 09:19:23

试用报告MoonlightSilverlight

2021-12-20 21:01:52

鸿蒙HarmonyOS应用

2022-06-28 14:30:29

camera组件照片回传
点赞
收藏

51CTO技术栈公众号