3-step experience to complete OpenHarmony docking with Huawei Cloud IoT on the DAYU200 development board

This article is shared from the Huawei Cloud Community "DAYU200+OpenHarmony 3.1.1 Connecting with Huawei Cloud IOT [Huawei Cloud IoT+Hongmeng]" , author: DS Xiaolongge.

I. Introduction

OpenHarmony 3.1.1 is an open source smart terminal operating system, mainly used for smart terminal devices such as smart homes, smartphones, tablets, and smart wearable devices. It is a distributed operating system that supports multiple hardware platforms and multiple programming languages, and can be easily transplanted and customized.

Features:

(1) Distributed: Supports distributed computing and distributed storage, and can perform task scheduling and data sharing among multiple devices.
(2) Cuttable: Supports cutting according to the needs of the device to meet the resource limitations of different devices.
(3) Security: Provides a security mechanism to protect the privacy and security of the device.
(4) Portable: supports multiple hardware platforms and multiple programming languages, and can be easily transplanted and customized.

The current article introduces the programming of the OpenHarmony system on the DAYU200 development board, and the use of it huaweicloud-iot-device-sdkto complete the docking of Huawei Cloud IOT platform and complete the Internet of Things data communication.

Required hardware: DAYU200 development board (RK3568)

2. DAYU200 development board

2.1 Introduction to development board

The introduction to the development board is here: https://gitee.com/hihope_iot/docs/tree/master/HiHope_DAYU200

Based on Rockchip RK3568, it integrates dual-core architecture GPU and high-performance NPU; 
the onboard quad-core 64-bit Cortex-A55 processor adopts 22nm advanced technology and has a main frequency of up to 2.0GHz; 
supports Bluetooth, Wi-Fi, audio, video and camera functions , has rich expansion interfaces and supports a variety of video input and output interfaces; 
it is equipped with dual Gigabit adaptive RJ45 Ethernet ports, which can meet the needs of multi-network port products such as NVRs and industrial gateways.

image.png

image.png

image.png

2.2 Physical picture of development board

image.png

image.png

image.png

image.png

cke_37905.png

3. Environment installation

3.1 Serial terminal

After the development board is plugged in with the serial port cable, open the device manager and you will see that the serial port is recognized COM8. This is the Debug serial port of the development board. (The USB to serial port driver needs to be installed in advance).

image.png

Open the serial port tool, create a new session, and select the serial port protocol.

image.png

The baud rate selection 1500000 is 1.5M.

image.png

Creation completed.

image.png

Select the session you just created and click Connect.

image.png

Reset the development board (there is a RESET button on the development board). Under normal circumstances, you will see the kernel startup process on the terminal; after the startup is completed, press Enter to enter the Linux terminal command line.

The current device configuration can be seen from the printed command line data:

This is the configuration information printed by uboot. From the current information, we can see that the CPU model is RK3568 and the memory is 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

Information printed by the kernel startup; it can be seen from the information that the kernel currently used by the system is the Linux 5.10 kernel.

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

image.png

You can set the terminal type on the serial port software to easily distinguish Linux terminal file types.

image.png

3.2 Firmware programming

Documentation: 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】Download the programming tool

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

After coming down, open HiHope_DAYU200 the folder.

image.png

I am programming here under windows.

image.png

In DriverAssitant_v5.1.1.zip the directory is the USB driver, unzip it and install it. If an older version of the programming tool has been installed, you need to click the "Driver Uninstall" button to download the driver, and then click the "Driver Install" button to install the driver.

image.png

RKDevTool.exe It's a programming tool.

image.png

【2】HiHope official firmware download

Address: https://gitee.com/hihope_iot/images/tree/master/HiHope_DAYU200

image.png

Download, unzip, open the programming tool and select the path.

image.png

Confirm whether the development board enters programming mode

(1) If the interface displays "A LOADER device was found", it means that the development board has entered the Loader mode and is waiting to burn the firmware.

(2) If the interface displays "A MASKROM device was found", it means that the development board has entered Maskrom mode and is waiting to program the firmware.

(3) If the interface displays "No device found", it means that the development board has not entered the programming mode, and the development board needs to enter the programming mode first.

Steps to enter programming mode

(1) First press and hold the VOL-/RECOVERY button and the RESET button without releasing it. The burning tool will display "No device found" at this time.

(2) Then release the RESER button. The programming tool displays "A LOADER device was found", indicating that it has entered the programming mode.

image.png

3.3 huaweicloud-iot-device-sdk-c introduction

Documentation: https://gitee.com/openharmony-sig/iot_device_sdk_c/blob/master/README_CN.md#5

Huawei Cloud IOT Device SDK for C is an IoT development kit for embedded devices launched by Huawei Cloud, supporting C language development. The suite provides a series of APIs that can easily realize functions such as connection between the device and the cloud, data reporting, and remote control.

The kit mainly includes the following components:

  • Huawei LiteOS: A lightweight operating system that provides kernel-level task scheduling, memory management, network protocol stack and other functions.
  • Huawei Cloud IOT Agent: A device-side IoT agent program that is responsible for connecting to the cloud IoT platform to implement device registration, data reporting, remote control and other functions.
  • Huawei Cloud IOT SDK for C: A C language development kit that provides a series of APIs to easily implement functions such as connection between devices and the cloud, data reporting, and remote control.

