lightduer_coap_defs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (2017) Baidu Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // Author: Su Hao (suhao@baidu.com)
  17. //
  18. // Description: CoAP common definitions
  19. #ifndef LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_COAP_DEFS_H
  20. #define LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_COAP_DEFS_H
  21. #include "lightduer_types.h"
  22. #include "lightduer_network_defs.h"
  23. #include "lightduer_net_transport.h"
  24. /*
  25. * The message codes.
  26. */
  27. typedef enum {
  28. DUER_MSG_EMPTY_MESSAGE = 0,
  29. // Request message code
  30. DUER_MSG_REQ_GET = 1,
  31. DUER_MSG_REQ_POST = 2,
  32. DUER_MSG_REQ_PUT = 3,
  33. DUER_MSG_REQ_DELETE = 4,
  34. // Response message code
  35. DUER_MSG_RSP_CREATED = 65, // 2.01
  36. DUER_MSG_RSP_DELETED = 66, // 2.02
  37. DUER_MSG_RSP_VALID = 67, // 2.03
  38. DUER_MSG_RSP_CHANGED = 68, // 2.04
  39. DUER_MSG_RSP_CONTENT = 69, // 2.05
  40. DUER_MSG_RSP_CONTINUE = 95, // 2.31
  41. DUER_MSG_RSP_BAD_REQUEST = 128, // 4.00
  42. DUER_MSG_RSP_UNAUTHORIZED = 129, // 4.01
  43. DUER_MSG_RSP_BAD_OPTION = 130, // 4.02
  44. DUER_MSG_RSP_FORBIDDEN = 131, // 4.03
  45. DUER_MSG_RSP_NOT_FOUND = 132, // 4.04
  46. DUER_MSG_RSP_METHOD_NOT_ALLOWED = 133, // 4.05
  47. DUER_MSG_RSP_NOT_ACCEPTABLE = 134, // 4.06
  48. DUER_MSG_RSP_REQUEST_ENTITY_INCOMPLETE = 136, // 4.08
  49. DUER_MSG_RSP_PRECONDITION_FAILED = 140, // 4.12
  50. DUER_MSG_RSP_REQUEST_ENTITY_TOO_LARGE = 141, // 4.13
  51. DUER_MSG_RSP_UNSUPPORTED_CONTENT_FORMAT = 143, // 4.15
  52. DUER_MSG_RSP_INTERNAL_SERVER_ERROR = 160, // 5.00
  53. DUER_MSG_RSP_NOT_IMPLEMENTED = 161, // 5.01
  54. DUER_MSG_RSP_BAD_GATEWAY = 162, // 5.02
  55. DUER_MSG_RSP_SERVICE_UNAVAILABLE = 163, // 5.03
  56. DUER_MSG_RSP_GATEWAY_TIMEOUT = 164, // 5.04
  57. DUER_MSG_RSP_PROXYING_NOT_SUPPORTED = 165, // 5.05
  58. DUER_MSG_RSP_INVALID = 0xFF, // used in seperate response
  59. } duer_msg_code_e;
  60. /**
  61. * CoAP Message type, used in CoAP Header
  62. */
  63. typedef enum {
  64. DUER_MSG_TYPE_CONFIRMABLE = 0x00, // Reliable Request messages
  65. DUER_MSG_TYPE_NON_CONFIRMABLE = 0x10, // Non-reliable Request and Response messages
  66. DUER_MSG_TYPE_ACKNOWLEDGEMENT = 0x20, // Response to a Confirmable Request
  67. DUER_MSG_TYPE_RESET = 0x30 // Answer a Bad Request
  68. } duer_msg_type_e;
  69. /*
  70. * The resource operation permission
  71. */
  72. typedef enum {
  73. DUER_RES_OP_GET = 0x01, // Get operation allowed
  74. DUER_RES_OP_PUT = 0x02, // Put operation allowed
  75. DUER_RES_OP_POST = 0x04, // Post operation allowed
  76. DUER_RES_OP_DELETE = 0x08 // Delete operation allowed
  77. } duer_resource_operation_e;
  78. /*
  79. * The resource mode
  80. */
  81. typedef enum {
  82. DUER_RES_MODE_STATIC, // Static resources have some value that doesn't change
  83. DUER_RES_MODE_DYNAMIC, // Dynamic resources are handled in application
  84. } duer_resource_mode_e;
  85. typedef struct _duer_context_s duer_context_t;
  86. typedef int (*duer_report_callback)(duer_context_t *);
  87. struct _duer_context_s {
  88. void * _param;
  89. int _status;
  90. duer_report_callback _on_report_start;
  91. duer_report_callback _on_report_finish;
  92. };
  93. /*
  94. * The message definition
  95. */
  96. typedef struct _duer_message_s {
  97. duer_u16_t token_len;
  98. duer_u8_t msg_type;
  99. duer_u8_t msg_code;
  100. duer_u16_t msg_id;
  101. duer_u16_t path_len;
  102. duer_u16_t query_len;
  103. duer_u16_t payload_len;
  104. duer_u8_t* token;
  105. duer_u8_t* path;
  106. duer_u8_t* query;
  107. duer_u8_t* payload;
  108. duer_u32_t create_timestamp;
  109. duer_context_t *context;
  110. } duer_msg_t;
  111. /*
  112. * The status notification to user.
  113. */
  114. typedef duer_status_t (*duer_notify_f)(duer_context ctx,
  115. duer_msg_t* msg,
  116. duer_addr_t* addr);
  117. /*
  118. * The transmit coap data callback.
  119. */
  120. typedef duer_status_t (*duer_transmit_f)(duer_trans_handler hdlr, const void *data, duer_size_t size, duer_addr_t *addr);
  121. /*
  122. * The resource for user
  123. */
  124. typedef struct _duer_resource_s {
  125. duer_u8_t mode: 2; // the resource mode, SEE in ${link duer_resource_mode_e}
  126. duer_u8_t allowed: 6; // operation permission, SEE in ${link duer_resource_operation_e}
  127. char* path; // the resource path identify
  128. union {
  129. duer_notify_f f_res; // dynamic resource handle function, NULL if static
  130. struct {
  131. void* data; // static resource value data, NULL if dynamic
  132. duer_size_t size; // static resource size
  133. } s_res;
  134. } res;
  135. } duer_res_t;
  136. #define DUER_MESSAGE_IS_RESPONSE(_code) ((_code) > DUER_MSG_REQ_DELETE)
  137. #endif // LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_COAP_DEFS_H