cmd_squeezelite.c 5.2 KB

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