您当前的位置: 首页 >  qt

跋扈洋

暂无认证

  • 3浏览

    0关注

    221博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

鸿蒙之连接MQTT

跋扈洋 发布时间:2022-02-04 11:09:12 ,浏览量:3

鸿蒙之连接MQTT
  • 移植MQTT
  • 修改文件
  • 编写测试代码
  • 运行
  • 后续

移植MQTT

鸿蒙系统中通过移植第3方软件包 paho mqtt去实现MQTT协议功能 首先下载MQTT移植文件

https://download.csdn.net/download/qq_44629109/79195528

我们在鸿蒙系统源码的 third_party 文件夹下创建一个 pahomqtt 文件夹,然后把解压后的所有文件都拷贝到 pahomqtt 文件夹下 下一步,我们在pahomqtt 文件夹下面新建BUILD.gn文件,用来构建编译。其内容如下

import("//build/lite/config/component/lite_component.gni")
import("//build/lite/ndk/ndk.gni")

config("pahomqtt_config") {
    include_dirs = [
        "MQTTPacket/src",
        "MQTTClient-C/src",
        "MQTTClient-C/src/liteOS",
        "//kernel/liteos_m/components/cmsis/2.0",
    ]
}

pahomqtt_sources = [
"MQTTClient-C/src/liteOS/MQTTLiteOS.c",
"MQTTClient-C/src/MQTTClient.c",

"MQTTPacket/src/MQTTConnectClient.c",
"MQTTPacket/src/MQTTConnectServer.c",
"MQTTPacket/src/MQTTDeserializePublish.c",
"MQTTPacket/src/MQTTFormat.c",
"MQTTPacket/src/MQTTPacket.c",
"MQTTPacket/src/MQTTSerializePublish.c",
"MQTTPacket/src/MQTTSubscribeClient.c",
"MQTTPacket/src/MQTTSubscribeServer.c",
"MQTTPacket/src/MQTTUnsubscribeClient.c",
"MQTTPacket/src/MQTTUnsubscribeServer.c",
]

lite_library("pahomqtt_static") {
    target_type = "static_library"
    sources = pahomqtt_sources
    public_configs = [ ":pahomqtt_config" ]
}

lite_library("pahomqtt_shared") {
    target_type = "shared_library"
    sources = pahomqtt_sources
    public_configs = [ ":pahomqtt_config" ]
}

ndk_lib("pahomqtt_ndk") {
    if (board_name != "hi3861v100") {
        lib_extension = ".so"
        deps = [
            ":pahomqtt_shared"
        ]
    } else {
        deps = [
            ":pahomqtt_static"
        ]
    }
    head_files = [
        "//third_party/pahomqtt"
    ]
}

打开vendor\hisi\hi3861\hi3861\BUILD.gn 文件, 在lite_component(“sdk”) 中增加 “//third_party/pahomqtt:pahomqtt_static”

修改文件

打开 third_party\pahomqtt\MQTTPacket\samples\transport.c 文件,这个文件也是我们主要移植的文件,我们需要实现 socket相关的操作,包括发送、接收数据。其实移植就3步。

(1)在transport.c导入三个头文件

#include "lwip/ip_addr.h"
#include "lwip/sockets.h"
#include "lwip/netifapi.h"

(2)修改 transport_sendPacketBuffer 函数:

int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen)
{
	int rc = 0;
	rc = send(sock, buf, buflen, 0);
	return rc;
}

(3)修改 transport_close 函数:

int transport_close(int sock)
{
int rc;
	rc = shutdown(sock, SHUT_WR);
	rc = recv(sock, NULL, (size_t)0, 0);
	rc = lwip_close(sock);
 return rc;
 }
编写测试代码

在./applications/sample/wifi-iot/app路径下新建一个目录(或一套目录结构),用于存放业务源码文件。 其中有四个文件:

  1. BUILD.gn
static_library("mqtt_test_at") {
    sources = [
        "mqtt_test.c",
        "at_entry.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",
        "//third_party/pahomqtt/MQTTPacket/src",
        "//third_party/pahomqtt/MQTTPacket/samples",
        "//vendor\hisi\hi3861\hi3861\components\at\src"
    ]
}
  1. at_entry.c
#include 
#include 
#include "ohos_init.h"
#include "cmsis_os2.h"
#include 
#include 
#include 
#include "hi_wifi_api.h"
#include "mqtt_test.h"
void mqtt_test_thread(void * argv)
{
    argv = argv;
    mqtt_test();
}
hi_u32 at_exe_mqtt_test_cmd(void)
{
    osThreadAttr_t attr;
    attr.name = "wifi_config_thread";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4096;
    attr.priority = 36;

    if (osThreadNew((osThreadFunc_t)mqtt_test_thread, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
    AT_RESPONSE_OK;
    return HI_ERR_SUCCESS;
}
const at_cmd_func g_at_mqtt_func_tbl[] = {
    {"+MQTTTEST", 9, HI_NULL, HI_NULL, HI_NULL, (at_call_back_func)at_exe_mqtt_test_cmd},
};
void AtExampleEntry(void)
{
    hi_at_register_cmd(g_at_mqtt_func_tbl, sizeof(g_at_mqtt_func_tbl)/sizeof(g_at_mqtt_func_tbl[0]));
}
SYS_RUN(AtExampleEntry);
  1. mqtt_test.c
#include 

#include 

#include "ohos_init.h"
#include "cmsis_os2.h"

#include 
#include "hi_wifi_api.h"
//#include "wifi_sta.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"

#include "lwip/sockets.h"

#include "MQTTPacket.h"
#include "transport.h"

int toStop = 0;

int mqtt_connect(void)
{
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	int rc = 0;
	int mysock = 0;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	int msgid = 1;
	MQTTString topicString = MQTTString_initializer;
	int req_qos = 0;
	char* payload = "hello HarmonyOS";
	int payloadlen = strlen(payload);
	int len = 0;
	char *host = "MQTT服务器的ip地址";
	int port = 1883;


	mysock = transport_open(host, port);
	if(mysock             
关注
打赏
1663745539
查看更多评论
0.0397s