manager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifndef _MANAGER_H_
  8. #define _MANAGER_H_
  9. #ifdef CONFIG_OTA_WITH_PROTOCOMM
  10. #include <protocomm.h>
  11. #include "esp_event.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. ESP_EVENT_DECLARE_BASE(ESP_BLE_OTA_EVENT);
  16. typedef enum {
  17. OTA_INIT,
  18. OTA_DEINIT,
  19. OTA_END,
  20. OTA_FILE_RCV,
  21. } esp_ble_ota_cb_event_t;
  22. typedef void (*esp_ble_ota_cb_func_t)(void *user_data, esp_ble_ota_cb_event_t event, void *event_data);
  23. /**
  24. * @brief Event handler that is used by the manager while
  25. * ota service is active
  26. */
  27. typedef struct {
  28. /**
  29. * Callback function to be executed on ota events
  30. */
  31. esp_ble_ota_cb_func_t event_cb;
  32. /**
  33. * User context data to pass as parameter to callback function
  34. */
  35. void *user_data;
  36. } esp_ble_ota_event_handler_t;
  37. /**
  38. * @brief Event handler can be set to none if not used
  39. */
  40. #define ESP_BLE_OTA_EVENT_HANDLER_NONE { \
  41. .event_cb = NULL, \
  42. .user_data = NULL \
  43. }
  44. /**
  45. * @brief Structure for specifying the ota scheme to be
  46. * followed by the manager
  47. *
  48. * @note Ready to use schemes are available:
  49. * - esp_ble_ota_scheme_ble : for ota over BLE transport + GATT server
  50. */
  51. typedef struct esp_ble_ota_scheme {
  52. /**
  53. * Function which is to be called by the manager when it is to
  54. * start the ota service associated with a protocomm instance
  55. * and a scheme specific configuration
  56. */
  57. esp_err_t (*ota_start) (protocomm_t *pc, void *config);
  58. /**
  59. * Function which is to be called by the manager to stop the
  60. * ota service previously associated with a protocomm instance
  61. */
  62. esp_err_t (*ota_stop) (protocomm_t *pc);
  63. /**
  64. * Function which is to be called by the manager to generate
  65. * a new configuration for the ota service, that is
  66. * to be passed to ota_start()
  67. */
  68. void *(*new_config) (void);
  69. /**
  70. * Function which is to be called by the manager to delete a
  71. * configuration generated using new_config()
  72. */
  73. void (*delete_config) (void *config);
  74. /**
  75. * Function which is to be called by the manager to set the
  76. * service name and key values in the configuration structure
  77. */
  78. esp_err_t (*set_config_service) (void *config, const char *service_name, const char *service_key);
  79. /**
  80. * Function which is to be called by the manager to set a protocomm endpoint
  81. * with an identifying name and UUID in the configuration structure
  82. */
  83. esp_err_t (*set_config_endpoint) (void *config, const char *endpoint_name, uint16_t uuid);
  84. } esp_ble_ota_scheme_t;
  85. /**
  86. * @brief Structure for specifying the manager configuration
  87. */
  88. typedef struct {
  89. /**
  90. * Provisioning scheme to use. Following schemes are already available:
  91. * - esp_ble_ota_scheme_ble : for ota over BLE transport + GATT server
  92. */
  93. esp_ble_ota_scheme_t scheme;
  94. /**
  95. * Event handler required by the scheme for incorporating scheme specific
  96. * behavior while ota manager is running. Various options may be
  97. * provided by the scheme for setting this field. Use ESP_BLE_OTA_EVENT_HANDLER_NONE
  98. * when not used. When using scheme esp_ble_ota_scheme_ble, the following
  99. * options are available:
  100. * - ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
  101. * - ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BLE
  102. * - ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BT
  103. */
  104. esp_ble_ota_event_handler_t scheme_event_handler;
  105. /**
  106. * Event handler that can be set for the purpose of incorporating application
  107. * specific behavior. Use ESP_BLE_OTA_EVENT_HANDLER_NONE when not used.
  108. */
  109. esp_ble_ota_event_handler_t app_event_handler;
  110. } esp_ble_ota_config_t;
  111. /**
  112. * @brief Security modes supported by the ota Manager.
  113. *
  114. * These are same as the security modes provided by protocomm
  115. */
  116. typedef enum esp_ble_ota_security {
  117. #ifdef CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0
  118. /**
  119. * No security (plain-text communication)
  120. */
  121. ESP_BLE_OTA_SECURITY_0 = 0,
  122. #endif
  123. #ifdef CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_1
  124. /**
  125. * This secure communication mode consists of
  126. * X25519 key exchange
  127. * + proof of possession (pop) based authentication
  128. * + AES-CTR encryption
  129. */
  130. ESP_BLE_OTA_SECURITY_1,
  131. #endif
  132. #ifdef CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2
  133. /**
  134. * This secure communication mode consists of
  135. * SRP6a based authentication and key exchange
  136. * + AES-GCM encryption/decryption
  137. */
  138. ESP_BLE_OTA_SECURITY_2
  139. #endif
  140. } esp_ble_ota_security_t;
  141. /**
  142. * @brief Security 1 params structure
  143. * This needs to be passed when using ESP_BLE_OTA_SECURITY_1
  144. */
  145. typedef const char esp_ble_ota_security1_params_t;
  146. /**
  147. * @brief Security 2 params structure
  148. * This needs to be passed when using ESP_BLE_OTA_SECURITY_2
  149. */
  150. typedef protocomm_security2_params_t esp_ble_ota_security2_params_t;
  151. /**
  152. * @brief Initialize ota manager instance
  153. *
  154. * Configures the manager and allocates internal resources
  155. *
  156. * Configuration specifies the ota scheme (transport)
  157. * and event handlers
  158. *
  159. * Event OTA_INIT is emitted right after initialization
  160. * is complete
  161. * @param[in] config Configuration structure
  162. *
  163. * @return
  164. * - ESP_OK : Success
  165. * - ESP_FAIL : Fail
  166. */
  167. esp_err_t esp_ble_ota_init(esp_ble_ota_config_t config);
  168. /**
  169. * @brief Stop ota (if running) and release
  170. * resource used by the manager
  171. *
  172. * Event OTA_DEINIT is emitted right after de-initialization
  173. * is finished
  174. *
  175. * If ota service is still active when this API is called,
  176. * it first stops the service, hence emitting OTA_END, and
  177. * then performs the de-initialization
  178. */
  179. void esp_ble_ota_deinit(void);
  180. /**
  181. * @brief Start ota service
  182. *
  183. * This starts the ota service according to the scheme
  184. * configured at the time of initialization. For scheme :
  185. * - ota_scheme_ble : This starts protocomm_ble, which internally initializes
  186. * BLE transport and starts GATT server for handling
  187. * ota requests
  188. *
  189. * Event OTA_START is emitted right after ota starts without failure
  190. *
  191. * @param[in] security Specify which protocomm security scheme to use :
  192. * - OTA_SECURITY_0 : For no security
  193. * - OTA_SECURITY_1 : x25519 secure handshake for session
  194. * establishment followed by AES-CTR encryption of ota messages
  195. * - OTA_SECURITY_2: SRP6a based authentication and key exchange
  196. * followed by AES-GCM encryption/decryption of ota messages
  197. * @param[in] ota_sec_params
  198. * Pointer to security params (NULL if not needed).
  199. * This is not needed for protocomm security 0
  200. * This pointer should hold the struct of type
  201. * ota_security1_params_t for protocomm security 1
  202. * and ota_security2_params_t for protocomm security 2 respectively.
  203. * This pointer and its contents should be valid till the ota service is
  204. * running and has not been stopped or de-inited.
  205. * @param[in] service_name Unique name of the service. This translates to:
  206. * - Device name when ota mode is BLE
  207. * @param[in] service_key Key required by client to access the service (NULL if not needed).
  208. * This translates to:
  209. * - ignored when ota mode is BLE
  210. *
  211. * @return
  212. * - ESP_OK : OTA started successfully
  213. * - ESP_FAIL : Failed to start ota service
  214. * - ESP_ERR_INVALID_STATE : OTA manager not initialized or already started
  215. */
  216. esp_err_t esp_ble_ota_start(esp_ble_ota_security_t security, const void *esp_ble_ota_sec_params, const char *service_name, const char *service_key);
  217. /**
  218. * @brief Stop ota service
  219. *
  220. * If ota service is active, this API will initiate a process to stop
  221. * the service and return. Once the service actually stops, the event OTA_END
  222. * will be emitted.
  223. *
  224. * If esp_ble_ota_deinit() is called without calling this API first, it will
  225. * automatically stop the ota service and emit the OTA_END, followed
  226. * by OTA_DEINIT, before returning.
  227. *
  228. * This API will generally be used in the scenario when the main application
  229. * has registered its own endpoints, and wishes that the ota service is stopped
  230. * only when some protocomm command from the client side application is received.
  231. *
  232. * Calling this API inside an endpoint handler, with sufficient cleanup_delay,
  233. * will allow the response / acknowledgment to be sent successfully before the
  234. * underlying protocomm service is stopped.
  235. *
  236. * For straightforward cases, using this API is usually not necessary as
  237. * ota is stopped automatically once OTA_CRED_SUCCESS is emitted.
  238. */
  239. void esp_ble_ota_stop(void);
  240. /**
  241. * @brief Wait for ota service to finish
  242. *
  243. * Calling this API will block until ota service is stopped
  244. * i.e. till event OTA_END is emitted.
  245. *
  246. * This will not block if ota is not started or not initialized.
  247. */
  248. void esp_ble_ota_wait(void);
  249. /**
  250. * @brief Create an additional endpoint and allocate internal resources for it
  251. *
  252. * This API is to be called by the application if it wants to create an additional
  253. * endpoint. All additional endpoints will be assigned UUIDs starting from 0xFF54
  254. * and so on in the order of execution.
  255. *
  256. * protocomm handler for the created endpoint is to be registered later using
  257. * esp_ble_ota_endpoint_register() after ota has started.
  258. *
  259. * @note This API can only be called BEFORE ota is started
  260. *
  261. * @note Additional endpoints can be used for configuring client provided
  262. * parameters, that are necessary for the
  263. * main application and hence must be set prior to starting the application
  264. *
  265. * @note After session establishment, the additional endpoints must be targeted
  266. * first by the client side application before sending OTA configuration,
  267. * because once OTA configuration finishes the ota service is
  268. * stopped and hence all endpoints are unregistered
  269. *
  270. * @param[in] ep_name unique name of the endpoint
  271. *
  272. * @return
  273. * - ESP_OK : Success
  274. * - ESP_FAIL : Failure
  275. */
  276. esp_err_t esp_ble_ota_endpoint_create(const char *ep_name);
  277. /**
  278. * @brief Register a handler for the previously created endpoint
  279. *
  280. * This API can be called by the application to register a protocomm handler
  281. * to any endpoint that was created using esp_ble_ota_endpoint_create().
  282. *
  283. * @note This API can only be called AFTER ota has started
  284. *
  285. * @note Additional endpoints can be used for configuring client provided
  286. * parameters, that are necessary for the
  287. * main application and hence must be set prior to starting the application
  288. *
  289. * @note After session establishment, the additional endpoints must be targeted
  290. * first by the client side application before sending OTA configuration,
  291. * because once OTA configuration finishes the ota service is
  292. * stopped and hence all endpoints are unregistered
  293. *
  294. * @param[in] ep_name Name of the endpoint
  295. * @param[in] handler Endpoint handler function
  296. * @param[in] user_ctx User data
  297. *
  298. * @return
  299. * - ESP_OK : Success
  300. * - ESP_FAIL : Failure
  301. */
  302. esp_err_t esp_ble_ota_endpoint_register(const char *ep_name,
  303. protocomm_req_handler_t handler,
  304. void *user_ctx);
  305. /**
  306. * @brief Unregister the handler for an endpoint
  307. *
  308. * This API can be called if the application wants to selectively
  309. * unregister the handler of an endpoint while the OTA
  310. * is still in progress.
  311. *
  312. * All the endpoint handlers are unregistered automatically when
  313. * the OTA stops.
  314. *
  315. * @param[in] ep_name Name of the endpoint
  316. */
  317. void esp_ble_ota_endpoint_unregister(const char *ep_name);
  318. /**
  319. * @brief Register the callback handler for writing data to partition
  320. *
  321. * This API is called by handlers once data is decrpyted and unpacked
  322. * and to be written on OTA partition.
  323. *
  324. * @param[in] buf Data to be written on OTA partition
  325. * @param[in] length Length of data to be written
  326. */
  327. void ota_recv_fw_cb(uint8_t *buf, uint32_t length);
  328. #ifdef __cplusplus
  329. }
  330. #endif
  331. #endif /* CONFIG_OTA_WITH_PROTOCOMM */
  332. #endif