当前位置: 首页 > news >正文

libcoap3对接华为云平台

文章目录

  • 前言
  • 一、平台注册
  • 二、引入源码库
    • 1.libcoap仓库编译
    • 2.分析网络报文
    • 3.案例代码
    • 4.编译&运行
  • 总结


前言

通过libcoap3开源代码库对接华为云平台,本文章将讨论加密与不加密的方式对接华为云平台。


一、平台注册

首先,你需要在华为云平台上创建一个coap协议的设备,并制定好数据格式,这个自行百度。

二、引入源码库

1.libcoap仓库编译

步骤如下(linux环境):

#参考https://raw.githubusercontent.com/obgm/libcoap/develop/BUILDING
git clone https://github.com/obgm/libcoap.git
cd libcoap
cd ext
# v0.9-rc1 这个版本的加密库没啥问题,其它版本没试
git clone --branch v0.9-rc1 --single-branch https://github.com/eclipse/tinydtls.git
cd ..
#这一步要做,不然报错
cp autogen.sh ext/tinydtls/ 
cmake -E remove_directory build
cmake -E make_directory build
cd build
cmake --build . -- install
cp  lib/libtinydtls.so /usr/local/lib/

2.分析网络报文

在这里插入图片描述
(1)连接报文
注意这几个选项,在下面的案例代码将体现
在这里插入图片描述
(2)服务端资源请求报文
重点关注这个token,后面主动上传全靠它
在这里插入图片描述
(3)数据上传报文
在这里插入图片描述

3.案例代码

先找到设备标识码

在这里插入图片描述

