libcoap3对接华为云平台

devtools/2024/10/20 15:46:46/

文章目录

  • 前言
  • 一、平台注册
  • 二、引入源码库
    • 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)运行
在这里插入图片描述
在这里插入图片描述

总结

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


http://www.ppmy.cn/devtools/59338.html

相关文章

文件安全传输系统,如何保障信创环境下数据的安全传输?

文件安全传输系统是一套旨在保护数据在传输过程中的安全性和完整性的技术或解决方案。通常包括以下几个关键组件&#xff1a; 加密&#xff1a;使用强加密算法来确保文件在传输过程中不被未授权访问。 身份验证&#xff1a;确保只有授权用户才能访问或传输文件。 完整性校验…

甘肃美食于兰洽会数智电商馆展现魅力

在近日盛大开幕的兰洽会上&#xff0c;数智电商馆成为了备受瞩目的焦点&#xff0c;而甘肃平凉的特产更是在其中大放异彩。 平凉&#xff0c;这座拥有深厚历史文化底蕴的城市&#xff0c;带着其独具特色的物产走进了兰洽会的舞台。走进数智电商馆&#xff0c;首先映入眼帘的便是…

网络安全-网络安全及其防护措施2

6.安全设计和日志 安全审计 安全审计是对系统和网络活动进行检查和记录的过程&#xff0c;确保合规性和安全性。审计过程可以帮助发现潜在的安全漏洞和违规行为&#xff0c;并验证系统配置和操作的正确性。 定期审计 定义&#xff1a;定期检查系统和网络的安全配置和活动记录…

web今日学习目标

- [ ] 能够说出CSS的三种书写位置 行内式 内嵌式 外链式 - [ ] 能够写出实体化三属性的三个css属性 width height background-color - [ ] 能够说出标签的3种显示模式和他们的特点 块级 行内 行内块 display:block/ inline/ inline-block - [ ] 能…

「Python」基于Gunicorn、Flask和Docker的高并发部署

目标预期 使用Gunicorn作为WSGI HTTP服务器,提供高效的Python应用服务。使用Flask作为轻量级Web应用框架,快速开发Web应用。利用Docker容器化技术,确保应用的可移植性和一致性。实现高并发处理,提高应用的响应速度和稳定性。过程 环境准备:安装Docker和Docker Compose。编…

数据结构-C语言-排序(1)

代码位置&#xff1a;test-c-2024: 对C语言习题代码的练习 (gitee.com) 一、前言&#xff1a; 1.1-排序定义&#xff1a; 排序就是将一组杂乱无章的数据按照一定的规律&#xff08;升序或降序&#xff09;组织起来。 1.2-排序分类&#xff1a; 常见的排序算法&#xff1a; 插…

Springboot定义阿里云oss工具类

Springboot定义阿里云oss工具类 文章目录 Springboot定义阿里云oss工具类1、定义OSS相关配置2、读取OSS配置3、生成OSS工具类对象4、定义使用工具类 1、定义OSS相关配置 首先&#xff0c;在 application.yml 文件中定义阿里云 OSS 的相关配置信息。这些配置包括 endpoint、acc…

深度学习中激活函数的演变与应用:一个综述

摘要 本文全面回顾了深度学习中激活函数的发展历程,从早期的Sigmoid和Tanh函数,到广泛应用的ReLU系列,再到近期提出的Swish、Mish和GeLU等新型激活函数。深入分析了各类激活函数的数学表达、特点优势、局限性以及在典型模型中的应用情况。通过系统的对比分析,本文探讨了激…