cmd_squeezelite.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "esp_pthread.h"
  7. #include "../cmd_system.h"
  8. #include "argtable3/argtable3.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/event_groups.h"
  11. #include "pthread.h"
  12. #include "platform_esp32.h"
  13. #include "platform_config.h"
  14. #include "esp_app_format.h"
  15. #include "tools.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 (4*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 int main(int argc, char **argv);
  38. static int launchsqueezelite(int argc, char **argv);
  39. pthread_t thread_squeezelite;
  40. pthread_t thread_squeezelite_runner;
  41. /** Arguments used by 'squeezelite' function */
  42. static struct {
  43. struct arg_str *parameters;
  44. struct arg_end *end;
  45. } squeezelite_args;
  46. static struct {
  47. int argc;
  48. char ** argv;
  49. } thread_parms ;
  50. static void * squeezelite_runner_thread(){
  51. ESP_LOGI(TAG ,"Calling squeezelite");
  52. main(thread_parms.argc,thread_parms.argv);
  53. return NULL;
  54. }
  55. #define ADDITIONAL_SQUEEZELITE_ARGS 5
  56. static void * squeezelite_thread(){
  57. int * exit_code;
  58. static bool isRunning=false;
  59. if(isRunning) {
  60. ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
  61. return NULL;
  62. }
  63. isRunning=true;
  64. ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
  65. ESP_LOGV(TAG ,"Values:");
  66. for(int i = 0;i<thread_parms.argc; i++){
  67. ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
  68. }
  69. ESP_LOGV(TAG,"Starting Squeezelite runner Thread");
  70. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  71. cfg.thread_name= "squeezelite-run";
  72. cfg.inherit_cfg = true;
  73. cfg.stack_size = SQUEEZELITE_THREAD_STACK_SIZE ;
  74. esp_pthread_set_cfg(&cfg);
  75. // no attribute if we want esp stack stack to prevail
  76. pthread_create(&thread_squeezelite_runner, NULL, squeezelite_runner_thread,NULL);
  77. // Wait for thread completion so we can free up memory.
  78. pthread_join(thread_squeezelite_runner,(void **)&exit_code);
  79. ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
  80. for(int i=0;i<thread_parms.argc;i++){
  81. ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
  82. free(thread_parms.argv[i]);
  83. }
  84. ESP_LOGV(TAG ,"Freeing argv pointer");
  85. free(thread_parms.argv);
  86. isRunning=false;
  87. ESP_LOGE(TAG, "Exited from squeezelite thread, something's wrong ... rebooting (wait 30s for user to take action)");
  88. if(!wait_for_commit()){
  89. ESP_LOGW(TAG,"Unable to commit configuration. ");
  90. }
  91. vTaskDelay( pdMS_TO_TICKS( 30*1000 ) );
  92. esp_restart();
  93. return NULL;
  94. }
  95. static int launchsqueezelite(int argc, char **argv)
  96. {
  97. ESP_LOGV(TAG ,"Begin");
  98. ESP_LOGV(TAG, "Parameters:");
  99. for(int i = 0;i<argc; i++){
  100. ESP_LOGV(TAG, " %s",argv[i]);
  101. }
  102. ESP_LOGV(TAG,"Saving args in thread structure");
  103. thread_parms.argc=0;
  104. thread_parms.argv = malloc_init_external(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  105. for(int i=0;i<argc;i++){
  106. ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
  107. thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
  108. }
  109. if(argc==1){
  110. // There isn't a default configuration that would actually work
  111. // if no parameter is passed.
  112. ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
  113. thread_parms.argv[thread_parms.argc++]=strdup("-?");
  114. }
  115. ESP_LOGD(TAG,"Starting Squeezelite Thread");
  116. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  117. cfg.thread_name= "squeezelite";
  118. cfg.inherit_cfg = true;
  119. esp_pthread_set_cfg(&cfg);
  120. pthread_create(&thread_squeezelite, NULL, squeezelite_thread,NULL);
  121. ESP_LOGD(TAG ,"Back to console thread!");
  122. return 0;
  123. }
  124. void register_squeezelite(){
  125. squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
  126. squeezelite_args.end = arg_end(1);
  127. const esp_console_cmd_t launch_squeezelite = {
  128. .command = "squeezelite",
  129. .help = "Starts squeezelite",
  130. .hint = NULL,
  131. .func = &launchsqueezelite,
  132. .argtable = &squeezelite_args
  133. };
  134. ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
  135. }
  136. esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length)
  137. {
  138. if(!bin_url){
  139. ESP_LOGE(TAG,"missing URL parameter. Unable to start OTA");
  140. return ESP_ERR_INVALID_ARG;
  141. }
  142. ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
  143. if(config_set_value(NVS_TYPE_STR, "fwurl", bin_url) != ESP_OK){
  144. ESP_LOGE(TAG,"Failed to save the OTA url into nvs cache");
  145. return ESP_FAIL;
  146. }
  147. if(!wait_for_commit()){
  148. ESP_LOGW(TAG,"Unable to commit configuration. ");
  149. }
  150. ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
  151. return guided_factory();
  152. return ESP_OK;
  153. }