monitor.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/timers.h"
  13. #include "esp_system.h"
  14. #include "esp_log.h"
  15. #include "monitor.h"
  16. #include "driver/gpio.h"
  17. #include "buttons.h"
  18. #include "led.h"
  19. #include "globdefs.h"
  20. #include "platform_config.h"
  21. #include "accessors.h"
  22. #include "messaging.h"
  23. #include "cJSON.h"
  24. #include "tools.h"
  25. #define MONITOR_TIMER (10*1000)
  26. #define SCRATCH_SIZE 256
  27. static const char *TAG = "monitor";
  28. static TimerHandle_t monitor_timer;
  29. static monitor_gpio_t jack = { CONFIG_JACK_GPIO, 0 };
  30. static monitor_gpio_t spkfault = { CONFIG_SPKFAULT_GPIO, 0 };
  31. void (*jack_handler_svc)(bool inserted);
  32. bool jack_inserted_svc(void);
  33. void (*spkfault_handler_svc)(bool inserted);
  34. bool spkfault_svc(void);
  35. /****************************************************************************************
  36. *
  37. */
  38. static void task_stats( cJSON* top ) {
  39. #ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY
  40. #pragma message("Compiled with trace facility")
  41. static struct {
  42. TaskStatus_t *tasks;
  43. uint32_t total, n;
  44. } current, previous;
  45. cJSON * tlist=cJSON_CreateArray();
  46. current.n = uxTaskGetNumberOfTasks();
  47. current.tasks = malloc_init_external( current.n * sizeof( TaskStatus_t ) );
  48. current.n = uxTaskGetSystemState( current.tasks, current.n, &current.total );
  49. cJSON_AddNumberToObject(top,"ntasks",current.n);
  50. static EXT_RAM_ATTR char scratch[SCRATCH_SIZE];
  51. *scratch = '\0';
  52. #ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
  53. #pragma message("Compiled with runtime stats")
  54. uint32_t elapsed = current.total - previous.total;
  55. for(int i = 0, n = 0; i < current.n; i++ ) {
  56. for (int j = 0; j < previous.n; j++) {
  57. if (current.tasks[i].xTaskNumber == previous.tasks[j].xTaskNumber) {
  58. n += snprintf(scratch + n, SCRATCH_SIZE - n, "%16s (%u) %2u%% s:%5u", current.tasks[i].pcTaskName,
  59. current.tasks[i].eCurrentState,
  60. 100 * (current.tasks[i].ulRunTimeCounter - previous.tasks[j].ulRunTimeCounter) / elapsed,
  61. current.tasks[i].usStackHighWaterMark);
  62. cJSON * t=cJSON_CreateObject();
  63. cJSON_AddNumberToObject(t,"cpu",100 * (current.tasks[i].ulRunTimeCounter - previous.tasks[j].ulRunTimeCounter) / elapsed);
  64. cJSON_AddNumberToObject(t,"minstk",current.tasks[i].usStackHighWaterMark);
  65. cJSON_AddNumberToObject(t,"bprio",current.tasks[i].uxBasePriority);
  66. cJSON_AddNumberToObject(t,"cprio",current.tasks[i].uxCurrentPriority);
  67. cJSON_AddStringToObject(t,"nme",current.tasks[i].pcTaskName);
  68. cJSON_AddNumberToObject(t,"st",current.tasks[i].eCurrentState);
  69. cJSON_AddNumberToObject(t,"num",current.tasks[i].xTaskNumber);
  70. cJSON_AddItemToArray(tlist,t);
  71. if (i % 3 == 2 || i == current.n - 1) {
  72. ESP_LOGI(TAG, "%s", scratch);
  73. n = 0;
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. #else
  80. #pragma message("Compiled WITHOUT runtime stats")
  81. for (int i = 0, n = 0; i < current.n; i ++) {
  82. n += sprintf(scratch + n, "%16s s:%5u\t", current.tasks[i].pcTaskName, current.tasks[i].usStackHighWaterMark);
  83. cJSON * t=cJSON_CreateObject();
  84. cJSON_AddNumberToObject(t,"minstk",current.tasks[i].usStackHighWaterMark);
  85. cJSON_AddNumberToObject(t,"bprio",current.tasks[i].uxBasePriority);
  86. cJSON_AddNumberToObject(t,"cprio",current.tasks[i].uxCurrentPriority);
  87. cJSON_AddStringToObject(t,"nme",current.tasks[i].pcTaskName);
  88. cJSON_AddNumberToObject(t,"st",current.tasks[i].eCurrentState);
  89. cJSON_AddNumberToObject(t,"num",current.tasks[i].xTaskNumber);
  90. cJSON_AddItemToArray(tlist,t);
  91. if (i % 3 == 2 || i == current.n - 1) {
  92. ESP_LOGI(TAG, "%s", scratch);
  93. n = 0;
  94. }
  95. }
  96. #endif
  97. cJSON_AddItemToObject(top,"tasks",tlist);
  98. if (previous.tasks) free(previous.tasks);
  99. previous = current;
  100. #else
  101. #pragma message("Compiled WITHOUT trace facility")
  102. #endif
  103. }
  104. /****************************************************************************************
  105. *
  106. */
  107. static void monitor_callback(TimerHandle_t xTimer) {
  108. cJSON * top=cJSON_CreateObject();
  109. cJSON_AddNumberToObject(top,"free_iram",heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  110. cJSON_AddNumberToObject(top,"min_free_iram",heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL));
  111. cJSON_AddNumberToObject(top,"free_spiram",heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
  112. cJSON_AddNumberToObject(top,"min_free_spiram",heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  113. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu) dma:%zu (min:%zu)",
  114. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  115. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  116. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  117. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  118. heap_caps_get_free_size(MALLOC_CAP_DMA),
  119. heap_caps_get_minimum_free_size(MALLOC_CAP_DMA));
  120. task_stats(top);
  121. char * top_a= cJSON_PrintUnformatted(top);
  122. if(top_a){
  123. messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_STATS,top_a);
  124. FREE_AND_NULL(top_a);
  125. }
  126. cJSON_Delete(top);
  127. }
  128. /****************************************************************************************
  129. *
  130. */
  131. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  132. ESP_LOGI(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  133. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  134. }
  135. /****************************************************************************************
  136. *
  137. */
  138. bool jack_inserted_svc (void) {
  139. if (jack.gpio != -1) return button_is_pressed(jack.gpio, NULL);
  140. else return true;
  141. }
  142. /****************************************************************************************
  143. *
  144. */
  145. static void spkfault_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  146. ESP_LOGD(TAG, "Speaker status %s", event == BUTTON_PRESSED ? "faulty" : "normal");
  147. if (event == BUTTON_PRESSED) led_on(LED_RED);
  148. else led_off(LED_RED);
  149. if (spkfault_handler_svc) (*spkfault_handler_svc)(event == BUTTON_PRESSED);
  150. }
  151. /****************************************************************************************
  152. *
  153. */
  154. bool spkfault_svc (void) {
  155. return button_is_pressed(spkfault.gpio, NULL);
  156. }
  157. /****************************************************************************************
  158. *
  159. */
  160. #ifndef CONFIG_JACK_LOCKED
  161. static void set_jack_gpio(int gpio, char *value) {
  162. if (strcasestr(value, "jack")) {
  163. char *p;
  164. jack.gpio = gpio;
  165. if ((p = strchr(value, ':')) != NULL) jack.active = atoi(p + 1);
  166. }
  167. else {
  168. jack.gpio = -1;
  169. }
  170. }
  171. #endif
  172. /****************************************************************************************
  173. *
  174. */
  175. #ifndef CONFIG_SPKFAULT_LOCKED
  176. static void set_spkfault_gpio(int gpio, char *value) {
  177. if (strcasestr(value, "spkfault")) {
  178. char *p;
  179. spkfault.gpio = gpio;
  180. if ((p = strchr(value, ':')) != NULL) spkfault.active = atoi(p + 1);
  181. }
  182. else {
  183. spkfault.gpio = -1;
  184. }
  185. }
  186. #endif
  187. /****************************************************************************************
  188. *
  189. */
  190. void monitor_svc_init(void) {
  191. ESP_LOGI(TAG, "Initializing monitoring");
  192. #ifdef CONFIG_JACK_GPIO_LEVEL
  193. jack.active = CONFIG_JACK_GPIO_LEVEL;
  194. #endif
  195. #ifndef CONFIG_JACK_LOCKED
  196. parse_set_GPIO(set_jack_gpio);
  197. #endif
  198. // re-use button management for jack handler, it's a GPIO after all
  199. if (jack.gpio != -1) {
  200. ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", jack.active ? "high" : "low", jack.gpio);
  201. button_create(NULL, jack.gpio, jack.active ? BUTTON_HIGH : BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
  202. }
  203. #ifdef CONFIG_SPKFAULT_GPIO_LEVEL
  204. spkfault.active = CONFIG_SPKFAULT_GPIO_LEVEL;
  205. #endif
  206. #ifndef CONFIG_SPKFAULT_LOCKED
  207. parse_set_GPIO(set_spkfault_gpio);
  208. #endif
  209. // re-use button management for speaker fault handler, it's a GPIO after all
  210. if (spkfault.gpio != -1) {
  211. ESP_LOGI(TAG,"Adding speaker fault (%s) detection GPIO %d", spkfault.active ? "high" : "low", spkfault.gpio);
  212. button_create(NULL, spkfault.gpio, spkfault.active ? BUTTON_HIGH : BUTTON_LOW, false, 0, spkfault_handler_default, 0, -1);
  213. }
  214. // do we want stats
  215. char *p = config_alloc_get_default(NVS_TYPE_STR, "stats", "n", 0);
  216. if (p && (*p == '1' || *p == 'Y' || *p == 'y')) {
  217. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  218. xTimerStart(monitor_timer, portMAX_DELAY);
  219. }
  220. FREE_AND_NULL(p);
  221. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu) dma:%zu (min:%zu)",
  222. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  223. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  224. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  225. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  226. heap_caps_get_free_size(MALLOC_CAP_DMA),
  227. heap_caps_get_minimum_free_size(MALLOC_CAP_DMA));
  228. }
  229. /****************************************************************************************
  230. *
  231. */
  232. monitor_gpio_t * get_spkfault_gpio(){
  233. return &spkfault ;
  234. }
  235. /****************************************************************************************
  236. *
  237. */
  238. monitor_gpio_t * get_jack_insertion_gpio(){
  239. return &jack;
  240. }