esp_local_ctrl.h 13 KB

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