一、前言
OpenHarmony 3.1.1 是一个开源的智能终端操作系统,主要用于智能家居、智能手机、平板电脑、智能穿戴设备等智能终端设备。是一个分布式操作系统,支持多种硬件平台和多种编程语言,可以方便地进行移植和定制。
特点:
(1) 分布式:支持分布式计算和分布式存储,可以在多个设备之间进行任务调度和数据共享。
(2)可裁剪:支持根据设备的需求进行裁剪,以满足不同设备的资源限制。
(3)安全:提供了安全机制,保护设备的隐私和安全。
(4)可移植:支持多种硬件平台和多种编程语言,可以方便地进行移植和定制。
当前文章介绍在DAYU200开发板上烧写OpenHarmony系统,利用huaweicloud-iot-device-sdk
完成华为云IOT平台对接,完成物联网数据通信。
所需硬件:DAYU200开发板(RK3568)
二、DAYU200开发板
2.1 开发板介绍
基于Rockchip RK3568,集成双核心架构GPU以及高效能NPU;
板载四核64位Cortex-A55 处理器采用22nm先进工艺,主频高达2.0GHz;
支持蓝牙、Wi-Fi、音频、视频和摄像头等功能,拥有丰富的扩展接口,支持多种视频输入输出接口;
配置双千兆自适应RJ45以太网口,可满足NVR、工业网关等多网口产品需求。
2.2 开发板实物图
三、环境安装
3.1 串口终端
开发板插上串口线之后,打开设备管理器可以看到识别到串口COM8
,这就是开发板的Debug串口。(需要提前安装好USB转串口驱动)。
打开串口工具,新建会话,选择串口协议。
波特率选择1500000
也就是1.5M。
创建完成。
选择刚才创建好的会话,点击连接。
复位开发板(开发板上有一个RESET按钮),正常情况下会在终端上看到内核启动过程;启动完成后按下回车即可进入到Linux终端命令行。
从打印的命令行数据可以看出当前设备的配置:
这是uboot打印出来的配置信息。从当前信息里可以看出CPU型号RK3568,内存2GB。
U-Boot 2017.09-gb448fedf55-211210 #addy (Dec 20 2021 - 10:58:09 +0800)Model: Rockchip RK3568 Evaluation Board
PreSerial: 2, raw, 0xfe660000
DRAM: 2 GiB
Sysmem: init
Relocation Offset: 7d34d000
Relocation fdt: 7b9f87e8 - 7b9fecd0
CR: M/C/I
Using default environment
内核启动打印的信息;从信息里可以看出当前系统使用的内核是Linux 5.10内核。
Starting kernel ...[ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x412fd050]
[ 0.000000] Linux version 5.10.79 (root@ecs-58f4) (aarch64-linux-gnu-gcc (Linaro GCC 7.5-2019.12) 7.5.0, GNU ld (Linaro_Binutils-2019.12) 2.28.2.20170706) #1 SMP Thu Jun 29 09:39:49 CST 2023
[ 0.000000] Machine model: rockchip,rk3568-toybrick-dev-linux-x0
[ 0.000000] earlycon: uart8250 at MMIO32 0x00000000fe660000 (options '')
[ 0.000000] printk: bootconsole [uart8250] enabled
[ 0.000000] Zone ranges:
[ 0.000000] DMA [mem 0x0000000000200000-0x000000007fffffff]
[ 0.000000] DMA32 empty
[ 0.000000] Normal empty
[ 0.000000] Movable zone start for each node
[ 0.000000] Early memory node ranges
在串口软件上可以设置终端类型,方便区分Linux终端文件类型。
3.2 固件烧写
说明文档:https://gitee.com/hihope_iot/docs/blob/master/HiHope_DAYU200/docs/%E7%83%A7%E5%BD%95%E6%8C%87%E5%AF%BC%E6%96%87%E6%A1%A3.md#https://gitee.com/link?target=http%3A%2F%2Fci.openharmony.cn%2Fdailys%2Fdailybuilds
【1】烧写工具下载
https://gitee.com/hihope_iot/docs/tree/master/HiHope_DAYU200/%E7%83%A7%E5%86%99%E5%B7%A5%E5%85%B7%E5%8F%8A%E6%8C%87%E5%8D%97
下来下来之后打开HiHope_DAYU200
文件夹。
我这里在windows下进行烧写。
在DriverAssitant_v5.1.1.zip
目录下是USB驱动,解压后进行安装。如果已经安装旧版本的烧写工具, 需要先点击“驱动卸载” 按钮下载驱动, 然后再点击“驱动安装” 按钮安装驱动。
RKDevTool.exe
就是烧写工具。
【2】HiHope官方固件下载
地址:https://gitee.com/hihope_iot/images/tree/master/HiHope_DAYU200
下载下来解压,打开烧写工具选择路径。
确认开发板是否进入烧写模式
(1)如果界面显示"发现一个 LOADER 设备", 说明开发板进入 Loader 模式等待烧写固件。
(2)如果界面显示"发现一个 MASKROM 设备", 说明开发板进入 Maskrom 模式等待烧写固 件。
(3)如果界面显示"没有发现设备", 说明开发板没有进入烧写模式, 需要先让开发板进入烧写模式
进入烧写模式的步骤
(1)先按住 VOL-/RECOVERY 按键 和 RESET 按钮不松开, 烧录工具此时显示“没有发现设备”
(2)然后再 松开 RESER 键, 烧录工具显示“发现一个 LOADER 设备” , 说明此时已经进入烧写模式。
3.3 huaweicloud-iot-device-sdk-c 介绍
说明文档:https://gitee.com/openharmony-sig/iot_device_sdk_c/blob/master/README_CN.md#5
Huawei Cloud IOT Device SDK for C 是华为云推出的一套面向嵌入式设备的物联网开发套件,支持 C 语言开发。该套件提供了一系列 API,可以方便地实现设备与云端的连接、数据上报、远程控制等功能。
该套件主要包括以下组件:
- Huawei LiteOS:一个轻量级的操作系统,提供了内核级别的任务调度、内存管理、网络协议栈等功能。
- Huawei Cloud IOT Agent:一个设备端的物联网代理程序,负责与云端的物联网平台进行连接,实现设备注册、数据上报、远程控制等功能。
- Huawei Cloud IOT SDK for C:一个 C 语言的开发包,提供了一系列 API,可以方便地实现设备与云端的连接、数据上报、远程控制等功能。
使用 Huawei Cloud IOT Device SDK for C 可以大大简化物联网设备的开发流程,提高开发效率,同时也可以保证设备与云端之间的连接稳定性和安全性。该套件适用于各种类型的嵌入式设备,例如智能家居、智能家电、智能穿戴等。
Huawei Cloud IOT SDK for C SDK面向运算、存储能力较强的嵌入式终端设备,开发者通过调用SDK接口,便可实现设备与物联网平台的上下行通讯。SDK当前支持的功能有:
- 支持物模型:设备消息/属性/事件上报,设备命令/消息/属性/事件接收
- 支持子设备消息转发、子设备管理
- 支持OTA升级
- 支持密码认证和证书认证两种设备接入方式
- 支持自定义topic
- 支持设备影子查询
- 支持自定义日志收集能力
- 支持端侧规则引擎
- 支持SSH远程登录
- 支持对接边缘M2M
四、上云实验
4.1 物联网平台介绍
华为云物联网平台(IoT 设备接入云服务)提供海量设备的接入和管理能力,将物理设备联接到云,支撑设备数据采集上云和云端下发命令给设备进行远程控制,配合华为云其他产品,帮助我们快速构筑物联网解决方案。
使用物联网平台构建一个完整的物联网解决方案主要包括3部分:物联网平台、业务应用和设备。
物联网平台作为连接业务应用和设备的中间层,屏蔽了各种复杂的设备接口,实现设备的快速接入;同时提供强大的开放能力,支撑行业用户构建各种物联网解决方案。
设备可以通过固网、2G/3G/4G/5G、NB-IoT、Wifi等多种网络接入物联网平台,并使用LWM2M/CoAP、MQTT、HTTPS协议将业务数据上报到平台,平台也可以将控制命令下发给设备。
业务应用通过调用物联网平台提供的API,实现设备数据采集、命令下发、设备管理等业务场景。
4.2 开通物联网服务
地址:https://www.huaweicloud.com/product/iothub.html
点击总览
,查看接入信息。 我们当前设备准备采用MQTT协议接入华为云平台,这里可以看到MQTT协议的地址和端口号等信息。
总结:
端口号: MQTT (1883)| MQTTS (8883)
接入地址: a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com
根据域名地址得到IP地址信息:
Microsoft Windows [版本 10.0.19045.2965]
© Microsoft Corporation。保留所有权利。
MQTT协议接入端口号有两个,1883是非加密端口,8883是证书加密端口,单片机无法加载证书,所以使用1883端口比较合适。 接下来的ESP8266就采用1883端口连接华为云物联网平台。
4.3 创建产品
(1)创建产品
点击产品页,再点击左上角创建产品。
(2)填写产品信息
根据自己产品名字填写。
(3)产品创建成功
(4)添加自定义模型
产品创建完成之后,点击进入产品详情页面,翻到最下面可以看到模型定义。
这个模型就是定义自己设备接下来需要向服务器上传那些数据类型。根据自己的数据类型进行编写。
先点击自定义模型。
再创建一个服务ID。
接着点击新增属性。
4.4 添加设备
产品是属于上层的抽象模型,接下来在产品模型下添加实际的设备。添加的设备最终需要与真实的设备关联在一起,完成数据交互。
(1)注册设备
(2)根据自己的设备填写
(3)保存设备信息
创建完毕之后,点击保存并关闭,得到创建的设备密匙信息。该信息在后续生成MQTT三元组的时候需要使用。
(4)设备创建完成
可以点击设备进入到设备详情页面。
4.5 OpenHarmony环境构建
在文件Harmony源码路径/build/subsystem_config.json中添加构建脚本:
"iot-device-sdk-c" : {
"path": "third\_party/iot-device-sdk-c",
"name": "iot-device-sdk-c"
},
(1)添加子系统编译构建
在文件OpenHarmony源码路径/build/subsystem_config.json中添加如下构建脚本:
"iot-device-sdk-c" : {
"path": "third\_party/iot-device-sdk-c",
"name": "iot-device-sdk-c"
},
在产品配置文件中添加子系统,其子系统名称与文件中添加内容对应,就是iot-device-sdk-c:iot-device-sdk-c
。
配置文件位于`MY_OHOS_
(2)编译依赖的动态库
此sdk主要依赖于libboundscheck.so
,libpaho-mqtt3as.so
,libssh.so
,libnopoll.so
这些动态库,其间接依赖于libz.so
,libssl.so
,libcrypto.so
。
(3)编译第三方动态库
对于libboundscheck.so
,libpaho-mqtt3as.so
,libssh.so
,libnopoll.so
, 这些库对应的源码没有在OpenHarmony中集成,如果开发板中没有这些动态库,需要自行下载并编译。
执行以下命令拉取对应源码:
cd $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c
mkdir third\_party
cd third\_party
git clone https://gitee.com/Janisa/huawei\_secure\_c.git \\ && git clone -b v1.3.12 --single-branch https://github.com/eclipse/paho.mqtt.c.git \\ && git clone -b libssh-0.9.6 --single-branch https://git.libssh.org/projects/libssh.git \\ && git clone -b 0.4.8 --single-branch https://github.com/ASPLes/nopoll.git \\
分别在OpenHarmony源码路径/third_party/iot_device_sdk_c/third_party/huawei_secure_c,OpenHarmony源码路径/third_party/iot_device_sdk_c/third_party/paho.mqtt.c, OpenHarmony源码路径/third_party/iot_device_sdk_c/third_party/libssh, OpenHarmony源码路径/third_party/iot_device_sdk_c/third_party/nopoll文件夹中放入或替换BUILD.gn文件,
内容如下:
\# 放入 $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c/third\_party/huawei\_secure\_c
#
# Copyright (c) \[2020\] Huawei Technologies Co.,Ltd.All rights reserved.
#
# OpenArkCompiler is licensed under the Mulan PSL v1.
# You can use this software according to the terms and conditions of the Mulan PSL v1.
# You may obtain a copy of Mulan PSL v1 at:
#
# http://license.coscl.org.cn/MulanPSL
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v1 for more details.
#
import("//build/ohos.gni")src\_libHWSecureC = \["src/vsprintf\_s.c","src/wmemmove\_s.c","src/strncat\_s.c","src/vsnprintf\_s.c","src/fwscanf\_s.c","src/scanf\_s.c","src/strcat\_s.c","src/sscanf\_s.c","src/secureprintoutput\_w.c","src/wmemcpy\_s.c","src/wcsncat\_s.c","src/secureprintoutput\_a.c","src/secureinput\_w.c","src/memcpy\_s.c","src/fscanf\_s.c","src/vswscanf\_s.c","src/secureinput\_a.c","src/sprintf\_s.c","src/memmove\_s.c","src/swscanf\_s.c","src/snprintf\_s.c","src/vscanf\_s.c","src/vswprintf\_s.c","src/wcscpy\_s.c","src/vfwscanf\_s.c","src/memset\_s.c","src/wscanf\_s.c","src/vwscanf\_s.c","src/strtok\_s.c","src/wcsncpy\_s.c","src/vfscanf\_s.c","src/vsscanf\_s.c","src/wcstok\_s.c","src/securecutil.c","src/gets\_s.c","src/swprintf\_s.c","src/strcpy\_s.c","src/wcscat\_s.c","src/strncpy\_s.c",
\]include\_common = \["include","src",
\]ohos\_static\_library("libHWSecureC") {sources = src\_libHWSecureCinclude\_dirs = include\_common
}ohos\_shared\_library("libboundscheck") {sources = src\_libHWSecureCinclude\_dirs = include\_common
}
# 放入 $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c/third\_party/paho.mqtt.c
#
# Copyright (c) \[2020\] Huawei Technologies Co.,Ltd.All rights reserved.
#
# OpenArkCompiler is licensed under the Mulan PSL v1.
# You can use this software according to the terms and conditions of the Mulan PSL v1.
# You may obtain a copy of Mulan PSL v1 at:
#
# http://license.coscl.org.cn/MulanPSL
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v1 for more details.
#import("//build/ohos.gni")
MQTT\_SRC\_DIR = "src"MQTT\_AS\_SOURCE\_FILES = \["${MQTT\_SRC\_DIR}/Base64.c","${MQTT\_SRC\_DIR}/Clients.c","${MQTT\_SRC\_DIR}/Heap.c","${MQTT\_SRC\_DIR}/LinkedList.c","${MQTT\_SRC\_DIR}/Log.c","${MQTT\_SRC\_DIR}/Messages.c","${MQTT\_SRC\_DIR}/MQTTAsync.c","${MQTT\_SRC\_DIR}/MQTTAsyncUtils.c","${MQTT\_SRC\_DIR}/MQTTPacket.c","${MQTT\_SRC\_DIR}/MQTTPacketOut.c","${MQTT\_SRC\_DIR}/MQTTPersistence.c","${MQTT\_SRC\_DIR}/MQTTPersistenceDefault.c","${MQTT\_SRC\_DIR}/MQTTProperties.c","${MQTT\_SRC\_DIR}/MQTTProtocolClient.c","${MQTT\_SRC\_DIR}/MQTTProtocolOut.c","${MQTT\_SRC\_DIR}/MQTTReasonCodes.c","${MQTT\_SRC\_DIR}/MQTTTime.c","${MQTT\_SRC\_DIR}/OsWrapper.c","${MQTT\_SRC\_DIR}/Proxy.c","${MQTT\_SRC\_DIR}/SHA1.c","${MQTT\_SRC\_DIR}/Socket.c","${MQTT\_SRC\_DIR}/SocketBuffer.c","${MQTT\_SRC\_DIR}/SSLSocket.c","${MQTT\_SRC\_DIR}/StackTrace.c","${MQTT\_SRC\_DIR}/Thread.c","${MQTT\_SRC\_DIR}/Tree.c","${MQTT\_SRC\_DIR}/utf-8.c","${MQTT\_SRC\_DIR}/WebSocket.c",
\]ohos\_shared\_library("libpaho-mqtt3as") {sources = MQTT\_AS\_SOURCE\_FILESinclude\_dirs = \["./src", "./build"\]deps=\["//third\_party/openssl:libcrypto\_static","//third\_party/openssl:ssl\_source"\]cflags = \["-w", "-fPIC", "-Os", "-fvisibility=hidden", "-DOPENSSL", "-D\_GNU\_SOURCE", "-DPAHO\_MQTT\_EXPORTS=1"\]libs= \["pthread", "dl"\]ldflags = \["-Wl,-init,MQTTAsync\_init"\]
}
# 放入 $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c/third\_party/libssh
#
# Copyright (c) \[2020\] Huawei Technologies Co.,Ltd.All rights reserved.
#
# OpenArkCompiler is licensed under the Mulan PSL v1.
# You can use this software according to the terms and conditions of the Mulan PSL v1.
# You may obtain a copy of Mulan PSL v1 at:
#
# http://license.coscl.org.cn/MulanPSL
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v1 for more details.
#import("//build/ohos.gni")src\_libHWSecureC = \[
"src/agent.c",
"src/auth.c",
"src/base64.c",
"src/bignum.c",
"src/buffer.c",
"src/callbacks.c",
"src/channels.c",
"src/client.c",
"src/config.c",
"src/connect.c",
"src/connector.c",
"src/curve25519.c",
"src/dh.c",
"src/ecdh.c",
"src/error.c",
"src/getpass.c",
"src/init.c",
"src/kdf.c",
"src/kex.c",
"src/known\_hosts.c",
"src/knownhosts.c",
"src/legacy.c",
"src/log.c",
"src/match.c",
"src/messages.c",
"src/misc.c",
"src/options.c",
"src/packet.c",
"src/packet\_cb.c",
"src/packet\_crypt.c",
"src/pcap.c",
"src/pki.c",
"src/pki\_container\_openssh.c",
"src/poll.c",
"src/session.c",
"src/scp.c",
"src/socket.c",
"src/string.c",
"src/threads.c",
"src/wrapper.c",
"src/external/bcrypt\_pbkdf.c",
"src/external/blowfish.c",
"src/external/chacha.c",
"src/external/poly1305.c",
"src/chachapoly.c",
"src/config\_parser.c",
"src/token.c",
"src/pki\_ed25519\_common.c",
"src/threads/noop.c",
"src/threads/pthread.c",
"src/threads/libcrypto.c",
"src/pki\_crypto.c",
"src/ecdh\_crypto.c",
"src/libcrypto.c",
"src/dh\_crypto.c",
"src/pki\_ed25519.c",
"src/external/ed25519.c",
"src/external/fe25519.c",
"src/external/ge25519.c",
"src/external/sc25519.c",
"src/sftp.c",
"src/sftpserver.c",
"src/server.c",
"src/bind.c",
"src/bind\_config.c",
"src/dh-gex.c",
"src/external/curve25519\_ref.c",
\]include\_common = \["./build","./include/","//third\_party/openssl/include","./include/libssh","./build/include","./build/src",
\]
ohos\_shared\_library("libssh") {sources = src\_libHWSecureCinclude\_dirs = include\_commondeps=\["//third\_party/openssl:libcrypto\_static","//third\_party/openssl:ssl\_source"\]cflags = \["-w", "-std=gnu99", "-fPIC", "-DLIBSSH\_EXPORTS", "-D\_GNU\_SOURCE"\]
}
# 放入 $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c/third\_party/nopoll
#
# Copyright (c) \[2020\] Huawei Technologies Co.,Ltd.All rights reserved.
#
# OpenArkCompiler is licensed under the Mulan PSL v1.
# You can use this software according to the terms and conditions of the Mulan PSL v1.
# You may obtain a copy of Mulan PSL v1 at:
#
# http://license.coscl.org.cn/MulanPSL
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
# FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v1 for more details.
#
import("//build/ohos.gni")src\_nopoll = \["src/nopoll.c","src/nopoll\_conn.c","src/nopoll\_conn\_opts.c","src/nopoll\_ctx.c","src/nopoll\_decl.c","src/nopoll\_io.c","src/nopoll\_listener.c","src/nopoll\_log.c","src/nopoll\_loop.c","src/nopoll\_msg.c","src/nopoll\_win32.c",
\]include\_common = \["include","../../include/","src",
\]
ohos\_shared\_library("libnopoll") {sources = src\_nopollinclude\_dirs = include\_commondeps=\["//third\_party/openssl:libcrypto\_static","//third\_party/openssl:ssl\_source"\]cflags = \["-w"\]
}
由于libssh需要cmake生成一些包含各种配置的头文件,所以此处先用cmake命令生成中间文件。
此处需要根据开发板支持的功能向cmake传入参数,以便开启或关闭对应特性,如示例中的
-DHAVED_GLOB=0
命令如下:
cd $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c/third\_party/libssh \\&& mkdir -p build \\&& cd build \\&& cmake .. \\-DCMAKE\_BUILD\_TYPE=Release -DWITH\_ZLIB=OFF \\-DOPENSSL\_ROOT\_DIR=$MY\_OHOS\_DIR/third\_party/openssl \\-DOPENSSL\_INCLUDE\_DIR=$MY\_OHOS\_DIR/third\_party/openssl/include \\-DOPENSSL\_CRYPTO\_LIBRARY=$MY\_OHOS\_DIR/out/rk3568/packages/phone/system/lib/libcrypto.z.so \\-DHAVE\_STRTOULL=1 -DUNIX=1 -DHAVE\_POLL=0 -DHAVE\_GLOB=0 \\-DHAVE\_OPENSSL\_CRYPTO\_CTR128\_ENCRYPT=1
paho.mqtt.c需要用make生成VersionInfo.h
,执行如下命令生成:
cd $MY\_OHOS\_DIR/third\_party/iot\_device\_sdk\_c/third\_party/paho.mqtt.c
make build/VersionInfo.h
nopoll需要对应的OpenHarmony源码路径/third_party/iot_device_sdk_c/third_party/nopoll/src/nopoll_config.h:
/\*\* Nopoll Library nopoll\_config.h\* Platform dependant definitions.\*\* This is a generated file. Please modify 'configure.in'\*/#ifndef
\_\_NOPOLL\_CONFIG\_H\_\_
#define
\_\_NOPOLL\_CONFIG\_H\_\_/\*\*\* \\addtogroup nopoll\_decl\_module\* @{\*//\*\*\* @brief Allows to convert integer value (including constant values)\* into a pointer representation.\*\* Use the oposite function to restore the value from a pointer to a\* integer: \\ref PTR\_TO\_INT.\*\* @param integer The integer value to cast to pointer.\*\* @return A \\ref noPollPtr reference.\*/
#ifndef
INT\_TO\_PTR
#define
INT\_TO\_PTR(integer) ((noPollPtr) (long) ((int)integer))
#endif/\*\*\* @brief Allows to convert a pointer reference (\\ref noPollPtr),\* which stores an integer that was stored using \\ref INT\_TO\_PTR.\*\* Use the oposite function to restore the pointer value stored in the\* integer value.\*\* @param ptr The pointer to cast to a integer value.\*\* @return A int value.\*/
#ifndef
PTR\_TO\_INT
#define
PTR\_TO\_INT(ptr) ((int) (long) (ptr))
#endif/\*\*\* @brief Allows to get current platform configuration. This is used\* by Nopoll library but could be used by applications built on top of\* Nopoll to change its configuration based on the platform information.\*/
#define
NOPOLL\_OS\_UNIX (1)/\*\*\* @internal Allows to now if the platform support vasprintf\* function. Do not use this macro as it is supposed to be for\* internal use.\*/
#define
NOPOLL\_HAVE\_VASPRINTF (1)/\*\*\* @brief Indicates that this platform have support for 64bits.\*/
#define
NOPOLL\_64BIT\_PLATFORM (1)
/\*\*\* @brief Indicates where we have support for TLSv1.0 support.\*/
#define
NOPOLL\_HAVE\_TLSv10\_ENABLED (1)/\*\*\* @brief Indicates where we have support for TLSv1.1 support.\*/
#define
NOPOLL\_HAVE\_TLSv11\_ENABLED (1)/\*\*\* @brief Indicates where we have support for TLSv1.2 support.\*/
#define
NOPOLL\_HAVE\_TLSv12\_ENABLED (1)/\*\*\* @brief Indicates where we have support for TLS flexible method where the highest TLS version will be negotiated.\*/
#define
NOPOLL\_HAVE\_TLS\_FLEXIBLE\_ENABLED (1)/\* @} \*/#endif
最后修改文件OpenHarmony源码路径/third_party/iot_device_sdk_c/BUILD.gn,在executable(“mqtt_device_demo”)的deps中加入以下依赖:
"third\_party/huawei\_secure\_c:libboundscheck",
"third\_party/paho.mqtt.c:libpaho-mqtt3as",
"third\_party/libssh:libssh",
"third\_party/nopoll:libnopoll",
结果如图所示:
(4)其它三方库依赖
libz.so和libssl.so对应的源码在拉取OpenHarmony时会自获取,其分别对应OpenHarmony源码路径/third_party/libz,则属于OpenHarmony源码路径/third_party/openssl。 这些库在编译libboundscheck.so等动态库时会以依赖的方式来驱使其编译,所以只需要将对应的产物拷贝到目标设备中。
部分产物位于OpenHarmony源码路径/out/{产品型号}/common/common/目录下,部分则位于特殊的位置。这些动态库通常.z.so结尾。以rk3568为例,其`libssh.so和libz.so分别处于:
$MY\_OHOS\_DIR/out/rk3568/common/common/libz.z.so
$MY\_OHOS\_DIR/out/rk3568/packages/phone/system/lib/libssl.z.so
对于示例开发板rk3861
,其内部已经存在这些动态库,所以示例中无需拷贝这些库。如果目标设备缺少这些库,可以使用以下命令来查找动态库位置:
find $MY\_OHOS\_DIR/out -name {动态库名称}.z.so
find $MY\_OHOS\_DIR/out -name {动态库名称}.so
(5)配置华为云接入参数并编译主程序
打开OpenHarmony源码路径/third_party/iot_device_sdk_c/src/device_demo/device_demo.c,修改接入地址,设备id和设备密钥,如下图所示:
最后执行编译:
cd $OpenHarmony源码路径
./build.sh --product-name 使用的产品 --build-target iot\_device\_sdk\_c
输出如下内容则表示编译成功;
(6)验证对接华为云
将需要的动态库和主程序./MQTT_Demo拷贝到目标设备的同一路径下,执行export LD_LIBRARY_PATH=. && ./MQTT_Demo,出现MqttBase_OnConnectSuccess则表示成功连接华为云:
为了能让大家更好的学习鸿蒙 (OpenHarmony) 开发技术,这边特意整理了《鸿蒙 (OpenHarmony)开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
《鸿蒙 (OpenHarmony)开发学习手册》
入门必看:https://qr21.cn/FV7h05
- 应用开发导读(ArkTS)
- ……
HarmonyOS 概念:https://qr21.cn/FV7h05
- 系统定义
- 技术架构
- 技术特性
- 系统安全
如何快速入门?:https://qr21.cn/FV7h05
- 基本概念
- 构建第一个ArkTS应用
- 构建第一个JS应用
- ……
开发基础知识:https://qr21.cn/FV7h05
- 应用基础知识
- 配置文件
- 应用数据管理
- 应用安全管理
- 应用隐私保护
- 三方应用调用管控机制
- 资源分类与访问
- 学习ArkTS语言
- ……
基于ArkTS 开发:https://qr21.cn/FV7h05
1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……