esp_local_ctrl.c 15 KB

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