esp_local_ctrl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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(void);
  169. /**
  170. * @brief Function for obtaining HTTPD transport mode
  171. */
  172. const esp_local_ctrl_transport_t *esp_local_ctrl_get_transport_httpd(void);
  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 Security types for esp_local_control
  208. */
  209. typedef enum esp_local_ctrl_proto_sec {
  210. PROTOCOM_SEC0 = 0,
  211. PROTOCOM_SEC1,
  212. PROTOCOM_SEC_CUSTOM,
  213. } esp_local_ctrl_proto_sec_t;
  214. /**
  215. * Protocom security configs
  216. */
  217. typedef struct esp_local_ctrl_proto_sec_cfg {
  218. /**
  219. * This sets protocom security version, sec0/sec1 or custom
  220. * If custom, user must provide handle via `proto_sec_custom_handle` below
  221. */
  222. esp_local_ctrl_proto_sec_t version;
  223. /**
  224. * Custom security handle if security is set custom via `proto_sec` above
  225. * This handle must follow `protocomm_security_t` signature
  226. */
  227. void *custom_handle;
  228. /**
  229. * Proof of possession to be used for local control. Could be NULL.
  230. */
  231. void *pop;
  232. } esp_local_ctrl_proto_sec_cfg_t;
  233. /**
  234. * @brief Configuration structure to pass to `esp_local_ctrl_start()`
  235. */
  236. typedef struct esp_local_ctrl_config {
  237. /**
  238. * Transport layer over which service will be provided
  239. */
  240. const esp_local_ctrl_transport_t *transport;
  241. /**
  242. * Transport layer over which service will be provided
  243. */
  244. esp_local_ctrl_transport_config_t transport_config;
  245. /**
  246. * Security version and POP
  247. */
  248. esp_local_ctrl_proto_sec_cfg_t proto_sec;
  249. /**
  250. * Register handlers for responding to get/set requests on properties
  251. */
  252. esp_local_ctrl_handlers_t handlers;
  253. /**
  254. * This limits the number of properties that are available at a time
  255. */
  256. size_t max_properties;
  257. } esp_local_ctrl_config_t;
  258. /**
  259. * @brief Start local control service
  260. *
  261. * @param[in] config Pointer to configuration structure
  262. *
  263. * @return
  264. * - ESP_OK : Success
  265. * - ESP_FAIL : Failure
  266. */
  267. esp_err_t esp_local_ctrl_start(const esp_local_ctrl_config_t *config);
  268. /**
  269. * @brief Stop local control service
  270. */
  271. esp_err_t esp_local_ctrl_stop(void);
  272. /**
  273. * @brief Add a new property
  274. *
  275. * This adds a new property and allocates internal resources for it.
  276. * The total number of properties that could be added is limited by
  277. * configuration option `max_properties`
  278. *
  279. * @param[in] prop Property description structure
  280. *
  281. * @return
  282. * - ESP_OK : Success
  283. * - ESP_FAIL : Failure
  284. */
  285. esp_err_t esp_local_ctrl_add_property(const esp_local_ctrl_prop_t *prop);
  286. /**
  287. * @brief Remove a property
  288. *
  289. * This finds a property by name, and releases the internal resources
  290. * which are associated with it.
  291. *
  292. * @param[in] name Name of the property to remove
  293. *
  294. * @return
  295. * - ESP_OK : Success
  296. * - ESP_ERR_NOT_FOUND : Failure
  297. */
  298. esp_err_t esp_local_ctrl_remove_property(const char *name);
  299. /**
  300. * @brief Get property description structure by name
  301. *
  302. * This API may be used to get a property's context structure
  303. * `esp_local_ctrl_prop_t` when its name is known
  304. *
  305. * @param[in] name Name of the property to find
  306. *
  307. * @return
  308. * - Pointer to property
  309. * - NULL if not found
  310. */
  311. const esp_local_ctrl_prop_t *esp_local_ctrl_get_property(const char *name);
  312. /**
  313. * @brief Register protocomm handler for a custom endpoint
  314. *
  315. * This API can be called by the application to register a protocomm handler
  316. * for an endpoint after the local control service has started.
  317. *
  318. * @note In case of BLE transport the names and uuids of all custom
  319. * endpoints must be provided beforehand as a part of the `protocomm_ble_config_t`
  320. * structure set in `esp_local_ctrl_config_t`, and passed to `esp_local_ctrl_start()`.
  321. *
  322. * @param[in] ep_name Name of the endpoint
  323. * @param[in] handler Endpoint handler function
  324. * @param[in] user_ctx User data
  325. *
  326. * @return
  327. * - ESP_OK : Success
  328. * - ESP_FAIL : Failure
  329. */
  330. esp_err_t esp_local_ctrl_set_handler(const char *ep_name,
  331. protocomm_req_handler_t handler,
  332. void *user_ctx);
  333. #ifdef __cplusplus
  334. }
  335. #endif