cmd_squeezelite.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "argtable3/argtable3.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/event_groups.h"
  9. #include "pthread.h"
  10. #include "platform_esp32.h"
  11. #include "platform_config.h"
  12. #include "esp_app_format.h"
  13. static const char * TAG = "squeezelite_cmd";
  14. #define SQUEEZELITE_THREAD_STACK_SIZE (6*1024)
  15. const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
  16. .magic_word = ESP_APP_DESC_MAGIC_WORD,
  17. .version = PROJECT_VER,
  18. .project_name = PROJECT_NAME,
  19. .idf_ver = IDF_VER,
  20. #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
  21. .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
  22. #else
  23. .secure_version = 0,
  24. #endif
  25. #ifdef CONFIG_APP_COMPILE_TIME_DATE
  26. .time = __TIME__,
  27. .date = __DATE__,
  28. #else
  29. .time = "",
  30. .date = "",
  31. #endif
  32. };
  33. extern int main(int argc, char **argv);
  34. static int launchsqueezelite(int argc, char **argv);
  35. pthread_t thread_squeezelite;
  36. pthread_t thread_squeezelite_runner;
  37. /** Arguments used by 'squeezelite' function */
  38. static struct {
  39. struct arg_str *parameters;
  40. struct arg_end *end;
  41. } squeezelite_args;
  42. static struct {
  43. int argc;
  44. char ** argv;
  45. } thread_parms ;
  46. static void * squeezelite_runner_thread(){
  47. ESP_LOGI(TAG ,"Calling squeezelite");
  48. main(thread_parms.argc,thread_parms.argv);
  49. return NULL;
  50. }
  51. #define ADDITIONAL_SQUEEZELITE_ARGS 5
  52. static void * squeezelite_thread(){
  53. int * exit_code;
  54. static bool isRunning=false;
  55. if(isRunning) {
  56. ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
  57. return NULL;
  58. }
  59. isRunning=true;
  60. ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
  61. ESP_LOGV(TAG ,"Values:");
  62. for(int i = 0;i<thread_parms.argc; i++){
  63. ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
  64. }
  65. ESP_LOGV(TAG,"Starting Squeezelite runner Thread");
  66. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  67. cfg.thread_name= "squeezelite-run";
  68. cfg.inherit_cfg = true;
  69. cfg.stack_size = SQUEEZELITE_THREAD_STACK_SIZE ;
  70. esp_pthread_set_cfg(&cfg);
  71. // no attribute if we want esp stack stack to prevail
  72. pthread_create(&thread_squeezelite_runner, NULL, squeezelite_runner_thread,NULL);
  73. // Wait for thread completion so we can free up memory.
  74. pthread_join(thread_squeezelite_runner,(void **)&exit_code);
  75. ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
  76. for(int i=0;i<thread_parms.argc;i++){
  77. ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
  78. free(thread_parms.argv[i]);
  79. }
  80. ESP_LOGV(TAG ,"Freeing argv pointer");
  81. free(thread_parms.argv);
  82. isRunning=false;
  83. ESP_LOGE(TAG, "Exited from squeezelite thread, something's wrong ... rebooting");
  84. if(!wait_for_commit()){
  85. ESP_LOGW(TAG,"Unable to commit configuration. ");
  86. }
  87. esp_restart();
  88. return NULL;
  89. }
  90. static int launchsqueezelite(int argc, char **argv)
  91. {
  92. ESP_LOGV(TAG ,"Begin");
  93. ESP_LOGV(TAG, "Parameters:");
  94. for(int i = 0;i<argc; i++){
  95. ESP_LOGV(TAG, " %s",argv[i]);
  96. }
  97. ESP_LOGV(TAG,"Saving args in thread structure");
  98. thread_parms.argc=0;
  99. thread_parms.argv = malloc(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  100. memset(thread_parms.argv,'\0',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. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  113. cfg.thread_name= "squeezelite";
  114. cfg.inherit_cfg = true;
  115. esp_pthread_set_cfg(&cfg);
  116. pthread_create(&thread_squeezelite, NULL, squeezelite_thread,NULL);
  117. ESP_LOGD(TAG ,"Back to console thread!");
  118. return 0;
  119. }
  120. void register_squeezelite(){
  121. squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
  122. squeezelite_args.end = arg_end(1);
  123. const esp_console_cmd_t launch_squeezelite = {
  124. .command = "squeezelite",
  125. .help = "Starts squeezelite",
  126. .hint = NULL,
  127. .func = &launchsqueezelite,
  128. .argtable = &squeezelite_args
  129. };
  130. ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
  131. }