esp_local_ctrl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright 2019 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. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <protocomm.h>
  19. /**
  20. * @brief Property description data structure, which is to be populated
  21. * and passed to the `esp_local_ctrl_add_property()` function
  22. *
  23. * Once a property is added, its structure is available for read-only access
  24. * inside `get_prop_values()` and `set_prop_values()` handlers.
  25. */
  26. typedef struct esp_local_ctrl_prop {
  27. /**
  28. * Unique name of property
  29. */
  30. char *name;
  31. /**
  32. * Type of property. This may be set to application defined enums
  33. */
  34. uint32_t type;
  35. /**
  36. * Size of the property value, which:
  37. * - if zero, the property can have values of variable size
  38. * - if non-zero, the property can have values of fixed size only,
  39. * therefore, checks are performed internally by esp_local_ctrl
  40. * when setting the value of such a property
  41. */
  42. size_t size;
  43. /**
  44. * Flags set for this property. This could be a bit field.
  45. * A flag may indicate property behavior, e.g. read-only / constant
  46. */
  47. uint32_t flags;
  48. /**
  49. * Pointer to some context data relevant for this property. This will
  50. * be available for use inside the `get_prop_values` and `set_prop_values`
  51. * handlers as a part of this property structure. When set, this is valid
  52. * throughout the lifetime of a property, till either the property is
  53. * removed or the esp_local_ctrl service is stopped.
  54. */
  55. void *ctx;
  56. /**
  57. * Function used by esp_local_ctrl to internally free the property
  58. * context when `esp_local_ctrl_remove_property()` or
  59. * `esp_local_ctrl_stop()` is called.
  60. */
  61. void (*ctx_free_fn)(void *ctx);
  62. } esp_local_ctrl_prop_t;
  63. /**
  64. * @brief Property value data structure. This gets passed to the
  65. * `get_prop_values()` and `set_prop_values()` handlers for
  66. * the purpose of retrieving or setting the present value
  67. * of a property.
  68. */
  69. typedef struct esp_local_ctrl_prop_val {
  70. /**
  71. * Pointer to memory holding property value
  72. */
  73. void *data;
  74. /**
  75. * Size of property value
  76. */
  77. size_t size;
  78. /**
  79. * This may be set by the application in `get_prop_values()` handler
  80. * to tell `esp_local_ctrl` to call this function on the data pointer
  81. * above, for freeing its resources after sending the `get_prop_values`
  82. * response.
  83. */
  84. void (*free_fn)(void *data);
  85. } esp_local_ctrl_prop_val_t;
  86. /**
  87. * @brief Handlers for receiving and responding to local
  88. * control commands for getting and setting properties.
  89. */
  90. typedef struct esp_local_ctrl_handlers {
  91. /**
  92. * @brief Handler function to be implemented for retrieving current
  93. * values of properties
  94. *
  95. * @note If any of the properties have fixed sizes, the size field of
  96. * corresponding element in `prop_values` need to be set
  97. *
  98. * @param[in] props_count Total elements in the props array
  99. * @param[in] props Array of properties, the current values for which
  100. * have been requested by the client
  101. * @param[out] prop_values Array of empty property values, the elements of
  102. * which need to be populated with the current values
  103. * of those properties specified by props argument
  104. * @param[in] usr_ctx This provides value of the `usr_ctx` field of
  105. * `esp_local_ctrl_handlers_t` structure
  106. *
  107. * @return Returning different error codes will convey the corresponding
  108. * protocol level errors to the client :
  109. * - ESP_OK : Success
  110. * - ESP_ERR_INVALID_ARG : InvalidArgument
  111. * - ESP_ERR_INVALID_STATE : InvalidProto
  112. * - All other error codes : InternalError
  113. */
  114. esp_err_t (*get_prop_values)(size_t props_count,
  115. const esp_local_ctrl_prop_t props[],
  116. esp_local_ctrl_prop_val_t prop_values[],
  117. void *usr_ctx);
  118. /**
  119. * @brief Handler function to be implemented for changing values of properties
  120. *
  121. * @note If any of the properties have variable sizes, the size field
  122. * of the corresponding element in `prop_values` must be checked
  123. * explicitly before making any assumptions on the size.
  124. *
  125. * @param[in] props_count Total elements in the props array
  126. * @param[in] props Array of properties, the values for which the
  127. * client requests to change
  128. * @param[in] prop_values Array of property values, the elements of which
  129. * need to be used for updating those properties
  130. * specified by props argument
  131. * @param[in] usr_ctx This provides value of the `usr_ctx` field of
  132. * `esp_local_ctrl_handlers_t` structure
  133. *
  134. * @return Returning different error codes will convey the corresponding
  135. * protocol level errors to the client :
  136. * - ESP_OK : Success
  137. * - ESP_ERR_INVALID_ARG : InvalidArgument
  138. * - ESP_ERR_INVALID_STATE : InvalidProto
  139. * - All other error codes : InternalError
  140. */
  141. esp_err_t (*set_prop_values)(size_t props_count,
  142. const esp_local_ctrl_prop_t props[],
  143. const esp_local_ctrl_prop_val_t prop_values[],
  144. void *usr_ctx);
  145. /**
  146. * Context pointer to be passed to above handler functions upon invocation.
  147. * This is different from the property level context, as this is valid
  148. * throughout the lifetime of the `esp_local_ctrl` service, and freed only
  149. * when the service is stopped.
  150. */
  151. void *usr_ctx;
  152. /**
  153. * Pointer to function which will be internally invoked on `usr_ctx` for
  154. * freeing the context resources when `esp_local_ctrl_stop()` is called.
  155. */
  156. void (*usr_ctx_free_fn)(void *usr_ctx);
  157. } esp_local_ctrl_handlers_t;
  158. /**
  159. * @brief Transport mode (BLE / HTTPD) over which the service will be provided
  160. *
  161. * This is forward declaration of a private structure, implemented internally
  162. * by `esp_local_ctrl`.
  163. */
  164. typedef struct esp_local_ctrl_transport esp_local_ctrl_transport_t;
  165. /**
  166. * @brief Function for obtaining BLE transport mode
  167. */
  168. const esp_local_ctrl_transport_t *esp_local_ctrl_get_transport_ble();
  169. /**
  170. * @brief Function for obtaining HTTPD transport mode
  171. */
  172. const esp_local_ctrl_transport_t *esp_local_ctrl_get_transport_httpd();
  173. #define ESP_LOCAL_CTRL_TRANSPORT_BLE esp_local_ctrl_get_transport_ble()
  174. #define ESP_LOCAL_CTRL_TRANSPORT_HTTPD esp_local_ctrl_get_transport_httpd()
  175. /**
  176. * @brief Configuration for transport mode BLE
  177. *
  178. * This is a forward declaration for `protocomm_ble_config_t`.
  179. * To use this, application must set CONFIG_BT_BLUEDROID_ENABLED
  180. * and include `protocomm_ble.h`.
  181. */
  182. typedef struct protocomm_ble_config esp_local_ctrl_transport_config_ble_t;
  183. /**
  184. * @brief Configuration for transport mode HTTPD
  185. *
  186. * This is a forward declaration for `httpd_ssl_config_t`.
  187. * To use this, application must set CONFIG_ESP_HTTPS_SERVER_ENABLE
  188. * and include `esp_https_server.h`
  189. */
  190. typedef struct httpd_ssl_config esp_local_ctrl_transport_config_httpd_t;
  191. /**
  192. * @brief Transport mode (BLE / HTTPD) configuration
  193. */
  194. typedef union {
  195. /**
  196. * This is same as `protocomm_ble_config_t`. See `protocomm_ble.h` for
  197. * available configuration parameters.
  198. */
  199. esp_local_ctrl_transport_config_ble_t *ble;
  200. /**
  201. * This is same as `httpd_ssl_config_t`. See `esp_https_server.h` for
  202. * available configuration parameters.
  203. */
  204. esp_local_ctrl_transport_config_httpd_t *httpd;
  205. } esp_local_ctrl_transport_config_t;
  206. /**
  207. * @brief Configuration structure to pass to `esp_local_ctrl_start()`
  208. */
  209. typedef struct esp_local_ctrl_config {
  210. /**
  211. * Transport layer over which service will be provided
  212. */
  213. const esp_local_ctrl_transport_t *transport;
  214. /**
  215. * Transport layer over which service will be provided
  216. */
  217. esp_local_ctrl_transport_config_t transport_config;
  218. /**
  219. * Register handlers for responding to get/set requests on properties
  220. */
  221. esp_local_ctrl_handlers_t handlers;
  222. /**
  223. * This limits the number of properties that are available at a time
  224. */
  225. size_t max_properties;
  226. } esp_local_ctrl_config_t;
  227. /**
  228. * @brief Start local control service
  229. *
  230. * @param[in] config Pointer to configuration structure
  231. *
  232. * @return
  233. * - ESP_OK : Success
  234. * - ESP_FAIL : Failure
  235. */
  236. esp_err_t esp_local_ctrl_start(const esp_local_ctrl_config_t *config);
  237. /**
  238. * @brief Stop local control service
  239. */
  240. esp_err_t esp_local_ctrl_stop(void);
  241. /**
  242. * @brief Add a new property
  243. *
  244. * This adds a new property and allocates internal resources for it.
  245. * The total number of properties that could be added is limited by
  246. * configuration option `max_properties`
  247. *
  248. * @param[in] prop Property description structure
  249. *
  250. * @return
  251. * - ESP_OK : Success
  252. * - ESP_FAIL : Failure
  253. */
  254. esp_err_t esp_local_ctrl_add_property(const esp_local_ctrl_prop_t *prop);
  255. /**
  256. * @brief Remove a property
  257. *
  258. * This finds a property by name, and releases the internal resources
  259. * which are associated with it.
  260. *
  261. * @param[in] name Name of the property to remove
  262. *
  263. * @return
  264. * - ESP_OK : Success
  265. * - ESP_ERR_NOT_FOUND : Failure
  266. */
  267. esp_err_t esp_local_ctrl_remove_property(const char *name);
  268. /**
  269. * @brief Get property description structure by name
  270. *
  271. * This API may be used to get a property's context structure
  272. * `esp_local_ctrl_prop_t` when its name is known
  273. *
  274. * @param[in] name Name of the property to find
  275. *
  276. * @return
  277. * - Pointer to property
  278. * - NULL if not found
  279. */
  280. const esp_local_ctrl_prop_t *esp_local_ctrl_get_property(const char *name);
  281. /**
  282. * @brief Register protocomm handler for a custom endpoint
  283. *
  284. * This API can be called by the application to register a protocomm handler
  285. * for an endpoint after the local control service has started.
  286. *
  287. * @note In case of BLE transport the names and uuids of all custom
  288. * endpoints must be provided beforehand as a part of the `protocomm_ble_config_t`
  289. * structure set in `esp_local_ctrl_config_t`, and passed to `esp_local_ctrl_start()`.
  290. *
  291. * @param[in] ep_name Name of the endpoint
  292. * @param[in] handler Endpoint handler function
  293. * @param[in] user_ctx User data
  294. *
  295. * @return
  296. * - ESP_OK : Success
  297. * - ESP_FAIL : Failure
  298. */
  299. esp_err_t esp_local_ctrl_set_handler(const char *ep_name,
  300. protocomm_req_handler_t handler,
  301. void *user_ctx);
  302. #ifdef __cplusplus
  303. }
  304. #endif