cmd_squeezelite.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
  2. #include "cmd_squeezelite.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "cmd_decl.h"
  6. #include "esp_log.h"
  7. #include "esp_console.h"
  8. #include "esp_pthread.h"
  9. #include "argtable3/argtable3.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/event_groups.h"
  12. #include "pthread.h"
  13. #include "platform_esp32.h"
  14. #include "config.h"
  15. static const char * TAG = "squeezelite_cmd";
  16. #define SQUEEZELITE_THREAD_STACK_SIZE (6*1024)
  17. extern int main(int argc, char **argv);
  18. static int launchsqueezelite(int argc, char **argv);
  19. pthread_t thread_squeezelite;
  20. pthread_t thread_squeezelite_runner;
  21. /** Arguments used by 'squeezelite' function */
  22. static struct {
  23. struct arg_str *parameters;
  24. struct arg_end *end;
  25. } squeezelite_args;
  26. static struct {
  27. int argc;
  28. char ** argv;
  29. } thread_parms ;
  30. static void * squeezelite_runner_thread(){
  31. ESP_LOGI(TAG ,"Calling squeezelite");
  32. main(thread_parms.argc,thread_parms.argv);
  33. return NULL;
  34. }
  35. #define ADDITIONAL_SQUEEZELITE_ARGS 5
  36. static void * squeezelite_thread(){
  37. int * exit_code;
  38. static bool isRunning=false;
  39. if(isRunning) {
  40. ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
  41. return NULL;
  42. }
  43. isRunning=true;
  44. ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
  45. ESP_LOGV(TAG ,"Values:");
  46. for(int i = 0;i<thread_parms.argc; i++){
  47. ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
  48. }
  49. ESP_LOGV(TAG,"Starting Squeezelite runner Thread");
  50. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  51. cfg.thread_name= "squeezelite-run";
  52. cfg.inherit_cfg = true;
  53. cfg.stack_size = SQUEEZELITE_THREAD_STACK_SIZE ;
  54. esp_pthread_set_cfg(&cfg);
  55. // no attribute if we want esp stack stack to prevail
  56. pthread_create(&thread_squeezelite_runner, NULL, squeezelite_runner_thread,NULL);
  57. // Wait for thread completion so we can free up memory.
  58. pthread_join(thread_squeezelite_runner,(void **)&exit_code);
  59. ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
  60. for(int i=0;i<thread_parms.argc;i++){
  61. ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
  62. free(thread_parms.argv[i]);
  63. }
  64. ESP_LOGV(TAG ,"Freeing argv pointer");
  65. free(thread_parms.argv);
  66. isRunning=false;
  67. ESP_LOGE(TAG, "Exited from squeezelite thread, something's wrong ... rebooting (wait 30s for user to take action)");
  68. if(!wait_for_commit()){
  69. ESP_LOGW(TAG,"Unable to commit configuration. ");
  70. }
  71. vTaskDelay( pdMS_TO_TICKS( 30*1000 ) );
  72. esp_restart();
  73. return NULL;
  74. }
  75. static int launchsqueezelite(int argc, char **argv)
  76. {
  77. ESP_LOGV(TAG ,"Begin");
  78. ESP_LOGV(TAG, "Parameters:");
  79. for(int i = 0;i<argc; i++){
  80. ESP_LOGV(TAG, " %s",argv[i]);
  81. }
  82. ESP_LOGV(TAG,"Saving args in thread structure");
  83. thread_parms.argc=0;
  84. thread_parms.argv = malloc(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  85. memset(thread_parms.argv,'\0',sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
  86. for(int i=0;i<argc;i++){
  87. ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
  88. thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
  89. }
  90. if(argc==1){
  91. // There isn't a default configuration that would actually work
  92. // if no parameter is passed.
  93. ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
  94. thread_parms.argv[thread_parms.argc++]=strdup("-?");
  95. }
  96. ESP_LOGD(TAG,"Starting Squeezelite Thread");
  97. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  98. cfg.thread_name= "squeezelite";
  99. cfg.inherit_cfg = true;
  100. esp_pthread_set_cfg(&cfg);
  101. pthread_create(&thread_squeezelite, NULL, squeezelite_thread,NULL);
  102. ESP_LOGD(TAG ,"Back to console thread!");
  103. return 0;
  104. }
  105. void register_squeezelite(){
  106. squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
  107. squeezelite_args.end = arg_end(1);
  108. const esp_console_cmd_t launch_squeezelite = {
  109. .command = "squeezelite",
  110. .help = "Starts squeezelite",
  111. .hint = NULL,
  112. .func = &launchsqueezelite,
  113. .argtable = &squeezelite_args
  114. };
  115. ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
  116. }