monitor.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "config.h"
  21. #include "accessors.h"
  22. #include "messaging.h"
  23. #include "cJSON.h"
  24. #include "trace.h"
  25. #define MONITOR_TIMER (10*1000)
  26. static const char *TAG = "monitor";
  27. static TimerHandle_t monitor_timer;
  28. static struct {
  29. int gpio;
  30. int active;
  31. } jack = { CONFIG_JACK_GPIO, 0 },
  32. spkfault = { CONFIG_SPKFAULT_GPIO, 0 };
  33. void (*jack_handler_svc)(bool inserted);
  34. bool jack_inserted_svc(void);
  35. void (*spkfault_handler_svc)(bool inserted);
  36. bool spkfault_svc(void);
  37. /****************************************************************************************
  38. *
  39. */
  40. static void task_stats( cJSON* top ) {
  41. #ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY
  42. static struct {
  43. TaskStatus_t *tasks;
  44. uint32_t total, n;
  45. } current, previous;
  46. cJSON * tlist=cJSON_CreateArray();
  47. current.n = uxTaskGetNumberOfTasks();
  48. current.tasks = malloc( current.n * sizeof( TaskStatus_t ) );
  49. current.n = uxTaskGetSystemState( current.tasks, current.n, &current.total );
  50. cJSON_AddNumberToObject(top,"ntasks",current.n);
  51. static EXT_RAM_ATTR char scratch[128+1];
  52. *scratch = '\0';
  53. #ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_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 += sprintf(scratch + n, "%16s %2u%% s:%5u", current.tasks[i].pcTaskName,
  59. 100 * (current.tasks[i].ulRunTimeCounter - previous.tasks[j].ulRunTimeCounter) / elapsed,
  60. current.tasks[i].usStackHighWaterMark);
  61. cJSON * t=cJSON_CreateObject();
  62. cJSON_AddNumberToObject(t,"cpu",100 * (current.tasks[i].ulRunTimeCounter - previous.tasks[j].ulRunTimeCounter) / elapsed);
  63. cJSON_AddNumberToObject(t,"minstk",current.tasks[i].usStackHighWaterMark);
  64. cJSON_AddNumberToObject(t,"bprio",current.tasks[i].uxBasePriority);
  65. cJSON_AddNumberToObject(t,"cprio",current.tasks[i].uxCurrentPriority);
  66. cJSON_AddStringToObject(t,"nme",current.tasks[i].pcTaskName);
  67. cJSON_AddNumberToObject(t,"st",current.tasks[i].eCurrentState);
  68. cJSON_AddNumberToObject(t,"num",current.tasks[i].xTaskNumber);
  69. cJSON_AddItemToArray(tlist,t);
  70. if (i % 3 == 2 || i == current.n - 1) {
  71. ESP_LOGI(TAG, "%s", scratch);
  72. n = 0;
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. #else
  79. for (int i = 0, n = 0; i < current.n; i ++) {
  80. n += sprintf(scratch + n, "%16s s:%5u\t", current.tasks[i].pcTaskName, current.tasks[i].usStackHighWaterMark);
  81. cJSON * t=cJSON_CreateObject();
  82. cJSON_AddNumberToObject(t,"minstk",current.tasks[i].usStackHighWaterMark);
  83. cJSON_AddNumberToObject(t,"bprio",current.tasks[i].uxBasePriority);
  84. cJSON_AddNumberToObject(t,"cprio",current.tasks[i].uxCurrentPriority);
  85. cJSON_AddStringToObject(t,"nme",current.tasks[i].pcTaskName);
  86. cJSON_AddStringToObject(t,"st",current.tasks[i].eCurrentState);
  87. cJSON_AddNumberToObject(t,"num",current.tasks[i].xTaskNumber);
  88. cJSON_AddItemToArray(tlist,t);
  89. if (i % 3 == 2 || i == current.n - 1) {
  90. ESP_LOGI(TAG, "%s", scratch);
  91. n = 0;
  92. }
  93. }
  94. #endif
  95. cJSON_AddItemToObject(top,"tasks",tlist);
  96. if (previous.tasks) free(previous.tasks);
  97. previous = current;
  98. #endif
  99. }
  100. /****************************************************************************************
  101. *
  102. */
  103. static void monitor_callback(TimerHandle_t xTimer) {
  104. cJSON * top=cJSON_CreateObject();
  105. cJSON_AddNumberToObject(top,"free_iram",heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  106. cJSON_AddNumberToObject(top,"min_free_iram",heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL));
  107. cJSON_AddNumberToObject(top,"free_spiram",heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
  108. cJSON_AddNumberToObject(top,"min_free_spiram",heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  109. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  110. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  111. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  112. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  113. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  114. task_stats(top);
  115. char * top_a= cJSON_PrintUnformatted(top);
  116. if(top_a){
  117. messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_STATS,top_a);
  118. FREE_AND_NULL(top_a);
  119. }
  120. cJSON_free(top);
  121. }
  122. /****************************************************************************************
  123. *
  124. */
  125. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  126. ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  127. messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_SYSTEM,"jack is %s",BUTTON_PRESSED ? "inserted" : "removed");
  128. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  129. }
  130. /****************************************************************************************
  131. *
  132. */
  133. bool jack_inserted_svc (void) {
  134. return button_is_pressed(jack.gpio, NULL);
  135. }
  136. /****************************************************************************************
  137. *
  138. */
  139. static void spkfault_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  140. ESP_LOGD(TAG, "Speaker status %s", event == BUTTON_PRESSED ? "faulty" : "normal");
  141. if (event == BUTTON_PRESSED) led_on(LED_RED);
  142. else led_off(LED_RED);
  143. if (spkfault_handler_svc) (*spkfault_handler_svc)(event == BUTTON_PRESSED);
  144. }
  145. /****************************************************************************************
  146. *
  147. */
  148. bool spkfault_svc (void) {
  149. return button_is_pressed(spkfault.gpio, NULL);
  150. }
  151. /****************************************************************************************
  152. *
  153. */
  154. void set_jack_gpio(int gpio, char *value) {
  155. if (strcasestr(value, "jack")) {
  156. char *p;
  157. jack.gpio = gpio;
  158. if ((p = strchr(value, ':')) != NULL) jack.active = atoi(p + 1);
  159. }
  160. }
  161. /****************************************************************************************
  162. *
  163. */
  164. void set_spkfault_gpio(int gpio, char *value) {
  165. if (strcasestr(value, "spkfault")) {
  166. char *p;
  167. spkfault.gpio = gpio;
  168. if ((p = strchr(value, ':')) != NULL) spkfault.active = atoi(p + 1);
  169. }
  170. }
  171. /****************************************************************************************
  172. *
  173. */
  174. void monitor_svc_init(void) {
  175. ESP_LOGI(TAG, "Initializing monitoring");
  176. #ifdef CONFIG_JACK_GPIO_LEVEL
  177. jack.active = CONFIG_JACK_GPIO_LEVEL;
  178. #endif
  179. #ifndef CONFIG_JACK_LOCKED
  180. parse_set_GPIO(set_jack_gpio);
  181. #endif
  182. // re-use button management for jack handler, it's a GPIO after all
  183. if (jack.gpio != -1) {
  184. ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", jack.active ? "high" : "low", jack.gpio);
  185. button_create(NULL, jack.gpio, jack.active ? BUTTON_HIGH : BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
  186. }
  187. #ifdef CONFIG_SPKFAULT_GPIO_LEVEL
  188. spkfault.active = CONFIG_SPKFAULT_GPIO_LEVEL;
  189. #endif
  190. #ifndef CONFIG_SPKFAULT_LOCKED
  191. parse_set_GPIO(set_spkfault_gpio);
  192. #endif
  193. // re-use button management for speaker fault handler, it's a GPIO after all
  194. if (spkfault.gpio != -1) {
  195. ESP_LOGI(TAG,"Adding speaker fault (%s) detection GPIO %d", spkfault.active ? "high" : "low", spkfault.gpio);
  196. button_create(NULL, spkfault.gpio, spkfault.active ? BUTTON_HIGH : BUTTON_LOW, false, 0, spkfault_handler_default, 0, -1);
  197. }
  198. // do we want stats
  199. char *p = config_alloc_get_default(NVS_TYPE_STR, "stats", "n", 0);
  200. if (p && (*p == '1' || *p == 'Y' || *p == 'y')) {
  201. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  202. xTimerStart(monitor_timer, portMAX_DELAY);
  203. }
  204. free(p);
  205. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  206. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  207. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  208. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  209. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  210. }