cmd_squeezelite.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "esp_log.h"
  4. #include "esp_console.h"
  5. #include "esp_pthread.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. extern esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length);
  15. static const char * TAG = "squeezelite_cmd";
  16. #define SQUEEZELITE_THREAD_STACK_SIZE (6*1024)
  17. const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
  18. .magic_word = ESP_APP_DESC_MAGIC_WORD,
  19. .version = PROJECT_VER,
  20. .project_name = PROJECT_NAME,
  21. .idf_ver = IDF_VER,
  22. #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
  23. .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
  24. #else
  25. .secure_version = 0,
  26. #endif
  27. #ifdef CONFIG_APP_COMPILE_TIME_DATE
  28. .time = __TIME__,
  29. .date = __DATE__,
  30. #else
  31. .time = "",
  32. .date = "",
  33. #endif
  34. };
  35. extern int main(int argc, char **argv);
  36. static int launchsqueezelite(int argc, char **argv);
  37. pthread_t thread_squeezelite;
  38. pthread_t thread_squeezelite_runner;
  39. /** Arguments used by 'squeezelite' function */
  40. static struct {
  41. struct arg_str *parameters;
  42. struct arg_end *end;
  43. } squeezelite_args;
  44. static struct {
  45. int argc;
  46. char ** argv;
  47. } thread_parms ;
  48. static void * squeezelite_runner_thread(){
  49. ESP_LOGI(TAG ,"Calling squeezelite");
  50. main(thread_parms.argc,thread_parms.argv);
  51. return NULL;
  52. }
  53. #define ADDITIONAL_SQUEEZELITE_ARGS 5
  54. static void * squeezelite_thread(){
  55. int * exit_code;
  56. static bool isRunning=false;
  57. if(isRunning) {
  58. ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
  59. return NULL;
  60. }
  61. isRunning=true;
  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_LOGV(TAG,"Starting Squeezelite runner Thread");
  68. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  69. cfg.thread_name= "squeezelite-run";
  70. cfg.inherit_cfg = true;
  71. cfg.stack_size = SQUEEZELITE_THREAD_STACK_SIZE ;
  72. esp_pthread_set_cfg(&cfg);
  73. // no attribute if we want esp stack stack to prevail
  74. pthread_create(&thread_squeezelite_runner, NULL, squeezelite_runner_thread,NULL);
  75. // Wait for thread completion so we can free up memory.
  76. pthread_join(thread_squeezelite_runner,(void **)&exit_code);
  77. ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
  78. for(int i=0;i<thread_parms.argc;i++){
  79. ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
  80. free(thread_parms.argv[i]);
  81. }
  82. ESP_LOGV(TAG ,"Freeing argv pointer");
  83. free(thread_parms.argv);
  84. isRunning=false;
  85. ESP_LOGE(TAG, "Exited from squeezelite thread, something's wrong ... rebooting (wait 30s for user to take action)");
  86. if(!wait_for_commit()){
  87. ESP_LOGW(TAG,"Unable to commit configuration. ");
  88. }
  89. vTaskDelay( pdMS_TO_TICKS( 30*1000 ) );
  90. esp_restart();
  91. return NULL;
  92. }
  93. static int launchsqueezelite(int argc, char **argv)
  94. {
  95. ESP_LOGV(TAG ,"Begin");
  96. ESP_LOGV(TAG, "Parameters:");
  97. for(int i = 0;i<argc; i++){
  98. ESP_LOGV(TAG, " %s",argv[i]);
  99. }
  100. ESP_LOGV(TAG,"Saving args in thread structure");
  101. thread_parms.argc=0;
  102. thread_parms.argv = malloc(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  103. memset(thread_parms.argv,'\0',sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  104. for(int i=0;i<argc;i++){
  105. ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
  106. thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
  107. }
  108. if(argc==1){
  109. // There isn't a default configuration that would actually work
  110. // if no parameter is passed.
  111. ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
  112. thread_parms.argv[thread_parms.argc++]=strdup("-?");
  113. }
  114. ESP_LOGD(TAG,"Starting Squeezelite Thread");
  115. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  116. cfg.thread_name= "squeezelite";
  117. cfg.inherit_cfg = true;
  118. esp_pthread_set_cfg(&cfg);
  119. pthread_create(&thread_squeezelite, NULL, squeezelite_thread,NULL);
  120. ESP_LOGD(TAG ,"Back to console thread!");
  121. return 0;
  122. }
  123. void register_squeezelite(){
  124. squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
  125. squeezelite_args.end = arg_end(1);
  126. const esp_console_cmd_t launch_squeezelite = {
  127. .command = "squeezelite",
  128. .help = "Starts squeezelite",
  129. .hint = NULL,
  130. .func = &launchsqueezelite,
  131. .argtable = &squeezelite_args
  132. };
  133. ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
  134. }
  135. esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length)
  136. {
  137. if(!bin_url){
  138. ESP_LOGE(TAG,"missing URL parameter. Unable to start OTA");
  139. return ESP_ERR_INVALID_ARG;
  140. }
  141. ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
  142. if(config_set_value(NVS_TYPE_STR, "fwurl", bin_url) != ESP_OK){
  143. ESP_LOGE(TAG,"Failed to save the OTA url into nvs cache");
  144. return ESP_FAIL;
  145. }
  146. if(!wait_for_commit()){
  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. }