服务器之家

服务器之家 > 正文

OpenHarmony继续上云之腾讯云平台

时间:2021-12-30 21:51     来源/作者:鸿蒙社区

OpenHarmony继续上云之腾讯云平台

一.前面的话

说实话,腾讯云物联网平台一直是我比较喜欢的物联网云太平,除了有腾讯大厂作为品质背书之外,提供的sdk也是相当好用,更有腾讯连连小程序可以帮助做界面,调试功能,后端的API有java,c++,Golang,js,python等等语言版本,非常方便,简直是上云首选.

二.首先下载sdk

我们首先来找官方sdk,茫茫文档中给我看到了那熟悉的身影:

OpenHarmony继续上云之腾讯云平台

文档地址在这,拿走不谢:

https://cloud.tencent.com/document/product/1081/48356

把sdk下载之后,熟练的放进thirdparty文件夹,

OpenHarmony继续上云之腾讯云平台

不得不提一下,2.x版本的OpenHarmony比1.x版本的代码结构清晰多了.

1.实现几个重要接口

这个时候依然要看文档,因为有些函数需要自己实现,具体是哪些呢,在这里:

https://cloud.tencent.com/document/product/1081/48389

这篇文档写了,我们要实现里面的这些接口,此处列举一二:

OpenHarmony继续上云之腾讯云平台
OpenHarmony继续上云之腾讯云平台

仔细一看,霍,好家伙还不少呢,但是不怕,都是打工人,谁怕谁啊,二话不说我就写,结果就给我给写出来了:

OpenHarmony继续上云之腾讯云平台

写出来这些后就可以准备编译了吗?

nonono,我们还没做BUILD.gn文件呢,话不多说,直接教你写。

  1. # Copyright (c) 2020 Huawei Device Co., Ltd.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13.  
  14. import("//build/lite/config/component/lite_component.gni")
  15. import("//build/lite/ndk/ndk.gni")
  16.  
  17. config("qcloud_sdk_config") {
  18.  
  19. include_dirs = [
  20. "sdk_src/internal_inc",
  21. "sdk_src/library",
  22. "include",
  23. "include/exports",
  24.  
  25. "//kernel/liteos_m/kernel/include",
  26.  
  27. # "//third_party/cmsis",
  28. "//third_party/mbedtls/include",
  29. "//third_party/mbedtls/include/mbedtls",
  30.  
  31. ]
  32.  
  33.  
  34. }
  35. cflags = [ "-Wno-unused-variable" ]
  36. cflags += [ "-Wno-unused-but-set-variable" ]
  37. cflags += [ "-Wno-unused-parameter" ]
  38. cflags += [ "-Wno-sign-compare" ]
  39. cflags += [ "-Wno-unused-function" ]
  40. cflags += [ "-Wno-return-type" ]
  41.  
  42. qcloud_sdk_sources = [
  43. "sdk_src/network/socket/network_socket.c",
  44. "sdk_src/network/tls/network_tls.c",
  45. "sdk_src/network/network_interface.c",
  46.  
  47. "sdk_src/utils/utils_list.c",
  48. "sdk_src/utils/utils_base64.c",
  49. "sdk_src/utils/qcloud_iot_ca.c",
  50. "sdk_src/utils/utils_aes.c",
  51. "sdk_src/utils/utils_getopt.c",
  52. "sdk_src/utils/utils_hmac.c",
  53. "sdk_src/utils/utils_md5.c",
  54. "sdk_src/utils/utils_sha1.c",
  55. "sdk_src/utils/json_parser.c",
  56. "sdk_src/utils/json_token.c",
  57. "sdk_src/utils/string_utils.c",
  58. "sdk_src/utils/utils_ringbuff.c",
  59. "sdk_src/utils/qcloud_iot_log.c",
  60. "sdk_src/utils/qcloud_iot_device.c",
  61. "sdk_src/utils/utils_timer.c",
  62.  
  63. "sdk_src/protocol/mqtt/mqtt_client_common.c",
  64. "sdk_src/protocol/mqtt/mqtt_client_connect.c",
  65. "sdk_src/protocol/mqtt/mqtt_client_net.c",
  66. "sdk_src/protocol/mqtt/mqtt_client_publish.c",
  67. "sdk_src/protocol/mqtt/mqtt_client_subscribe.c",
  68. "sdk_src/protocol/mqtt/mqtt_client_unsubscribe.c",
  69. "sdk_src/protocol/mqtt/mqtt_client_yield.c",
  70. "sdk_src/protocol/mqtt/mqtt_client.c",
  71.  
  72. "sdk_src/services/data_template/data_template_action.c",
  73. "sdk_src/services/data_template/data_template_client.c",
  74. "sdk_src/services/data_template/data_template_client_common.c",
  75. "sdk_src/services/data_template/data_template_client_json.c",
  76. "sdk_src/services/data_template/data_template_client_manager.c",
  77. "sdk_src/services/data_template/data_template_event.c",
  78.  
  79. "platform/os/liteos_m/HAL_Device_liteos_m.c",
  80. "platform/os/liteos_m/HAL_OS_liteos_m.c",
  81. "platform/os/liteos_m/HAL_TCP_liteos_m.c",
  82. "platform/os/liteos_m/HAL_Timer_liteos_m.c",
  83. # "./os/liteos_m/HAL_TLS_mbedtls_liteos_m.c",
  84. # "./tls/mbedtls/HAL_DTLS_mbedtls.c",
  85. "platform/tls/mbedtls/HAL_TLS_mbedtls.c",
  86.  
  87.  
  88. ]
  89.  
  90. lite_library("qcloud_sdk_static") {
  91. target_type = "static_library"
  92.  
  93. sources = qcloud_sdk_sources
  94. public_configs = [ ":qcloud_sdk_config" ]
  95. }
  96.  
  97. lite_library("qcloud_sdk_shared") {
  98. target_type = "shared_library"
  99. sources = qcloud_sdk_sources
  100. public_configs = [ ":qcloud_sdk_config" ]
  101. }
  102.  
  103. ndk_lib("qcloud_ndk") {
  104. if (board_name != "hi3861v100") {
  105. lib_extension = ".so"
  106. deps = [
  107. ":qcloud_sdk_shared"
  108. ]
  109. } else {
  110. deps = [
  111. ":qcloud_sdk_static"
  112. ]
  113. }
  114. head_files = [
  115. "//third_party/iot_link/network/mqtt/paho_mqtt/paho"
  116. ]
  117. }

