cmd_squeezelite.c 4.1 KB

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