diff --git a/mqtt/BUILD.gn b/mqtt/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..83607420709100c4c867eae84acb2c5483f7aee9 --- /dev/null +++ b/mqtt/BUILD.gn @@ -0,0 +1,155 @@ +# Copyright (c) 2023 iSoftStone Information Technology (Group) Co.,Ltd. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# and Eclipse Distribution License v1.0 which accompany this distribution. +# +# The Eclipse Public License is available at +# https://www.eclipse.org/legal/epl-2.0/ +# and the Eclipse Distribution License is available at +# http://www.eclipse.org/org/documents/edl-v10.php. + +import ("//build/ohos.gni") + +mqtt_path = "//third_party/mqtt" +mqtt_part_name = "mqtt" +mqtt_subsystem_name = "thirdparty" + +config("mqtt_config") { + cflags = [ + "-Wno-sign-compare", + "-Wno-unused-variable", + "-Wno-deprecated-declarations", + "-Wno-format", + "-fvisibility=hidden", + "-Wno-error", + "-Wall", + "-fPIC", + "-Os", + "-g", + ] + + defines = [ + "_GNU_SOURCE", + "PAHO_MQTT_EXPORTS", + ] + + if (use_musl) { + cflags += [ "-Wno-bool-operation" ] + } +} + + +ohos_source_set("mqtt_commom_dynamic") { + sources = [ + "${mqtt_path}/src/MQTTTime.c", + "${mqtt_path}/src/MQTTProtocolClient.c", + "${mqtt_path}/src/Clients.c", + "${mqtt_path}/src/utf-8.c", + "${mqtt_path}/src/MQTTPacket.c", + "${mqtt_path}/src/MQTTPacketOut.c", + "${mqtt_path}/src/Messages.c", + "${mqtt_path}/src/Tree.c", + "${mqtt_path}/src/Socket.c", + "${mqtt_path}/src/Log.c", + "${mqtt_path}/src/MQTTPersistence.c", + "${mqtt_path}/src/Thread.c", + "${mqtt_path}/src/MQTTProtocolOut.c", + "${mqtt_path}/src/MQTTPersistenceDefault.c", + "${mqtt_path}/src/SocketBuffer.c", + "${mqtt_path}/src/LinkedList.c", + "${mqtt_path}/src/MQTTProperties.c", + "${mqtt_path}/src/MQTTReasonCodes.c", + "${mqtt_path}/src/Base64.c", + "${mqtt_path}/src/SHA1.c", + "${mqtt_path}/src/WebSocket.c", + "${mqtt_path}/src/Proxy.c", + ] + + if (!defined(PAHO_HIGH_PERFORMANCE)) { + sources += [ + "${mqtt_path}/src/StackTrace.c", + "${mqtt_path}/src/Heap.c", + ] + + } + include_dirs = [ "${mqtt_path}/src" ] + configs = [ ":mqtt_config" ] +} + +ohos_shared_library("paho-mqtt3a") { + include_dirs = [ "${mqtt_path}/src" ] + configs = [ ":mqtt_config" ] + sources = [ + "${mqtt_path}/src/MQTTAsync.c", + "${mqtt_path}/src/MQTTAsyncUtils.c", + ] + deps = [ ":mqtt_commom_dynamic" ] + install_images = [ "system" ] + install_enable = true + part_name = "${mqtt_part_name}" + subsystem_name = "${mqtt_subsystem_name}" +} + +ohos_shared_library("paho-mqtt3c") { + include_dirs = [ "${mqtt_path}/src" ] + configs = [ ":mqtt_config" ] + sources = [ "${mqtt_path}/src/MQTTClient.c" ] + deps = [ ":mqtt_commom_dynamic" ] + install_images = [ "system" ] + install_enable = true + part_name = "${mqtt_part_name}" + subsystem_name = "${mqtt_subsystem_name}" +} + +ohos_shared_library("paho-mqtt3as") { + include_dirs = [ "${mqtt_path}/src" ] + configs = [ ":mqtt_config" ] + sources = [ + "${mqtt_path}/src/MQTTAsync.c", + "${mqtt_path}/src/MQTTAsyncUtils.c", + "${mqtt_path}/src/SSLSocket.c", + ] + deps = [ + ":mqtt_commom_dynamic", + "//third_party/openssl:libssl_shared", + ] + install_images = [ "system" ] + install_enable = true + part_name = "${mqtt_part_name}" + subsystem_name = "${mqtt_subsystem_name}" +} + +ohos_shared_library("paho-mqtt3cs") { + include_dirs = [ "${mqtt_path}/src" ] + configs = [ ":mqtt_config" ] + sources = [ + "${mqtt_path}/src/MQTTClient.c", + "${mqtt_path}/src/SSLSocket.c", + ] + deps = [ + ":mqtt_commom_dynamic", + "//third_party/openssl:libssl_shared", + ] + install_images = [ "system" ] + install_enable = true + part_name = "${mqtt_part_name}" + subsystem_name = "${mqtt_subsystem_name}" +} + +group("mqtt") +{ + deps = [ + "${mqtt_path}:paho-mqtt3a", + "${mqtt_path}:paho-mqtt3c", + "${mqtt_path}:paho-mqtt3as", + "${mqtt_path}:paho-mqtt3cs", + ] +} + +group("mqtt_tests") { + deps = [ + "${mqtt_path}/src/samples:mqtt_samples", + "${mqtt_path}/test:mqtt_test", + ] +} diff --git a/mqtt/README.OpenSource b/mqtt/README.OpenSource new file mode 100755 index 0000000000000000000000000000000000000000..fac051b9b3cef7721b75d0a402bc3566f5a111df --- /dev/null +++ b/mqtt/README.OpenSource @@ -0,0 +1,11 @@ +[ + { + "Name": "mqtt", + "License": "Eclipse Public License v2.0 and Eclipse Distribution License v1.0", + "License File": "LICENSE", + "Version Number": "1.3.12", + "Owner": "swanlink", + "Upstream URL": "https://github.com/eclipse/paho.mqtt.c", + "Description": "This repository contains the source code for the Eclipse Paho MQTT C client library. For further information related MQTT, see http://mqtt.org." + } +] diff --git a/mqtt/README_zh.md b/mqtt/README_zh.md new file mode 100755 index 0000000000000000000000000000000000000000..cadda24c46fd9dd6dbbab6cc00c12ae7d63258da --- /dev/null +++ b/mqtt/README_zh.md @@ -0,0 +1,16 @@ +# mqtt 三方库说明 + +## 功能简介 + +MQTT 是用 C 语言编写的用于MQTT协议的Eclipse Paho C客户端库。 + +## 使用约束 + +- ROM版本:OpenHarmony3.2 beta4 +- 三方库版本:v1.3.12 +- 当前适配的功能:完成Eclipse Paho C客户端库编译的适配 +- License:[Eclipse Public License v2.0](https://github.com/eclipse/paho.mqtt.c/blob/v1.3.12/LICENSE) + +## 集成方式 + +- [系统Rom包集成](./docs/rom_integrate.md)。 diff --git a/mqtt/adapted/src/VersionInfo.h b/mqtt/adapted/src/VersionInfo.h new file mode 100755 index 0000000000000000000000000000000000000000..ae34598a203a562858b1b0a3d55ce285e98cd3a7 --- /dev/null +++ b/mqtt/adapted/src/VersionInfo.h @@ -0,0 +1,7 @@ +#ifndef VERSIONINFO_H +#define VERSIONINFO_H + +#define BUILD_TIMESTAMP "Tue 31 Jan 2023 10:34:16 AM UTC" +#define CLIENT_VERSION "1.3.12" + +#endif /* VERSIONINFO_H */ diff --git a/mqtt/adapted/src/samples/BUILD.gn b/mqtt/adapted/src/samples/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..3c3631f013b8d08ea03240cdb66239eaa75c453a --- /dev/null +++ b/mqtt/adapted/src/samples/BUILD.gn @@ -0,0 +1,118 @@ +# Copyright (c) 2023 iSoftStone Information Technology (Group) Co.,Ltd. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# and Eclipse Distribution License v1.0 which accompany this distribution. +# +# The Eclipse Public License is available at +# https://www.eclipse.org/legal/epl-2.0/ +# and the Eclipse Distribution License is available at +# http://www.eclipse.org/org/documents/edl-v10.php. + +import ("//build/ohos.gni") + +mqtt_path = "//third_party/mqtt" +mqtt_part_name = "mqtt" +mqtt_subsystem_name = "thirdparty" + +config("mqtt_samples_config") { + cflags = [ + "-Wno-unused-variable", + "-Wno-error", + ] + defines = [ "PAHO_MQTT_IMPORTS" ] +} + +template("swanlink_mqtt_samples") { + ohos_executable(target_name) { + include_dirs = [ "${mqtt_path}/src" ] + configs = [ ":mqtt_samples_config" ] + part_name = "${mqtt_part_name}" + subsystem_name = "${mqtt_subsystem_name}" + forward_variables_from(invoker, "*") + } +} + +swanlink_mqtt_samples("MQTTAsync_publish") { + sources = [ "${mqtt_path}/src/samples/MQTTAsync_publish.c" ] + deps = [ "${mqtt_path}:paho-mqtt3a" ] +} + +swanlink_mqtt_samples("MQTTAsync_subscribe") { + sources = [ "${mqtt_path}/src/samples/MQTTAsync_subscribe.c" ] + deps = [ "${mqtt_path}:paho-mqtt3a" ] +} + +swanlink_mqtt_samples("MQTTClient_publish") { + sources = [ "${mqtt_path}/src/samples/MQTTClient_publish.c" ] + deps = [ "${mqtt_path}:paho-mqtt3c" ] +} + +swanlink_mqtt_samples("MQTTClient_publish_async") { + sources = [ "${mqtt_path}/src/samples/MQTTClient_publish_async.c" ] + deps = [ "${mqtt_path}:paho-mqtt3c" ] +} + +swanlink_mqtt_samples("MQTTClient_subscribe") { + sources = [ "${mqtt_path}/src/samples/MQTTClient_subscribe.c" ] + deps = [ "${mqtt_path}:paho-mqtt3c" ] +} + +swanlink_mqtt_samples("paho_c_pub") { + sources = [ + "${mqtt_path}/src/samples/paho_c_pub.c", + "${mqtt_path}/src/samples/pubsub_opts.c", + ] + deps = [ + "${mqtt_path}:mqtt_commom_dynamic", + "${mqtt_path}:paho-mqtt3as", + ] +} + +swanlink_mqtt_samples("paho_c_sub") { + sources = [ + "${mqtt_path}/src/samples/paho_c_sub.c", + "${mqtt_path}/src/samples/pubsub_opts.c", + ] + deps = [ + "${mqtt_path}:mqtt_commom_dynamic", + "${mqtt_path}:paho-mqtt3as" + ] +} + +swanlink_mqtt_samples("paho_cs_pub") { + sources = [ + "${mqtt_path}/src/samples/paho_cs_pub.c", + "${mqtt_path}/src/samples/pubsub_opts.c", + ] + deps = [ + "${mqtt_path}:mqtt_commom_dynamic", + "${mqtt_path}:paho-mqtt3cs", + ] +} + +swanlink_mqtt_samples("paho_cs_sub") { + sources = [ + "${mqtt_path}/src/samples/paho_cs_sub.c", + "${mqtt_path}/src/samples/pubsub_opts.c", + ] + deps = [ + "${mqtt_path}:mqtt_commom_dynamic", + "${mqtt_path}:paho-mqtt3cs", + ] +} + +group("mqtt_samples") +{ + deps = [ + ":MQTTAsync_publish", + ":MQTTAsync_subscribe", + ":MQTTClient_publish", + ":MQTTClient_publish_async", + ":MQTTClient_subscribe", + ":paho_c_pub", + ":paho_c_sub", + ":paho_cs_pub", + ":paho_cs_sub", + ] +} \ No newline at end of file diff --git a/mqtt/bundle.json b/mqtt/bundle.json new file mode 100755 index 0000000000000000000000000000000000000000..7a37ffbaff0845fbc9af4ca54f7fe45c9b66da18 --- /dev/null +++ b/mqtt/bundle.json @@ -0,0 +1,33 @@ +{ + "name": "@ohos/mqtt", + "description": "This repository contains the source code for the [Eclipse Paho](http://mqtt.org) MQTT C/C++ client library for Embedded platorm.", + "version": "1.3.12", + "license": "Eclipse Public License v2.0 and Eclipse Distribution License v1.0", + "publishAs": "code-segment", + "segment": { + "destPath": "third_party/mqtt" + }, + "dirs": {}, + "scripts": {}, + "readmePath": { + "en": "README" + }, + "component": { + "name": "mqtt", + "subsystem": "thirdparty", + "syscap": [], + "features": [], + "adapted_system_type": [], + "rom": "", + "ram": "", + "deps": { + "components": [], + "third_party": [] + }, + "build": { + "sub_component": ["//third_party/mqtt:mqtt"], + "inner_kits": [], + "test": [] + } + } +} diff --git a/mqtt/docs/pic/running_result.png b/mqtt/docs/pic/running_result.png new file mode 100755 index 0000000000000000000000000000000000000000..edf029d37157b214ede7f04350d88624292af48b Binary files /dev/null and b/mqtt/docs/pic/running_result.png differ diff --git a/mqtt/docs/rom_integrate.md b/mqtt/docs/rom_integrate.md new file mode 100755 index 0000000000000000000000000000000000000000..206a4bb6e65d2785414c761cf5b3376fb17d2325 --- /dev/null +++ b/mqtt/docs/rom_integrate.md @@ -0,0 +1,152 @@ +# mqtt如何集成到系统Rom + +## 准备源码工程 + +本库是基于OpenHarmony 3.2 beta4版本,在润和RK3568开发板上验证的。如果是从未使用过RK3568,可以先查看[润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld)。 + +### 准备系统Rom源码 + +系统源码获取方法请参照:[OpenHarmony3.2beta4源码下载](https://gitee.com/openharmony/docs/blob/OpenHarmony-v3.2-Beta5/zh-cn/release-notes/OpenHarmony-v3.2-beta4.md#源码获取) + +### 增加构建脚本及配置文件 + +- 下载本仓库代码 + + ```shell + cd ~/ + git clone git@gitee.com:openharmony-sig/tpc_c_cplusplus.git --depth=1 + ``` + +- 仓库代码目录结构说明 + + ```shell + cd ~/tpc_c_cplusplus/mqtt/ # 进入到仓库代码库目录 + ``` + + ```shell + mqtt + |-- adapted # 存放三方库适配需要的代码文件 + |-- docs # 存放 mqtt 相关文档的文件夹 + |-- BUILD.gn # 编译构建脚本 + |-- bundle.json # 三方库组件定义文件 + |-- README_zh.md # mqtt 主页说明文档 + |-- README.OpenSource # mqtt 源码的下载地址,版本,license等信息 + ``` + +- 将本仓库文件夹拷贝到OpenHarmony源码的third_party目录下 + + ```shell + cp -arf ~/tpc_c_cplusplus/mqtt ~/OpenHarmony/third_party + ``` + +### 准备三方库源码 + +- 三方库源码下载 + + 将mqtt-1.3.12源码下载到mqtt目录并将代码拷贝出来。 + + ```shell + cd ~/OpenHarmony/third_party/mqtt + git clone https://github.com/eclipse/paho.mqtt.c.git -b v1.3.12 + ``` + +## 系统Rom中引入三方库 + +准备完三方库代码后,我们需要将三方库加入到编译构建体系中。标准系统编译构建可以参考文档[编译构建指导](https://gitee.com/openharmony/docs/blob/OpenHarmony-3.2-Beta4/zh-cn/device-dev/subsystems/subsys-build-all.md)。
+我们默认三方库是属于OpenHarmony的thirdparty子系统,如果需要自己定义子系统参考文档[如何为三方库组件中添加一个三方库](https://gitee.com/openharmony-sig/knowledge/blob/master/docs/openharmony_getstarted/port_thirdparty/README.md)。
+新增需要编译的组件,在OpenHarmony源码的vendor/hihope/rk3568/config.json文件中,新增需要编译的组件,如下代码段所示,在thirdparty子系统下面新增mqtt组件 + +```json +{ + "subsystem": "thirdparty", + "components": [ + { + "component": "musl", + "features": [] + }, + { + "component": "mqtt", + "features": [] + } + ] +} +``` + +## 系统Rom中引入三方库测试程序 + +如果需要编译测试用例,在OpenHarmony源码的vendor/hihope/rk3568/config.json文件,在对应组件的features中打开编译选项,如下 + +```json +{ + "subsystem": "thirdparty", + "components": [ + { + "component": "musl", + "features": [] + }, + { + "component": "mqtt", + "features": [] + } + ] +} +``` + +## 编译工程 + +- 进入源码根目录 + + ```shell + cd ~/openharmony + ``` + +- 选择产品 + + ```shell + hb set # 运行hb set后会出现产品列表,在此我们选择 rk3568 + ``` + +- 执行编译 + + ```shell + hb build --target-cpu arm #编译32位系统 + hb build --target-cpu arm64 #编译64位系统 + ``` + +- 生成的可执行文件和库文件都在out/rk3568/thirdparty/mqtt目录下,同时也打包到了镜像中 + +## 运行效果 + +将编译生成的库和测试文件放到板子上运行,为避免每次将文件推入设备都要烧录整个镜像,我们使用hdc_std工具将文件推到开发板上 + +- 首先将hdc_std工具编译出来
+ 执行以下命令进行编译hdc_std工具(工具编译出来所在路径out/sdk/ohos-sdk/windows/toolchains/hdc_std.exe): + + ```shell + hb set #源码根目录下使用hb set 选择产品ohos-sdk + hb build #然后编译 + ``` + +- 将工具拷贝到Windows,可以为工具目录配置环境变量,也可以在工具所在目录打开windows命令行 +- 将原生库测试需要的所有文件打包成mqtt.tar,并拷贝到windows下 +- 将文件推送到开发板,在windows命令行进行如下操作 + + ```shell + hdc_std shell mount -o remount,rw / #修改系统权限为可读写 + hdc_std file send mqtt.tar / #将文件包推入开发板 + hdc_std shell #进入开发板 + tar xvf mqtt.tar #解压 + #注意需要将库拷贝到/system/lib 或者/system/lib64目录下 + ``` + +- 运行测试程序 + 在测试目录运行测试程序MQTTClient_subscribe,然后再另一个窗口运行测试程序MQTTClient_publish,则在订阅窗口会打印“Message arrived”,如下图所示:
+  ![image-20220922143536085](./pic/running_result.png) + +## 参考资料 + +- [润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld)。 +- [OpenHarmony3.2beta4](https://gitee.com/openharmony/docs/blob/OpenHarmony-v3.2-Beta5/zh-cn/release-notes/OpenHarmony-v3.2-beta4.md)。 +- [OpenHarmony三方库组织地址](https://gitee.com/openharmony-tpc)。 +- [如何为三方库组件中添加一个三方库](https://gitee.com/openharmony-sig/knowledge/blob/master/docs/openharmony_getstarted/port_thirdparty/README.md)。 +- [知识体系](https://gitee.com/openharmony-sig/knowledge)。