• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

智能家居服务发现实现

武飞扬头像
qq_52484093
帮助1

服务设备软件架构设计

学新通

代码复用

网络通信框架移植到开发板,之后,可以使用框架中的组件实现 Response Task 和 Service Task。

框架移植注意事项

LWIP 是微型 TCP/IP 协议栈 (并非完整 TCP/IP 协议栈)

支持 socket 接口,但一些功能未实现

socket 接口所在头文件不同于 Linux 和 Windows 平台

完成可移植性测试,对测试中出现的问题及时修复

可移植性测试

通过网络调试助手验证 TCP 数据收发

  • 设备端运行服务端,客户端连接,数据收发功能

通过网络调试助手验证 UDP 数据收发

  • 设备接收广播,并回复数据

Service Task 关键实现

学新通

Response Task 关键实现

学新通

网络通信框架移植

main_entry.c

  1.  
    #include <stdio.h>
  2.  
    #include "ohos_init.h"
  3.  
    #include "cmsis_os2.h"
  4.  
    #include "wifi_connect.h"
  5.  
    #include "response_task.h"
  6.  
    #include "service_task.h"
  7.  
     
  8.  
    static void* Task_Init(const char* arg)
  9.  
    {
  10.  
    Wifi_Init();
  11.  
    Wifi_Connect("wifi_test", "12345678");
  12.  
    Wifi_Start();
  13.  
     
  14.  
    if(Wifi_IsOk)
  15.  
    {
  16.  
    osThreadAttr_t attr = {0};
  17.  
     
  18.  
    printf("connect wifi success! ip = %s\n", Wifi_IpAddr());
  19.  
     
  20.  
    attr.name = "Response Task";
  21.  
    attr.stack_size = 4 * 1024;
  22.  
    attr.priority = 20;
  23.  
     
  24.  
    if(osThreadNew((osThreadFunc_t)Response_Task, NULL, &attr) == NULL)
  25.  
    {
  26.  
    printf("failed to create response task!\n");
  27.  
    }
  28.  
     
  29.  
    attr.name = "Service Task";
  30.  
    attr.stack_size = 4 * 1024;
  31.  
    attr.priority = 20;
  32.  
     
  33.  
    if(osThreadNew((osThreadFunc_t)Service_Task, NULL, &attr) == NULL)
  34.  
    {
  35.  
    printf("failed to create service task!\n");
  36.  
    }
  37.  
    }
  38.  
     
  39.  
    return arg;
  40.  
    }
  41.  
     
  42.  
    static void Main_Entry(void)
  43.  
    {
  44.  
    osThreadAttr_t attr = {0};
  45.  
     
  46.  
    attr.name = "Task_Iniit";
  47.  
    attr.stack_size = 4 * 1024;
  48.  
    attr.priority = 20;
  49.  
     
  50.  
    if(osThreadNew((osThreadFunc_t)Task_Init, NULL, &attr) == NULL)
  51.  
    {
  52.  
    printf("failed to create task!\n");
  53.  
    }
  54.  
    }
  55.  
     
  56.  
    SYS_RUN(Main_Entry);
学新通

response_task.h

  1.  
    #ifndef RESPONSE_TASK
  2.  
    #define RESPONSE_TASK
  3.  
     
  4.  
    void* Response_Task(const char* arg);
  5.  
     
  6.  
    #endif

