123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include <stdio.h>
- #include <string.h>
- #include "application_name.h"
- #include "esp_log.h"
- #include "esp_console.h"
- #include "../cmd_system.h"
- #include "argtable3/argtable3.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/event_groups.h"
- #include "pthread.h"
- #include "platform_esp32.h"
- #include "platform_config.h"
- #include "esp_app_format.h"
- #include "tools.h"
- extern esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length);
- static const char * TAG = "squeezelite_cmd";
- #define SQUEEZELITE_THREAD_STACK_SIZE (8*1024)
- const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
- .magic_word = ESP_APP_DESC_MAGIC_WORD,
- .version = PROJECT_VER,
- .project_name = CONFIG_PROJECT_NAME,
- .idf_ver = IDF_VER,
- #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
- .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
- #else
- .secure_version = 0,
- #endif
- #ifdef CONFIG_APP_COMPILE_TIME_DATE
- .time = __TIME__,
- .date = __DATE__,
- #else
- .time = "",
- .date = "",
- #endif
- };
- extern int main(int argc, char **argv);
- static int launchsqueezelite(int argc, char **argv);
- /** Arguments used by 'squeezelite' function */
- static struct {
- struct arg_str *parameters;
- struct arg_end *end;
- } squeezelite_args;
- static struct {
- int argc;
- char ** argv;
- } thread_parms ;
- #define ADDITIONAL_SQUEEZELITE_ARGS 5
- static void squeezelite_thread(void *arg){
- ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
- ESP_LOGV(TAG ,"Values:");
- for(int i = 0;i<thread_parms.argc; i++){
- ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
- }
- ESP_LOGI(TAG ,"Calling squeezelite");
- main(thread_parms.argc,thread_parms.argv);
- ESP_LOGV(TAG ,"Exited from squeezelite's main(). Freeing argv structure.");
- for(int i=0;i<thread_parms.argc;i++){
- ESP_LOGV(TAG ,"Freeing char buffer for parameter %u", i+1);
- free(thread_parms.argv[i]);
- }
- ESP_LOGV(TAG ,"Freeing argv pointer");
- free(thread_parms.argv);
-
- ESP_LOGE(TAG, "Exited from squeezelite thread, something's wrong ... rebooting (wait 30s for user to take action)");
- if(!wait_for_commit()){
- ESP_LOGW(TAG,"Unable to commit configuration. ");
- }
- vTaskDelay( pdMS_TO_TICKS( 30*1000 ) );
- esp_restart();
- }
- static int launchsqueezelite(int argc, char **argv) {
- static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
- static EXT_RAM_ATTR StackType_t xStack[SQUEEZELITE_THREAD_STACK_SIZE] __attribute__ ((aligned (4)));
- static bool isRunning = false;
- if (isRunning) {
- ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
- return -1;
- }
-
- ESP_LOGV(TAG ,"Begin");
- isRunning = true;
- ESP_LOGV(TAG, "Parameters:");
- for(int i = 0;i<argc; i++){
- ESP_LOGV(TAG, " %s",argv[i]);
- }
- ESP_LOGV(TAG,"Saving args in thread structure");
- thread_parms.argc=0;
- thread_parms.argv = malloc_init_external(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
- for(int i=0;i<argc;i++){
- ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
- thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
- }
- if(argc==1){
- // There isn't a default configuration that would actually work
- // if no parameter is passed.
- ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
- thread_parms.argv[thread_parms.argc++]=strdup("-?");
- }
- ESP_LOGD(TAG,"Starting Squeezelite Thread");
- xTaskCreateStaticPinnedToCore(squeezelite_thread, "squeezelite", SQUEEZELITE_THREAD_STACK_SIZE,
- NULL, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT, xStack, &xTaskBuffer, CONFIG_PTHREAD_TASK_CORE_DEFAULT);
- ESP_LOGD(TAG ,"Back to console thread!");
- return 0;
- }
- void register_squeezelite() {
- squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
- squeezelite_args.end = arg_end(1);
- const esp_console_cmd_t launch_squeezelite = {
- .command = "squeezelite",
- .help = "Starts squeezelite",
- .hint = NULL,
- .func = &launchsqueezelite,
- .argtable = &squeezelite_args
- };
- ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
- }
- esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length) {
- if(!bin_url){
- ESP_LOGE(TAG,"missing URL parameter. Unable to start OTA");
- return ESP_ERR_INVALID_ARG;
- }
- ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
- if(config_set_value(NVS_TYPE_STR, "fwurl", bin_url) != ESP_OK){
- ESP_LOGE(TAG,"Failed to save the OTA url into nvs cache");
- return ESP_FAIL;
- }
- if(!wait_for_commit()){
- ESP_LOGW(TAG,"Unable to commit configuration. ");
- }
- ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
- return guided_factory();
- return ESP_OK;
- }
|