protocomm.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /**
  18. * @brief Function prototype for protocomm endpoint handler
  19. */
  20. typedef esp_err_t (*protocomm_req_handler_t)(
  21. uint32_t session_id, /*!< Session ID for identifying protocomm client */
  22. const uint8_t *inbuf, /*!< Pointer to user provided input data buffer */
  23. ssize_t inlen, /*!< Length o the input buffer */
  24. uint8_t **outbuf, /*!< Pointer to output buffer allocated by handler */
  25. ssize_t *outlen, /*!< Length of the allocated output buffer */
  26. void *priv_data /*!< Private data passed to the handler (NULL if not used) */
  27. );
  28. /**
  29. * @brief This structure corresponds to a unique instance of protocomm
  30. * returned when the API `protocomm_new()` is called. The remaining
  31. * Protocomm APIs require this object as the first parameter.
  32. *
  33. * @note Structure of the protocomm object is kept private
  34. */
  35. typedef struct protocomm protocomm_t;
  36. /**
  37. * @brief Create a new protocomm instance
  38. *
  39. * This API will return a new dynamically allocated protocomm instance
  40. * with all elements of the protocomm_t structure initialised to NULL.
  41. *
  42. * @return
  43. * - protocomm_t* : On success
  44. * - NULL : No memory for allocating new instance
  45. */
  46. protocomm_t *protocomm_new();
  47. /**
  48. * @brief Delete a protocomm instance
  49. *
  50. * This API will deallocate a protocomm instance that was created
  51. * using `protocomm_new()`.
  52. *
  53. * @param[in] pc Pointer to the protocomm instance to be deleted
  54. */
  55. void protocomm_delete(protocomm_t *pc);
  56. /**
  57. * @brief Add endpoint request handler for a protocomm instance
  58. *
  59. * This API will bind an endpoint handler function to the specified
  60. * endpoint name, along with any private data that needs to be pass to
  61. * the handler at the time of call.
  62. *
  63. * @note
  64. * - An endpoint must be bound to a valid protocomm instance,
  65. * created using `protocomm_new()`.
  66. * - This function internally calls the registered `add_endpoint()`
  67. * function which is a member of the protocomm_t instance structure.
  68. *
  69. * @param[in] pc Pointer to the protocomm instance
  70. * @param[in] ep_name Endpoint identifier(name) string
  71. * @param[in] h Endpoint handler function
  72. * @param[in] priv_data Pointer to private data to be passed as a
  73. * parameter to the handler function on call.
  74. * Pass NULL if not needed.
  75. *
  76. * @return
  77. * - ESP_OK : Added new endpoint succesfully
  78. * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists
  79. * - ESP_ERR_NO_MEM : Error allocating endpoint resource
  80. * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments
  81. */
  82. esp_err_t protocomm_add_endpoint(protocomm_t *pc, const char *ep_name,
  83. protocomm_req_handler_t h, void *priv_data);
  84. /**
  85. * @brief Remove endpoint request handler for a protocomm instance
  86. *
  87. * This API will remove a registered endpoint handler identified by
  88. * an endpoint name.
  89. *
  90. * @note
  91. * - This function internally calls the registered `remove_endpoint()`
  92. * function which is a member of the protocomm_t instance structure.
  93. *
  94. * @param[in] pc Pointer to the protocomm instance
  95. * @param[in] ep_name Endpoint identifier(name) string
  96. *
  97. * @return
  98. * - ESP_OK : Added new endpoint succesfully
  99. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  100. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  101. */
  102. esp_err_t protocomm_remove_endpoint(protocomm_t *pc, const char *ep_name);
  103. /**
  104. * @brief Calls the registered handler of an endpoint session
  105. * for processing incoming data and giving the output
  106. *
  107. * @note
  108. * - An endpoint must be bound to a valid protocomm instance,
  109. * created using `protocomm_new()`.
  110. * - Resulting output buffer must be deallocated by the user.
  111. *
  112. * @param[in] pc Pointer to the protocomm instance
  113. * @param[in] ep_name Endpoint identifier(name) string
  114. * @param[in] session_id Unique ID for a communication session
  115. * @param[in] inbuf Input buffer contains input request data which is to be
  116. * processed by the registered handler
  117. * @param[in] inlen Length of the input buffer
  118. * @param[out] outbuf Pointer to internally allocated output buffer,
  119. * where the resulting response data output from
  120. * the registered handler is to be stored
  121. * @param[out] outlen Buffer length of the allocated output buffer
  122. *
  123. * @return
  124. * - ESP_OK : Request handled succesfully
  125. * - ESP_FAIL : Internal error in execution of registered handler
  126. * - ESP_ERR_NO_MEM : Error allocating internal resource
  127. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  128. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  129. */
  130. esp_err_t protocomm_req_handle(protocomm_t *pc, const char *ep_name, uint32_t session_id,
  131. const uint8_t *inbuf, ssize_t inlen,
  132. uint8_t **outbuf, ssize_t *outlen);
  133. /**
  134. * @brief Add endpoint security for a protocomm instance
  135. *
  136. * This API will bind a security session establisher to the specified
  137. * endpoint name, along with any proof of possession that may be required
  138. * for authenticating a session client.
  139. *
  140. * @note
  141. * - An endpoint must be bound to a valid protocomm instance,
  142. * created using `protocomm_new()`.
  143. * - The choice of security can be any `protocomm_security_t` instance.
  144. * Choices `protocomm_security0` and `protocomm_security1` are readily available.
  145. *
  146. * @param[in] pc Pointer to the protocomm instance
  147. * @param[in] ep_name Endpoint identifier(name) string
  148. * @param[in] sec Pointer to endpoint security instance
  149. * @param[in] pop Pointer to proof of possession for authenticating a client
  150. *
  151. * @return
  152. * - ESP_OK : Added new security endpoint succesfully
  153. * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists
  154. * - ESP_ERR_INVALID_STATE : Security endpoint already set
  155. * - ESP_ERR_NO_MEM : Error allocating endpoint resource
  156. * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments
  157. */
  158. esp_err_t protocomm_set_security(protocomm_t *pc, const char *ep_name,
  159. const protocomm_security_t *sec,
  160. const protocomm_security_pop_t *pop);
  161. /**
  162. * @brief Remove endpoint security for a protocomm instance
  163. *
  164. * This API will remove a registered security endpoint identified by
  165. * an endpoint name.
  166. *
  167. * @param[in] pc Pointer to the protocomm instance
  168. * @param[in] ep_name Endpoint identifier(name) string
  169. *
  170. * @return
  171. * - ESP_OK : Added new endpoint succesfully
  172. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  173. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  174. */
  175. esp_err_t protocomm_unset_security(protocomm_t *pc, const char *ep_name);
  176. /**
  177. * @brief Set endpoint for version verification
  178. *
  179. * This API can be used for setting an application specific protocol
  180. * version which can be verfied by clients through the endpoint.
  181. *
  182. * @note
  183. * - An endpoint must be bound to a valid protocomm instance,
  184. * created using `protocomm_new()`.
  185. * @param[in] pc Pointer to the protocomm instance
  186. * @param[in] ep_name Endpoint identifier(name) string
  187. * @param[in] version Version identifier(name) string
  188. *
  189. * @return
  190. * - ESP_OK : Added new security endpoint succesfully
  191. * - ESP_FAIL : Error adding endpoint / Endpoint with this name already exists
  192. * - ESP_ERR_INVALID_STATE : Version endpoint already set
  193. * - ESP_ERR_NO_MEM : Error allocating endpoint resource
  194. * - ESP_ERR_INVALID_ARG : Null instance/name/handler arguments
  195. */
  196. esp_err_t protocomm_set_version(protocomm_t *pc, const char *ep_name,
  197. const char *version);
  198. /**
  199. * @brief Remove version verification endpoint from a protocomm instance
  200. *
  201. * This API will remove a registered version endpoint identified by
  202. * an endpoint name.
  203. *
  204. * @param[in] pc Pointer to the protocomm instance
  205. * @param[in] ep_name Endpoint identifier(name) string
  206. *
  207. * @return
  208. * - ESP_OK : Added new endpoint succesfully
  209. * - ESP_ERR_NOT_FOUND : Endpoint with specified name doesn't exist
  210. * - ESP_ERR_INVALID_ARG : Null instance/name arguments
  211. */
  212. esp_err_t protocomm_unset_version(protocomm_t *pc, const char *ep_name);