libcoap3对接华为云平台

news2024/7/30 10:57:43

文章目录

  • 前言
  • 一、平台注册
  • 二、引入源码库
    • 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 100
  unsigned 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.coloradmin.cn/o/1917432.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【C++课程学习】:new和delete为什么要配套使用,new,delete和malloc,free的比较

&#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;C课程学习 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 目录 &#x1f3a1;1.new&#xff0c;delete和malloc&#xff0c;free的区别&#xff1a; ⌚️相同点&…

沃尔玛、亚马逊跨境电商测评补单自养号实操指南

通过自养号测评补单来提升流量是一种被众多卖家采用的运营手段&#xff0c;它可以帮助卖家快速提高商品的曝光度和吸引潜在买家。以下是自养号测评的详解&#xff1a; 一、自养号测评的定义 自养号测评是指卖家通过注册并管理自己的海外买家账号&#xff0c;对自家商品进行模…

linux 命令:Debian、ubuntu系统的离线软件包管理工具dkpg详解

目录 一、背景 二、工作原理 1、基本原理 2、与apt工具的对比 2.1优势 2.1.1直接为底层操作&#xff0c;可操控性好 2.1.2安装卸载和配置都可实现 2.2劣势 2.2.1依赖关系处理不足 2.2.2无法从软件仓库获取软件包 2.2.3用户界面不友好 2.3 总结 三、语法和使用 1、…

演唱会售票系统(Springboot+MySQL+Mybatis+BootStrap)

本演唱会售票系统结合了多个流行的技术栈&#xff0c;提供了全面的功能模块&#xff0c;包括用户和管理员两个角色。前端采用Bootstrap框架设计响应式界面&#xff0c;后端采用Spring Boot和MyBatis Plus实现业务逻辑和数据库操作&#xff0c;Sa-Token确保系统的安全性。通过这…

TikTok短视频矩阵管理系统源码

在数字化浪潮汹涌的今天&#xff0c;短视频已成为人们生活中不可或缺的一部分。TikTok作为短视频领域的佼佼者&#xff0c;其用户基数庞大&#xff0c;影响力深远。然而&#xff0c;对于众多内容创作者和营销人员来说&#xff0c;如何高效管理多个TikTok账号&#xff0c;实现批…

【AI大模型新型智算中心技术建设白皮书 2024】

文末有福利&#xff01; 一、新算效——重塑计算架构 1.1 下一代 AI 芯片设计思路 以 GPU 为 代 表 的 高 性 能 并 行 计 算 芯 片 架 构 和 以 针 对 AI 领 域 专 用 加 速&#xff08;DSA, Domain Specific Architecture&#xff0c;DSA&#xff09;为代表的芯片架构是目…

从头开始搭建一套Elasticsearch集群

前言 刚开始使用ES接触的就是rpm或者是云上提供的ES服务&#xff0c;基本上开箱即用。特别是云上的ES服务&#xff0c;开局就是集群版本&#xff0c;提供的是优化后的参数配置、开箱即匹配访问鉴权及常用插件&#xff0c;如无特殊需要基本上屏蔽了所有细节&#xff0c;直接可投…

视频压缩电脑软件,视频压缩电脑怎么操作

在数字媒体时代&#xff0c;视频文件的大小往往让人头疼。如何在不损失画质的情况下压缩视频&#xff0c;成为了许多创作者和普通用户的需求。本文将为你详细介绍电脑压缩软件&#xff0c;让你轻松应对视频压缩任务。 1.下载并安装视频压缩软件。 2.打开软件&#xff0c;选择视…

HTML集成优雅的实时输入清除功能

引言 你好呀&#xff0c;我是小邹。 点击访问 我的个人博客 在现代网页设计中&#xff0c;用户体验是至关重要的。一个流畅、直观且反应迅速的界面能够显著提升用户满意度。本文将介绍如何在网页表单中集成实时输入清除功能&#xff0c;即在输入框中显示一个“x”图标&#…

华为手机联系人不见了怎么恢复?3个解决方案

华为手机联系人列表就像是我们精心编织的社交网络之网。然而&#xff0c;有时&#xff0c;这张网可能会因为各种原因而意外破损&#xff0c;联系人信息消失得无影无踪&#xff0c;让我们陷入“人脉孤岛”的困境。华为手机联系人不见了怎么恢复&#xff1f;别担心&#xff0c;我…

