diff --git a/usr/shelly/shelly.c b/usr/shelly/shelly.c index 4c2c285..9a8214c 100644 --- a/usr/shelly/shelly.c +++ b/usr/shelly/shelly.c @@ -12,10 +12,10 @@ #include #include #include - #include #include #include +#include #include "shelly.h" @@ -44,7 +44,8 @@ static void handle_lpuart_interrupt(void * arg) { global_state->buf[global_state->buffer_i] = '\0'; global_state->buffer_i += 1; - handle_shelly_cmd(global_state->buf, global_state->buffer_i); + err = handle_shelly_cmd(global_state->buf, global_state->buffer_i); + if (err_is_fail(err)) USER_PANIC_ERR(err, "while running command"); global_state->buffer_i = 0; diff --git a/usr/shelly/shelly_cmd.c b/usr/shelly/shelly_cmd.c index 169ec28..d2cc1e6 100644 --- a/usr/shelly/shelly_cmd.c +++ b/usr/shelly/shelly_cmd.c @@ -1,48 +1,82 @@ #include #include "shelly.h" +__attribute__((__used__)) +static void parse_cmd_line(char * line, int * argc, char ** argv) { + *argc = 0; + char *token = strtok(line, " "); + while (token != NULL) { + argv[*argc] = token; + token = strtok(NULL, " "); + *argc += 1; + } +} - -static void shelly_cmd_echo(char* argv[], int argc) { +static errval_t shelly_cmd_echo(char * line) { errval_t err; SHELLY_DEBUG("[shelly_cmd_echo] argc=%d\n", argc); - for (int i = 0; argv[1][i] != '\0'; ++i) { - err = lpuart_putchar(global_state->uart_s, argv[1][i]); + for (int i = 0; line[i] != '\0'; ++i) { + err = lpuart_putchar(global_state->uart_s, line[i]); if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character"); } err = lpuart_putchar(global_state->uart_s, '\0'); if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character"); + + return SYS_ERR_OK; } -static void handle_shelly_cmd(char* str, size_t len) { +static errval_t shelly_cmd_run(char * line) { + errval_t err; + + SHELLY_DEBUG("[shelly_cmd_run] line=%s\n", line); + + // start process + domainid_t pid; + struct aos_rpc * rpc = aos_rpc_get_process_channel(); + err = aos_rpc_process_spawn(rpc, line, 0, &pid); + if (err_is_fail(err)) USER_PANIC_ERR(err, "Failed to spawn process"); + SHELLY_DEBUG("[shelly_cmd_run] spawned process\n"); + + // // get process name + // char *name; + // err = aos_rpc_process_get_name(rpc, pid, &name); + // if (err_is_fail(err)) USER_PANIC_ERR(err, "Failed to get process name"); + // printf("Process Name: %s\n", name); + + // // get all PIDs + // domainid_t *pids; + // size_t pid_count; + // err = aos_rpc_process_get_all_pids(rpc, &pids, &pid_count); + // if (err_is_fail(err)) USER_PANIC_ERR(err, "Failed to get all PIDs"); + // printf("PIDs:\n"); + // for(size_t i = 0; i < pid_count; ++i) { + // printf(" %lu\n", pids[i]); + // } + + return SYS_ERR_OK; +} + +static errval_t handle_shelly_cmd(char* line, size_t len) { SHELLY_DEBUG("[handle_shelly_cmd] len=%d\n", len); - if (len <= 1) { - return; + return SYS_ERR_OK; } - int argc = 1; - for (int i = 0; i < len; ++i) { - if (str[i] == ' ') ++argc; - } - char * argv[argc]; + char * cmd = strtok(line, " "); - const char delim[2] = " "; + char * remaining_line = line + strlen(cmd) + 1; - for (int i = 0; i < argc; ++i) { - if (i == 0) { - argv[i] = strtok(str, delim); - } else { - argv[i] = strtok(NULL, delim); - } - SHELLY_DEBUG("[handle_shelly_cmd] arg[%d]=%s\n", i, argv[i]); - } + // debug_printf("remaining_line = %s\n", remaining_line); - if (strcmp(argv[0], "echo") == 0) { - shelly_cmd_echo(argv, argc); + if (strcmp(cmd, "echo") == 0) { + return shelly_cmd_echo(remaining_line); + } else if (strcmp(cmd, "run") == 0) { + return shelly_cmd_run(remaining_line); } else { SHELLY_DEBUG("unknown command %s\n", argv[0]); } + + return SYS_ERR_OK; }