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 "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 8192
  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. ESP_LOGI(TAG,"Waiting for WiFi.");
  47. while(!wait_for_wifi()){usleep(100000);};
  48. ESP_LOGD(TAG ,"Number of args received: %u",thread_parms.argc );
  49. ESP_LOGD(TAG ,"Values:");
  50. for(int i = 0;i<thread_parms.argc; i++){
  51. ESP_LOGD(TAG ," %s",thread_parms.argv[i]);
  52. }
  53. ESP_LOGD(TAG,"Starting Squeezelite runner Thread");
  54. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  55. cfg.thread_name= "squeezelite-run";
  56. cfg.inherit_cfg = true;
  57. cfg.stack_size = SQUEEZELITE_THREAD_STACK_SIZE ;
  58. esp_pthread_set_cfg(&cfg);
  59. // no attribute if we want esp stack stack to prevail
  60. pthread_create(&thread_squeezelite_runner, NULL, squeezelite_runner_thread,NULL);
  61. // Wait for thread completion so we can free up memory.
  62. pthread_join(thread_squeezelite_runner,(void **)&exit_code);
  63. ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
  64. for(int i=0;i<thread_parms.argc;i++){
  65. ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
  66. free(thread_parms.argv[i]);
  67. }
  68. ESP_LOGV(TAG ,"Freeing argv pointer");
  69. free(thread_parms.argv);
  70. isRunning=false;
  71. return NULL;
  72. }
  73. static int launchsqueezelite(int argc, char **argv)
  74. {
  75. ESP_LOGV(TAG ,"Begin");
  76. ESP_LOGD(TAG, "Parameters:");
  77. for(int i = 0;i<argc; i++){
  78. ESP_LOGD(TAG, " %s",argv[i]);
  79. }
  80. ESP_LOGV(TAG,"Saving args in thread structure");
  81. thread_parms.argc=0;
  82. thread_parms.argv = malloc(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELILTE_ARGS));
  83. memset(thread_parms.argv,'\0',sizeof(char**)*(argc+ADDITIONAL_SQUEEZELILTE_ARGS));
  84. for(int i=0;i<argc;i++){
  85. ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
  86. thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
  87. }
  88. if(argc==1){
  89. // There isn't a default configuration that would actually work
  90. // if no parameter is passed.
  91. ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
  92. thread_parms.argv[thread_parms.argc++]=strdup("-?");
  93. }
  94. ESP_LOGD(TAG,"Starting Squeezelite Thread");
  95. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  96. cfg.thread_name= "squeezelite";
  97. cfg.inherit_cfg = true;
  98. esp_pthread_set_cfg(&cfg);
  99. pthread_attr_t attr;
  100. pthread_attr_init(&attr);
  101. pthread_create(&thread_squeezelite, &attr, squeezelite_thread,NULL);
  102. pthread_attr_destroy(&attr);
  103. ESP_LOGD(TAG ,"Back to console thread!");
  104. return 0;
  105. }
  106. void register_squeezelite(){
  107. squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
  108. squeezelite_args.end = arg_end(1);
  109. const esp_console_cmd_t launch_squeezelite = {
  110. .command = "squeezelite",
  111. .help = "Starts squeezelite",
  112. .hint = NULL,
  113. .func = &launchsqueezelite,
  114. .argtable = &squeezelite_args
  115. };
  116. ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
  117. }