coap.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef COAP_H
  2. #define COAP_H 1
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdint.h>
  7. #include <stddef.h>
  8. #include "lauxlib.h"
  9. #define MAXOPT 16
  10. #define MAX_MESSAGE_SIZE 1152
  11. #define MAX_PAYLOAD_SIZE 1024
  12. #define MAX_REQUEST_SIZE 576
  13. #define MAX_REQ_SCRATCH_SIZE 60
  14. #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
  15. //http://tools.ietf.org/html/rfc7252#section-3
  16. typedef struct
  17. {
  18. uint8_t ver; /* CoAP version number */
  19. uint8_t t; /* CoAP Message Type */
  20. uint8_t tkl; /* Token length: indicates length of the Token field */
  21. uint8_t code; /* CoAP status code. Can be request (0.xx), success reponse (2.xx),
  22. * client error response (4.xx), or rever error response (5.xx)
  23. * For possible values, see http://tools.ietf.org/html/rfc7252#section-12.1 */
  24. uint8_t id[2];
  25. } coap_header_t;
  26. typedef struct
  27. {
  28. const uint8_t *p;
  29. size_t len;
  30. } coap_buffer_t;
  31. typedef struct
  32. {
  33. uint8_t *p;
  34. size_t len;
  35. } coap_rw_buffer_t;
  36. typedef struct
  37. {
  38. uint8_t num; /* Option number. See http://tools.ietf.org/html/rfc7252#section-5.10 */
  39. coap_buffer_t buf; /* Option value */
  40. } coap_option_t;
  41. typedef struct
  42. {
  43. coap_header_t hdr; /* Header of the packet */
  44. coap_buffer_t tok; /* Token value, size as specified by hdr.tkl */
  45. uint8_t numopts; /* Number of options */
  46. coap_option_t opts[MAXOPT]; /* Options of the packet. For possible entries see
  47. * http://tools.ietf.org/html/rfc7252#section-5.10 */
  48. coap_buffer_t payload; /* Payload carried by the packet */
  49. coap_rw_buffer_t content; // content->p = malloc(...) , and free it when done.
  50. } coap_packet_t;
  51. /////////////////////////////////////////
  52. //http://tools.ietf.org/html/rfc7252#section-12.2
  53. typedef enum
  54. {
  55. COAP_OPTION_IF_MATCH = 1,
  56. COAP_OPTION_URI_HOST = 3,
  57. COAP_OPTION_ETAG = 4,
  58. COAP_OPTION_IF_NONE_MATCH = 5,
  59. COAP_OPTION_OBSERVE = 6,
  60. COAP_OPTION_URI_PORT = 7,
  61. COAP_OPTION_LOCATION_PATH = 8,
  62. COAP_OPTION_URI_PATH = 11,
  63. COAP_OPTION_CONTENT_FORMAT = 12,
  64. COAP_OPTION_MAX_AGE = 14,
  65. COAP_OPTION_URI_QUERY = 15,
  66. COAP_OPTION_ACCEPT = 17,
  67. COAP_OPTION_LOCATION_QUERY = 20,
  68. COAP_OPTION_PROXY_URI = 35,
  69. COAP_OPTION_PROXY_SCHEME = 39
  70. } coap_option_num_t;
  71. //http://tools.ietf.org/html/rfc7252#section-12.1.1
  72. typedef enum
  73. {
  74. COAP_METHOD_GET = 1,
  75. COAP_METHOD_POST = 2,
  76. COAP_METHOD_PUT = 3,
  77. COAP_METHOD_DELETE = 4
  78. } coap_method_t;
  79. //http://tools.ietf.org/html/rfc7252#section-12.1.1
  80. typedef enum
  81. {
  82. COAP_TYPE_CON = 0,
  83. COAP_TYPE_NONCON = 1,
  84. COAP_TYPE_ACK = 2,
  85. COAP_TYPE_RESET = 3
  86. } coap_msgtype_t;
  87. //http://tools.ietf.org/html/rfc7252#section-5.2
  88. //http://tools.ietf.org/html/rfc7252#section-12.1.2
  89. #define MAKE_RSPCODE(clas, det) ((clas << 5) | (det))
  90. typedef enum
  91. {
  92. COAP_RSPCODE_CONTENT = MAKE_RSPCODE(2, 5),
  93. COAP_RSPCODE_NOT_FOUND = MAKE_RSPCODE(4, 4),
  94. COAP_RSPCODE_BAD_REQUEST = MAKE_RSPCODE(4, 0),
  95. COAP_RSPCODE_CHANGED = MAKE_RSPCODE(2, 4)
  96. } coap_responsecode_t;
  97. //http://tools.ietf.org/html/rfc7252#section-12.3
  98. typedef enum
  99. {
  100. COAP_CONTENTTYPE_NONE = -1, // bodge to allow us not to send option block
  101. COAP_CONTENTTYPE_TEXT_PLAIN = 0,
  102. COAP_CONTENTTYPE_APPLICATION_LINKFORMAT = 40,
  103. COAP_CONTENTTYPE_APPLICATION_XML = 41,
  104. COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM = 42,
  105. COAP_CONTENTTYPE_APPLICATION_EXI = 47,
  106. COAP_CONTENTTYPE_APPLICATION_JSON = 50,
  107. } coap_content_type_t;
  108. ///////////////////////
  109. typedef enum
  110. {
  111. COAP_ERR_NONE = 0,
  112. COAP_ERR_HEADER_TOO_SHORT = 1,
  113. COAP_ERR_VERSION_NOT_1 = 2,
  114. COAP_ERR_TOKEN_TOO_SHORT = 3,
  115. COAP_ERR_OPTION_TOO_SHORT_FOR_HEADER = 4,
  116. COAP_ERR_OPTION_TOO_SHORT = 5,
  117. COAP_ERR_OPTION_OVERRUNS_PACKET = 6,
  118. COAP_ERR_OPTION_TOO_BIG = 7,
  119. COAP_ERR_OPTION_LEN_INVALID = 8,
  120. COAP_ERR_BUFFER_TOO_SMALL = 9,
  121. COAP_ERR_UNSUPPORTED = 10,
  122. COAP_ERR_OPTION_DELTA_INVALID = 11,
  123. } coap_error_t;
  124. ///////////////////////
  125. typedef struct coap_endpoint_t coap_endpoint_t;
  126. typedef int (*coap_endpoint_func)(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo);
  127. #define MAX_SEGMENTS 3 // 2 = /foo/bar, 3 = /foo/bar/baz
  128. #define MAX_SEGMENTS_SIZE 16
  129. typedef struct
  130. {
  131. int count;
  132. const char *elems[MAX_SEGMENTS];
  133. } coap_endpoint_path_t;
  134. typedef struct coap_luser_entry coap_luser_entry;
  135. struct coap_luser_entry{
  136. // int ref;
  137. // char name[MAX_SEGMENTS_SIZE+1]; // +1 for string '\0'
  138. const char *name;
  139. coap_luser_entry *next;
  140. int content_type;
  141. };
  142. struct coap_endpoint_t{
  143. coap_method_t method; /* (i.e. POST, PUT or GET) */
  144. coap_endpoint_func handler; /* callback function which handles this
  145. * type of endpoint (and calls
  146. * coap_make_response() at some point) */
  147. const coap_endpoint_path_t *path; /* path towards a resource (i.e. foo/bar/) */
  148. const char *core_attr; /* the 'ct' attribute, as defined in RFC7252, section 7.2.1.:
  149. * "The Content-Format code "ct" attribute
  150. * provides a hint about the
  151. * Content-Formats this resource returns."
  152. * (Section 12.3. lists possible ct values.) */
  153. coap_luser_entry *user_entry;
  154. };
  155. ///////////////////////
  156. void coap_dumpPacket(coap_packet_t *pkt);
  157. int coap_parse(coap_packet_t *pkt, const uint8_t *buf, size_t buflen);
  158. int coap_buffer_to_string(char *strbuf, size_t strbuflen, const coap_buffer_t *buf);
  159. const coap_option_t *coap_findOptions(const coap_packet_t *pkt, uint8_t num, uint8_t *count);
  160. int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt);
  161. void coap_dump(const uint8_t *buf, size_t buflen, bool bare);
  162. 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);
  163. int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt);
  164. void coap_option_nibble(uint32_t value, uint8_t *nibble);
  165. void coap_setup(void);
  166. void endpoint_setup(void);
  167. int coap_buildOptionHeader(uint32_t optDelta, size_t length, uint8_t *buf, size_t buflen);
  168. int check_token(coap_packet_t *pkt);
  169. #include "uri.h"
  170. int coap_make_request(coap_rw_buffer_t *scratch, coap_packet_t *pkt, coap_msgtype_t t, coap_method_t m, coap_uri_t *uri, const uint8_t *payload, size_t payload_len);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif