esp_local_ctrl.h 12 KB

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