整完之后就可以尝试一下有没有错误了.

2.在main里面写逻辑,处理事情

我们把官方sdk里面的点灯demo拿过来,直接新建一个demo文件夹,把文件放进去:

OpenHarmony继续上云之腾讯云平台

自己写好BUILD.gn,跟之前华为云对接一个套路,大家这么聪明,不用我多说了:

  1. # Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13.  
  14. static_library("qcloud_demo") {
  15. sources = [
  16. # "iot_thread.c",
  17. "light.c",
  18. "light_data_template_sample.c"
  19.  
  20. ]
  21.  
  22. cflags = [ "-Wno-unused-variable" ]
  23. cflags += [ "-Wno-unused-but-set-variable" ]
  24.  
  25. include_dirs = [
  26. ".",
  27. "//foundation/communication/softbus_lite/os_adapter/include",
  28. "//utils/native/lite/include",
  29. "//kernel/liteos_m/components/cmsis/2.0",
  30. "//base/iot_hardware/peripheral/interfaces/kits",
  31. "//third_party"
  32.  
  33. ]
  34.  
  35. deps = [
  36. "//third_party/qcloud-iot-explorer-sdk-embedded-c-3.2.0:qcloud_sdk_static",
  37. # "//third_party/qcloud-iot-explorer-sdk-embedded-c-3.2.0/external_libs/mbedtls:mbedtls_static",
  38. ]
  39.  
  40.  
  41. }

然后在sample里面的sdk把这个文件夹开启编译:

OpenHarmony继续上云之腾讯云平台

这样就完成了代码的编写了,很快乐有木有。

三.在云平台上创建设备

其实创建设备的过程官方文档也的确挺详细的,几乎不用再另外加工,这里给出官方的智能灯接入指南:

https://cloud.tencent.com/document/product/1081/41155

官方定义了许多的物模型,其实我们的产品可能是官方物模型没有定义的,此时我们就需要自己定义产品的属性,事件和动作等,这块自己仔细阅读官方文档就能搞懂

https://cloud.tencent.com/document/product/1081/34916

搞定了物模型,创建一个设备,记录下设备的产品id,设备id和连接秘钥

这点跟华为云平台不太一样,腾讯云的产品秘钥是平台生成的,而华为云平台是你自己定义好设备的秘钥,在创建设备的时候传上去

四.编译,运行

编译的过程倒是很顺利,这里就不过多废话了.

不过在运行的时候出现一个情况,就是mutex不够用了

自己折腾了一晚上也没找不解决办法,后面经过请教猴哥才搞明白:

OpenHarmony继续上云之腾讯云平台

阿不对,下面这个候哥才对,大家可以去他主页关注一下:

https://harmonyos.51cto.com/user/posts/13519852

候哥真大神也,短短一两句话就帮我搞定了,在此特别感谢侯哥的帮助:

OpenHarmony继续上云之腾讯云平台

