coap.h 6.8 KB

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