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

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

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

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

a1dcde0507fbc8ddd8385469a4374c4ca3f598.jpg

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测试 1 首先在..\applications\sample\wifi-iot\app目录下创建sta_demo目录 2 编写代码 3 创建BUILD.gn文件 4 编译 5烧录(注意如果想多个功能一起使用,必须在app下的BUILD.gn中进行配置)77df37d37052127655e8173f1db1a16fa552ba.jpg

#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);



# 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",
        
    ]
}

559fea586e7b1e78f11583e5c54a20a009487a.jpg

  • 运行结果030833f64b23be77f589511467c5fd5f1fe964.jpg


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

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

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