coap.h 5.8 KB

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