Using Huawei Cloud IOT Device SDK for C can greatly simplify the development process of IoT devices and improve development efficiency. It can also ensure the stability and security of the connection between the device and the cloud. This kit is suitable for various types of embedded devices, such as smart homes, smart appliances, smart wearables, etc.

Huawei Cloud IOT SDK for C SDK is designed for embedded terminal devices with strong computing and storage capabilities. Developers can achieve uplink and downlink communication between the device and the IoT platform by calling the SDK interface. The functions currently supported by the SDK are:

  • Support model: device message/property/event reporting, device command/message/property/event reception
  • Supports sub-device message forwarding and sub-device management
  • Support OTA upgrade
  • Supports two device access methods, password authentication and certificate authentication.
  • Support custom topics
  • Support device shadow query
  • Support custom log collection capabilities
  • Support end-side rule engine
  • Support SSH remote login
  • Support docking edge M2M

image.png

4. Cloud experiment

4.1 Introduction to IoT platform

Huawei Cloud Internet of Things Platform (IoT device access cloud service) provides access and management capabilities for massive devices, connects physical devices to the cloud, supports device data collection on the cloud and the cloud issues commands to devices for remote control, and cooperates with Huawei Cloud Other products help us quickly build IoT solutions.

Using the IoT platform to build a complete IoT solution mainly includes 3 parts: IoT platform, business applications and equipment.

As the middle layer that connects business applications and devices, the IoT platform shields various complex device interfaces and enables quick access to devices. It also provides powerful open capabilities to support industry users in building various IoT solutions.

Devices can access the IoT platform through fixed network, 2G/3G/4G/5G, NB-IoT, Wifi and other networks, and use LWM2M/CoAP, MQTT, HTTPS protocols to report business data to the platform. The platform can also Send control commands to the device.

Business applications implement business scenarios such as device data collection, command issuance, and device management by calling the API provided by the IoT platform.

image.png

image.png

4.2 Activate IoT services

Address: https://www.huaweicloud.com/product/iothub.html 

image.png

Click 总览to view access information. Our current equipment is going to use the MQTT protocol to connect to the Huawei Cloud platform. Here you can see the address and port number of the MQTT protocol and other information.

image.png

Summarize:

Port number: MQTT (1883) | MQTTS (8883)    
Access address: a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com

Get IP address information based on domain name address:

Microsoft Windows [Version 10.0.19045.2965] 
(c) Microsoft Corporation. all rights reserved. 

C:\Users\11266>ping a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com 

Pinging a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com [121.36.42.100] with 32 bytes Data: 
Reply from 121.36.42.100: Bytes=32 Time=38ms TTL=94 
Reply from 121.36.42.100: Bytes=32 Time=37ms TTL=94 
Reply from 121.36.42.100: Bytes=32 Time=38ms TTL=94 
Reply from 121.36.42.100: Bytes=32 Time=36ms TTL=94 

Ping statistics for 121.36.42.100: 
    Packets: Sent=4, Received=4, Lost=0 (0% loss), 
Estimated round trip time in milliseconds: 
    shortest = 36ms, longest = 38ms, average = 37ms 

C:\Users\11266>

image.png

There are two MQTT protocol access port numbers. 1883 is a non-encrypted port and 8883 is a certificate encryption port. The microcontroller cannot load the certificate, so it is more appropriate to use port 1883. The next ESP8266 uses port 1883 to connect to the Huawei Cloud IoT platform.

4.3 Create products

(1) Create products

Click on the product page, then click Create Product in the upper left corner.

image.png

(2) Fill in product information

Fill in the form based on your product name.

image.png

(3) Product creation is successful

image.png

(4) Add custom model

After the product is created, click to enter the product details page and scroll to the bottom to see the model definition.

This model is to define the data types that your device needs to upload to the server next. Write according to your own data type.

Click on Custom Model first.

image.png

Create another service ID.

image.png

Then click Add attribute.

image.png

image.png

4.4 Add device

The product is an abstract model belonging to the upper layer, and then the actual equipment is added under the product model. The added device eventually needs to be associated with the real device to complete data interaction.

(1) Register device

image.png

(2) Fill in according to your own equipment

image.png

(3) Save device information

After creation, click Save and Close to get the created device key information. This information is needed when generating MQTT triples later.

image.png

(4) Device creation completed

You can click on the device to enter the device details page.

image.png

4.5 OpenHarmony environment construction

Add the build script in the file Harmony source code path/build/subsystem_config.json:

"iot-device-sdk-c" : {
"path": "third_party/iot-device-sdk-c",
"name": "iot-device-sdk-c"
},

(1) Add subsystem compilation and build

Add the following build script to the file OpenHarmony source code path/build/subsystem_config.json:

"iot-device-sdk-c" : {
"path": "third_party/iot-device-sdk-c",
"name": "iot-device-sdk-c"
},

image.png

Add a subsystem to the product configuration file, and its subsystem name corresponds to the content added in the file, that is iot-device-sdk-c:iot-device-sdk-c.

