Link Kit C-SDK  4.0.0
宏定义 | 函数
aiot_coap_api.c 文件参考

COAP模块实现, 其中包含了连接到物联网平台和收发数据的API接口 更多...

#include "coap_private.h"
#include "core_log.h"
#include "core_auth.h"
#include "core_string.h"
#include "core_sha256.h"
#include "aiot_state_api.h"
aiot_coap_api.c 的引用(Include)关系图:

宏定义

#define COAP_SIGN_SRC_FMT   "clientId%sdeviceName%sproductKey%stimestamp%s"
 

函数

static int32_t _coap_message_assemble_header (const coap_message_t *message, uint8_t *buff)
 
static void _coap_token_iteration (uint8_t *token, uint8_t tkl)
 
static int32_t _coap_message_parse (coap_handle_t *coap_handle, coap_message_t *coap_message, const uint8_t *buff, uint32_t len)
 
static int32_t _coap_uri_to_option (const char *uri, coap_option_t *option_array, uint8_t *option_cnt)
 
static int32_t _coap_malloc_message (aiot_sysdep_portfile_t *sysdep, coap_message_t **msg, uint32_t buff_len)
 
static int32_t _coap_create_request_message (void *handle, coap_request_method_t method, aiot_coap_msg_type_t msg_type, coap_message_t **request)
 
static int32_t _coap_create_empty_message (void *handle, aiot_coap_msg_type_t type, uint32_t msgid, coap_message_t **empty)
 
static int32_t _coap_add_option (coap_handle_t *coap_handle, coap_message_t *message, uint16_t opt_num, uint8_t *opt_data, uint32_t opt_len)
 
static int32_t _coap_connect (void *handle)
 
static int32_t _coap_send_message (void *handle, coap_message_t *coap_message, uint8_t *payload, uint32_t payload_len)
 
static int32_t _cal_auth_payload_buff (coap_message_t *coap_message, uint8_t **auth_payload)
 
static int32_t _send_coap_auth_msg (coap_handle_t *coap_handle, coap_message_t *message, uint32_t auth_info_len)
 
static void _coap_release_message (void *handle, coap_message_t *message)
 
static uint32_t _token_str2uint32 (aiot_sysdep_portfile_t *sysdep, uint8_t *token, uint8_t token_len)
 
static int32_t _coap_recv_message_handler (coap_handle_t *coap_handle, coap_message_t *message, coap_request_cache_t *auth_req_cache)
 
static int32_t _coap_recv (void *handle, coap_request_cache_t *auth_req_cache)
 
static int32_t _coap_auth_response_handler (void *handle, const coap_message_t *response, void *user_data)
 
static int32_t is_content_format_supported (aiot_coap_content_format_t format)
 
void * aiot_coap_init (void)
 初始化coap实例并设置默认参数 更多...
 
int32_t aiot_coap_setopt (void *handle, aiot_coap_option_t option, void *data)
 设置coap参数 更多...
 
int32_t aiot_coap_auth (void *handle)
 让coap模块与服务器通信, 并且获取auth token 更多...
 
int32_t aiot_coap_send (void *handle, const char *uri, aiot_coap_request_t *request)
 发送COAP报文 更多...
 
int32_t aiot_coap_recv (void *handle)
 从网络收取COAP报文 更多...
 
int32_t aiot_coap_deinit (void **handle)
 释放COAP实例句柄的资源 更多...
 

详细描述

COAP模块实现, 其中包含了连接到物联网平台和收发数据的API接口

日期
2020-02-28

宏定义说明

#define COAP_SIGN_SRC_FMT   "clientId%sdeviceName%sproductKey%stimestamp%s"

函数说明

static int32_t _coap_message_assemble_header ( const coap_message_t *  message,
uint8_t *  buff 
)
static
static void _coap_token_iteration ( uint8_t *  token,
uint8_t  tkl 
)
static
static int32_t _coap_message_parse ( coap_handle_t *  coap_handle,
coap_message_t *  coap_message,
const uint8_t *  buff,
uint32_t  len 
)
static
static int32_t _coap_uri_to_option ( const char *  uri,
coap_option_t *  option_array,
uint8_t *  option_cnt 
)
static
static int32_t _coap_malloc_message ( aiot_sysdep_portfile_t sysdep,
coap_message_t **  msg,
uint32_t  buff_len 
)
static
static int32_t _coap_create_request_message ( void *  handle,
coap_request_method_t  method,
aiot_coap_msg_type_t  msg_type,
coap_message_t **  request 
)
static

函数调用图:

static int32_t _coap_create_empty_message ( void *  handle,
aiot_coap_msg_type_t  type,
uint32_t  msgid,
coap_message_t **  empty 
)
static

函数调用图:

static int32_t _coap_add_option ( coap_handle_t *  coap_handle,
coap_message_t *  message,
uint16_t  opt_num,
uint8_t *  opt_data,
uint32_t  opt_len 
)
static
static int32_t _coap_connect ( void *  handle)
static
static int32_t _coap_send_message ( void *  handle,
coap_message_t *  coap_message,
uint8_t *  payload,
uint32_t  payload_len 
)
static

函数调用图:

