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

烛火夜光
发布于 2020-11-17 22:57
浏览
1收藏

      开发环境都准备就绪以后,就迫不及待的想来一个入门篇-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就行烧录

【开发板试用报告】HarmonyOS之HelloWorld,WIFI连接,Socket tcp-鸿蒙开发者社区

static_library("myapp") {
    sources = [
        "hello_world.c"
    ]
    include_dirs = [
        "//utils/native/lite/include"
    ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
#include <stdio.h>
#include "ohos_init.h"
#include "ohos_types.h"
 
void HelloWorld(void)
{
    printf("[xm] Hello world.\n");
}
SYS_RUN(HelloWorld);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
    •  WIFI连接及Socket tcp测试 1 首先在..\applications\sample\wifi-iot\app目录下创建sta_demo目录 2 编写代码 3 创建BUILD.gn文件 4 编译 5烧录(注意如果想多个功能一起使用,必须在app下的BUILD.gn中进行配置)【开发板试用报告】HarmonyOS之HelloWorld,WIFI连接,Socket tcp-鸿蒙开发者社区
      #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 */
          //热点名称
          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).
           */
          //热点加密方式
          assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
      
          /* 热点密码 */
          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;
      
          /* 创建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);
          }
          /*主机字节顺序*/
          /*协议*/
          my_addr.sin_family = AF_INET;
          my_addr.sin_port = htons(8888);
          /*当前ip地址写入*/
          my_addr.sin_addr.s_addr = INADDR_ANY;
      
          /*将结构体其余的都清零*/
          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);
          }
          /*开始监听*/
          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));
              }
              //处理目标
              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);
      • 1.
      • 2.
      • 3.
      • 4.
      • 5.
      • 6.
      • 7.
      • 8.
      • 9.
      • 10.
      • 11.
      • 12.
      • 13.
      • 14.
      • 15.
      • 16.
      • 17.
      • 18.
      • 19.
      • 20.
      • 21.
      • 22.
      • 23.
      • 24.
      • 25.
      • 26.
      • 27.
      • 28.
      • 29.
      • 30.
      • 31.
      • 32.
      • 33.
      • 34.
      • 35.
      • 36.
      • 37.
      • 38.
      • 39.
      • 40.
      • 41.
      • 42.
      • 43.
      • 44.
      • 45.
      • 46.
      • 47.
      • 48.
      • 49.
      • 50.
      • 51.
      • 52.
      • 53.
      • 54.
      • 55.
      • 56.
      • 57.
      • 58.
      • 59.
      • 60.
      • 61.
      • 62.
      • 63.
      • 64.
      • 65.
      • 66.
      • 67.
      • 68.
      • 69.
      • 70.
      • 71.
      • 72.
      • 73.
      • 74.
      • 75.
      • 76.
      • 77.
      • 78.
      • 79.
      • 80.
      • 81.
      • 82.
      • 83.
      • 84.
      • 85.
      • 86.
      • 87.
      • 88.
      • 89.
      • 90.
      • 91.
      • 92.
      • 93.
      • 94.
      • 95.
      • 96.
      • 97.
      • 98.
      • 99.
      • 100.
      • 101.
      • 102.
      • 103.
      • 104.
      • 105.
      • 106.
      • 107.
      • 108.
      • 109.
      • 110.
      • 111.
      • 112.
      • 113.
      • 114.
      • 115.
      • 116.
      • 117.
      • 118.
      • 119.
      • 120.
      • 121.
      • 122.
      • 123.
      • 124.
      • 125.
      • 126.
      • 127.
      • 128.
      • 129.
      • 130.
      • 131.
      • 132.
      • 133.
      • 134.
      • 135.
      • 136.
      • 137.
      • 138.
      • 139.
      • 140.
      • 141.
      • 142.
      • 143.
      • 144.
      • 145.
      • 146.
      • 147.
      • 148.
      • 149.
      • 150.
      • 151.
      • 152.
      • 153.
      • 154.
      • 155.
      • 156.
      • 157.
      • 158.
      • 159.
      • 160.
      • 161.
      • 162.
      • 163.
      • 164.
      • 165.
      • 166.
      • 167.
      • 168.
      • 169.
      • 170.
      • 171.
      • 172.
      • 173.
      • 174.
      • 175.
      • 176.
      • 177.
      • 178.
      • 179.
      • 180.
      • 181.
      • 182.
      • 183.
      • 184.
      • 185.
      • 186.
      • 187.
      • 188.
      • 189.
      • 190.
      • 191.
      • 192.
      • 193.
      • 194.
      • 195.
      • 196.
      • 197.
      • 198.
      • 199.
      • 200.
      • 201.
      • 202.
      • 203.
      • 204.
      • 205.
      • 206.
      • 207.
      • 208.
      • 209.
      • 210.
      • 211.
      • 212.
      • 213.
      • 214.
      • 215.
      • 216.
      • 217.
      • 218.
      • 219.
      • 220.
      • 221.
      • 222.
      • 223.
      • 224.
      • 225.
      • 226.
      • 227.
      • 228.
      • 229.
      • 230.
      • 231.
      • 232.
      • 233.
      • 234.
      • 235.
      • 236.
      • 237.
      • 238.
      • 239.
      • 240.
      • 241.
      • 242.
      • 243.
      • 244.
      • 245.
      • 246.
      • 247.
      • 248.
      • 249.
      • 250.
      • 251.
      • 252.
      • 253.
      • 254.
      • 255.
      • 256.
      • 257.
      • 258.
      • 259.
      • 260.
      • 261.
      • 262.
      • 263.
      • 264.
      • 265.
      • 266.
      • 267.
      • 268.
      • 269.
      • 270.
      • 271.
      • 272.
      • 273.
      • 274.
      # 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",
              
          ]
      }
      
      • 1.
      • 2.
      • 3.
      • 4.
      • 5.
      • 6.
      • 7.
      • 8.
      • 9.
      • 10.
      • 11.
      • 12.
      • 13.
      • 14.
      • 15.
      • 16.
      • 17.
      • 18.
      • 19.
      • 20.
      • 21.
      • 22.
      • 23.
      • 24.
      • 25.
      • 26.
      • 27.

      【开发板试用报告】HarmonyOS之HelloWorld,WIFI连接,Socket tcp-鸿蒙开发者社区

    • 运行结果 【开发板试用报告】HarmonyOS之HelloWorld,WIFI连接,Socket tcp-鸿蒙开发者社区

分类
收藏 1
回复
举报
1
1
1条回复
按时间正序
/
按时间倒序
鸿蒙开发者社区官方账号
鸿蒙开发者社区官方账号

内容不错,翻个牌子。

回复
2020-11-20 16:27:14


回复
    相关推荐