#include <threads.h>
#include <coap3/coap.h>#include <ctype.h>
#include <stdio.h>#define COAP_CLIENT_URI "coap://015f8fcbf7.iot-coaps.cn-north-4.myhuaweicloud.com"#define COAP_USE_PSK_ID "561342" //修改这个地方的标识码#define RD_ROOT_STR   "t/d"typedef unsigned char method_t;
unsigned char msgtype = COAP_MESSAGE_NON;
static coap_context_t *main_coap_context = NULL;
static int quit = 0;
static int is_mcast = 0;uint8_t sensor_data[2]={0x00,0x33};	unsigned int wait_seconds = 5; /* default timeout in seconds */static unsigned char _token_data[24]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38}; /* With support for RFC8974 */
coap_binary_t the_token = { 8, _token_data };static void handle_observe_request(coap_resource_t *resource COAP_UNUSED,coap_session_t *session,const coap_pdu_t *request,const coap_string_t *query COAP_UNUSED,coap_pdu_t *response) {unsigned char observe_op=0;coap_log_info("handle_observe_request  \n");/* 打印服务端的报文信息 */coap_show_pdu(COAP_LOG_INFO, request);coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);/* 存储服务端发来的token   ,后面数据上传需要这个token */coap_bin_const_t token = coap_pdu_get_token(request);memcpy(the_token.s,token.s,token.length);the_token.length = token.length;coap_add_option(response,COAP_OPTION_OBSERVE,1,&observe_op);}static void
free_xmit_data(coap_session_t *session COAP_UNUSED, void *app_ptr) {coap_free(app_ptr);return;
}/* 上报设备信息 */
static int 
coap_report(coap_context_t *ctx,coap_session_t *session,method_t m,unsigned char *data,size_t length) {coap_pdu_t *pdu;(void)ctx;coap_log_debug("sending CoAP request\n");if (!(pdu = coap_new_pdu(COAP_MESSAGE_NON, m, session))) {free_xmit_data(session, data);return 1;}if (!coap_add_token(pdu, the_token.length,the_token.s)) {coap_log_debug("cannot add token to request\n");}if (!length) {return 1;}coap_add_data( pdu,  length,  data);if (coap_send(session, pdu) == COAP_INVALID_MID) {coap_log_err("cannot send CoAP pdu\n");return 1;}return 0;
}static coap_response_t
message_handler(coap_session_t *session COAP_UNUSED,const coap_pdu_t *sent,const coap_pdu_t *received,const coap_mid_t id COAP_UNUSED) {coap_opt_t *block_opt;coap_opt_iterator_t opt_iter;size_t len;const uint8_t *databuf;size_t offset;size_t total;coap_pdu_code_t rcv_code = coap_pdu_get_code(received);coap_pdu_type_t rcv_type = coap_pdu_get_type(received);coap_bin_const_t token = coap_pdu_get_token(received);coap_string_t *query = coap_get_query(received);coap_log_debug("** process incoming %d.%02d response:\n",COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F);coap_show_pdu(COAP_LOG_INFO, received);return COAP_RESPONSE_OK;}static void init_resources(coap_context_t * ctx)
{coap_resource_t *r;/* 创建设备资源,后面服务器要访问这个资源 */r = coap_resource_init(coap_make_str_const(RD_ROOT_STR), 0);/* 绑定get 方法,这个是通过抓包发现的 */coap_register_request_handler(r, COAP_REQUEST_GET, handle_observe_request);coap_add_resource(ctx, r);}static int
resolve_address(const char *host, const char *service, coap_address_t *dst,int scheme_hint_bits)
{uint16_t port = service ? atoi(service) : 0;int ret = 0;coap_str_const_t str_host;coap_addr_info_t *addr_info;str_host.s = (const uint8_t *)host;str_host.length = strlen(host);addr_info = coap_resolve_address_info(&str_host, port, port, port, port,AF_UNSPEC, scheme_hint_bits,COAP_RESOLVE_TYPE_REMOTE);if (addr_info){ret = 1;*dst = addr_info->addr;is_mcast = coap_is_mcast(dst);}coap_free_address_info(addr_info);return ret;
}void client_coap_init(void)
{coap_session_t *session = NULL;coap_pdu_t *pdu;coap_address_t dst;coap_mid_t mid;int len;coap_uri_t uri;char portbuf[8];char uri_query_op[20]={0};unsigned int wait_ms = 0;int result = -1;
#define BUFSIZE 100unsigned char buf[BUFSIZE];int res;const char *coap_uri = COAP_CLIENT_URI;/* Initialize libcoap library */coap_startup();coap_set_log_level(COAP_MAX_LOGGING_LEVEL);/* Parse the URI */len = coap_split_uri((const unsigned char *)coap_uri, strlen(coap_uri), &uri);if (len != 0){coap_log_warn("Failed to parse uri %s\n", coap_uri);goto fail;}snprintf(portbuf, sizeof(portbuf), "%d", uri.port);snprintf((char *)buf, sizeof(buf), "%*.*s", (int)uri.host.length,(int)uri.host.length, (const char *)uri.host.s);/* resolve destination address where packet should be sent */len = resolve_address((const char *)buf, portbuf, &dst, 1 << uri.scheme);if (len <= 0){coap_log_warn("Failed to resolve address %*.*s\n", (int)uri.host.length,(int)uri.host.length, (const char *)uri.host.s);goto fail;}main_coap_context = coap_new_context(NULL);if (!main_coap_context){coap_log_warn("Failed to initialize context\n");goto fail;}init_resources(main_coap_context);coap_context_set_block_mode(main_coap_context, COAP_BLOCK_USE_LIBCOAP);coap_context_set_keepalive(main_coap_context, 60);session = coap_new_client_session(main_coap_context, NULL, &dst,COAP_PROTO_UDP);coap_session_init_token(session, the_token.length, the_token.s);coap_register_response_handler(main_coap_context, message_handler);/* construct CoAP message */pdu = coap_pdu_init(COAP_MESSAGE_CON,COAP_REQUEST_CODE_POST,coap_new_message_id(session),coap_session_max_pdu_size(session));if (!pdu) {coap_log_warn("Failed to create PDU\n");goto fail;}if (!coap_add_token(pdu, the_token.length, the_token.s)) {coap_log_debug("cannot add token to request\n");}coap_add_option(pdu, COAP_OPTION_URI_PATH, 1, "t");coap_add_option(pdu, COAP_OPTION_URI_PATH, 1, "r");unsigned char opbuf[40];coap_add_option(pdu, COAP_OPTION_CONTENT_FORMAT,coap_encode_var_safe(opbuf, sizeof(opbuf),COAP_MEDIATYPE_APPLICATION_OCTET_STREAM),opbuf);sprintf(uri_query_op,"ep=%s",COAP_USE_PSK_ID);coap_add_option(pdu, COAP_OPTION_URI_QUERY, strlen(uri_query_op), uri_query_op);if (is_mcast){wait_seconds = coap_session_get_default_leisure(session).integer_part + 1;}wait_ms = wait_seconds * 1000;/* and send the PDU */mid = coap_send(session, pdu);if (mid == COAP_INVALID_MID){coap_log_warn("Failed to send PDU\n");goto fail;}while (!quit || is_mcast){result = coap_io_process(main_coap_context, 1000);coap_log_info("result  %d  wait_ms %d\n",result,wait_ms);if (result >= 0){if (wait_ms > 0){if ((unsigned)result >= wait_ms){//产生一个随机值sensor_data[1] = wait_ms/100+result-wait_ms;if ( 1 == coap_report(main_coap_context, session, COAP_RESPONSE_CODE_CONTENT,sensor_data, 2)) {goto fail;}wait_ms = wait_seconds * 1000;}else{wait_ms -= result;}}}}
fail:/* Clean up library usage so client can be run again */quit = 0;coap_session_release(session);session = NULL;coap_free_context(main_coap_context);main_coap_context = NULL;coap_cleanup();
}int main()
{client_coap_init();  }

