Демонстрационная прошивка отладочного комплекта МТС NB-IoT
coap_essentials.h
См. документацию.
1 #ifndef COAP_ESSENTIALS_H_INCLUDED
2 #define COAP_ESSENTIALS_H_INCLUDED
3 
20 #include "stdint.h"
21 #include "string.h"
22 
23 #ifndef NULL
24 #define NULL ((void *)0)
25 #endif
26 
27 /*
28  CoAP-specific constants
29 */
36 #define COAP_MSG_VERSION 0x01
37 #define COAP_TYPE_CONFIRMABLE 0x00
38 #define COAP_TYPE_NON_CONFIRMABLE 0x01
39 #define COAP_TYPE_ACK 0x02
40 #define COAP_TYPE_RESET 0x03
41 #define COAP_CLASS_REQUEST 0
42 #define COAP_CLASS_RESP_SUCCESS 2
43 #define COAP_CLASS_CLIENT_ERROR 4
44 #define COAP_CLASS_SERVER_ERROR 5
45 
55 #define COAP_METHOD_GET 1
56 #define COAP_METHOD_POST 2
57 #define COAP_METHOD_PUT 3
58 #define COAP_METHOD_DELETE 4
59 
69 #define COAP_OPTION_URI_PATH 11
70 #define COAP_OPTION_CONTENT_FORMAT 12
71 //The header is followed by one extra byte of option delta
72 #define COAP_OPTION_DELTA_EXTRA1B 13
73 //The header is followed by two extra bytes of option delta
74 #define COAP_OPTION_DELTA_EXTRA2B 14
75 //This value can only be encountered in a data marker, 0xFF
76 #define COAP_OPTION_DELTA_DATA_MKR 15
77 //The data are preceded by one extra byte of data length
78 #define COAP_OPTION_LENGTH_EXTRA1B 13
79 //The data are preceded by two extra bytes of data length
80 #define COAP_OPTION_LENGTH_EXTRA2B 14
81 //This value can only be encountered in a data marker, 0xFF
82 #define COAP_OPTION_LENGTH_DATA_MKR 15
83 //Converts a normal GET request into an OBSERVE request
84 #define COAP_OPTION_OBSERVE 6
85 
86 #define COAP_MSG_HDR_LENGTH 4
87 
92 //The parser will give up searching for options after this limit
94 #define MAX_PARSED_OPTIONS 10
95 //Maximum number of URI entries allowed for CoAP_assemble_request()
97 #define MAX_URI_ENTRIES 10
98 //Maximum URI string length
100 #define MAX_URI_LENGTH 128
101 
102 typedef struct {
103 
105  uint8_t version;
107  uint8_t type;
109  uint8_t token_length;
111  uint8_t code_class;
113  uint8_t code_detail;
115  uint16_t message_ID;
116 
118 
119 typedef struct {
120 
121  //The code of a current option
122  uint8_t option_id;
123  //The length of actual data of the option
124  uint8_t data_length;
125  //Pointer to a position in a buffer where this option's data start
126  uint8_t *data_ptr;
127 
129 
130 typedef enum {
140  ERR_TOO_MUCH_OPTIONS, /* There's more options in a packet that is allowed by MAX_PARSED_OPTIONS */
142  ERR_NONE /* No error, everything is OK */
144 
151 void CoAP_parse_message_header(uint8_t *hdr,CoAP_header_info_t *out);
152 
178 CoAP_parsing_error_t CoAP_parse_message(uint8_t *buf,uint16_t buf_length,uint8_t **data_start);
179 
196 uint16_t CoAP_assemble_request_extended(uint8_t request_method,uint8_t is_observe_request,uint8_t *msg_buffer,uint16_t buffer_length,uint8_t *url_string,uint8_t *data,uint16_t data_length);
197 
202 #define CoAP_assemble_request(request_method,buffer,buffer_length,url_string,data,data_length) CoAP_assemble_request_extended(request_method,0,buffer,buffer_length,url_string,data,data_length)
203 
209 #endif // COAP_ESSENTIALS_H_INCLUDED
CoAP_header_info_t
Definition: coap_essentials.h:102
CoAP_parsing_error_t
CoAP_parsing_error_t
Definition: coap_essentials.h:130
ERR_WRONG_PROTOCOL_VER
Definition: coap_essentials.h:132
ERR_WRONG_OPTION
Definition: coap_essentials.h:138
CoAP_header_info_t::code_class
uint8_t code_class
Definition: coap_essentials.h:111
ERR_TOO_MUCH_OPTIONS
Definition: coap_essentials.h:140
ERR_UNKNOWN_MSG_TYPE
Definition: coap_essentials.h:134
ERR_WRONG_TKL
Definition: coap_essentials.h:136
CoAP_parse_message
CoAP_parsing_error_t CoAP_parse_message(uint8_t *buf, uint16_t buf_length, uint8_t **data_start)
Выполняет поиск данных в пакете CoAP.
Definition: coap_essentials.c:99
CoAP_header_info_t::message_ID
uint16_t message_ID
Definition: coap_essentials.h:115
ERR_NONE
Definition: coap_essentials.h:142
CoAP_option_info_t
Definition: coap_essentials.h:119
CoAP_assemble_request_extended
uint16_t CoAP_assemble_request_extended(uint8_t request_method, uint8_t is_observe_request, uint8_t *msg_buffer, uint16_t buffer_length, uint8_t *url_string, uint8_t *data, uint16_t data_length)
Формирует CoAP-сообщение с запросом к серверу
Definition: coap_essentials.c:174
CoAP_header_info_t::version
uint8_t version
Definition: coap_essentials.h:105
CoAP_header_info_t::token_length
uint8_t token_length
Definition: coap_essentials.h:109
CoAP_parse_message_header
void CoAP_parse_message_header(uint8_t *hdr, CoAP_header_info_t *out)
Выполняет разбор заголовка CoAP-сообщения с извлечением информации в структуру типа CoAP_header_info_...
Definition: coap_essentials.c:12
CoAP_header_info_t::type
uint8_t type
Definition: coap_essentials.h:107
CoAP_header_info_t::code_detail
uint8_t code_detail
Definition: coap_essentials.h:113