然后我就把我的mutex相关代码改成posix接口,这是修改之前的:

  1. void *HAL_MutexCreate(void)
  2. {
  3. osMutexAttr_t attr;
  4. osMutexId_t mutex;
  5. char mutexName[RT_NAME_MAX];
  6. static uint32_t mutex_v;
  7.  
  8. attr.name = mutexName;
  9. attr.cb_mem = &mutex_v;
  10. attr.cb_size = 4;
  11.  
  12. mutex = osMutexNew(&attr);
  13. if (NULL == mutex) {
  14. HAL_Printf("create mutex failed");
  15. }
  16.  
  17. return mutex;
  18. }
  19.  
  20. void HAL_MutexDestroy(_IN_ void *mutex)
  21. {
  22. int err_num;
  23.  
  24. err_num = osMutexDelete((osMutexId_t)mutex);
  25.  
  26. if (0 != err_num) {
  27. HAL_Printf("destroy mutex failed");
  28. }
  29. }
  30.  
  31. void HAL_MutexLock(_IN_ void *mutex)
  32. {
  33. int err_num;
  34.  
  35. err_num = osMutexAcquire((osMutexId_t)mutex, osWaitForever);
  36.  
  37. if (0 != err_num) {
  38. HAL_Printf("lock mutex failed");
  39. }
  40. }
  41.  
  42. void HAL_MutexUnlock(_IN_ void *mutex)
  43. {
  44. int err_num;
  45.  
  46. err_num = osMutexRelease((osMutexId_t)mutex);
  47.  
  48. if (0 != err_num) {
  49. HAL_Printf("unlock mutex failed");
  50. }
  51. }
  52.  
  53. int HAL_MutexTryLock(_IN_ void *mutex)
  54. {
  55. int err_num;
  56.  
  57. err_num = osMutexAcquire((osMutexId_t)mutex, osNoWait);
  58.  
  59. if (0 != err_num) {
  60. HAL_Printf("trylock mutex failed");
  61. }
  62. return err_num;
  63. }

改完之后是这样:

  1. void *HAL_MutexCreate(void)
  2. {
  3. // osMutexAttr_t attr;
  4. // osMutexId_t mutex;
  5. char mutexName[RT_NAME_MAX];
  6. static uint32_t mutex_v;
  7.  
  8. pthread_mutex_t * mutex = HAL_Malloc(sizeof(pthread_mutex_t));
  9. pthread_mutexattr_t attr;
  10.  
  11. int ret = pthread_mutex_init(mutex, &attr); // osMutexNew(&attr);
  12. // if (NULL == mutex) {
  13. if (ret != 0) {
  14. HAL_Printf("create mutex failed ");
  15. }
  16.  
  17. return mutex;
  18. }
  19.  
  20. void HAL_MutexDestroy(_IN_ void *mutex)
  21. {
  22. int err_num;
  23.  
  24. err_num = pthread_mutex_destroy(mutex);
  25.  
  26. if (0 != err_num) {
  27. HAL_Printf("destroy mutex failed");
  28. }
  29. }
  30.  
  31. void HAL_MutexLock(_IN_ void *mutex)
  32. {
  33. int err_num;
  34.  
  35. err_num = pthread_mutex_lock(mutex);
  36.  
  37. if (0 != err_num) {
  38. HAL_Printf("lock mutex failed");
  39. }
  40. }
  41.  
  42. void HAL_MutexUnlock(_IN_ void *mutex)
  43. {
  44. int err_num;
  45.  
  46. err_num = pthread_mutex_unlock(mutex);
  47.  
  48. if (0 != err_num) {
  49. HAL_Printf("unlock mutex failed");
  50. }
  51. }
  52.  
  53. int HAL_MutexTryLock(_IN_ void *mutex)
  54. {
  55. int err_num;
  56. struct timespec absTimeout={0,0};
  57.  
  58. err_num = pthread_mutex_timedlock(mutex, &absTimeout);
  59.  
  60. if (0 != err_num) {
  61. HAL_Printf("trylock mutex failed");
  62. }
  63. return err_num;
  64. }

使用的时候记得加上头文件: #include

处理完这个异常情况,接下来就很顺利的登录腾讯云平台收发数据了

五.云平台控制展示

直接查看动图,控制还是很及时的:

OpenHarmony继续上云之腾讯云平台

六.总结

其实对接腾讯云还是蛮简单的,最新版的sdk在打印上也比以前好多了.OpenHarmony的2.x版本优化了不少东西,记得去年这时候用1.x版本对接的时候还是很费劲的,给OpenHarmony团队点赞,也给腾讯云团队点赞

大家赶紧玩起来吧

原文链接:https://harmonyos.51cto.com

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部