Skip to content

Commit

Permalink
flash saving a better wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
essele committed Jul 23, 2022
1 parent 75631b5 commit d8d632c
Show file tree
Hide file tree
Showing 13 changed files with 1,514 additions and 27 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ add_executable(pico-debug

cmdline.c cmdline.h
utils.c utils.h

config/config.h
config/config.c

filedata.c filedata.h

Expand Down Expand Up @@ -70,6 +73,8 @@ add_subdirectory(modules/lerp_circ)
add_subdirectory(modules/lerp_debug)
add_subdirectory(modules/lerp_io)
add_subdirectory(modules/lerp_interact)
add_subdirectory(modules/lerp_flash)
add_subdirectory(modules/lerp_tokeniser)

target_compile_definitions(pico-debug PRIVATE
# We don't want the default alarm pool (and the IRQ's associated!)
Expand Down Expand Up @@ -130,4 +135,6 @@ target_link_libraries(pico-debug
lerp_io
lerp_debug
lerp_interact
lerp_flash
lerp_tokeniser
)
129 changes: 102 additions & 27 deletions cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
#include "lerp/task.h"
#include "lerp/interact.h"
#include "lerp/io.h"
#include "lerp/tokeniser.h"
#include "config/config.h"

#include "pico/cyw43_arch.h"


// Static buffer for the command line ...
static char buffer[2048];

static char wifi_ssid[32];
static char wifi_creds[32];
// Circular buffer for the command line ...
CIRC_DEFINE(buffer, 2048);

//
// Output some status information
//
void cmd_status(struct io *io) {
char mac[6];
void cmd_status(struct io *io, __unused struct circ *circ) {
uint8_t mac[6];
int wifi_state;
char *state;
uint32_t ip;

static const char *states[] = { "joined", "fail", "no-net", "bad-auth" };

cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, mac);
io_printf(io, "MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

Expand Down Expand Up @@ -56,39 +53,114 @@ void cmd_status(struct io *io) {
// set creds=rest of the line
// wifi-join
//
void cmd_set(struct io *io, char *buffer, int len) {
if (strncmp(buffer, "set ssid=", 9) == 0) {
void cmd_set(struct io *io, struct circ *circ) {
int tok;
char item[32];

tok = token_get(circ);
if (tok == TOK_END) {
// should output a list
int maxlen = cf_max_name_len();
char *item = cf_next_item(NULL);
while (item) {
int padding = maxlen - strlen(item);
io_printf(io, "%s:%*s %s\r\n", item, padding, "", cf_get_strval(item));
item = cf_next_item(item);
}
return;
}

if (tok != TOK_WORD) {
io_printf(io, "expect set <word>=<value>\r\n");
return;
}
strcpy(item, token_string());

tok = token_get(circ);
if (tok == TOK_END) {
// this is a request of the value
io_printf(io, "have requested value of %s\r\n", item);
return;
}
if (tok != TOK_EQUALS) {
io_printf(io, "expect set <word>=<value>\r\n");
return;
}

char *err = cf_set_with_tokens(item, circ);
if (err) {
io_printf(io, "Blah error: %s\r\n", err);
}

// Now do a set with tokens...
/*
if (strncmp(word, "set ssid=", 9) == 0) {
strcpy(wifi_ssid, buffer+9);
} else if (strncmp(buffer, "set creds=", 10) == 0) {
strcpy(wifi_creds, buffer+10);
} else {
io_printf(io, "Error: can only use 'set ssid=<something>' or 'set creds=<something>'\r\n");
}
*/
}

void cmd_join(struct io *io, __unused struct circ *circ) {
if (*cf->main->wifi.ssid && *cf->main->wifi.creds) {
cyw43_arch_wifi_connect_async(cf->main->wifi.ssid, cf->main->wifi.creds, CYW43_AUTH_WPA2_AES_PSK);
io_printf(io, "join: started.\r\n");
} else {
io_printf(io, "join: wifi ssid or credentials missing.\r\n");
}
}

void cmd_join(struct io *io) {
cyw43_arch_wifi_connect_async(wifi_ssid, wifi_creds, CYW43_AUTH_WPA2_AES_PSK);
void cmd_save(struct io *io, __unused struct circ *circ) {
config_save();
io_printf(io, "config saved to flash.\r\n");
}


struct cmd_item {
char *cmd;
void (*func)(struct io *io, struct circ *circ);
};

struct cmd_item commands[] = {
{ "status", cmd_status },
{ "set", cmd_set },
{ "join", cmd_join },
{ "save", cmd_save },
{ NULL, NULL },
};


//
// Quick command line interpretation.... will need to be redone, perhaps rethinking the
// tokeniser approach.
//
int process_cmdline(struct io *io, char *buffer, int len) {
int process_cmdline(struct io *io, struct circ *buffer) {
// let's make sure we are zero terminated...
buffer[len] = 0;

// Very limited capabilities for the moment...
if (strcmp(buffer, "status") == 0) {
cmd_status(io);
} else if (strncmp(buffer, "set ", 4) == 0) {
cmd_set(io, buffer, len);
} else if (strcmp(buffer, "wifi-join") == 0) {
cmd_join(io);
} else {
io_printf(io, "uunrecognised command.\r\n");
*buffer->head = 0;

int tok = token_get(buffer);
if (tok == TOK_END) return 0;
if (tok != TOK_WORD) {
io_printf(io, "expected a command word.\r\n");
return -1;
}

char *word = token_string();

struct cmd_item *c = commands;
while (c->cmd) {
if (strcmp(word, c->cmd) == 0) {
c->func(io, buffer);
return 0;
}
c++;
}

io_printf(io, "uunrecognised command.\r\n");
return 1;
}


Expand Down Expand Up @@ -130,6 +202,8 @@ char *asf() {
struct task *cmdline_task = NULL;
struct io *cmdline_io = NULL;



DEFINE_TASK(cmdline, 1024);

DEFINE_TASK(dummy, 1024);
Expand All @@ -141,7 +215,8 @@ void func_cmdline(void *arg) {
cmdline_io = io_init(CMD_CDC, CMD_TCP, 4096);
cmdline_io->support_telnet = 1;

struct interact *i = interact_with_buf(cmdline_io, buffer, sizeof(buffer), "pico-debug> ");
// struct interact *i = interact_with_buf(cmdline_io, buffer, sizeof(buffer), "pico-debug> ");
struct interact *i = interact_with_circ(cmdline_io, buffer, "pico-debug> ");

cmdline_task = current_task();

Expand All @@ -153,7 +228,7 @@ void func_cmdline(void *arg) {
debug_printf("Have i=%d\r\n", err);
int len = circ_used(i->cmd);
debug_printf("CMD is [%.*s] (len=%d)\r\n", len, buffer, len);
process_cmdline(cmdline_io, buffer, len);
process_cmdline(cmdline_io, buffer);
}
}

Expand Down
Loading

0 comments on commit d8d632c

Please sign in to comment.