static int32_t _cal_auth_payload_buff ( coap_message_t *  coap_message,
uint8_t **  auth_payload 
)
static
static int32_t _send_coap_auth_msg ( coap_handle_t *  coap_handle,
coap_message_t *  message,
uint32_t  auth_info_len 
)
static

函数调用图:

static void _coap_release_message ( void *  handle,
coap_message_t *  message 
)
static
static uint32_t _token_str2uint32 ( aiot_sysdep_portfile_t sysdep,
uint8_t *  token,
uint8_t  token_len 
)
static
static int32_t _coap_recv_message_handler ( coap_handle_t *  coap_handle,
coap_message_t *  message,
coap_request_cache_t *  auth_req_cache 
)
static

函数调用图:

static int32_t _coap_recv ( void *  handle,
coap_request_cache_t *  auth_req_cache 
)
static

函数调用图:

static int32_t _coap_auth_response_handler ( void *  handle,
const coap_message_t *  response,
void *  user_data 
)
static
static int32_t is_content_format_supported ( aiot_coap_content_format_t  format)
static
void* aiot_coap_init ( void  )

初始化coap实例并设置默认参数

返回
void*
返回值
非NULLCOAP实例句柄
NULL初始化失败, 一般是内存分配失败导致

函数调用图:

int32_t aiot_coap_setopt ( void *  handle,
aiot_coap_option_t  option,
void *  data 
)

设置coap参数

下面列出常用的配置选项, 至少需要配置以下选项才可使用COAP的基本功能

其余配置选项均设有默认值, 可按业务需要进行调整

  • AIOT_COAPOPT_HOST: 配置连接的阿里云COAP站点地址
  • AIOT_COAPOPT_PORT: 配置连接的阿里云COAP站点端口号
  • AIOT_COAPOPT_PRODUCT_KEY: 配置设备的 productKey
  • AIOT_COAPOPT_DEVICE_NAME: 配置设备的 deviceName
  • AIOT_COAPOPT_DEVICE_SECRET: 配置设备的 deviceSecret
  • AIOT_COAPOPT_NETWORK_CRED: 配置建立COAP连接时的安全凭据
  • AIOT_COAPOPT_RECV_HANDLER: 配置默认的数据接收回调函数
  • AIOT_COAPOPT_EVENT_HANDLER: 配置COAP事件通知回调函数
参数
[in]handleCOAP句柄
[in]option配置选项, 更多信息请参考aiot_coap_option_t
[in]data配置选项数据, 更多信息请参考aiot_coap_option_t
返回
int32_t
返回值
<STATE_SUCCESS参数设置失败, 更多信息请本文件后面关于错误码的定义
=STATE_SUCCESS参数设置成功
int32_t aiot_coap_auth ( void *  handle)

让coap模块与服务器通信, 并且获取auth token

参数
[in]handleCOAP句柄
返回
int32_t
返回值
STATE_COAP_AUTH_HANDLE_IS_NULL获取auth token时coap handle为空
STATE_COAP_AUTH_DEVICE_INFO_MISSING获取auth token时设备的三元组为空
STATE_COAP_SIGN_SRC_MALLOC_FAILED获取auth token时为计算签名分配内存失败
STATE_COAP_AUTH_TIMEOUT获取auth token发生超时, 超过了 AIOT_COAPOPT_AUTH_TIMEOUT 所定义的超时时间
<STATE_SUCCESS的其他其他错误主要是网络有关的, 具体见本文件下面的错误码定义
=STATE_SUCCESS获取auth token成功

函数调用图:

int32_t aiot_coap_send ( void *  handle,
const char *  topic,
aiot_coap_request_t message 
)

发送COAP报文

参数
[in]handleCOAP句柄
[in]topic目标topic
[in]message指向要发送报文的内容的指针
返回
int32_t
返回值
STATE_COAP_SEND_HANDLE_IS_NULL发送的报文时, coap_handle为空
STATE_COAP_SEND_TOPIC_IS_NULL发送的报文时, topic为空
STATE_COAP_SEND_REQUEST_IS_NULL发送的报文时, request为空
STATE_COAP_SEND_PAYLOAD_IS_NULL发送的报文时, payload为空
STATE_COAP_SEND_WITHOUT_AUTHORIZATION发送消息时auth_token还没有获取到
STATE_COAP_SEND_INVALID_MSG_TYPE发送消息时, 消息的类型既不是CON, 也不是NON
<STATE_SUCCESS的其他错误主要是网络有关的, 具体见本文件下面的错误码定义
STATE_SUCCESS发送coap报文成功

函数调用图:

int32_t aiot_coap_recv ( void *  handle)

从网络收取COAP报文

参数
[in]handleCOAP句柄
返回
int32_t
返回值
STATE_COAP_RECV_HANDLE_IS_NULL收取网络报文时, coap handle为空
STATE_COAP_RECV_WITHOUT_AUTHORIZATION收取网络报文时, auth token还没有获取到
<STATE_SUCCESS的其他错误主要是网络有关的, 具体见本文件下面的错误码定义
STATE_SUCCESS收取COAP报文成功

函数调用图:

int32_t aiot_coap_deinit ( void **  handle)

释放COAP实例句柄的资源

参数
[in]handle指向mqtt实例句柄的指针
返回
int32_t
返回值
STATE_SUCCESS执行成功