cmd_squeezelite.c 3.9 KB

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