4.编译&运行

(1)编译
gcc -o test_coap_nodtls test_coap_nodtls.c -I/usr/local/include/coap3/ -lcoap-3 -ltinydtls -I/usr/local/include/tinydtls/
(2)运行
在这里插入图片描述
在这里插入图片描述

总结

加密对接的代码就不放出来,这个先参考着搞吧。

相关文章:

libcoap3对接华为云平台

文章目录 前言一、平台注册二、引入源码库1.libcoap仓库编译2.分析网络报文3.案例代码4.编译&运行 总结 前言 通过libcoap3开源代码库对接华为云平台&#xff0c;本文章将讨论加密与不加密的方式对接华为云平台。 一、平台注册 首先&#xff0c;你需要在华为云平台上创建…...

【鸿蒙学习笔记】关系型数据库概述

目录标题 关系型数据库的运行机制样例代码共通方法 DBUtilsIndex 代码效果 关系型数据库的运行机制 1、 关系型数据库对应用提供通用的操作接口&#xff0c;底层使用SQLite作为持久化存储引擎&#xff0c;支持SQLite具有的数据库特性&#xff0c;包括但不限于事务、索引、视图…...

Find My网球拍|苹果Find My技术与网球拍结合,智能防丢,全球定位

网球是球类运动项目之一&#xff0c;网球拍作为这项运动的必备工具&#xff0c;有木质球拍、铝合金球拍、钢质球拍和复合物&#xff08;尼龙、碳素&#xff09;球拍&#xff0c;任何材质的球拍均可用于比赛。网球拍由拍头、拍喉、拍柄组成&#xff0c;在使用时还需要配合网球线…...

windows环境下部署多个端口Tomcat服务和开机自启动设置保姆级教程

前言 本文主要介绍了 windows环境下&#xff0c;配置多个Tomcat设置不同端口启动服务。其实在思路上Linux上也是适用的&#xff0c;只是 Linux 上没有可视化客户端&#xff0c;会麻烦些&#xff0c;但总体的思路上是一样的。 注&#xff1a;文章中涉及些文字和图片是搬运了其他…...

科普文:一文搞懂jvm实战(四)深入理解逃逸分析Escape Analysis

概叙 Java 中的对象是否都分配在堆内存中&#xff1f; 好了太抽象了&#xff0c;那具体一点&#xff0c;看看下面这个对象是在哪里分配内存&#xff1f; public void test() { Object object new Object(); }这个方法中的object对象&#xff0c;是在堆中分配内存么&#xff1…...

中文大模型发展到哪一个阶段了?

中文大模型发展到哪一个阶段了&#xff1f; 近日&#xff0c;中文大模型综合性测评基准SuperCLUE&#xff0c;发布了上半年大模型中文综合评测报告。“百模大战”中&#xff0c;OpenAI的GPT-4o是表现最优秀的大模型&#xff0c;但国内大模型已将差缩小至4.8%。国内大模型崛起迅…...

【PostgreSQL】Spring boot + Mybatis-plus + PostgreSQL 处理json类型情况

Spring boot Mybatis-plus PostgreSQL 处理json类型情况 一、前言二、技术栈三、背景分析四、方案分析4.1 在PostgreSQL 数据库中直接存储 json 对象4.2 在PostgreSQL 数据库中存储 json 字符串 五、自定义类型处理器5.1 定义类型处理器5.2 使用自定义类型处理器 一、前言 在…...

华为910b推理Qwen1.5-72b

