1 /* 2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited 3 */ 4 5 #ifndef __IOTX_COAP_API_H__ 6 #define __IOTX_COAP_API_H__ 7 8 #if defined(__cplusplus) 9 extern "C" { 10 #endif 11 12 #include "linkkit/infra/infra_defs.h" 13 14 /*iotx return code definition*/ 15 typedef enum { 16 IOTX_ERR_RECV_MSG_TIMEOUT = -9, /*Receive message timeout */ 17 IOTX_ERR_SEND_MSG_FAILED = -8, /* Send message failed*/ 18 IOTX_ERR_MSG_TOO_LOOG = -7, /* The payload too loog */ 19 IOTX_ERR_URI_TOO_LOOG = -6, /* URI length too long */ 20 IOTX_ERR_NOT_AUTHED = -5, /* Client isn't authed */ 21 IOTX_ERR_AUTH_FAILED = -4, /* Client authed failed */ 22 IOTX_ERR_BUFF_TOO_SHORT = -3, /* Buffer too short */ 23 IOTX_ERR_NO_MEM = -2, /* Malloc failed */ 24 IOTX_ERR_INVALID_PARAM = -1, /* Invalid parameter */ 25 IOTX_SUCCESS = 0, /* Success */ 26 } iotx_ret_code_t; 27 28 /* The message payload encode format */ 29 typedef enum { 30 IOTX_CONTENT_TYPE_JSON, 31 IOTX_CONTENT_TYPE_CBOR, 32 } iotx_content_type_t; 33 34 /* The message type */ 35 typedef enum { 36 IOTX_MESSAGE_CON = 0, /* confirmable message */ 37 IOTX_MESSAGE_NON = 1, /* non-confirmable message */ 38 IOTX_MESSAGE_ACK = 2, /* acknowledgement message */ 39 IOTX_MESSAGE_RST = 3, /* reset message */ 40 } iotx_msg_type_t; 41 42 /* IoTx events to notify application */ 43 typedef enum { 44 IOTX_COAP_EVENT_SEND_FAILED = 0, 45 IOTX_COAP_EVENT_RECV_FAILED = 1, 46 IOTX_COAP_EVENT_AUTH_FAILED = 2, 47 } iotx_coap_event_t; 48 49 typedef enum { 50 IOTX_COAP_RESP_CODE_CONTENT = 0x45, /* Mapping to 2.05, Content*/ 51 IOTX_COAP_RESP_CODE_BAD_REQUEST = 0x80, /* Mapping to 4.00, Bad Request*/ 52 IOTX_COAP_RESP_CODE_UNAUTHORIZED = 53 0x81, /* Mapping to 4.01, Token is invalid or expire*/ 54 IOTX_COAP_RESP_CODE_NOT_FOUND = 55 0x84, /* Mapping to 4.04, Path or uri is not found*/ 56 IOTX_COAP_RESP_CODE_URL_TOO_LONG = 57 0x8E, /* Mapping to 4.14, The request url is too long*/ 58 IOTX_COAP_RESP_CODE_INTERNAL_SERVER_ERROR = 59 0xA0, /* Mapping to 5.00, Internal server error*/ 60 61 } iotx_coap_resp_code_t; 62 63 /* Callback function to notify the application events.*/ 64 typedef void (*iotx_event_handle_t)(void *context, iotx_coap_event_t event, 65 void *p_data); 66 67 typedef struct { 68 char product_key[IOTX_PRODUCT_KEY_LEN + 1]; 69 char device_name[IOTX_DEVICE_NAME_LEN + 1]; 70 char device_id[IOTX_DEVICE_ID_LEN + 1]; 71 char device_secret[IOTX_DEVICE_SECRET_LEN + 1]; 72 } iotx_coap_device_info_t; 73 74 /* IoTx initializa parameters */ 75 typedef struct { 76 char *p_url; /*Can be NULL*/ 77 int wait_time_ms; /*unit is micro second*/ 78 iotx_coap_device_info_t *p_devinfo; /*Device info*/ 79 iotx_event_handle_t event_handle; /*TODO, not supported now*/ 80 } iotx_coap_config_t; 81 82 /* Callback function to handle the response message.*/ 83 typedef void (*iotx_response_callback_t)(void *p_arg, void *p_message); 84 85 /* IoTx message definition */ 86 typedef struct { 87 unsigned char *p_payload; 88 unsigned short payload_len; 89 iotx_content_type_t content_type; 90 iotx_msg_type_t msg_type; 91 void *user_data; 92 iotx_response_callback_t resp_callback; 93 } iotx_message_t; 94 95 /*iotx coap context definition*/ 96 typedef void iotx_coap_context_t; 97 98 /** @defgroup group_api api 99 * @{ 100 */ 101 102 /** @defgroup group_api_coap coap 103 * @{ 104 */ 105 106 /** 107 * @brief Initialize the CoAP client. 108 * This function initialize the data structures and network, 109 * and create the DTLS session. 110 * 111 * @param [in] p_config: Specify the CoAP client parameter. 112 * 113 * @retval NULL : Initialize failed. 114 * @retval NOT_NULL : The contex of CoAP client. 115 * @see None. 116 */ 117 iotx_coap_context_t *IOT_CoAP_Init(iotx_coap_config_t *p_config); 118 119 /** 120 * @brief De-initialize the CoAP client. 121 * This function release CoAP DTLS session. 122 * and release the related resource. 123 * 124 * @param [in] p_context: Pointer of contex, specify the CoAP client. 125 * 126 * @return None. 127 * @see None. 128 */ 129 void IOT_CoAP_Deinit(iotx_coap_context_t **p_context); 130 131 /** 132 * @brief Handle device name authentication with remote server. 133 * 134 * @param [in] p_context: Pointer of contex, specify the CoAP client. 135 * 136 * @retval IOTX_SUCCESS : Authenticate success. 137 * @retval IOTX_ERR_SEND_MSG_FAILED : Send authentication message failed. 138 * @retval IOTX_ERR_AUTH_FAILED : Authenticate failed or timeout. 139 * @see iotx_ret_code_t. 140 */ 141 int IOT_CoAP_DeviceNameAuth(iotx_coap_context_t *p_context); 142 143 /** 144 * @brief Handle CoAP response packet from remote server, 145 * and process timeout request etc.. 146 * 147 * @param [in] p_context : Pointer of contex, specify the CoAP client. 148 * 149 * @return status. 150 * @see iotx_ret_code_t. 151 */ 152 int IOT_CoAP_Yield(iotx_coap_context_t *p_context); 153 154 /** 155 * @brief Send a message with specific path to server. 156 * Client must authentication with server before send message. 157 * 158 * @param [in] p_context : Pointer of contex, specify the CoAP client. 159 * @param [in] p_path: Specify the path name. 160 * @param [in] p_message: Message to be sent. 161 * 162 * @retval IOTX_SUCCESS : Send the message success. 163 * @retval IOTX_ERR_MSG_TOO_LOOG : The message length is too long. 164 * @retval IOTX_ERR_NOT_AUTHED : The client hasn't authenticated with 165 * server 166 * @see iotx_ret_code_t. 167 */ 168 int IOT_CoAP_SendMessage(iotx_coap_context_t *p_context, char *p_path, 169 iotx_message_t *p_message); 170 171 /** 172 * @brief Retrieves the length and payload pointer of specified message. 173 * 174 * @param [in] p_message: Pointer to the message to get the payload. Should not 175 *be NULL. 176 * @param [out] pp_payload: Pointer to the payload. 177 * @param [out] p_len: Size of the payload. 178 * 179 * @retval IOTX_SUCCESS : Get the payload success. 180 * @retval IOTX_ERR_INVALID_PARAM : Can't get the payload due to invalid 181 *parameter. 182 * @see iotx_ret_code_t. 183 **/ 184 int IOT_CoAP_GetMessagePayload(void *p_message, unsigned char **pp_payload, 185 int *p_len); 186 187 /** 188 * @brief Get the response code from a CoAP message. 189 * 190 * @param [in] p_message: Pointer to the message to add the address information 191 *to. Should not be NULL. 192 * @param [out] p_resp_code: The response code. 193 * 194 * @retval IOTX_SUCCESS : When get the response code to message 195 *success. 196 * @retval IOTX_ERR_INVALID_PARAM : Pointer to the message is NULL. 197 * @see iotx_ret_code_t. 198 **/ 199 int IOT_CoAP_GetMessageCode(void *p_message, 200 iotx_coap_resp_code_t *p_resp_code); 201 202 /** @} */ /* end of api_coap */ 203 /** @} */ /* end of api */ 204 205 #if defined(__cplusplus) 206 } 207 #endif 208 #endif 209