cmd_squeezelite.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "application_name.h"
  4. #include "esp_log.h"
  5. #include "esp_console.h"
  6. #include "../cmd_system.h"
  7. #include "argtable3/argtable3.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/event_groups.h"
  10. #include "pthread.h"
  11. #include "platform_esp32.h"
  12. #include "Configurator.h"
  13. #include "esp_app_format.h"
  14. #include "tools.h"
  15. #include "messaging.h"
  16. extern esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length);
  17. extern int squeezelite_main_start();
  18. static const char * TAG = "squeezelite_cmd";
  19. #define SQUEEZELITE_THREAD_STACK_SIZE (8*1024)
  20. #define ADDITIONAL_SQUEEZELITE_ARGS 5
  21. const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
  22. .magic_word = ESP_APP_DESC_MAGIC_WORD,
  23. .version = PROJECT_VER,
  24. .project_name = CONFIG_PROJECT_NAME,
  25. .idf_ver = IDF_VER,
  26. #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
  27. .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
  28. #else
  29. .secure_version = 0,
  30. #endif
  31. #ifdef CONFIG_APP_COMPILE_TIME_DATE
  32. .time = __TIME__,
  33. .date = __DATE__,
  34. #else
  35. .time = "",
  36. .date = "",
  37. #endif
  38. };
  39. extern void register_audio_config(void);
  40. // extern void register_rotary_config(void);
  41. extern void register_nvs();
  42. extern cJSON * get_gpio_list_handler(bool refresh);
  43. cJSON * get_gpio_list(bool refresh){
  44. #if CONFIG_WITH_CONFIG_UI
  45. return get_gpio_list_handler(refresh);
  46. #else
  47. return cJSON_CreateArray();
  48. #endif
  49. }
  50. void register_optional_cmd(void) {
  51. // #if CONFIG_WITH_CONFIG_UI
  52. // register_rotary_config();
  53. // #endif
  54. register_audio_config();
  55. // register_ledvu_config();
  56. }
  57. /** Arguments used by 'squeezelite' function */
  58. static struct {
  59. struct arg_str *parameters;
  60. struct arg_end *end;
  61. } squeezelite_args;
  62. static struct {
  63. int argc;
  64. char ** argv;
  65. } thread_parms ;
  66. static void squeezelite_thread(void *arg){
  67. ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
  68. ESP_LOGV(TAG ,"Values:");
  69. for(int i = 0;i<thread_parms.argc; i++){
  70. ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
  71. }
  72. ESP_LOGI(TAG ,"Calling squeezelite");
  73. int ret = squeezelite_main_start(thread_parms.argc, thread_parms.argv);
  74. cmd_send_messaging("cfg-audio-tmpl",ret > 1 ? MESSAGING_ERROR : MESSAGING_WARNING,"squeezelite exited with error code %d\n", ret);
  75. if (ret <= 1) {
  76. int wait = 60;
  77. // wait_for_commit();
  78. // TODO: Add support for the commented code
  79. cmd_send_messaging("cfg-audio-tmpl",MESSAGING_ERROR,"Rebooting in %d sec\n", wait);
  80. vTaskDelay( pdMS_TO_TICKS(wait * 1000));
  81. esp_restart();
  82. } else {
  83. cmd_send_messaging("cfg-audio-tmpl",MESSAGING_ERROR,"Correct command line and reboot\n");
  84. vTaskSuspend(NULL);
  85. }
  86. ESP_LOGV(TAG, "Exited from squeezelite's main(). Freeing argv structure.");
  87. for(int i=0;i<thread_parms.argc;i++) free(thread_parms.argv[i]);
  88. free(thread_parms.argv);
  89. }
  90. static int launchsqueezelite(int argc, char **argv) {
  91. static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
  92. static EXT_RAM_ATTR StackType_t xStack[SQUEEZELITE_THREAD_STACK_SIZE] __attribute__ ((aligned (4)));
  93. static bool isRunning = false;
  94. if (isRunning) {
  95. ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
  96. return -1;
  97. }
  98. ESP_LOGV(TAG ,"Begin");
  99. isRunning = true;
  100. // ESP_LOGV(TAG, "Parameters:");
  101. // for(int i = 0;i<argc; i++){
  102. // ESP_LOGV(TAG, " %s",argv[i]);
  103. // }
  104. // ESP_LOGV(TAG,"Saving args in thread structure");
  105. // thread_parms.argc=0;
  106. // thread_parms.argv = malloc_init_external(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  107. // for(int i=0;i<argc;i++){
  108. // ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
  109. // thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
  110. // }
  111. // if(argc==1){
  112. // // There isn't a default configuration that would actually work
  113. // // if no parameter is passed.
  114. // ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
  115. // thread_parms.argv[thread_parms.argc++]=strdup("-?");
  116. // }
  117. ESP_LOGD(TAG,"Starting Squeezelite Thread");
  118. xTaskCreateStaticPinnedToCore(squeezelite_thread, "squeezelite", SQUEEZELITE_THREAD_STACK_SIZE,
  119. NULL, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT, xStack, &xTaskBuffer, CONFIG_PTHREAD_TASK_CORE_DEFAULT);
  120. ESP_LOGD(TAG ,"Back to console thread!");
  121. return 0;
  122. }
  123. void start_squeezelite(){
  124. launchsqueezelite(0,NULL);
  125. }
  126. // void register_squeezelite() {
  127. // squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
  128. // squeezelite_args.end = arg_end(1);
  129. // const esp_console_cmd_t launch_squeezelite = {
  130. // .command = "squeezelite",
  131. // .help = "Starts squeezelite",
  132. // .hint = NULL,
  133. // .func = &launchsqueezelite,
  134. // .argtable = &squeezelite_args
  135. // };
  136. // ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
  137. // }
  138. esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length) {
  139. if(!bin_url || strlen(bin_url)==0){
  140. ESP_LOGE(TAG,"missing URL parameter. Unable to start OTA");
  141. return ESP_ERR_INVALID_ARG;
  142. }
  143. ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
  144. configurator_set_string(&sys_State_msg,sys_State_ota_url_tag,sys_state,bin_url);
  145. configurator_raise_state_changed();
  146. if(!configurator_waitcommit()){
  147. ESP_LOGW(TAG,"Unable to commit configuration. ");
  148. }
  149. ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
  150. return guided_factory();
  151. return ESP_OK;
  152. }