前情提要&#xff1a;华为910b部署训练推理大模型&#xff0c;本人之前并没有接触过&#xff0c;所以&#xff0c;写此文档进行记录。 &#xff08;注意&#xff1a;版本适配很重要&#xff01;&#xff01;不然就像我一样走了好多坑~~~&#xff09; 首先&#xff0c;看一张图…...

legoloam算法环境配置和调试笔记

安装gtsam 参考 Ubuntu20.04安装gtsam记录_gtsam安装-CSDN博客 mkdir buildcd buildcmake .. make -...

如何用CSS3画一个三角形?

要用 CSS3 画一个三角形&#xff0c;可以利用元素的边框和透明边框的特性来实现。以下是一个简单的示例代码&#xff1a; .triangle {width: 0;height: 0;border-left: 50px solid transparent; /* 左边框为透明&#xff0c;控制三角形的左斜边 */border-right: 50px solid tr…...

不同型号的GD32 MCU如何区分?

大家是否碰到过以下应用场景&#xff1a;同一套软件代码希望跑在不同型号的GD32 MCU中&#xff0c;但有些地方需要根据MCU型号进行调整&#xff1f;或者上位机或其他MCU与GD32 MCU通信时需要知道对应的MCU型号是哪个&#xff1f; 此时&#xff0c;我们就需要了解如何获取以及区…...

关于windows下编译xLua插件的流程记录

1.工程准备 1.xLua工程&#xff1a;GitHub - Tencent/xLua: xLua is a lua programming solution for C# ( Unity, .Net, Mono) , it supports android, ios, windows, linux, osx, etc. 2.build_xlua_with_libs工程&#xff1a;GitHub - chexiongsheng/build_xlua_with_libs…...

Hadoop简明教程

文章目录 关于HadoopHadoop拓扑结构Namenode 和 Datanode 基本管理启动Hadoop启动YARN验证Hadoop服务停止Hadoop停止HDFS Hadoop集群搭建步骤准备阶段Java环境配置Hadoop安装与配置HDFS格式化与启动服务测试集群安装额外组件监控与维护&#xff1a; 使用Docker搭建集群使用Hado…...

基于STM32设计的药品柜温湿度监测系统(华为云IOT)(184)

基于STM32设计的药品柜温湿度监测系统(华为云IOT)(184) 文章目录 一、前言1.1 项目介绍【1】项目功能介绍【2】整体需求总结【3】项目硬件模块组成1.2 设计思路【1】整体设计思路【2】ESP8266工作模式配置【3】华为云IOT手机APP界面开发思路1.3 项目开发背景【1】选题的意义【2…...

SpringBoot源码阅读(10)——后处理器

后处理器是在监听器EnvironmentPostProcessorApplicationListener中被加载。 入口在SpringApplication实例方法prepareEnvironment&#xff0c;第343行。 listeners.environmentPrepared(bootstrapContext, environment);这里触发了事件ApplicationEnvironmentPreparedEvent 相…...

【源码开源】C#桌面应用开发:串口调试助手

c#桌面应用开发 1、环境搭建和工程创建&#xff1a;参照番茄定时器项目 工程创建参照 2、界面布局设计 3、具体功能函数 &#xff08;1&#xff09;端口扫描&#xff1a; private void btn_com_scan_Click(object sender, EventArgs e){//端口号扫描ReflashPortToComboBox(…...

malloc与free函数的用法(精简全面 · 一看即懂)

前言&#xff1a;Hello大家好&#x1f618;&#xff0c;我是心跳sy&#xff0c;今天为大家带来malloc函数与free函数的用法&#xff0c;我们一起来看看吧&#xff01; 目录 一、malloc函数 &#x1f4ab; 1、⭐️malloc函数对应的头文件⭐️ 2、⭐️malloc函数的作用⭐️ 3…...

强制升级最新系统,微软全面淘汰Win10和部分11用户

说出来可能不信&#xff0c;距离 Windows 11 正式发布已过去整整三年时间&#xff0c;按理说现在怎么也得人均 Win 11 水平了吧&#xff1f; 然而事实却是&#xff0c;三年时间过去 Win 11 占有率仅仅突破到 29%&#xff0c;也就跳起来摸 Win 10 屁股的程度。 2024 年 6 月 Wi…...

java-命令行连接 mysql

