libcoap3对接华为云平台


前言

通过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)运行
在这里插入图片描述
在这里插入图片描述

总结

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

相关推荐

  1. 对象存储】SpringBoot集成华为OBS对象存储

    2024-07-12 16:16:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 16:16:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 16:16:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 16:16:03       57 阅读
  4. Python语言-面向对象

    2024-07-12 16:16:03       68 阅读

热门阅读

  1. spark 中hint使用总结

    2024-07-12 16:16:03       22 阅读
  2. 安卓文件上传照片单张及多张照片上传实现

    2024-07-12 16:16:03       17 阅读
  3. 编译Linux内核, 制作迷你系统并在虚拟机里运行

    2024-07-12 16:16:03       21 阅读
  4. 力扣1209.删除字符串中的所有相邻重复项 II

    2024-07-12 16:16:03       20 阅读
  5. Python使用总结之jieba形容词提取详解

    2024-07-12 16:16:03       22 阅读
  6. postman接口测试工具详解

    2024-07-12 16:16:03       22 阅读
  7. 迅为RK3568手册上新 | RK3568开发板NPU例程测试

    2024-07-12 16:16:03       21 阅读
  8. Yolo的离线运行

    2024-07-12 16:16:03       24 阅读
  9. 2024.07.04校招 实习 内推 面经

    2024-07-12 16:16:03       20 阅读
  10. 从零开始学习嵌入式----C语言指针函数

    2024-07-12 16:16:03       18 阅读