cmd_squeezelite.c 4.6 KB

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