在 Java 中&#xff0c;通过命令行连接 MySQL 可以使用以下步骤。假设您已经安装并配置了 MySQL 5.7。 ### 一、通过命令行连接 MySQL #### 1. 打开命令行终端 在不同的操作系统上打开命令行终端的方式&#xff1a; - **Windows**&#xff1a;按 Win R 键&#xff0c;输入…...

RK3588部署YOLOV8-seg的问题

在使用YOLOV8-seg训练出来的pt模型转为onnx的时候&#xff0c;利用以下仓库地址转。 git clone https://github.com/airockchip/ultralytics_yolov8.git 在修改ultralytics/cfg/default.yaml中的task&#xff0c;mode为model为自己需要的内容后&#xff0c; 执行以下语句 cd …...

Python|GIF 解析与构建(5):手搓截屏和帧率控制

目录 Python&#xff5c;GIF 解析与构建&#xff08;5&#xff09;&#xff1a;手搓截屏和帧率控制 一、引言 二、技术实现&#xff1a;手搓截屏模块 2.1 核心原理 2.2 代码解析&#xff1a;ScreenshotData类 2.2.1 截图函数&#xff1a;capture_screen 三、技术实现&…...

多模态2025:技术路线“神仙打架”,视频生成冲上云霄

文&#xff5c;魏琳华 编&#xff5c;王一粟 一场大会&#xff0c;聚集了中国多模态大模型的“半壁江山”。 智源大会2025为期两天的论坛中&#xff0c;汇集了学界、创业公司和大厂等三方的热门选手&#xff0c;关于多模态的集中讨论达到了前所未有的热度。其中&#xff0c;…...

大数据零基础学习day1之环境准备和大数据初步理解

学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 &#xff08;1&#xff09;设置网关 打开VMware虚拟机&#xff0c;点击编辑…...

大数据学习(132)-HIve数据分析

​​​​&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4…...

uniapp 开发ios, xcode 提交app store connect 和 testflight内测

uniapp 中配置 配置manifest 文档&#xff1a;manifest.json 应用配置 | uni-app官网 hbuilderx中本地打包 下载IOS最新SDK 开发环境 | uni小程序SDK hbulderx 版本号&#xff1a;4.66 对应的sdk版本 4.66 两者必须一致 本地打包的资源导入到SDK 导入资源 | uni小程序SDK …...

【前端异常】JavaScript错误处理:分析 Uncaught (in promise) error

在前端开发中&#xff0c;JavaScript 异常是不可避免的。随着现代前端应用越来越多地使用异步操作&#xff08;如 Promise、async/await 等&#xff09;&#xff0c;开发者常常会遇到 Uncaught (in promise) error 错误。这个错误是由于未正确处理 Promise 的拒绝&#xff08;r…...

DAY 26 函数专题1

函数定义与参数知识点回顾&#xff1a;1. 函数的定义2. 变量作用域&#xff1a;局部变量和全局变量3. 函数的参数类型&#xff1a;位置参数、默认参数、不定参数4. 传递参数的手段&#xff1a;关键词参数5 题目1&#xff1a;计算圆的面积 任务&#xff1a; 编写一…...

node.js的初步学习

那什么是node.js呢&#xff1f; 和JavaScript又是什么关系呢&#xff1f; node.js 提供了 JavaScript的运行环境。当JavaScript作为后端开发语言来说&#xff0c; 需要在node.js的环境上进行当JavaScript作为前端开发语言来说&#xff0c;需要在浏览器的环境上进行 Node.js 可…...

Python 高级应用10:在python 大型项目中 FastAPI 和 Django 的相互配合

无论是python&#xff0c;或者java 的大型项目中&#xff0c;都会涉及到 自身平台微服务之间的相互调用&#xff0c;以及和第三发平台的 接口对接&#xff0c;那在python 中是怎么实现的呢&#xff1f; 在 Python Web 开发中&#xff0c;FastAPI 和 Django 是两个重要但定位不…...

ArcGIS Pro+ArcGIS给你的地图加上北回归线!

今天来看ArcGIS Pro和ArcGIS中如何给制作的中国地图或者其他大范围地图加上北回归线。 我们将在ArcGIS Pro和ArcGIS中一同介绍。 1 ArcGIS Pro中设置北回归线 1、在ArcGIS Pro中初步设置好经纬格网等&#xff0c;设置经线、纬线都以10间隔显示。 2、需要插入背会归线&#xf…...