esp_event.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. // Copyright 2018 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdbool.h>
  17. #include "esp_log.h"
  18. #include "esp_event.h"
  19. #include "esp_event_internal.h"
  20. #include "esp_event_private.h"
  21. #ifdef CONFIG_EVENT_LOOP_PROFILING
  22. #include "esp_timer.h"
  23. #endif
  24. /* ---------------------------- Definitions --------------------------------- */
  25. #ifdef CONFIG_EVENT_LOOP_PROFILING
  26. // loop@<address,name> rx:<total_recieved> dr:<total_dropped> inv:<total_number_of_invocations> run:<total_runtime>
  27. #define LOOP_DUMP_FORMAT "loop@%p,%s rx:%u dr:%u inv:%u run:%lld us\n"
  28. // event@<base:id> proc:<total_processed> run:<total_runtime>
  29. #define EVENT_DUMP_FORMAT "\tevent@%s:%d proc:%u run:%lld us\n"
  30. // handler@<address> inv:<total_invoked> run:<total_runtime>
  31. #define HANDLER_DUMP_FORMAT "\t\thandler@%p inv:%u run:%lld us\n"
  32. #define PRINT_DUMP_INFO(dst, sz, ...) do { \
  33. int cb = snprintf(dst, sz, __VA_ARGS__); \
  34. dst += cb; \
  35. sz -= cb; \
  36. } while(0);
  37. #endif
  38. /* ------------------------- Static Variables ------------------------------- */
  39. static const char* TAG = "event";
  40. static const char* esp_event_any_base = "any";
  41. #ifdef CONFIG_EVENT_LOOP_PROFILING
  42. static SLIST_HEAD(esp_event_loop_instance_list_t, esp_event_loop_instance) s_event_loops =
  43. SLIST_HEAD_INITIALIZER(s_event_loops);
  44. static portMUX_TYPE s_event_loops_spinlock = portMUX_INITIALIZER_UNLOCKED;
  45. #endif
  46. /* ------------------------- Static Functions ------------------------------- */
  47. #ifdef CONFIG_EVENT_LOOP_PROFILING
  48. static int esp_event_dump_prepare()
  49. {
  50. esp_event_loop_instance_t* loop_it;
  51. esp_event_base_instance_t* base_it;
  52. esp_event_id_instance_t* id_it;
  53. esp_event_handler_instance_t* handler_it;
  54. // Count the number of items to be printed. This is needed to compute how much memory to reserve.
  55. int loops = 0, events = 0, handlers = 0;
  56. portENTER_CRITICAL(&s_event_loops_spinlock);
  57. SLIST_FOREACH(loop_it, &s_event_loops, loop_entry) {
  58. SLIST_FOREACH(handler_it, &(loop_it->loop_handlers), handler_entry) {
  59. handlers++;
  60. }
  61. SLIST_FOREACH(base_it, &(loop_it->event_bases), event_base_entry) {
  62. SLIST_FOREACH(handler_it, &(base_it->base_handlers), handler_entry) {
  63. handlers++;
  64. }
  65. // Print event-level handlers
  66. SLIST_FOREACH(id_it, &(base_it->event_ids), event_id_entry) {
  67. SLIST_FOREACH(handler_it, &(id_it->handlers), handler_entry) {
  68. handlers++;
  69. }
  70. events++;
  71. }
  72. events++;
  73. }
  74. events++;
  75. loops++;
  76. }
  77. portEXIT_CRITICAL(&s_event_loops_spinlock);
  78. // Reserve slightly more memory than computed
  79. int allowance = 3;
  80. int size = (((loops + allowance) * (sizeof(LOOP_DUMP_FORMAT) + 10 + 20 + 3 * 11 + 20 )) +
  81. ((events + allowance) * (sizeof(EVENT_DUMP_FORMAT) + 10 + 20 + 11 + 20)) +
  82. ((handlers + allowance) * (sizeof(HANDLER_DUMP_FORMAT) + 10 + 11 + 20)));
  83. return size;
  84. }
  85. #endif
  86. static void esp_event_loop_run_task(void* args)
  87. {
  88. esp_err_t err;
  89. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  90. ESP_LOGD(TAG, "running task for loop %p", event_loop);
  91. while(1) {
  92. err = esp_event_loop_run(event_loop, portMAX_DELAY);
  93. if (err != ESP_OK) {
  94. break;
  95. }
  96. }
  97. ESP_LOGE(TAG, "suspended task for loop %p", event_loop);
  98. vTaskSuspend(NULL);
  99. }
  100. // Functions that operate on handler instance
  101. static esp_event_handler_instance_t* handler_instance_create(esp_event_handler_t event_handler, void* event_handler_arg)
  102. {
  103. esp_event_handler_instance_t* handler_instance = calloc(1, sizeof(*handler_instance));
  104. if (handler_instance != NULL) {
  105. handler_instance->handler = event_handler;
  106. handler_instance->arg = event_handler_arg;
  107. }
  108. return handler_instance;
  109. }
  110. static void handler_instance_delete(esp_event_handler_instance_t* handler_instance)
  111. {
  112. free(handler_instance);
  113. }
  114. // Functions that operate on handler instance list
  115. static esp_event_handler_instance_t* handler_instances_find(esp_event_handler_instances_t* handlers, esp_event_handler_t handler)
  116. {
  117. esp_event_handler_instance_t* it;
  118. SLIST_FOREACH(it, handlers, handler_entry) {
  119. if (it->handler == handler) {
  120. break;
  121. }
  122. }
  123. return it;
  124. }
  125. static void handler_instances_add(esp_event_handler_instances_t* handlers, esp_event_handler_instance_t* handler_instance)
  126. {
  127. SLIST_INSERT_HEAD(handlers, handler_instance, handler_entry);
  128. }
  129. static void handler_instances_remove(esp_event_handler_instances_t* handlers, esp_event_handler_instance_t* handler_instance)
  130. {
  131. SLIST_REMOVE(handlers, handler_instance, esp_event_handler_instance, handler_entry);
  132. handler_instance_delete(handler_instance);
  133. }
  134. static void handler_instances_remove_all(esp_event_handler_instances_t* handlers)
  135. {
  136. esp_event_handler_instance_t* it;
  137. esp_event_handler_instance_t* temp;
  138. SLIST_FOREACH_SAFE(it, handlers, handler_entry, temp) {
  139. handler_instances_remove(handlers, it);
  140. }
  141. }
  142. // Functions that operate on event id instance
  143. static void* event_id_instance_create(int32_t event_id)
  144. {
  145. esp_event_id_instance_t* event_id_instance = calloc(1, sizeof(*event_id_instance));
  146. if (event_id_instance != NULL) {
  147. event_id_instance->id = event_id;
  148. SLIST_INIT(&(event_id_instance->handlers));
  149. }
  150. return event_id_instance;
  151. }
  152. static void event_id_instance_delete(esp_event_id_instance_t* event_id_instance)
  153. {
  154. handler_instances_remove_all(&(event_id_instance->handlers));
  155. free(event_id_instance);
  156. }
  157. // Functions that operate on event id instance list
  158. static void event_id_instances_remove(esp_event_id_instances_t* head, esp_event_id_instance_t* event_id_instance)
  159. {
  160. SLIST_REMOVE(head, event_id_instance, esp_event_id_instance, event_id_entry);
  161. event_id_instance_delete(event_id_instance);
  162. }
  163. // Functions that operate on event base instance
  164. static esp_event_base_instance_t* event_base_instance_create(esp_event_base_t event_base)
  165. {
  166. esp_event_base_instance_t* event_base_instance = calloc(1, sizeof(*event_base_instance));
  167. if (event_base_instance != NULL) {
  168. event_base_instance->base = event_base;
  169. SLIST_INIT(&(event_base_instance->base_handlers));
  170. SLIST_INIT(&(event_base_instance->event_ids));
  171. }
  172. return event_base_instance;
  173. }
  174. static void event_base_instance_delete(esp_event_base_instance_t* event_base_instance)
  175. {
  176. esp_event_id_instance_t* it;
  177. esp_event_id_instance_t* temp;
  178. handler_instances_remove_all(&(event_base_instance->base_handlers));
  179. SLIST_FOREACH_SAFE(it, &(event_base_instance->event_ids), event_id_entry, temp) {
  180. event_id_instances_remove(&(event_base_instance->event_ids), it);
  181. }
  182. free(event_base_instance);
  183. }
  184. static void event_base_instance_add_event_id_instance(esp_event_base_instance_t* event_base_instance, esp_event_id_instance_t* event_id_instance)
  185. {
  186. SLIST_INSERT_HEAD(&(event_base_instance->event_ids), event_id_instance, event_id_entry);
  187. }
  188. static esp_event_id_instance_t* event_base_instance_find_event_id_instance(esp_event_base_instance_t* event_base_instance, int32_t event_id)
  189. {
  190. esp_event_id_instance_t* it;
  191. SLIST_FOREACH(it, &(event_base_instance->event_ids), event_id_entry) {
  192. if (it->id == event_id) {
  193. break;
  194. }
  195. }
  196. return it;
  197. }
  198. // Functions that operate on event base instances list
  199. static void event_base_instances_remove(esp_event_base_instances_t* head, esp_event_base_instance_t* event_base_instance)
  200. {
  201. SLIST_REMOVE(head, event_base_instance, esp_event_base_instance, event_base_entry);
  202. event_base_instance_delete(event_base_instance);
  203. }
  204. // Functions that operate on loop instances
  205. static void loop_add_event_base_instance(esp_event_loop_instance_t* loop, esp_event_base_instance_t* event_base_instance) {
  206. SLIST_INSERT_HEAD(&(loop->event_bases), event_base_instance, event_base_entry);
  207. }
  208. static void loop_remove_all_event_base_instance(esp_event_loop_instance_t* loop)
  209. {
  210. esp_event_base_instance_t* it;
  211. esp_event_base_instance_t* temp;
  212. SLIST_FOREACH_SAFE(it, &(loop->event_bases), event_base_entry, temp) {
  213. event_base_instances_remove(&(loop->event_bases), it);
  214. }
  215. }
  216. static esp_event_base_instance_t* loop_find_event_base_instance(esp_event_loop_instance_t* loop, esp_event_base_t event_base)
  217. {
  218. esp_event_base_instance_t* it;
  219. SLIST_FOREACH(it, &(loop->event_bases), event_base_entry) {
  220. if (it->base == event_base) {
  221. break;
  222. }
  223. }
  224. return it;
  225. }
  226. // Functions that operate on post instance
  227. static esp_err_t post_instance_create(esp_event_base_t event_base, int32_t event_id, void* event_data, int32_t event_data_size, esp_event_post_instance_t* post)
  228. {
  229. void** event_data_copy = NULL;
  230. // Make persistent copy of event data on heap.
  231. if (event_data != NULL && event_data_size != 0) {
  232. event_data_copy = calloc(1, event_data_size);
  233. if (event_data_copy == NULL) {
  234. ESP_LOGE(TAG, "alloc for post data to event %s:%d failed", event_base, event_id);
  235. return ESP_ERR_NO_MEM;
  236. }
  237. memcpy(event_data_copy, event_data, event_data_size);
  238. }
  239. post->base = event_base;
  240. post->id = event_id;
  241. post->data = event_data_copy;
  242. ESP_LOGD(TAG, "created post for event %s:%d", event_base, event_id);
  243. return ESP_OK;
  244. }
  245. static void post_instance_delete(esp_event_post_instance_t* post)
  246. {
  247. free(post->data);
  248. }
  249. static esp_event_handler_instances_t* find_handlers_list(esp_event_loop_instance_t* loop, esp_event_base_t event_base,
  250. int32_t event_id)
  251. {
  252. esp_event_handler_instances_t* handlers = NULL;
  253. esp_event_base_instance_t* base = NULL;
  254. esp_event_id_instance_t* event = NULL;
  255. if (event_base == esp_event_any_base && event_id == ESP_EVENT_ANY_ID) {
  256. handlers = &(loop->loop_handlers);
  257. } else {
  258. base = loop_find_event_base_instance(loop, event_base);
  259. if (base != NULL) {
  260. if (event_id == ESP_EVENT_ANY_ID) {
  261. handlers = &(base->base_handlers);
  262. } else {
  263. event = event_base_instance_find_event_id_instance(base, event_id);
  264. if (event != NULL) {
  265. handlers = &(event->handlers);
  266. }
  267. }
  268. }
  269. }
  270. return handlers;
  271. }
  272. /* ---------------------------- Public API --------------------------------- */
  273. esp_err_t esp_event_loop_create(const esp_event_loop_args_t* event_loop_args, esp_event_loop_handle_t* event_loop)
  274. {
  275. assert(event_loop_args);
  276. esp_event_loop_instance_t* loop;
  277. esp_err_t err = ESP_ERR_NO_MEM; // most likely error
  278. loop = calloc(1, sizeof(*loop));
  279. if (loop == NULL) {
  280. ESP_LOGE(TAG, "alloc for event loop failed");
  281. goto on_err;
  282. }
  283. loop->queue = xQueueCreate(event_loop_args->queue_size , sizeof(esp_event_post_instance_t));
  284. if (loop->queue == NULL) {
  285. ESP_LOGE(TAG, "create event loop queue failed");
  286. goto on_err;
  287. }
  288. loop->mutex = xSemaphoreCreateRecursiveMutex();
  289. if (loop->mutex == NULL) {
  290. ESP_LOGE(TAG, "create event loop mutex failed");
  291. goto on_err;
  292. }
  293. #ifdef CONFIG_EVENT_LOOP_PROFILING
  294. loop->profiling_mutex = xSemaphoreCreateMutex();
  295. if (loop->profiling_mutex == NULL) {
  296. ESP_LOGE(TAG, "create event loop profiling mutex failed");
  297. goto on_err;
  298. }
  299. #endif
  300. SLIST_INIT(&(loop->loop_handlers));
  301. SLIST_INIT(&(loop->event_bases));
  302. // Create the loop task if requested
  303. if (event_loop_args->task_name != NULL) {
  304. BaseType_t task_created = xTaskCreatePinnedToCore(esp_event_loop_run_task, event_loop_args->task_name,
  305. event_loop_args->task_stack_size, (void*) loop,
  306. event_loop_args->task_priority, &(loop->task), event_loop_args->task_core_id);
  307. if (task_created != pdPASS) {
  308. ESP_LOGE(TAG, "create task for loop failed");
  309. err = ESP_FAIL;
  310. goto on_err;
  311. }
  312. loop->name = event_loop_args->task_name;
  313. ESP_LOGD(TAG, "created task for loop %p", loop);
  314. } else {
  315. loop->name = "";
  316. loop->task = NULL;
  317. }
  318. loop->running_task = NULL;
  319. #ifdef CONFIG_EVENT_LOOP_PROFILING
  320. portENTER_CRITICAL(&s_event_loops_spinlock);
  321. SLIST_INSERT_HEAD(&s_event_loops, loop, loop_entry);
  322. portEXIT_CRITICAL(&s_event_loops_spinlock);
  323. #endif
  324. *event_loop = (esp_event_loop_handle_t) loop;
  325. ESP_LOGD(TAG, "created event loop %p", loop);
  326. return ESP_OK;
  327. on_err:
  328. if(loop->queue != NULL) {
  329. vQueueDelete(loop->queue);
  330. }
  331. if(loop->mutex != NULL) {
  332. vSemaphoreDelete(loop->mutex);
  333. }
  334. #ifdef CONFIG_EVENT_LOOP_PROFILING
  335. if(loop->profiling_mutex != NULL) {
  336. vSemaphoreDelete(loop->profiling_mutex);
  337. }
  338. #endif
  339. free(loop);
  340. return err;
  341. }
  342. // On event lookup performance: The library implements the event list as a linked list, which results to O(n)
  343. // lookup time. The test comparing this implementation to the O(lg n) performance of rbtrees
  344. // (https://github.com/freebsd/freebsd/blob/master/sys/sys/tree.h)
  345. // indicate that the difference is not that substantial, especially considering the additional
  346. // pointers per node of rbtrees. Code for the rbtree implementation of the event loop library is archived
  347. // in feature/esp_event_loop_library_rbtrees if needed.
  348. esp_err_t esp_event_loop_run(esp_event_loop_handle_t event_loop, TickType_t ticks_to_run)
  349. {
  350. assert(event_loop);
  351. esp_event_loop_instance_t* loop = (esp_event_loop_instance_t*) event_loop;
  352. esp_event_post_instance_t post;
  353. TickType_t marker = xTaskGetTickCount();
  354. TickType_t end = 0;
  355. esp_event_handler_instance_t* temp;
  356. #if( configUSE_16_BIT_TICKS == 1 )
  357. int32_t remaining_ticks = ticks_to_run;
  358. #else
  359. int64_t remaining_ticks = ticks_to_run;
  360. #endif
  361. while(xQueueReceive(loop->queue, &post, ticks_to_run) == pdTRUE) {
  362. esp_event_base_instance_t* base = NULL;
  363. esp_event_id_instance_t* event = NULL;
  364. // Reserve space for three possible matches: (1) the entry for handlers registered to all events in the loop, the
  365. // (2) entry matching events with a specified base and (3) the entry matching both base and id.
  366. #define LOOP_LEVEL_HANDLER 0
  367. #define BASE_LEVEL_HANDLER 1
  368. #define EVENT_LEVEL_HANDLER 2
  369. esp_event_handler_instances_t* handlers_list[EVENT_LEVEL_HANDLER + 1] = {0};
  370. // The event has already been unqueued, so ensure it gets executed.
  371. xSemaphoreTakeRecursive(loop->mutex, portMAX_DELAY);
  372. loop->running_task = xTaskGetCurrentTaskHandle();
  373. handlers_list[LOOP_LEVEL_HANDLER] = &(loop->loop_handlers);
  374. base = loop_find_event_base_instance(loop, post.base);
  375. if (base) {
  376. event = event_base_instance_find_event_id_instance(base, post.id);
  377. handlers_list[BASE_LEVEL_HANDLER] = &(base->base_handlers);
  378. if (event) {
  379. handlers_list[EVENT_LEVEL_HANDLER] = &(event->handlers);
  380. }
  381. }
  382. bool exec = false;
  383. for (int i = LOOP_LEVEL_HANDLER; i <= EVENT_LEVEL_HANDLER; i++) {
  384. if (handlers_list[i] != NULL) {
  385. esp_event_handler_instance_t* it;
  386. SLIST_FOREACH_SAFE(it, handlers_list[i], handler_entry, temp) {
  387. ESP_LOGD(TAG, "running post %s:%d with handler %p on loop %p", post.base, post.id, it->handler, event_loop);
  388. #ifdef CONFIG_EVENT_LOOP_PROFILING
  389. int64_t start, diff;
  390. start = esp_timer_get_time();
  391. #endif
  392. // Execute the handler
  393. (*(it->handler))(it->arg, post.base, post.id, post.data);
  394. #ifdef CONFIG_EVENT_LOOP_PROFILING
  395. diff = esp_timer_get_time() - start;
  396. xSemaphoreTake(loop->profiling_mutex, portMAX_DELAY);
  397. it->total_times_invoked++;
  398. it->total_runtime += diff;
  399. if (i == LOOP_LEVEL_HANDLER) {
  400. loop->loop_handlers_invoked++;
  401. loop->loop_handlers_runtime += diff;
  402. } else if (i == BASE_LEVEL_HANDLER) {
  403. base->base_handlers_invoked++;
  404. base->base_handlers_runtime += diff;
  405. } else {
  406. event->handlers_invoked++;
  407. event->handlers_runtime += diff;
  408. }
  409. loop->total_handlers_invoked++;
  410. loop->total_handlers_runtime += diff;
  411. xSemaphoreGive(loop->profiling_mutex);
  412. #endif
  413. }
  414. }
  415. exec |= true;
  416. }
  417. if (ticks_to_run != portMAX_DELAY) {
  418. end = xTaskGetTickCount();
  419. remaining_ticks -= end - marker;
  420. // If the ticks to run expired, return to the caller
  421. if (remaining_ticks <= 0) {
  422. xSemaphoreGiveRecursive(loop->mutex);
  423. break;
  424. } else {
  425. marker = end;
  426. }
  427. }
  428. loop->running_task = NULL;
  429. xSemaphoreGiveRecursive(loop->mutex);
  430. if (!exec) {
  431. // No handlers were registered, not even loop/base level handlers
  432. ESP_LOGW(TAG, "no handlers have been registered for event %s:%d posted to loop %p", post.base, post.id, event_loop);
  433. }
  434. }
  435. return ESP_OK;
  436. }
  437. esp_err_t esp_event_loop_delete(esp_event_loop_handle_t event_loop)
  438. {
  439. assert(event_loop);
  440. esp_event_loop_instance_t* loop = (esp_event_loop_instance_t*) event_loop;
  441. SemaphoreHandle_t loop_mutex = loop->mutex;
  442. xSemaphoreTakeRecursive(loop->mutex, portMAX_DELAY);
  443. #ifdef CONFIG_EVENT_LOOP_PROFILING
  444. portENTER_CRITICAL(&s_event_loops_spinlock);
  445. SLIST_REMOVE(&s_event_loops, loop, esp_event_loop_instance, loop_entry);
  446. portEXIT_CRITICAL(&s_event_loops_spinlock);
  447. #endif
  448. // Delete the task if it was created
  449. if (loop->task != NULL) {
  450. vTaskDelete(loop->task);
  451. }
  452. // Remove all registered events in the loop
  453. handler_instances_remove_all(&(loop->loop_handlers));
  454. loop_remove_all_event_base_instance(loop);
  455. // Drop existing posts on the queue
  456. esp_event_post_instance_t post;
  457. while(xQueueReceive(loop->queue, &post, 0) == pdTRUE) {
  458. free(post.data);
  459. }
  460. // Cleanup loop
  461. vQueueDelete(loop->queue);
  462. free(loop);
  463. // Free loop mutex before deleting
  464. xSemaphoreGiveRecursive(loop_mutex);
  465. vSemaphoreDelete(loop_mutex);
  466. ESP_LOGD(TAG, "deleted loop %p", (void*) event_loop);
  467. return ESP_OK;
  468. }
  469. esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop, esp_event_base_t event_base,
  470. int32_t event_id, esp_event_handler_t event_handler, void* event_handler_arg)
  471. {
  472. assert(event_loop);
  473. assert(event_handler);
  474. if (event_base == ESP_EVENT_ANY_BASE && event_id != ESP_EVENT_ANY_ID) {
  475. ESP_LOGE(TAG, "registering to any event base with specific id unsupported");
  476. return ESP_ERR_INVALID_ARG;
  477. }
  478. esp_event_loop_instance_t* loop = (esp_event_loop_instance_t*) event_loop;
  479. esp_event_base_instance_t* base = NULL;
  480. esp_event_id_instance_t* event = NULL;
  481. esp_event_handler_instance_t* handler = NULL;
  482. esp_event_handler_instances_t* handlers = NULL;
  483. bool base_created = false;
  484. bool event_created = false;
  485. if (event_base == ESP_EVENT_ANY_BASE) {
  486. event_base = esp_event_any_base;
  487. }
  488. xSemaphoreTakeRecursive(loop->mutex, portMAX_DELAY);
  489. if (event_base == esp_event_any_base && event_id == ESP_EVENT_ANY_ID) {
  490. // Add to the loop-level handlers
  491. handlers = &(loop->loop_handlers);
  492. } else {
  493. // If base instance does not exist, create one
  494. if ((base = loop_find_event_base_instance(loop, event_base)) == NULL) {
  495. base = event_base_instance_create(event_base);
  496. if (base == NULL) {
  497. xSemaphoreGiveRecursive(loop->mutex);
  498. return ESP_ERR_NO_MEM;
  499. }
  500. base_created = true;
  501. }
  502. // Add to the event base instance level handlers
  503. if (event_id == ESP_EVENT_ANY_ID) {
  504. handlers = &(base->base_handlers);
  505. } else {
  506. if (base_created ||
  507. (event = event_base_instance_find_event_id_instance(base, event_id)) == NULL) {
  508. event = event_id_instance_create(event_id);
  509. // If it does not exist, create one
  510. if (event == NULL) {
  511. if (base_created) {
  512. event_base_instance_delete(base);
  513. }
  514. xSemaphoreGiveRecursive(loop->mutex);
  515. return ESP_ERR_NO_MEM;
  516. }
  517. event_created = true;
  518. }
  519. // Add to the event id instance level handlers
  520. handlers = &(event->handlers);
  521. }
  522. }
  523. // Add handler to the list
  524. if (base_created || event_created ||
  525. (handler = handler_instances_find(handlers, event_handler)) == NULL) {
  526. handler = handler_instance_create(event_handler, event_handler_arg);
  527. if (handler == NULL) {
  528. if (event_created) {
  529. event_id_instance_delete(event);
  530. }
  531. if (base_created) {
  532. event_base_instance_delete(base);
  533. }
  534. xSemaphoreGiveRecursive(loop->mutex);
  535. return ESP_ERR_NO_MEM;
  536. }
  537. handler_instances_add(handlers, handler);
  538. // If a new event base/ event id instance was created, add them to the appropriate list
  539. if (event_created) {
  540. event_base_instance_add_event_id_instance(base, event);
  541. }
  542. if (base_created) {
  543. loop_add_event_base_instance(loop, base);
  544. }
  545. ESP_LOGD(TAG, "registered handler %p for event %s:%d", event_handler, event_base, event_id);
  546. } else {
  547. handler->arg = event_handler_arg;
  548. ESP_LOGW(TAG, "handler %p for event %s:%d already registered, overwriting", event_handler, event_base, event_id);
  549. }
  550. xSemaphoreGiveRecursive(loop->mutex);
  551. return ESP_OK;
  552. }
  553. esp_err_t esp_event_handler_unregister_with(esp_event_loop_handle_t event_loop, esp_event_base_t event_base,
  554. int32_t event_id, esp_event_handler_t event_handler)
  555. {
  556. assert(event_loop);
  557. assert(event_handler);
  558. if (event_base == ESP_EVENT_ANY_BASE && event_id != ESP_EVENT_ANY_ID) {
  559. ESP_LOGE(TAG, "unregistering to any event base with specific id unsupported");
  560. return ESP_FAIL;
  561. }
  562. if (event_base == ESP_EVENT_ANY_BASE) {
  563. event_base = esp_event_any_base;
  564. }
  565. esp_event_loop_instance_t* loop = (esp_event_loop_instance_t*) event_loop;
  566. esp_event_handler_instance_t* handler = NULL;
  567. esp_event_handler_instances_t* handlers = find_handlers_list(loop, event_base, event_id);
  568. xSemaphoreTakeRecursive(loop->mutex, portMAX_DELAY);
  569. if (handlers != NULL &&
  570. (handler = handler_instances_find(handlers, event_handler)) != NULL) {
  571. handler_instances_remove(handlers, handler);
  572. ESP_LOGD(TAG, "unregistered handler %p from event %s:%d", event_handler, event_base, event_id);
  573. } else {
  574. ESP_LOGW(TAG, "handler %p for event %s:%d not registered, ignoring", event_handler, event_base, event_id);
  575. }
  576. xSemaphoreGiveRecursive(loop->mutex);
  577. return ESP_OK;
  578. }
  579. esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop, esp_event_base_t event_base, int32_t event_id,
  580. void* event_data, size_t event_data_size, TickType_t ticks_to_wait)
  581. {
  582. assert(event_loop);
  583. if (event_base == ESP_EVENT_ANY_BASE || event_id == ESP_EVENT_ANY_ID) {
  584. ESP_LOGE(TAG, "posting nonspecific event base or id unsupported");
  585. return ESP_ERR_INVALID_ARG;
  586. }
  587. esp_event_loop_instance_t* loop = (esp_event_loop_instance_t*) event_loop;
  588. esp_event_post_instance_t post;
  589. esp_err_t err = post_instance_create(event_base, event_id, event_data, event_data_size, &post);
  590. if (err != ESP_OK) {
  591. return err;
  592. }
  593. BaseType_t result = pdFALSE;
  594. // Find the task that currently executes the loop. It is safe to query loop->task since it is
  595. // not mutated since loop creation. ENSURE THIS REMAINS TRUE.
  596. if (loop->task == NULL) {
  597. // The loop has no dedicated task. Find out what task is currently running it.
  598. result = xSemaphoreTakeRecursive(loop->mutex, ticks_to_wait);
  599. if (result == pdTRUE) {
  600. if (loop->running_task != xTaskGetCurrentTaskHandle()) {
  601. xSemaphoreGiveRecursive(loop->mutex);
  602. result = xQueueSendToBack(loop->queue, &post, ticks_to_wait);
  603. } else {
  604. xSemaphoreGiveRecursive(loop->mutex);
  605. result = xQueueSendToBack(loop->queue, &post, 0);
  606. }
  607. }
  608. } else {
  609. // The loop has a dedicated task.
  610. if (loop->task != xTaskGetCurrentTaskHandle()) {
  611. result = xQueueSendToBack(loop->queue, &post, ticks_to_wait);
  612. } else {
  613. result = xQueueSendToBack(loop->queue, &post, 0);
  614. }
  615. }
  616. if (result != pdTRUE) {
  617. post_instance_delete(&post);
  618. #ifdef CONFIG_EVENT_LOOP_PROFILING
  619. xSemaphoreTake(loop->profiling_mutex, portMAX_DELAY);
  620. loop->events_dropped++;
  621. xSemaphoreGive(loop->profiling_mutex);
  622. #endif
  623. return ESP_ERR_TIMEOUT;
  624. }
  625. #ifdef CONFIG_EVENT_LOOP_PROFILING
  626. xSemaphoreTake(loop->profiling_mutex, portMAX_DELAY);
  627. loop->events_recieved++;
  628. xSemaphoreGive(loop->profiling_mutex);
  629. #endif
  630. ESP_LOGD(TAG, "posted %s:%d to loop %p", post.base, post.id, event_loop);
  631. return ESP_OK;
  632. }
  633. esp_err_t esp_event_dump(FILE* file)
  634. {
  635. #ifdef CONFIG_EVENT_LOOP_PROFILING
  636. assert(file);
  637. esp_event_loop_instance_t* loop_it;
  638. esp_event_base_instance_t* base_it;
  639. esp_event_id_instance_t* id_it;
  640. esp_event_handler_instance_t* handler_it;
  641. // Allocate memory for printing
  642. int sz = esp_event_dump_prepare();
  643. char* buf = calloc(sz, sizeof(char));
  644. char* dst = buf;
  645. // Print info to buffer
  646. portENTER_CRITICAL(&s_event_loops_spinlock);
  647. SLIST_FOREACH(loop_it, &s_event_loops, loop_entry) {
  648. PRINT_DUMP_INFO(dst, sz, LOOP_DUMP_FORMAT, loop_it, loop_it->name, loop_it->events_recieved,
  649. loop_it->events_dropped, loop_it->total_handlers_invoked, loop_it->total_handlers_runtime);
  650. // Print loop-level handler
  651. PRINT_DUMP_INFO(dst, sz, esp_event_any_base, ESP_EVENT_ANY_ID, loop_it->loop_handlers_invoked,
  652. loop_it->loop_handlers_runtime);
  653. SLIST_FOREACH(handler_it, &(loop_it->loop_handlers), handler_entry) {
  654. PRINT_DUMP_INFO(dst, sz, HANDLER_DUMP_FORMAT, handler_it->handler, handler_it->total_times_invoked,
  655. handler_it->total_runtime);
  656. }
  657. SLIST_FOREACH(base_it, &(loop_it->event_bases), event_base_entry) {
  658. // Print base-level handler
  659. PRINT_DUMP_INFO(dst, sz, EVENT_DUMP_FORMAT, base_it->base, ESP_EVENT_ANY_ID,
  660. base_it->base_handlers_invoked, base_it->base_handlers_runtime);
  661. SLIST_FOREACH(handler_it, &(base_it->base_handlers), handler_entry) {
  662. PRINT_DUMP_INFO(dst, sz, HANDLER_DUMP_FORMAT, handler_it->handler,
  663. handler_it->total_times_invoked, handler_it->total_runtime);
  664. }
  665. // Print event-level handlers
  666. SLIST_FOREACH(id_it, &(base_it->event_ids), event_id_entry) {
  667. PRINT_DUMP_INFO(dst, sz, EVENT_DUMP_FORMAT, base_it->base, id_it->id,
  668. id_it->handlers_invoked, id_it->handlers_runtime);
  669. SLIST_FOREACH(handler_it, &(id_it->handlers), handler_entry) {
  670. PRINT_DUMP_INFO(dst, sz, HANDLER_DUMP_FORMAT, handler_it->handler,
  671. handler_it->total_times_invoked, handler_it->total_runtime);
  672. }
  673. }
  674. }
  675. }
  676. portEXIT_CRITICAL(&s_event_loops_spinlock);
  677. // Print the contents of the buffer to the file
  678. fprintf(file, buf);
  679. // Free the allocated buffer
  680. free(buf);
  681. #endif
  682. return ESP_OK;
  683. }