The configuration file is located in `MY_OHOS_

image.png

(2) Compile dependent dynamic libraries

This sdk mainly depends on libboundscheck.so, libpaho-mqtt3as.so, libssh.so, libnopoll.so these dynamic libraries, which indirectly depends on libz.so, libssl.so, . libcrypto.so

(3) Compile third-party dynamic library

For libboundscheck.so, libpaho-mqtt3as.so, libssh.so, libnopoll.so, the source codes corresponding to these libraries are not integrated in OpenHarmony. If these dynamic libraries are not included in the development board, you need to download and compile them yourself.

Execute the following command to pull the corresponding source code:

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  \

Respectively in the OpenHarmony source code path/third_party/iot_device_sdk_c/third_party/huawei_secure_c, OpenHarmony source code path/third_party/iot_device_sdk_c/third_party/paho.mqtt.c, OpenHarmony source code path/third_party/iot_device_sdk_c/third_party/libssh, OpenHarmony source code path/third_party/ iot_device_sdk_c/ Place or replace the BUILD.gn file in the third_party/nopoll folder,

The content is as follows:

# 放入 $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_libHWSecureC
  include_dirs = include_common
}

ohos_shared_library("libboundscheck") {
  sources = src_libHWSecureC
  include_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_FILES
  include_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_libHWSecureC
  include_dirs = include_common
  deps=[
    "//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_nopoll
  include_dirs = include_common
  deps=[
    "//third_party/openssl:libcrypto_static",
    "//third_party/openssl:ssl_source"
  ]
  cflags = ["-w"]
}

Since libssh requires cmake to generate some header files containing various configurations, here we first use the cmake command to generate intermediate files.

Here you need to pass parameters to cmake according to the functions supported by the development board in order to turn on or off the corresponding features, such as in the example -DHAVED_GLOB=0

The command is as follows:

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 needs to be generated with make VersionInfo.h, execute the following command to generate:

cd $MY_OHOS_DIR/third_party/iot_device_sdk_c/third_party/paho.mqtt.c
make build/VersionInfo.h

nopoll requires the corresponding OpenHarmony source code path/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

Finally, modify the file OpenHarmony source code path/third_party/iot_device_sdk_c/BUILD.gn, and add the following dependencies to the deps of executable("mqtt_device_demo"):

"third_party/huawei_secure_c:libboundscheck",
"third_party/paho.mqtt.c:libpaho-mqtt3as",
"third_party/libssh:libssh",
"third_party/nopoll:libnopoll",

The result is shown in the figure:

image.png

(4) Other third-party library dependencies

The source codes corresponding to libz.so and libssl.so will be obtained automatically when pulling OpenHarmony. They respectively correspond to the OpenHarmony source code path/third_party/libz, and belong to the OpenHarmony source code path/third_party/openssl. When compiling dynamic libraries such as libboundscheck.so, these libraries will drive their compilation in a dependency manner, so you only need to copy the corresponding products to the target device.

Some products are located in the OpenHarmony source code path /out/{product model}/common/common/ directory, and some are located in special locations. These dynamic libraries usually end with .z.so. Taking rk3568 as an example, its libssh.so and libz.so are respectively in:

$MY_OHOS_DIR/out/rk3568/common/common/libz.z.so
$MY_OHOS_DIR/out/rk3568/packages/phone/system/lib/libssl.z.so

For the example development board rk3861, these dynamic libraries already exist inside it, so there is no need to copy these libraries in the example. If the target device is missing these libraries, you can use the following command to find the dynamic library location:

find $MY_OHOS_DIR/out -name {dynamic library name}.z.so 
find $MY_OHOS_DIR/out -name {dynamic library name}.so

(5) Configure Huawei Cloud access parameters and compile the main program

Open the OpenHarmony source code path/third_party/iot_device_sdk_c/src/device_demo/device_demo.c, and modify the access address, device id and device key, as shown in the following figure:

image.png

Finally perform compilation:

cd $OpenHarmony source code 
path./build.sh --product-name Product used --build-target iot_device_sdk_c

The following output indicates that the compilation is successful;

image.png

(6) Verify connection with Huawei Cloud

Copy the required dynamic library and main program ./MQTT_Demo to the same path of the target device, and execute export LD_LIBRARY_PATH=. && ./MQTT_Demo. If MqttBase_OnConnectSuccess appears, it indicates a successful connection to Huawei Cloud:

image.png

Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~

JetBrains releases Rust IDE: RustRover Java 21 / JDK 21 (LTS) GA With so many Java developers in China, an ecological-level application development framework .NET 8 should be born. The performance is greatly improved, and it is far ahead of .NET 7. PostgreSQL 16 is released by a former member of the Rust team I deeply regret and asked to cancel my name. I completed the removal of Nue JS on the front end yesterday. The author said that I will create a new Web ecosystem. NetEase Fuxi responded to the death of an employee who was "threatened by HR due to BUG". Ren Zhengfei: We are about to enter the fourth industrial revolution, Apple Is Huawei's teacher Vercel's new product "v0": Generate UI interface code based on text
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/10111852