77#include "lerp/task.h"
88#include "lerp/interact.h"
99#include "lerp/io.h"
10+ #include "lerp/tokeniser.h"
11+ #include "config/config.h"
1012
1113#include "pico/cyw43_arch.h"
1214
1315
14- // Static buffer for the command line ...
15- static char buffer [2048 ];
16-
17- static char wifi_ssid [32 ];
18- static char wifi_creds [32 ];
16+ // Circular buffer for the command line ...
17+ CIRC_DEFINE (buffer , 2048 );
1918
2019//
2120// Output some status information
2221//
23- void cmd_status (struct io * io ) {
24- char mac [6 ];
22+ void cmd_status (struct io * io , __unused struct circ * circ ) {
23+ uint8_t mac [6 ];
2524 int wifi_state ;
2625 char * state ;
2726 uint32_t ip ;
2827
29- static const char * states [] = { "joined" , "fail" , "no-net" , "bad-auth" };
30-
3128 cyw43_wifi_get_mac (& cyw43_state , CYW43_ITF_STA , mac );
3229 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 ]);
3330
@@ -56,39 +53,114 @@ void cmd_status(struct io *io) {
5653// set creds=rest of the line
5754// wifi-join
5855//
59- void cmd_set (struct io * io , char * buffer , int len ) {
60- if (strncmp (buffer , "set ssid=" , 9 ) == 0 ) {
56+ void cmd_set (struct io * io , struct circ * circ ) {
57+ int tok ;
58+ char item [32 ];
59+
60+ tok = token_get (circ );
61+ if (tok == TOK_END ) {
62+ // should output a list
63+ int maxlen = cf_max_name_len ();
64+ char * item = cf_next_item (NULL );
65+ while (item ) {
66+ int padding = maxlen - strlen (item );
67+ io_printf (io , "%s:%*s %s\r\n" , item , padding , "" , cf_get_strval (item ));
68+ item = cf_next_item (item );
69+ }
70+ return ;
71+ }
72+
73+ if (tok != TOK_WORD ) {
74+ io_printf (io , "expect set <word>=<value>\r\n" );
75+ return ;
76+ }
77+ strcpy (item , token_string ());
78+
79+ tok = token_get (circ );
80+ if (tok == TOK_END ) {
81+ // this is a request of the value
82+ io_printf (io , "have requested value of %s\r\n" , item );
83+ return ;
84+ }
85+ if (tok != TOK_EQUALS ) {
86+ io_printf (io , "expect set <word>=<value>\r\n" );
87+ return ;
88+ }
89+
90+ char * err = cf_set_with_tokens (item , circ );
91+ if (err ) {
92+ io_printf (io , "Blah error: %s\r\n" , err );
93+ }
94+
95+ // Now do a set with tokens...
96+ /*
97+ if (strncmp(word, "set ssid=", 9) == 0) {
6198 strcpy(wifi_ssid, buffer+9);
6299 } else if (strncmp(buffer, "set creds=", 10) == 0) {
63100 strcpy(wifi_creds, buffer+10);
64101 } else {
65102 io_printf(io, "Error: can only use 'set ssid=<something>' or 'set creds=<something>'\r\n");
66103 }
104+ */
105+ }
106+
107+ void cmd_join (struct io * io , __unused struct circ * circ ) {
108+ if (* cf -> main -> wifi .ssid && * cf -> main -> wifi .creds ) {
109+ cyw43_arch_wifi_connect_async (cf -> main -> wifi .ssid , cf -> main -> wifi .creds , CYW43_AUTH_WPA2_AES_PSK );
110+ io_printf (io , "join: started.\r\n" );
111+ } else {
112+ io_printf (io , "join: wifi ssid or credentials missing.\r\n" );
113+ }
67114}
68115
69- void cmd_join (struct io * io ) {
70- cyw43_arch_wifi_connect_async (wifi_ssid , wifi_creds , CYW43_AUTH_WPA2_AES_PSK );
116+ void cmd_save (struct io * io , __unused struct circ * circ ) {
117+ config_save ();
118+ io_printf (io , "config saved to flash.\r\n" );
71119}
72120
121+
122+ struct cmd_item {
123+ char * cmd ;
124+ void (* func )(struct io * io , struct circ * circ );
125+ };
126+
127+ struct cmd_item commands [] = {
128+ { "status" , cmd_status },
129+ { "set" , cmd_set },
130+ { "join" , cmd_join },
131+ { "save" , cmd_save },
132+ { NULL , NULL },
133+ };
134+
135+
73136//
74137// Quick command line interpretation.... will need to be redone, perhaps rethinking the
75138// tokeniser approach.
76139//
77- int process_cmdline (struct io * io , char * buffer , int len ) {
140+ int process_cmdline (struct io * io , struct circ * buffer ) {
78141 // let's make sure we are zero terminated...
79- buffer [len ] = 0 ;
80-
81- // Very limited capabilities for the moment...
82- if (strcmp (buffer , "status" ) == 0 ) {
83- cmd_status (io );
84- } else if (strncmp (buffer , "set " , 4 ) == 0 ) {
85- cmd_set (io , buffer , len );
86- } else if (strcmp (buffer , "wifi-join" ) == 0 ) {
87- cmd_join (io );
88- } else {
89- io_printf (io , "uunrecognised command.\r\n" );
142+ * buffer -> head = 0 ;
143+
144+ int tok = token_get (buffer );
145+ if (tok == TOK_END ) return 0 ;
146+ if (tok != TOK_WORD ) {
147+ io_printf (io , "expected a command word.\r\n" );
148+ return -1 ;
90149 }
91150
151+ char * word = token_string ();
152+
153+ struct cmd_item * c = commands ;
154+ while (c -> cmd ) {
155+ if (strcmp (word , c -> cmd ) == 0 ) {
156+ c -> func (io , buffer );
157+ return 0 ;
158+ }
159+ c ++ ;
160+ }
161+
162+ io_printf (io , "uunrecognised command.\r\n" );
163+ return 1 ;
92164}
93165
94166
@@ -130,6 +202,8 @@ char *asf() {
130202struct task * cmdline_task = NULL ;
131203struct io * cmdline_io = NULL ;
132204
205+
206+
133207DEFINE_TASK (cmdline , 1024 );
134208
135209DEFINE_TASK (dummy , 1024 );
@@ -141,7 +215,8 @@ void func_cmdline(void *arg) {
141215 cmdline_io = io_init (CMD_CDC , CMD_TCP , 4096 );
142216 cmdline_io -> support_telnet = 1 ;
143217
144- struct interact * i = interact_with_buf (cmdline_io , buffer , sizeof (buffer ), "pico-debug> " );
218+ // struct interact *i = interact_with_buf(cmdline_io, buffer, sizeof(buffer), "pico-debug> ");
219+ struct interact * i = interact_with_circ (cmdline_io , buffer , "pico-debug> " );
145220
146221 cmdline_task = current_task ();
147222
@@ -153,7 +228,7 @@ void func_cmdline(void *arg) {
153228 debug_printf ("Have i=%d\r\n" , err );
154229 int len = circ_used (i -> cmd );
155230 debug_printf ("CMD is [%.*s] (len=%d)\r\n" , len , buffer , len );
156- process_cmdline (cmdline_io , buffer , len );
231+ process_cmdline (cmdline_io , buffer );
157232 }
158233}
159234
0 commit comments