华为编程语言仓颉2023

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。 需要这份系统化的资料的朋友,可以戳这里获取 一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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.solibpaho-mqtt3as.solibssh.solibnopoll.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\_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"\]
}

由于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",



![img](https://yyzqsoft.com/uploads/202407/03/d6b8115fd2540a7c.webp)
![img](https://yyzqsoft.com/uploads/202407/03/e8dcb83aaf71911e.webp)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以戳这里获取**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

AVE\_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",



[外链图片转存中...(img-bgeOXlJS-1715649905428)]
[外链图片转存中...(img-FCJVqe13-1715649905428)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以戳这里获取**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

知秋君
上一篇 2024-08-22 20:36
下一篇 2024-08-22 20:02

相关推荐