protocomm.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <protocomm_security.h>
  16. #include <esp_err.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * @brief Function prototype for protocomm endpoint handler
  22. */
  23. typedef esp_err_t (*protocomm_req_handler_t)(
  24. uint32_t session_id, /*!< Session ID for identifying protocomm client */
  25. const uint8_t *inbuf, /*!< Pointer to user provided input data buffer */
  26. ssize_t inlen, /*!< Length o the input buffer */
  27. uint8_t **outbuf, /*!< Pointer to output buffer allocated by handler */
  28. ssize_t *outlen, /*!< Length of the allocated output buffer */
  29. void *priv_data /*!< Private data passed to the handler (NULL if not used) */
  30. );
  31. /**
  32. * @brief This structure corresponds to a unique instance of protocomm
  33. * returned when the API `protocomm_new()` is called. The remaining
  34. * Protocomm APIs require this object as the first parameter.
  35. *
  36. * @note Structure of the protocomm object is kept private
  37. */
  38. typedef struct protocomm protocomm_t;
  39. /**
  40. * @brief Create a new protocomm instance
  41. *
  42. * This API will return a new dynamically allocated protocomm instance
  43. * with all elements of the protocomm_t structure initialized to NULL.
  44. *
  45. * @return
  46. * - protocomm_t* : On success
  47. * - NULL : No memory for allocating new instance
  48. */
  49. protocomm_t *protocomm_new(void);
  50. /**
  51. * @brief Delete a protocomm instance
  52. *
  53. * This API will deallocate a protocomm instance that was created
  54. * using `protocomm_new()`.
  55. *
  56. * @param[in] pc Pointer to the protocomm instance to be deleted
  57. */
  58. void protocomm_delete(protocomm_t *pc);
  59. /**
  60. * @brief Add endpoint request handler for a protocomm instance
  61. *
  62. * This API will bind an endpoint handler function to the specified
  63. * endpoint name, along with any private data that needs to be pass to
  64. * the handler at the time of call.
  65. *
  66. * @note
  67. * - An endpoint must be bound to a valid protocomm instance,
  68. * created using `protocomm_new()`.
  69. * - This function internally calls the registered `add_endpoint()`
  70. * function of the selected transport which is a member of the
  71. * protocomm_t instance structure.
  72. *
  73. * @param[in] pc Pointer to the protocomm instance
  74. * @param[in] ep_name Endpoint identifier(name) string
  75. * @param[in] h Endpoint handler function
  76. * @param[in] priv_data Pointer to private data to be passed as a
  77. * parameter to the handler function on call.
  78. * Pass NULL if not needed.
  79. *
  80. * @return
  81. * - ESP_OK : Success
  82. * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists
  83. * - ESP_ERR_NO_MEM : Error allocating endpoint resource
  84. * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments
  85. */
  86. esp_err_t protocomm_add_endpoint(protocomm_t *pc, const char *ep_name,
  87. protocomm_req_handler_t h, void *priv_data);
  88. /**
  89. * @brief Remove endpoint request handler for a protocomm instance
  90. *
  91. * This API will remove a registered endpoint handler identified by
  92. * an endpoint name.
  93. *
  94. * @note
  95. * - This function internally calls the registered `remove_endpoint()`
  96. * function which is a member of the protocomm_t instance structure.
  97. *
  98. * @param[in] pc Pointer to the protocomm instance
  99. * @param[in] ep_name Endpoint identifier(name) string
  100. *
  101. * @return
  102. * - ESP_OK : Success
  103. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  104. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  105. */
  106. esp_err_t protocomm_remove_endpoint(protocomm_t *pc, const char *ep_name);
  107. /**
  108. * @brief Allocates internal resources for new transport session
  109. *
  110. * @note
  111. * - An endpoint must be bound to a valid protocomm instance,
  112. * created using `protocomm_new()`.
  113. *
  114. * @param[in] pc Pointer to the protocomm instance
  115. * @param[in] session_id Unique ID for a communication session
  116. *
  117. * @return
  118. * - ESP_OK : Request handled successfully
  119. * - ESP_ERR_NO_MEM : Error allocating internal resource
  120. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  121. */
  122. esp_err_t protocomm_open_session(protocomm_t *pc, uint32_t session_id);
  123. /**
  124. * @brief Frees internal resources used by a transport session
  125. *
  126. * @note
  127. * - An endpoint must be bound to a valid protocomm instance,
  128. * created using `protocomm_new()`.
  129. *
  130. * @param[in] pc Pointer to the protocomm instance
  131. * @param[in] session_id Unique ID for a communication session
  132. *
  133. * @return
  134. * - ESP_OK : Request handled successfully
  135. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  136. */
  137. esp_err_t protocomm_close_session(protocomm_t *pc, uint32_t session_id);
  138. /**
  139. * @brief Calls the registered handler of an endpoint session
  140. * for processing incoming data and generating the response
  141. *
  142. * @note
  143. * - An endpoint must be bound to a valid protocomm instance,
  144. * created using `protocomm_new()`.
  145. * - Resulting output buffer must be deallocated by the caller.
  146. *
  147. * @param[in] pc Pointer to the protocomm instance
  148. * @param[in] ep_name Endpoint identifier(name) string
  149. * @param[in] session_id Unique ID for a communication session
  150. * @param[in] inbuf Input buffer contains input request data which is to be
  151. * processed by the registered handler
  152. * @param[in] inlen Length of the input buffer
  153. * @param[out] outbuf Pointer to internally allocated output buffer,
  154. * where the resulting response data output from
  155. * the registered handler is to be stored
  156. * @param[out] outlen Buffer length of the allocated output buffer
  157. *
  158. * @return
  159. * - ESP_OK : Request handled successfully
  160. * - ESP_FAIL : Internal error in execution of registered handler
  161. * - ESP_ERR_NO_MEM : Error allocating internal resource
  162. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  163. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  164. */
  165. esp_err_t protocomm_req_handle(protocomm_t *pc, const char *ep_name, uint32_t session_id,
  166. const uint8_t *inbuf, ssize_t inlen,
  167. uint8_t **outbuf, ssize_t *outlen);
  168. /**
  169. * @brief Add endpoint security for a protocomm instance
  170. *
  171. * This API will bind a security session establisher to the specified
  172. * endpoint name, along with any proof of possession that may be required
  173. * for authenticating a session client.
  174. *
  175. * @note
  176. * - An endpoint must be bound to a valid protocomm instance,
  177. * created using `protocomm_new()`.
  178. * - The choice of security can be any `protocomm_security_t` instance.
  179. * Choices `protocomm_security0` and `protocomm_security1` are readily available.
  180. *
  181. * @param[in] pc Pointer to the protocomm instance
  182. * @param[in] ep_name Endpoint identifier(name) string
  183. * @param[in] sec Pointer to endpoint security instance
  184. * @param[in] pop Pointer to proof of possession for authenticating a client
  185. *
  186. * @return
  187. * - ESP_OK : Success
  188. * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists
  189. * - ESP_ERR_INVALID_STATE : Security endpoint already set
  190. * - ESP_ERR_NO_MEM : Error allocating endpoint resource
  191. * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments
  192. */
  193. esp_err_t protocomm_set_security(protocomm_t *pc, const char *ep_name,
  194. const protocomm_security_t *sec,
  195. const protocomm_security_pop_t *pop);
  196. /**
  197. * @brief Remove endpoint security for a protocomm instance
  198. *
  199. * This API will remove a registered security endpoint identified by
  200. * an endpoint name.
  201. *
  202. * @param[in] pc Pointer to the protocomm instance
  203. * @param[in] ep_name Endpoint identifier(name) string
  204. *
  205. * @return
  206. * - ESP_OK : Success
  207. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  208. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  209. */
  210. esp_err_t protocomm_unset_security(protocomm_t *pc, const char *ep_name);
  211. /**
  212. * @brief Set endpoint for version verification
  213. *
  214. * This API can be used for setting an application specific protocol
  215. * version which can be verified by clients through the endpoint.
  216. *
  217. * @note
  218. * - An endpoint must be bound to a valid protocomm instance,
  219. * created using `protocomm_new()`.
  220. * @param[in] pc Pointer to the protocomm instance
  221. * @param[in] ep_name Endpoint identifier(name) string
  222. * @param[in] version Version identifier(name) string
  223. *
  224. * @return
  225. * - ESP_OK : Success
  226. * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists
  227. * - ESP_ERR_INVALID_STATE : Version endpoint already set
  228. * - ESP_ERR_NO_MEM : Error allocating endpoint resource
  229. * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments
  230. */
  231. esp_err_t protocomm_set_version(protocomm_t *pc, const char *ep_name,
  232. const char *version);
  233. /**
  234. * @brief Remove version verification endpoint from a protocomm instance
  235. *
  236. * This API will remove a registered version endpoint identified by
  237. * an endpoint name.
  238. *
  239. * @param[in] pc Pointer to the protocomm instance
  240. * @param[in] ep_name Endpoint identifier(name) string
  241. *
  242. * @return
  243. * - ESP_OK : Success
  244. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  245. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  246. */
  247. esp_err_t protocomm_unset_version(protocomm_t *pc, const char *ep_name);
  248. #ifdef __cplusplus
  249. }
  250. #endif