esp_local_ctrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <esp_err.h>
  9. #include <esp_log.h>
  10. #include <protocomm.h>
  11. #include <protocomm_security0.h>
  12. #include <protocomm_security1.h>
  13. #include <protocomm_security2.h>
  14. #include <esp_local_ctrl.h>
  15. #include "esp_local_ctrl_priv.h"
  16. #include "esp_local_ctrl.pb-c.h"
  17. #define ESP_LOCAL_CTRL_VERSION "v1.0"
  18. struct inst_ctx {
  19. protocomm_t *pc;
  20. esp_local_ctrl_config_t config;
  21. esp_local_ctrl_prop_t **props;
  22. size_t props_count;
  23. };
  24. struct inst_ctx *local_ctrl_inst_ctx;
  25. static const char *TAG = "esp_local_ctrl";
  26. esp_err_t esp_local_ctrl_start(const esp_local_ctrl_config_t *config)
  27. {
  28. esp_err_t ret;
  29. if (!config) {
  30. ESP_LOGE(TAG, "NULL configuration provided");
  31. return ESP_ERR_INVALID_ARG;
  32. }
  33. if (!config->transport) {
  34. ESP_LOGE(TAG, "No transport provided");
  35. return ESP_ERR_INVALID_ARG;
  36. }
  37. if (config->max_properties == 0) {
  38. ESP_LOGE(TAG, "max_properties must be greater than 0");
  39. return ESP_ERR_INVALID_ARG;
  40. }
  41. if (!config->handlers.get_prop_values ||
  42. !config->handlers.set_prop_values) {
  43. ESP_LOGE(TAG, "Handlers cannot be null");
  44. return ESP_ERR_INVALID_ARG;
  45. }
  46. if (local_ctrl_inst_ctx) {
  47. ESP_LOGW(TAG, "Service already active");
  48. return ESP_ERR_INVALID_STATE;
  49. }
  50. local_ctrl_inst_ctx = calloc(1, sizeof(struct inst_ctx));
  51. if (!local_ctrl_inst_ctx) {
  52. ESP_LOGE(TAG, "Failed to allocate memory for instance");
  53. return ESP_ERR_NO_MEM;
  54. }
  55. memcpy(&local_ctrl_inst_ctx->config, config, sizeof(local_ctrl_inst_ctx->config));
  56. local_ctrl_inst_ctx->props = calloc(local_ctrl_inst_ctx->config.max_properties,
  57. sizeof(esp_local_ctrl_prop_t *));
  58. if (!local_ctrl_inst_ctx->props) {
  59. ESP_LOGE(TAG, "Failed to allocate memory for properties");
  60. free(local_ctrl_inst_ctx);
  61. local_ctrl_inst_ctx = NULL;
  62. return ESP_ERR_NO_MEM;
  63. }
  64. /* Since the config structure will be different for different transport modes, each transport may
  65. * implement a `copy_config()` function, which accepts a configuration structure as input and
  66. * creates a copy of that, which can be kept in the context structure of the `esp_local_ctrl` instance.
  67. * This copy can be later be freed using `free_config()` */
  68. if (config->transport->copy_config) {
  69. ret = config->transport->copy_config(&local_ctrl_inst_ctx->config.transport_config,
  70. &config->transport_config);
  71. if (ret != ESP_OK) {
  72. esp_local_ctrl_stop();
  73. return ret;
  74. }
  75. }
  76. /* For a selected transport mode, endpoints may need to be declared prior to starting the
  77. * `esp_local_ctrl` service, e.g. in case of BLE. By declaration it means that the transport layer
  78. * allocates some resources for an endpoint, and later, after service has started, a handler
  79. * is assigned for that endpoint */
  80. if (config->transport->declare_ep) {
  81. /* UUIDs are 16bit unique IDs for each endpoint. This may or may not be relevant for
  82. * a chosen transport. We reserve all values from FF50 to FFFF for the internal endpoints.
  83. * The remaining endpoints can be used by the application for its own custom endpoints */
  84. uint16_t start_uuid = 0xFF50;
  85. ret = config->transport->declare_ep(&local_ctrl_inst_ctx->config.transport_config,
  86. "esp_local_ctrl/version", start_uuid++);
  87. if (ret != ESP_OK) {
  88. esp_local_ctrl_stop();
  89. return ret;
  90. }
  91. ret = config->transport->declare_ep(&local_ctrl_inst_ctx->config.transport_config,
  92. "esp_local_ctrl/session", start_uuid++);
  93. if (ret != ESP_OK) {
  94. esp_local_ctrl_stop();
  95. return ret;
  96. }
  97. ret = config->transport->declare_ep(&local_ctrl_inst_ctx->config.transport_config,
  98. "esp_local_ctrl/control", start_uuid++);
  99. if (ret != ESP_OK) {
  100. esp_local_ctrl_stop();
  101. return ret;
  102. }
  103. }
  104. local_ctrl_inst_ctx->pc = protocomm_new();
  105. if (!local_ctrl_inst_ctx->pc) {
  106. ESP_LOGE(TAG, "Failed to create new protocomm instance");
  107. esp_local_ctrl_stop();
  108. return ESP_FAIL;
  109. }
  110. if (config->transport->start_service) {
  111. ret = config->transport->start_service(local_ctrl_inst_ctx->pc,
  112. &local_ctrl_inst_ctx->config.transport_config);
  113. if (ret != ESP_OK) {
  114. esp_local_ctrl_stop();
  115. return ret;
  116. }
  117. }
  118. ret = protocomm_set_version(local_ctrl_inst_ctx->pc, "esp_local_ctrl/version",
  119. ESP_LOCAL_CTRL_VERSION);
  120. if (ret != ESP_OK) {
  121. ESP_LOGE(TAG, "Failed to set version endpoint");
  122. esp_local_ctrl_stop();
  123. return ret;
  124. }
  125. protocomm_security_t *proto_sec_handle;
  126. switch (local_ctrl_inst_ctx->config.proto_sec.version) {
  127. case PROTOCOM_SEC_CUSTOM:
  128. proto_sec_handle = local_ctrl_inst_ctx->config.proto_sec.custom_handle;
  129. break;
  130. case PROTOCOM_SEC1:
  131. proto_sec_handle = (protocomm_security_t *) &protocomm_security1;
  132. break;
  133. case PROTOCOM_SEC2:
  134. proto_sec_handle = (protocomm_security_t *) &protocomm_security2;
  135. break;
  136. case PROTOCOM_SEC0:
  137. default:
  138. proto_sec_handle = (protocomm_security_t *) &protocomm_security0;
  139. break;
  140. }
  141. ret = protocomm_set_security(local_ctrl_inst_ctx->pc, "esp_local_ctrl/session",
  142. proto_sec_handle, local_ctrl_inst_ctx->config.proto_sec.sec_params);
  143. if (ret != ESP_OK) {
  144. ESP_LOGE(TAG, "Failed to set session endpoint");
  145. esp_local_ctrl_stop();
  146. return ret;
  147. }
  148. ret = protocomm_add_endpoint(local_ctrl_inst_ctx->pc, "esp_local_ctrl/control",
  149. esp_local_ctrl_data_handler, NULL);
  150. if (ret != ESP_OK) {
  151. ESP_LOGE(TAG, "Failed to set control endpoint");
  152. esp_local_ctrl_stop();
  153. return ret;
  154. }
  155. return ESP_OK;
  156. }
  157. esp_err_t esp_local_ctrl_stop(void)
  158. {
  159. if (local_ctrl_inst_ctx) {
  160. if (local_ctrl_inst_ctx->config.transport->free_config) {
  161. local_ctrl_inst_ctx->config.transport->free_config(&local_ctrl_inst_ctx->config.transport_config);
  162. }
  163. if (local_ctrl_inst_ctx->pc) {
  164. if (local_ctrl_inst_ctx->config.transport->stop_service) {
  165. local_ctrl_inst_ctx->config.transport->stop_service(local_ctrl_inst_ctx->pc);
  166. }
  167. protocomm_delete(local_ctrl_inst_ctx->pc);
  168. }
  169. if (local_ctrl_inst_ctx->config.handlers.usr_ctx_free_fn) {
  170. local_ctrl_inst_ctx->config.handlers.usr_ctx_free_fn(
  171. local_ctrl_inst_ctx->config.handlers.usr_ctx);
  172. }
  173. /* Iterate through all properties one by one and free them */
  174. for (uint32_t i = 0; i < local_ctrl_inst_ctx->config.max_properties; i++) {
  175. if (local_ctrl_inst_ctx->props[i] == NULL) {
  176. continue;
  177. }
  178. /* Release memory allocated for property data */
  179. free(local_ctrl_inst_ctx->props[i]->name);
  180. if (local_ctrl_inst_ctx->props[i]->ctx_free_fn) {
  181. local_ctrl_inst_ctx->props[i]->ctx_free_fn(local_ctrl_inst_ctx->props[i]->ctx);
  182. }
  183. free(local_ctrl_inst_ctx->props[i]);
  184. }
  185. free(local_ctrl_inst_ctx->props);
  186. free(local_ctrl_inst_ctx);
  187. local_ctrl_inst_ctx = NULL;
  188. }
  189. return ESP_OK;
  190. }
  191. static int esp_local_ctrl_get_property_index(const char *name)
  192. {
  193. if (!local_ctrl_inst_ctx || !name) {
  194. return -1;
  195. }
  196. /* Iterate through all properties one by one
  197. * and find the one with matching name */
  198. for (uint32_t i = 0; i < local_ctrl_inst_ctx->props_count; i++) {
  199. if (strcmp(local_ctrl_inst_ctx->props[i]->name, name) == 0) {
  200. return i;
  201. }
  202. }
  203. return -1;
  204. }
  205. esp_err_t esp_local_ctrl_add_property(const esp_local_ctrl_prop_t *prop)
  206. {
  207. if (!local_ctrl_inst_ctx) {
  208. ESP_LOGE(TAG, "Service not running");
  209. return ESP_ERR_INVALID_STATE;
  210. }
  211. if (!prop || !prop->name) {
  212. return ESP_ERR_INVALID_ARG;
  213. }
  214. if (esp_local_ctrl_get_property_index(prop->name) >= 0) {
  215. ESP_LOGE(TAG, "Property with name %s exists", prop->name);
  216. return ESP_ERR_INVALID_STATE;
  217. }
  218. if (local_ctrl_inst_ctx->config.max_properties
  219. == local_ctrl_inst_ctx->props_count) {
  220. ESP_LOGE(TAG, "Max properties limit reached. Cannot add property %s", prop->name);
  221. return ESP_ERR_NO_MEM;
  222. }
  223. uint32_t i = local_ctrl_inst_ctx->props_count;
  224. local_ctrl_inst_ctx->props[i] = calloc(1, sizeof(esp_local_ctrl_prop_t));
  225. if (!local_ctrl_inst_ctx->props[i]) {
  226. ESP_LOGE(TAG, "Failed to allocate memory for new property %s", prop->name);
  227. return ESP_ERR_NO_MEM;
  228. }
  229. local_ctrl_inst_ctx->props[i]->name = strdup(prop->name);
  230. if (!local_ctrl_inst_ctx->props[i]->name) {
  231. ESP_LOGE(TAG, "Failed to allocate memory for property data %s", prop->name);
  232. free(local_ctrl_inst_ctx->props[i]);
  233. local_ctrl_inst_ctx->props[i] = NULL;
  234. return ESP_ERR_NO_MEM;
  235. }
  236. local_ctrl_inst_ctx->props[i]->type = prop->type;
  237. local_ctrl_inst_ctx->props[i]->size = prop->size;
  238. local_ctrl_inst_ctx->props[i]->flags = prop->flags;
  239. local_ctrl_inst_ctx->props[i]->ctx = prop->ctx;
  240. local_ctrl_inst_ctx->props[i]->ctx_free_fn = prop->ctx_free_fn;
  241. local_ctrl_inst_ctx->props_count++;
  242. return ESP_OK;
  243. }
  244. esp_err_t esp_local_ctrl_remove_property(const char *name)
  245. {
  246. int idx = esp_local_ctrl_get_property_index(name);
  247. if (idx < 0) {
  248. ESP_LOGE(TAG, "Property %s not found", name);
  249. return ESP_ERR_NOT_FOUND;
  250. }
  251. /* Release memory allocated for property data */
  252. if (local_ctrl_inst_ctx->props[idx]->ctx_free_fn) {
  253. local_ctrl_inst_ctx->props[idx]->ctx_free_fn(
  254. local_ctrl_inst_ctx->props[idx]->ctx);
  255. }
  256. free(local_ctrl_inst_ctx->props[idx]->name);
  257. free(local_ctrl_inst_ctx->props[idx]);
  258. local_ctrl_inst_ctx->props[idx++] = NULL;
  259. /* Move the following properties forward, so that there is
  260. * no empty space between two properties */
  261. for (uint32_t i = idx; i < local_ctrl_inst_ctx->props_count; i++) {
  262. if (local_ctrl_inst_ctx->props[i] == NULL) {
  263. break;
  264. }
  265. local_ctrl_inst_ctx->props[i-1] = local_ctrl_inst_ctx->props[i];
  266. }
  267. local_ctrl_inst_ctx->props_count--;
  268. return ESP_OK;
  269. }
  270. const esp_local_ctrl_prop_t *esp_local_ctrl_get_property(const char *name)
  271. {
  272. int idx = esp_local_ctrl_get_property_index(name);
  273. if (idx < 0) {
  274. ESP_LOGE(TAG, "Property %s not found", name);
  275. return NULL;
  276. }
  277. return local_ctrl_inst_ctx->props[idx];
  278. }
  279. esp_err_t esp_local_ctrl_get_prop_count(size_t *count)
  280. {
  281. if (!local_ctrl_inst_ctx) {
  282. ESP_LOGE(TAG, "Service not running");
  283. return ESP_ERR_INVALID_STATE;
  284. }
  285. if (!count) {
  286. return ESP_ERR_INVALID_ARG;
  287. }
  288. *count = local_ctrl_inst_ctx->props_count;
  289. return ESP_OK;
  290. }
  291. esp_err_t esp_local_ctrl_get_prop_values(size_t total_indices, uint32_t *indices,
  292. esp_local_ctrl_prop_t *props,
  293. esp_local_ctrl_prop_val_t *values)
  294. {
  295. if (!local_ctrl_inst_ctx) {
  296. ESP_LOGE(TAG, "Service not running");
  297. return ESP_ERR_INVALID_STATE;
  298. }
  299. if (!indices || !props || !values) {
  300. return ESP_ERR_INVALID_ARG;
  301. }
  302. /* Convert indices to names */
  303. for (size_t i = 0; i < total_indices; i++) {
  304. if (indices[i] >= local_ctrl_inst_ctx->props_count) {
  305. ESP_LOGE(TAG, "Invalid property index %d", indices[i]);
  306. return ESP_ERR_INVALID_ARG;
  307. }
  308. props[i].name = local_ctrl_inst_ctx->props[indices[i]]->name;
  309. props[i].type = local_ctrl_inst_ctx->props[indices[i]]->type;
  310. props[i].flags = local_ctrl_inst_ctx->props[indices[i]]->flags;
  311. props[i].size = local_ctrl_inst_ctx->props[indices[i]]->size;
  312. props[i].ctx = local_ctrl_inst_ctx->props[indices[i]]->ctx;
  313. }
  314. esp_local_ctrl_handlers_t *h = &local_ctrl_inst_ctx->config.handlers;
  315. esp_err_t ret = h->get_prop_values(total_indices, props, values, h->usr_ctx);
  316. /* Properties with fixed sizes need to be checked */
  317. for (size_t i = 0; i < total_indices; i++) {
  318. if (local_ctrl_inst_ctx->props[indices[i]]->size != 0) {
  319. values[i].size = local_ctrl_inst_ctx->props[indices[i]]->size;
  320. }
  321. }
  322. return ret;
  323. }
  324. esp_err_t esp_local_ctrl_set_prop_values(size_t total_indices, uint32_t *indices,
  325. const esp_local_ctrl_prop_val_t *values)
  326. {
  327. if (!local_ctrl_inst_ctx) {
  328. ESP_LOGE(TAG, "Service not running");
  329. return ESP_ERR_INVALID_STATE;
  330. }
  331. if (!indices || !values) {
  332. return ESP_ERR_INVALID_ARG;
  333. }
  334. esp_local_ctrl_prop_t *props = calloc(total_indices,
  335. sizeof(esp_local_ctrl_prop_t));
  336. if (!props) {
  337. ESP_LOGE(TAG, "Unable to allocate memory for properties array");
  338. return ESP_ERR_NO_MEM;
  339. }
  340. for (size_t i = 0; i < total_indices; i++) {
  341. if (indices[i] >= local_ctrl_inst_ctx->props_count) {
  342. ESP_LOGE(TAG, "Invalid property index %d", indices[i]);
  343. free(props);
  344. return ESP_ERR_INVALID_ARG;
  345. }
  346. /* Properties with fixed sizes need to be checked */
  347. if ((local_ctrl_inst_ctx->props[indices[i]]->size != values[i].size) &&
  348. (local_ctrl_inst_ctx->props[indices[i]]->size != 0)) {
  349. ESP_LOGE(TAG, "Invalid property size %d. Expected %d",
  350. values[i].size, local_ctrl_inst_ctx->props[indices[i]]->size);
  351. free(props);
  352. return ESP_ERR_INVALID_ARG;
  353. }
  354. props[i].name = local_ctrl_inst_ctx->props[indices[i]]->name;
  355. props[i].type = local_ctrl_inst_ctx->props[indices[i]]->type;
  356. props[i].flags = local_ctrl_inst_ctx->props[indices[i]]->flags;
  357. props[i].size = local_ctrl_inst_ctx->props[indices[i]]->size;
  358. props[i].ctx = local_ctrl_inst_ctx->props[indices[i]]->ctx;
  359. }
  360. esp_local_ctrl_handlers_t *h = &local_ctrl_inst_ctx->config.handlers;
  361. esp_err_t ret = h->set_prop_values(total_indices, props, values, h->usr_ctx);
  362. free(props);
  363. return ret;
  364. }
  365. esp_err_t esp_local_ctrl_set_handler(const char *ep_name,
  366. protocomm_req_handler_t handler,
  367. void *priv_data)
  368. {
  369. esp_err_t ret = ESP_ERR_INVALID_STATE;
  370. if (local_ctrl_inst_ctx) {
  371. ret = protocomm_add_endpoint(local_ctrl_inst_ctx->pc, ep_name,
  372. handler, priv_data);
  373. }
  374. if (ret != ESP_OK) {
  375. ESP_LOGE(TAG, "Failed to register endpoint handler");
  376. }
  377. return ret;
  378. }