mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2026-04-06 13:17:41 +03:00
style: apply clang-format and enforce left pointer alignment
This commit is contained in:
@@ -64,11 +64,11 @@ const char* recovery_prompt = LOG_COLOR_E "recovery-squeezelite-esp32> " LOG_RES
|
||||
#define HISTORY_PATH MOUNT_PATH "/history.txt"
|
||||
static esp_err_t run_command(char* line);
|
||||
#define ADD_TO_JSON(o, t, n) \
|
||||
if (t->n) cJSON_AddStringToObject(o, QUOTE(n), t->n);
|
||||
if(t->n) cJSON_AddStringToObject(o, QUOTE(n), t->n);
|
||||
#define ADD_PARMS_TO_CMD(o, t, n) \
|
||||
{ \
|
||||
cJSON* parms = ParmsToJSON(&t.n->hdr); \
|
||||
if (parms) cJSON_AddItemToObject(o, QUOTE(n), parms); \
|
||||
if(parms) cJSON_AddItemToObject(o, QUOTE(n), parms); \
|
||||
}
|
||||
cJSON* cmdList;
|
||||
cJSON* values_fn_list;
|
||||
@@ -81,13 +81,11 @@ cJSON* get_cmd_list() {
|
||||
cJSON_ArrayForEach(element, cmdList) {
|
||||
cJSON* name = cJSON_GetObjectItem(element, "name");
|
||||
cJSON* vals_fn = cJSON_GetObjectItem(values_fn_list, cJSON_GetStringValue(name));
|
||||
if (vals_fn != NULL) {
|
||||
if(vals_fn != NULL) {
|
||||
parm_values_fn_t* parm_values_fn = (parm_values_fn_t*)strtoul(cJSON_GetStringValue(vals_fn), NULL, 16);
|
||||
;
|
||||
|
||||
if (parm_values_fn) {
|
||||
cJSON_AddItemToObject(values, cJSON_GetStringValue(name), parm_values_fn());
|
||||
}
|
||||
if(parm_values_fn) { cJSON_AddItemToObject(values, cJSON_GetStringValue(name), parm_values_fn()); }
|
||||
}
|
||||
}
|
||||
return list;
|
||||
@@ -95,7 +93,7 @@ cJSON* get_cmd_list() {
|
||||
void console_set_bool_parameter(cJSON* root, char* nvs_name, struct arg_lit* arg) {
|
||||
char* p = NULL;
|
||||
bool enabled = false;
|
||||
if (!root) {
|
||||
if(!root) {
|
||||
ESP_LOGE(TAG, "Invalid json parameter. Cannot set %s from %s", arg->hdr.longopts ? arg->hdr.longopts : arg->hdr.glossary, nvs_name);
|
||||
return;
|
||||
}
|
||||
@@ -112,20 +110,18 @@ void console_set_bool_parameter(cJSON* root, char* nvs_name, struct arg_lit* arg
|
||||
// TODO: Add support for the commented code
|
||||
}
|
||||
struct arg_end* getParmsEnd(struct arg_hdr** argtable) {
|
||||
if (!argtable) return NULL;
|
||||
if(!argtable) return NULL;
|
||||
struct arg_hdr** table = (struct arg_hdr**)argtable;
|
||||
int tabindex = 0;
|
||||
while (!(table[tabindex]->flag & ARG_TERMINATOR)) {
|
||||
tabindex++;
|
||||
}
|
||||
while(!(table[tabindex]->flag & ARG_TERMINATOR)) { tabindex++; }
|
||||
return (struct arg_end*)table[tabindex];
|
||||
}
|
||||
cJSON* ParmsToJSON(struct arg_hdr** argtable) {
|
||||
if (!argtable) return NULL;
|
||||
if(!argtable) return NULL;
|
||||
cJSON* arg_list = cJSON_CreateArray();
|
||||
struct arg_hdr** table = (struct arg_hdr**)argtable;
|
||||
int tabindex = 0;
|
||||
while (!(table[tabindex]->flag & ARG_TERMINATOR)) {
|
||||
while(!(table[tabindex]->flag & ARG_TERMINATOR)) {
|
||||
cJSON* entry = cJSON_CreateObject();
|
||||
ADD_TO_JSON(entry, table[tabindex], datatype);
|
||||
ADD_TO_JSON(entry, table[tabindex], glossary);
|
||||
@@ -149,40 +145,30 @@ cJSON* ParmsToJSON(struct arg_hdr** argtable) {
|
||||
esp_err_t cmd_to_json(const esp_console_cmd_t* cmd) { return cmd_to_json_with_cb(cmd, NULL); }
|
||||
|
||||
esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t* cmd, parm_values_fn_t parm_values_fn) {
|
||||
if (!cmdList) {
|
||||
cmdList = cJSON_CreateArray();
|
||||
}
|
||||
if (!values_fn_list) {
|
||||
values_fn_list = cJSON_CreateObject();
|
||||
}
|
||||
if(!cmdList) { cmdList = cJSON_CreateArray(); }
|
||||
if(!values_fn_list) { values_fn_list = cJSON_CreateObject(); }
|
||||
|
||||
if (cmd->command == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (strchr(cmd->command, ' ') != NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if(cmd->command == NULL) { return ESP_ERR_INVALID_ARG; }
|
||||
if(strchr(cmd->command, ' ') != NULL) { return ESP_ERR_INVALID_ARG; }
|
||||
cJSON* jsoncmd = cJSON_CreateObject();
|
||||
ADD_TO_JSON(jsoncmd, cmd, help);
|
||||
ADD_TO_JSON(jsoncmd, cmd, hint);
|
||||
if (parm_values_fn) {
|
||||
if(parm_values_fn) {
|
||||
char addr[11] = {0};
|
||||
snprintf(addr, sizeof(addr), "%lx", (unsigned long)parm_values_fn);
|
||||
cJSON_AddStringToObject(values_fn_list, cmd->command, addr);
|
||||
}
|
||||
cJSON_AddBoolToObject(jsoncmd, "hascb", parm_values_fn != NULL);
|
||||
|
||||
if (cmd->argtable) {
|
||||
cJSON_AddItemToObject(jsoncmd, "argtable", ParmsToJSON(cmd->argtable));
|
||||
}
|
||||
if (cmd->hint) {
|
||||
if(cmd->argtable) { cJSON_AddItemToObject(jsoncmd, "argtable", ParmsToJSON(cmd->argtable)); }
|
||||
if(cmd->hint) {
|
||||
cJSON_AddStringToObject(jsoncmd, "hint", cmd->hint);
|
||||
} else if (cmd->argtable) {
|
||||
} else if(cmd->argtable) {
|
||||
/* Generate hint based on cmd->argtable */
|
||||
char* buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE* f = open_memstream(&buf, &buf_size);
|
||||
if (f != NULL) {
|
||||
if(f != NULL) {
|
||||
arg_print_syntax(f, cmd->argtable, NULL);
|
||||
fflush(f);
|
||||
fclose(f);
|
||||
@@ -192,7 +178,7 @@ esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t* cmd, parm_values_fn_t par
|
||||
}
|
||||
cJSON_AddStringToObject(jsoncmd, "name", cmd->command);
|
||||
char* b = cJSON_Print(jsoncmd);
|
||||
if (b) {
|
||||
if(b) {
|
||||
ESP_LOGD(TAG, "Adding command table %s", b);
|
||||
free(b);
|
||||
}
|
||||
@@ -202,11 +188,11 @@ esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t* cmd, parm_values_fn_t par
|
||||
int arg_parse_msg(int argc, char** argv, struct arg_hdr** args) {
|
||||
int nerrors = arg_parse(argc, argv, (void**)args);
|
||||
|
||||
if (nerrors != 0) {
|
||||
if(nerrors != 0) {
|
||||
char* buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE* f = open_memstream(&buf, &buf_size);
|
||||
if (f != NULL) {
|
||||
if(f != NULL) {
|
||||
arg_print_errors(f, getParmsEnd(args), argv[0]);
|
||||
fflush(f);
|
||||
cmd_send_messaging(argv[0], MESSAGING_ERROR, "%s", buf);
|
||||
@@ -219,34 +205,32 @@ int arg_parse_msg(int argc, char** argv, struct arg_hdr** args) {
|
||||
static ssize_t stdin_read(int fd, void* data, size_t size) {
|
||||
size_t bytes = -1;
|
||||
static size_t remain = 0;
|
||||
if (remain > 0) {
|
||||
if(remain > 0) {
|
||||
bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < remain ? size : remain, 0);
|
||||
remain -= bytes;
|
||||
for (int i = 0; i < bytes; i++)
|
||||
if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
|
||||
return bytes;
|
||||
for(int i = 0; i < bytes; i++)
|
||||
if(((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
|
||||
return bytes;
|
||||
}
|
||||
while (1) {
|
||||
while(1) {
|
||||
QueueSetMemberHandle_t activated = xQueueSelectFromSet(stdin_redir.queue_set, portMAX_DELAY);
|
||||
|
||||
if (activated == uart_queue) {
|
||||
if(activated == uart_queue) {
|
||||
uart_event_t event;
|
||||
xQueueReceive(uart_queue, &event, 0);
|
||||
if (event.type == UART_DATA) {
|
||||
if(event.type == UART_DATA) {
|
||||
bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < event.size ? size : event.size, 0);
|
||||
// we have to do our own line ending translation here
|
||||
for (int i = 0; i < bytes; i++)
|
||||
if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
|
||||
if (event.size > bytes) {
|
||||
remain = event.size - bytes;
|
||||
}
|
||||
for(int i = 0; i < bytes; i++)
|
||||
if(((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
|
||||
if(event.size > bytes) { remain = event.size - bytes; }
|
||||
break;
|
||||
}
|
||||
} else if (xRingbufferCanRead(stdin_redir.handle, activated)) {
|
||||
} else if(xRingbufferCanRead(stdin_redir.handle, activated)) {
|
||||
char* p = xRingbufferReceiveUpTo(stdin_redir.handle, &bytes, 0, size);
|
||||
// we might receive strings, replace null by \n
|
||||
for (int i = 0; i < bytes; i++)
|
||||
if (p[i] == '\0' || p[i] == '\r') p[i] = '\n';
|
||||
for(int i = 0; i < bytes; i++)
|
||||
if(p[i] == '\0' || p[i] == '\r') p[i] = '\n';
|
||||
memcpy(data, p, bytes);
|
||||
vRingbufferReturnItem(stdin_redir.handle, p);
|
||||
xRingbufferPrintInfo(stdin_redir.handle);
|
||||
@@ -300,8 +284,7 @@ void initialize_console() {
|
||||
setvbuf(stdin, NULL, _IONBF, 0);
|
||||
|
||||
/* Initialize the console */
|
||||
esp_console_config_t console_config = {
|
||||
.max_cmdline_args = 28,
|
||||
esp_console_config_t console_config = {.max_cmdline_args = 28,
|
||||
.max_cmdline_length = 600,
|
||||
#if CONFIG_LOG_COLORS
|
||||
.hint_color = atoi(LOG_COLOR_CYAN)
|
||||
@@ -321,7 +304,6 @@ void initialize_console() {
|
||||
|
||||
/* Set command history size */
|
||||
linenoiseHistorySetMaxLen(100);
|
||||
|
||||
}
|
||||
|
||||
bool console_push(const char* data, size_t size) { return xRingbufferSend(stdin_redir.handle, data, size, pdMS_TO_TICKS(100)) == pdPASS; }
|
||||
@@ -334,13 +316,11 @@ void console_start() {
|
||||
register_config_cmd();
|
||||
register_wifi();
|
||||
|
||||
if (is_recovery_running) {
|
||||
register_ota_cmd();
|
||||
}
|
||||
if(is_recovery_running) { register_ota_cmd(); }
|
||||
register_i2ctools();
|
||||
|
||||
printf("\n");
|
||||
if (is_recovery_running) {
|
||||
if(is_recovery_running) {
|
||||
printf("****************************************************************\n"
|
||||
"RECOVERY APPLICATION\n"
|
||||
"This mode is used to flash Squeezelite into the OTA partition\n"
|
||||
@@ -354,7 +334,7 @@ void console_start() {
|
||||
|
||||
/* Figure out if the terminal supports escape sequences */
|
||||
int probe_status = linenoiseProbe();
|
||||
if (probe_status) { /* zero indicates success */
|
||||
if(probe_status) { /* zero indicates success */
|
||||
printf("\n****************************\n"
|
||||
"Your terminal application does not support escape sequences.\n"
|
||||
"Line editing and history features are disabled.\n"
|
||||
@@ -365,9 +345,7 @@ void console_start() {
|
||||
/* Since the terminal doesn't support escape sequences,
|
||||
* don't use color codes in the prompt.
|
||||
*/
|
||||
if (is_recovery_running) {
|
||||
recovery_prompt = "recovery-squeezelite-esp32>";
|
||||
}
|
||||
if(is_recovery_running) { recovery_prompt = "recovery-squeezelite-esp32>"; }
|
||||
prompt = "squeezelite-esp32> ";
|
||||
#endif // CONFIG_LOG_COLORS
|
||||
}
|
||||
@@ -375,9 +353,7 @@ void console_start() {
|
||||
cfg.thread_name = "console";
|
||||
cfg.inherit_cfg = true;
|
||||
cfg.stack_size = 4 * 1024;
|
||||
if (is_recovery_running) {
|
||||
prompt = recovery_prompt;
|
||||
}
|
||||
if(is_recovery_running) { prompt = recovery_prompt; }
|
||||
esp_pthread_set_cfg(&cfg);
|
||||
pthread_create(&thread_console, NULL, console_thread, NULL);
|
||||
}
|
||||
@@ -387,16 +363,16 @@ static esp_err_t run_command(char* line) {
|
||||
int ret;
|
||||
esp_err_t err = esp_console_run(line, &ret);
|
||||
|
||||
if (err == ESP_ERR_NOT_FOUND) {
|
||||
if(err == ESP_ERR_NOT_FOUND) {
|
||||
ESP_LOGE(TAG, "Unrecognized command: %s", line);
|
||||
} else if (err == ESP_ERR_INVALID_ARG) {
|
||||
} else if(err == ESP_ERR_INVALID_ARG) {
|
||||
// command was empty
|
||||
} else if (err != ESP_OK && ret != ESP_OK) {
|
||||
} else if(err != ESP_OK && ret != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Command returned non-zero error code: 0x%x (%s)", ret, esp_err_to_name(err));
|
||||
} else if (err == ESP_OK && ret != ESP_OK) {
|
||||
} else if(err == ESP_OK && ret != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Command returned in error");
|
||||
err = ESP_FAIL;
|
||||
} else if (err != ESP_OK) {
|
||||
} else if(err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Internal error: %s", esp_err_to_name(err));
|
||||
}
|
||||
return err;
|
||||
@@ -404,12 +380,12 @@ static esp_err_t run_command(char* line) {
|
||||
|
||||
static void* console_thread() {
|
||||
/* Main loop */
|
||||
while (1) {
|
||||
while(1) {
|
||||
/* Get a line using linenoise.
|
||||
* The line is returned when ENTER is pressed.
|
||||
*/
|
||||
char* line = linenoise(prompt);
|
||||
if (line == NULL) { /* Ignore empty lines */
|
||||
if(line == NULL) { /* Ignore empty lines */
|
||||
continue;
|
||||
}
|
||||
/* Add the command to the history */
|
||||
|
||||
Reference in New Issue
Block a user