Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface Socket mbed-rtos mbed
coap.h
00001 #ifndef COAP_H 00002 #define COAP_H 1 00003 00004 #ifdef __cplusplus 00005 extern "C" { 00006 #endif 00007 00008 #include <stdint.h> 00009 #include <stdbool.h> 00010 #include <stddef.h> 00011 00012 #define MAXOPT 16 00013 00014 //http://tools.ietf.org/html/rfc7252#section-3 00015 typedef struct 00016 { 00017 uint8_t ver; /* CoAP version number */ 00018 uint8_t t; /* CoAP Message Type */ 00019 uint8_t tkl; /* Token length: indicates length of the Token field */ 00020 uint8_t code; /* CoAP status code. Can be request (0.xx), success reponse (2.xx), 00021 * client error response (4.xx), or rever error response (5.xx) 00022 * For possible values, see http://tools.ietf.org/html/rfc7252#section-12.1 */ 00023 uint8_t id[2]; 00024 } coap_header_t; 00025 00026 typedef struct 00027 { 00028 const uint8_t *p; 00029 size_t len; 00030 } coap_buffer_t; 00031 00032 typedef struct 00033 { 00034 uint8_t *p; 00035 size_t len; 00036 } coap_rw_buffer_t; 00037 00038 typedef struct 00039 { 00040 uint8_t num; /* Option number. See http://tools.ietf.org/html/rfc7252#section-5.10 */ 00041 coap_buffer_t buf; /* Option value */ 00042 } coap_option_t; 00043 00044 typedef struct 00045 { 00046 coap_header_t hdr; /* Header of the packet */ 00047 coap_buffer_t tok; /* Token value, size as specified by hdr.tkl */ 00048 uint8_t numopts; /* Number of options */ 00049 coap_option_t opts[MAXOPT]; /* Options of the packet. For possible entries see 00050 * http://tools.ietf.org/html/rfc7252#section-5.10 */ 00051 coap_buffer_t payload; /* Payload carried by the packet */ 00052 } coap_packet_t; 00053 00054 ///////////////////////////////////////// 00055 00056 //http://tools.ietf.org/html/rfc7252#section-12.2 00057 typedef enum 00058 { 00059 COAP_OPTION_IF_MATCH = 1, 00060 COAP_OPTION_URI_HOST = 3, 00061 COAP_OPTION_ETAG = 4, 00062 COAP_OPTION_IF_NONE_MATCH = 5, 00063 COAP_OPTION_OBSERVE = 6, 00064 COAP_OPTION_URI_PORT = 7, 00065 COAP_OPTION_LOCATION_PATH = 8, 00066 COAP_OPTION_URI_PATH = 11, 00067 COAP_OPTION_CONTENT_FORMAT = 12, 00068 COAP_OPTION_MAX_AGE = 14, 00069 COAP_OPTION_URI_QUERY = 15, 00070 COAP_OPTION_ACCEPT = 17, 00071 COAP_OPTION_LOCATION_QUERY = 20, 00072 COAP_OPTION_PROXY_URI = 35, 00073 COAP_OPTION_PROXY_SCHEME = 39 00074 } coap_option_num_t; 00075 00076 //http://tools.ietf.org/html/rfc7252#section-12.1.1 00077 typedef enum 00078 { 00079 COAP_METHOD_GET = 1, 00080 COAP_METHOD_POST = 2, 00081 COAP_METHOD_PUT = 3, 00082 COAP_METHOD_DELETE = 4 00083 } coap_method_t; 00084 00085 //http://tools.ietf.org/html/rfc7252#section-12.1.1 00086 typedef enum 00087 { 00088 COAP_TYPE_CON = 0, 00089 COAP_TYPE_NONCON = 1, 00090 COAP_TYPE_ACK = 2, 00091 COAP_TYPE_RESET = 3 00092 } coap_msgtype_t; 00093 00094 //http://tools.ietf.org/html/rfc7252#section-5.2 00095 //http://tools.ietf.org/html/rfc7252#section-12.1.2 00096 #define MAKE_RSPCODE(clas, det) ((clas << 5) | (det)) 00097 typedef enum 00098 { 00099 COAP_RSPCODE_CONTENT = MAKE_RSPCODE(2, 5), 00100 COAP_RSPCODE_NOT_FOUND = MAKE_RSPCODE(4, 4), 00101 COAP_RSPCODE_BAD_REQUEST = MAKE_RSPCODE(4, 0), 00102 COAP_RSPCODE_CHANGED = MAKE_RSPCODE(2, 4) 00103 } coap_responsecode_t; 00104 00105 //http://tools.ietf.org/html/rfc7252#section-12.3 00106 typedef enum 00107 { 00108 COAP_CONTENTTYPE_NONE = -1, // bodge to allow us not to send option block 00109 COAP_CONTENTTYPE_TEXT_PLAIN = 0, 00110 COAP_CONTENTTYPE_APPLICATION_LINKFORMAT = 40, 00111 COAP_CONTENTTYPE_APPLICATION_XML = 41, 00112 COAP_CONTENTTYPE_APPLICATION_OCTECT_STREAM = 42, 00113 COAP_CONTENTTYPE_APPLICATION_EXI = 47, 00114 COAP_CONTENTTYPE_APPLICATION_JSON = 50, 00115 } coap_content_type_t; 00116 00117 /////////////////////// 00118 00119 typedef enum 00120 { 00121 COAP_ERR_NONE = 0, 00122 COAP_ERR_HEADER_TOO_SHORT = 1, 00123 COAP_ERR_VERSION_NOT_1 = 2, 00124 COAP_ERR_TOKEN_TOO_SHORT = 3, 00125 COAP_ERR_OPTION_TOO_SHORT_FOR_HEADER = 4, 00126 COAP_ERR_OPTION_TOO_SHORT = 5, 00127 COAP_ERR_OPTION_OVERRUNS_PACKET = 6, 00128 COAP_ERR_OPTION_TOO_BIG = 7, 00129 COAP_ERR_OPTION_LEN_INVALID = 8, 00130 COAP_ERR_BUFFER_TOO_SMALL = 9, 00131 COAP_ERR_UNSUPPORTED = 10, 00132 COAP_ERR_OPTION_DELTA_INVALID = 11, 00133 } coap_error_t; 00134 00135 /////////////////////// 00136 00137 typedef int (*coap_endpoint_func)(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo); 00138 #define MAX_SEGMENTS 2 // 2 = /foo/bar, 3 = /foo/bar/baz 00139 typedef struct 00140 { 00141 int count; 00142 const char *elems[MAX_SEGMENTS]; 00143 } coap_endpoint_path_t; 00144 00145 typedef struct 00146 { 00147 coap_method_t method; /* (i.e. POST, PUT or GET) */ 00148 coap_endpoint_func handler; /* callback function which handles this 00149 * type of endpoint (and calls 00150 * coap_make_response() at some point) */ 00151 const coap_endpoint_path_t *path; /* path towards a resource (i.e. foo/bar/) */ 00152 const char *core_attr; /* the 'ct' attribute, as defined in RFC7252, section 7.2.1.: 00153 * "The Content-Format code "ct" attribute 00154 * provides a hint about the 00155 * Content-Formats this resource returns." 00156 * (Section 12.3. lists possible ct values.) */ 00157 } coap_endpoint_t; 00158 00159 00160 /////////////////////// 00161 void coap_dumpPacket(coap_packet_t *pkt); 00162 int coap_parse(coap_packet_t *pkt, const uint8_t *buf, size_t buflen); 00163 int coap_buffer_to_string(char *strbuf, size_t strbuflen, const coap_buffer_t *buf); 00164 const coap_option_t *coap_findOptions(const coap_packet_t *pkt, uint8_t num, uint8_t *count); 00165 int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt); 00166 void coap_dump(const uint8_t *buf, size_t buflen, bool bare); 00167 int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, const coap_buffer_t* tok, coap_responsecode_t rspcode, coap_content_type_t content_type); 00168 int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt); 00169 void coap_option_nibble(uint32_t value, uint8_t *nibble); 00170 void coap_setup(void); 00171 void endpoint_setup(void); 00172 00173 #ifdef __cplusplus 00174 } 00175 #endif 00176 00177 #endif 00178 00179 extern const coap_endpoint_t endpoints[];
Generated on Thu Jul 14 2022 07:13:16 by
1.7.2