3.js - 灯光与阴影 - 聚光灯

// ts-nocheckimport * as THREE from three // 导入轨道控制器 import { OrbitControls } from three/examples/jsm/controls/OrbitControls // 导入hdr加载器 import { RGBELoader } from three/examples/jsm/loaders/RGBELoader.js // 导入lil.gui import { GUI } from thre…

关于 Qt在国产麒麟系统上设置的setFixedSize、setMinimumFixed、setMaxmumFixed设置无效 的解决方法

若该文为原创文章&#xff0c;转载请注明原文出处 本文章博客地址&#xff1a;https://hpzwl.blog.csdn.net/article/details/140242881 长沙红胖子Qt&#xff08;长沙创微智科&#xff09;博文大全&#xff1a;开发技术集合&#xff08;包含Qt实用技术、树莓派、三维、OpenCV…

万物皆可嵌入--embedding在GPU中的实现

摘要 Embedding技术自从谷歌推出word2vec的工作后得到迅速的应用&#xff0c;典型应用之一是在广告推荐场景中&#xff0c;从word2vec演进到item2vec&#xff0c;embedding技术的出现也使深度学习进入广告推荐的场景成为可能。广告推荐模型动辄几十GB甚至TB的模型大小&#xf…

MVC 控制器 中Action 不能同名,参数不一样,路由器寻找不到对应的,要加特性

//1 方法不可能完全相同&#xff0c;参数不同//2 那还需要特性吗&#xff1f;需要的&#xff0c;因为MVC选择方法时&#xff0c;不是按参数选择&#xff1a;http请求发送很多数据&#xff0c;其实没法识别&#xff0c;//因为mvc找方法是通过反射来的&#xff0c;GetMethods(nam…

C#(asp.net)房屋租赁管理系统-计算机毕业设计源码64421

目 录 摘要 1 绪论 1.1 研究背景与意义 1.2开发现状 1.3论文结构与章节安排 2 房屋租赁管理系统分析 2.1 可行性分析 2.1.1 技术可行性分析 2.1.2 经济可行性分析 2.1.3 法律可行性分析 2.2 系统功能分析 2.2.1 功能性分析 2.2.2 非功能性分析 2.3 系统用例分析 …

vue3+springboot+mybatis+mysql项目实践--简单登录注册功能实现

这里是一次对vue3springbootmybatismysql的项目实现&#xff0c;简单实现前后端分离的登录注册功能&#xff0c;主要工具&#xff1a;idea,navicat 目录 一、创建vue3项目并初始配置 创建vue3项目 2.修改项目结构 1&#xff09;原始目录结构 2&#xff09;修改后目录结构 …

5G RedCap调查报告

一、5G RedCap技术背景 5G RedCap(Reduced Capability缩写,轻量化5G),是3GPP标准化组织定义下的5G裁剪版本,是5G面向中高速率连接场景的物联网技术,它的能力介于5G NR(含eMBB和uRLLC)和LPWA(如LTE-M和NR-IoT)之间,如图1所示,是5G-A(5G Advanced)的关键技术之一。…

用网络编程完成windows和linux跨平台之间的通信(服务器)

服务器代码逻辑&#xff1a; 服务器功能 创建 Socket&#xff1a; 服务器首先创建一个 Socket 对象&#xff0c;用于进行网络通信。通常使用 socket() 函数创建。 绑定&#xff08;Bind&#xff09;&#xff1a; 服务器将 Socket 绑定到一个特定的 IP 地址和端口号上。这是通过…

51单片机STC89C52RC——16.1 五线四相步进电机

目录 目的/效果 一&#xff0c;STC单片机模块 二&#xff0c;步进电机 2.2 什么是步进电机&#xff1f; 2.2.1 步进电机驱动板 静态参数 动态参数 2.2.2 五线四相 单相激励步进 双相激励步进 混合激励驱动 2.3 细分驱动 2.4 通过数字信号控制旋转位置和转速。 2…

JavaScript-map方法

map可以遍历数组处理数据&#xff0c;并返回新的数组 语法&#xff1a; ​const arr[元素1&#xff0c;元素2&#xff0c;元素3] const newarrarr.map(function(数组的元素,数组的索引)){return 新元素 } const arr[blue,red,green]const newarrarr.map(function(ele,index){co…