lightduer_coap.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: The CoAP adapter.
  19. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_H
  20. #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_H
  21. #include "lightduer_types.h"
  22. #include "lightduer_lib.h"
  23. #include "lightduer_network_defs.h"
  24. #include "lightduer_coap_defs.h"
  25. #include "lightduer_coap_ep.h"
  26. #define DUER_COAP_MESSAGE_ID_INVALID (0)
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. typedef void* duer_coap_handler;
  31. typedef duer_status_t (*duer_coap_result_f)(duer_context ctx,
  32. duer_coap_handler hdlr,
  33. const duer_msg_t* msg,
  34. const duer_addr_t* addr);
  35. /*
  36. * Acquire the coap handler.
  37. *
  38. * @Param f_result, in, the callback for user handling events
  39. * @Param context, in, the user context for f_result callback
  40. * @Param ctx_context, in, the user socket context
  41. * @Param key_info, something generated on up layer, but used on down layer
  42. * @Return duer_coap_handler, the coap context
  43. */
  44. DUER_INT duer_coap_handler duer_coap_acquire(duer_coap_result_f f_result,
  45. duer_context context,
  46. duer_transevt_func ctx_context,
  47. const void *key_info);
  48. /*
  49. * Set tx function callback
  50. *
  51. * @Param hdlr, in, the CoAP context
  52. * @Param tx, in, the tx function callback
  53. * @Return duer_status_t, the connect status
  54. */
  55. DUER_INT duer_status_t duer_coap_set_tx_callback(duer_coap_handler hdlr, duer_transmit_f tx);
  56. /*
  57. * Connect to remote CoAP server
  58. *
  59. * @Param hdlr, in, the CoAP context
  60. * @Param addr, in, the remote server address
  61. * @Param data, in, the security suite data
  62. * @Param size, in, the data size
  63. * @Return duer_status_t, the connect status
  64. */
  65. DUER_INT duer_status_t duer_coap_connect(duer_coap_handler hdlr,
  66. const duer_addr_t* addr,
  67. const void* data,
  68. duer_size_t size);
  69. /*
  70. * Disconnect from remote CoAP server
  71. *
  72. * @Param hdlr, in, the CoAP context
  73. * @Return duer_status_t, the connect status
  74. */
  75. DUER_INT duer_status_t duer_coap_disconnect(duer_coap_handler hdlr);
  76. /*
  77. * Add the resource for CoAP request from other endpoints
  78. *
  79. * @Param hdlr, in, the CoAP context
  80. * @Param res, in, the resource information
  81. * @Return duer_status_t, the result
  82. */
  83. DUER_INT duer_status_t duer_coap_resource_add(duer_coap_handler hdlr,
  84. const duer_res_t* res);
  85. /*
  86. * Remove the resource for CoAP request from other endpoints
  87. *
  88. * @Param hdlr, in, the CoAP context
  89. * @Param path, in, the resource path
  90. * @Return duer_status_t, the result
  91. */
  92. DUER_INT duer_status_t duer_coap_resource_remove(duer_coap_handler coap,
  93. const char* path);
  94. /*
  95. * Register endpoint by LWM2M
  96. *
  97. * @Param hdlr, in, the CoAP context
  98. * @Param ep, in, the endpoint information
  99. * @Return duer_status_t, if > 0, the result is the register message id, else is the error status
  100. */
  101. DUER_INT duer_status_t duer_coap_register(duer_coap_handler coap,
  102. const duer_coap_ep_t* ep);
  103. /*
  104. * Unregister the endpoint by LWM2M
  105. *
  106. * @Param hdlr, in, the CoAP context
  107. * @Return duer_status_t, if > 0, the result is the unregister message id, else is the error status
  108. */
  109. DUER_INT duer_status_t duer_coap_unregister(duer_coap_handler coap);
  110. /*
  111. * Update registration by LWM2M
  112. *
  113. * @Param hdlr, in, the CoAP context
  114. * @Param lifetime, in, the endpoint lifetime
  115. * @Return duer_status_t, if > 0, the result is the update registration message id,
  116. * else is the error status
  117. */
  118. DUER_INT duer_status_t duer_coap_update_registration(duer_coap_handler coap,
  119. duer_u32_t lifetime);
  120. /*
  121. * Send the CoAP message
  122. *
  123. * @Param hdlr, in, the CoAP context
  124. * @Param msg, in, the CoAP message
  125. * @Return duer_status_t, the result
  126. */
  127. DUER_INT duer_status_t duer_coap_send(duer_coap_handler coap, const duer_msg_t* msg);
  128. /*
  129. * Send the data directly
  130. *
  131. * @Param hdlr, in, the CoAP context
  132. * @Param data, in, the user data
  133. * @Param size, in, the data size
  134. * @Return duer_status_t, the result
  135. */
  136. DUER_INT duer_status_t duer_coap_send_data(duer_coap_handler hdlr, const void *data, duer_size_t size);
  137. /*
  138. * Receive the CoAP message
  139. *
  140. * @Param hdlr, in, the CoAP context
  141. * @Return duer_status_t, the result
  142. */
  143. DUER_INT duer_status_t duer_coap_data_available(duer_coap_handler coap);
  144. /*
  145. * Execute the cached CoAP data
  146. *
  147. * @Param hdlr, in, the CoAP context
  148. * @Param timestamp, in, the timestamp for mark the message
  149. * @Return duer_status_t, the result
  150. */
  151. DUER_INT duer_status_t duer_coap_exec(duer_coap_handler hdlr, duer_u32_t timestamp);
  152. /*
  153. * Release the CoAP context
  154. *
  155. * @Param hdlr, in, the CoAP context
  156. * @Return duer_status_t, the result
  157. */
  158. DUER_INT duer_status_t duer_coap_release(duer_coap_handler coap);
  159. /*
  160. * Set read timeout
  161. *
  162. * @Param hdlr, in, the CoAP context
  163. * @Param timeout, in, the timeout value by milliseconds
  164. * @Return duer_bool, if match return DUER_TRUE, else return DUER_FALSE
  165. */
  166. DUER_INT duer_status_t duer_coap_set_read_timeout(duer_coap_handler coap, duer_u32_t timeout);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_H