response_task.c

  1.  
    #include <stdio.h>
  2.  
    #include "response_task.h"
  3.  
    #include "udp_point.h"
  4.  
     
  5.  
    void* Response_Task(const char* arg)
  6.  
    {
  7.  
    UdpPoint* point = NULL;
  8.  
    Message* msg = NULL;
  9.  
    char remote[16] = {0};
  10.  
    int port = 0;
  11.  
     
  12.  
    point = UdpPoint_New(9999);
  13.  
     
  14.  
    if(point)
  15.  
    {
  16.  
    printf("point = 0x%X\n", point);
  17.  
     
  18.  
    while(1)
  19.  
    {
  20.  
    msg = UdpPoint_RecvMsg(point, remote, &port);
  21.  
     
  22.  
    if(msg != NULL)
  23.  
    {
  24.  
    printf("msg = 0x%X\n", msg);
  25.  
    printf("receive a message, ip = %s, port = %d\n", remote, port);
  26.  
    printf("type = %d, cmd = %d, index = %d, total = %d, lengh = %d\n", msg->type, msg->cmd,msg->index, msg->total, msg->length);
  27.  
     
  28.  
    if(msg->length > 0)
  29.  
    {
  30.  
    printf("data: %s\n", msg->payload);
  31.  
    }
  32.  
     
  33.  
    free(msg);
  34.  
    }
  35.  
    }
  36.  
     
  37.  
    UdpPoint_Del(point);
  38.  
    }
  39.  
     
  40.  
    return NULL;
  41.  
    }
学新通

service_task.h

  1.  
    #ifndef SERVICE_TASK_H
  2.  
    #define SERVICE_TASK_H
  3.  
     
  4.  
    void* Service_Task(const char* arg);
  5.  
     
  6.  
    #endif

service_task.c

  1.  
    #include <stdio.h>
  2.  
    #include "service_task.h"
  3.  
    #include "tcp_server.h"
  4.  
     
  5.  
    static void Server_Listener_Handler(TcpClient* client, int evt)
  6.  
    {
  7.  
    if(evt == EVT_COON)
  8.  
    {
  9.  
    printf("a client connect\n");
  10.  
    }
  11.  
    else if(evt == EVT_DATA)
  12.  
    {
  13.  
    Message* msg = NULL;
  14.  
     
  15.  
    msg = TcpClient_RecvMsg(client);
  16.  
     
  17.  
    if(msg)
  18.  
    {
  19.  
    printf("msg = 0x%X\n", msg);
  20.  
    printf("type = %d, cmd = %d, index = %d, total = %d, lengh = %d\n", msg->type, msg->cmd,msg->index, msg->total, msg->length);
  21.  
     
  22.  
    if(msg->length > 0)
  23.  
    {
  24.  
    printf("data: %s\n", msg->payload);
  25.  
    }
  26.  
     
  27.  
    free(msg);
  28.  
    }
  29.  
    }
  30.  
    else if(evt == EVT_CLOSE)
  31.  
    {
  32.  
    printf("a client left\n");
  33.  
    }
  34.  
     
  35.  
    return NULL;
  36.  
    }
  37.  
     
  38.  
    void* Service_Task(const char* arg)
  39.  
    {
  40.  
    TcpServer* server = NULL;
  41.  
     
  42.  
    server = TcpServer_New();
  43.  
     
  44.  
    if(server)
  45.  
    {
  46.  
    printf("server = 0x%X\n", server);
  47.  
     
  48.  
    TcpServer_SetListener(server, Server_Listener_Handler);
  49.  
    TcpServer_Start(server, 8888, 5);
  50.  
    TcpServer_DoWork(server);
  51.  
     
  52.  
    TcpServer_Del(server);
  53.  
    }
  54.  
     
  55.  
    return NULL;
  56.  
    }
学新通

问题及原因

LWIP 未实现选项组合 IPPORTO_TCP,TCP_INFO

int TcpClient_IsValid(TcpClient* client); 总是返回假,导致服务端逻辑错误

MParser 从内存解析数据且内存中的协议消息无 payload (length == 0)

Mparser 无法将解析状态推到最终态,导致解析结果为 NULL

实验结果

学新通

我们使用 UdpPoint 能够成功地收发数据

学新通 

 TCP 通信框架我们也成功的移植了过来,测试了并没有问题。

课后思考

Response Task 和 Service Task 的业务逻辑如何实现?客户端如何实现?

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhaagii
系列文章
更多 icon
同类精品
更多 icon
继续加载