add serial handler,
move network init to network.h
This commit is contained in:
		| @@ -13,6 +13,7 @@ target_include_directories(${PROGRAM_NAME} PRIVATE | |||||||
| target_link_libraries(${PROGRAM_NAME} | target_link_libraries(${PROGRAM_NAME} | ||||||
|     pico_cyw43_arch_lwip_threadsafe_background |     pico_cyw43_arch_lwip_threadsafe_background | ||||||
|     pico_stdlib |     pico_stdlib | ||||||
|  |     pico_multicore | ||||||
|     hardware_adc |     hardware_adc | ||||||
| ) | ) | ||||||
| pico_enable_stdio_usb(${PROGRAM_NAME} TRUE) | pico_enable_stdio_usb(${PROGRAM_NAME} TRUE) | ||||||
|   | |||||||
							
								
								
									
										47
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								main.c
									
									
									
									
									
								
							| @@ -6,18 +6,11 @@ | |||||||
|  |  | ||||||
| #include "secret.h" | #include "secret.h" | ||||||
| #include "pico_lib.h" | #include "pico_lib.h" | ||||||
|  | #include "network.h" | ||||||
| #include "bmc_handler.h" | #include "bmc_handler.h" | ||||||
| #include "http_serv.h" | #include "http_serv.h" | ||||||
| #include "serial_handler.h" | #include "serial_handler.h" | ||||||
|  |  | ||||||
| void set_host_name(const char * hostname) { |  | ||||||
| 	cyw43_arch_lwip_begin(); |  | ||||||
| 	struct netif * n = &cyw43_state.netif[CYW43_ITF_STA]; |  | ||||||
| 	netif_set_hostname(n, hostname); |  | ||||||
| 	netif_set_up(n); |  | ||||||
| 	cyw43_arch_lwip_end(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| int main() { | int main() { | ||||||
| 	stdio_init_all(); | 	stdio_init_all(); | ||||||
|  |  | ||||||
| @@ -25,34 +18,42 @@ int main() { | |||||||
| 		DEBUG_printf("[INIT] [ERR] Failed to initialise cyw43\n"); | 		DEBUG_printf("[INIT] [ERR] Failed to initialise cyw43\n"); | ||||||
| 		return 1; | 		return 1; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	serial_handler_init(); |  | ||||||
|  |  | ||||||
| 	cyw43_arch_enable_sta_mode(); | 	cyw43_arch_enable_sta_mode(); | ||||||
| 	set_host_name(BMC_HOSTNAME); |  | ||||||
| 	DEBUG_printf("[INIT] [OK ] Set hostname to %s\n", BMC_HOSTNAME); |  | ||||||
|  |  | ||||||
| 	if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASS, CYW43_AUTH_WPA2_AES_PSK, 30000)){ |  | ||||||
| 		DEBUG_printf("[INIT] [ERR] Wi-Fi failed to connect\n"); |  | ||||||
| 		return -1; |  | ||||||
| 	} |  | ||||||
| 	DEBUG_printf("[INIT] [OK ] Wi-Fi connected successfully\n"); |  | ||||||
|  |  | ||||||
|  | 	network_init(BMC_HOSTNAME, WIFI_SSID, WIFI_PASS); | ||||||
| 	http_serv_init(); | 	http_serv_init(); | ||||||
|  |  | ||||||
| 	// init bmc handler |  | ||||||
| 	bmc_handler_init(); | 	bmc_handler_init(); | ||||||
|  | 	serial_handler_init(); | ||||||
|  |  | ||||||
| 	// set LED to on to indicate it has connected and initialized | 	// set LED to on to indicate it has connected and initialized | ||||||
| 	cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); | 	cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); | ||||||
|  |  | ||||||
|  | 	char command [BUF_SIZE]; | ||||||
| 	while (!http_serv_state->complete) { | 	while (!http_serv_state->complete) { | ||||||
| 		sleep_ms(1000); | 		printf("> "); | ||||||
|  | 		serial_get_line(command, BUF_SIZE); | ||||||
|  |  | ||||||
|  | 		if (strcmp(command, "help") == 0) { | ||||||
|  | 			printf("PICO BMC Shell\n\nhelp\tshow this help message\ninfo\tshow network name, hostname, ip address, and http server port\nclear\tclears the screen\n"); | ||||||
|  | 		} | ||||||
|  | 		else if (strcmp(command, "info") == 0) { | ||||||
|  | 			printf("Network: %s\nHostname: %s\nAddress: %s\nHTTP Port: %i\n", WIFI_SSID, BMC_HOSTNAME, ip_ntoa(netif_ip4_addr(&cyw43_state.netif[CYW43_ITF_STA])), HTTP_PORT); | ||||||
|  | 		} | ||||||
|  | 		else if (strcmp(command, "clear") == 0) { | ||||||
|  | 			printf("\033[2J\033[H"); | ||||||
|  | 		} | ||||||
|  | 		else if (strcmp(command, "") == 0) { | ||||||
|  | 			continue; | ||||||
|  | 		} | ||||||
|  | 		else { | ||||||
|  | 			printf("Unknown command: %s\n", command); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	serial_handler_deinit(); | ||||||
| 	bmc_handler_deinit(); | 	bmc_handler_deinit(); | ||||||
| 	http_serv_deinit(); | 	http_serv_deinit(); | ||||||
| 	serial_handler_deinit(); | 	network_deinit(); | ||||||
| 	cyw43_arch_deinit(); | 	cyw43_arch_deinit(); | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
							
								
								
									
										28
									
								
								network.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								network.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | |||||||
|  | #ifndef NETWORK_H | ||||||
|  | #define NETWORK_H | ||||||
|  |  | ||||||
|  | void set_host_name(const char * hostname) { | ||||||
|  | 	cyw43_arch_lwip_begin(); | ||||||
|  | 	struct netif * n = &cyw43_state.netif[CYW43_ITF_STA]; | ||||||
|  | 	netif_set_hostname(n, hostname); | ||||||
|  | 	netif_set_up(n); | ||||||
|  | 	cyw43_arch_lwip_end(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int network_init (char * hostname, char * wifi_ssid, char * wifi_pass) { | ||||||
|  |     set_host_name(hostname); | ||||||
|  |     DEBUG_printf("[INIT] [OK ] Set hostname to %s\n", BMC_HOSTNAME); | ||||||
|  |  | ||||||
|  | 	if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASS, CYW43_AUTH_WPA2_AES_PSK, 30000)){ | ||||||
|  | 		DEBUG_printf("[INIT] [ERR] Wi-Fi failed to connect\n"); | ||||||
|  | 		return -1; | ||||||
|  | 	} | ||||||
|  | 	else { | ||||||
|  | 		DEBUG_printf("[INIT] [OK ] Wi-Fi connected successfully\n"); | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int network_deinit () {} | ||||||
|  |  | ||||||
|  | #endif | ||||||
| @@ -3,6 +3,8 @@ | |||||||
|  |  | ||||||
| #include "pico/cyw43_arch.h" | #include "pico/cyw43_arch.h" | ||||||
| #include "pico/stdlib.h" | #include "pico/stdlib.h" | ||||||
|  | #include "pico/multicore.h" | ||||||
|  | #include "pico/mutex.h" | ||||||
| #include "hardware/adc.h" | #include "hardware/adc.h" | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
| @@ -1,13 +1,18 @@ | |||||||
| #ifndef SERIAL_HANDLER_H | #ifndef SERIAL_HANDLER_H | ||||||
| #define SERIAL_HANDLER_H | #define SERIAL_HANDLER_H | ||||||
|  |  | ||||||
| #define INPUT_BUFFER_SIZE 1024 | #define BUF_SIZE 1024 | ||||||
|  |  | ||||||
| char * input_buffer; | char * input_buffer_0; | ||||||
|  | char * input_buffer_1; | ||||||
|  | char * input_buffers [2] = {NULL, NULL}; | ||||||
|  | bool active_write_input_buffer; | ||||||
| int write_offset; | int write_offset; | ||||||
|  | semaphore_t sem_line; | ||||||
|  |  | ||||||
| bool inc_write_offset () { | int append_char (char c) { | ||||||
| 	if (write_offset < INPUT_BUFFER_SIZE - 1) { | 	if (write_offset < BUF_SIZE - 1) { | ||||||
|  | 		input_buffers[active_write_input_buffer][write_offset] = c; | ||||||
| 		write_offset++; | 		write_offset++; | ||||||
| 		return true; | 		return true; | ||||||
| 	} | 	} | ||||||
| @@ -17,9 +22,10 @@ bool inc_write_offset () { | |||||||
| 	}	 | 	}	 | ||||||
| } | } | ||||||
|  |  | ||||||
| bool dec_write_offset () { | int delete_char () { | ||||||
| 	if (write_offset > 0) { | 	if (write_offset > 0) { | ||||||
| 		write_offset--; | 		write_offset--; | ||||||
|  | 		input_buffers[active_write_input_buffer][write_offset] = 0; | ||||||
| 		return true; | 		return true; | ||||||
| 	} | 	} | ||||||
| 	else { | 	else { | ||||||
| @@ -28,20 +34,13 @@ bool dec_write_offset () { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void rst_write_offset () { | void reset_input () { | ||||||
| 	write_offset = 0; | 	write_offset = 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| void handle_return () { | void serial_get_line (char * buf, int len) { | ||||||
| 	if (strcmp(input_buffer, "") == 0) { | 	sem_acquire_blocking(&sem_line); | ||||||
| 		return; | 	memcpy(buf, input_buffers[!active_write_input_buffer], BUF_SIZE > len ? BUF_SIZE : len); | ||||||
| 	} |  | ||||||
| 	else if (strcmp(input_buffer, "info") == 0) { |  | ||||||
| 		printf("Network: %s\nHostname: %s\nAddress: %s\nHTTP Port: %i\n", WIFI_SSID, BMC_HOSTNAME, ip_ntoa(netif_ip4_addr(&cyw43_state.netif[CYW43_ITF_STA])), HTTP_PORT); |  | ||||||
| 	} |  | ||||||
| 	else { |  | ||||||
| 		printf("Unknown command: %s\n", input_buffer); |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // interrupt code adapted from: https://github.com/raspberrypi/pico-examples/blob/eca13acf57916a0bd5961028314006983894fc84/pico_w/wifi/iperf/picow_iperf.c | // interrupt code adapted from: https://github.com/raspberrypi/pico-examples/blob/eca13acf57916a0bd5961028314006983894fc84/pico_w/wifi/iperf/picow_iperf.c | ||||||
| @@ -53,21 +52,20 @@ void key_pressed_worker_func(async_context_t * context, async_when_pending_worke | |||||||
| 	int key; | 	int key; | ||||||
| 	while ((key = getchar_timeout_us(0)) > 0) { | 	while ((key = getchar_timeout_us(0)) > 0) { | ||||||
| 		if (key == 0x08 || key == 0x7F) { // backspace & delete | 		if (key == 0x08 || key == 0x7F) { // backspace & delete | ||||||
| 			if (dec_write_offset()) { | 			if (delete_char()) { | ||||||
| 				printf("\b \b"); | 				printf("\b \b"); | ||||||
| 				input_buffer[write_offset] = 0; |  | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		else if (key == '\r' || key == '\n') { // return | 		else if (key == '\r' || key == '\n') { // return | ||||||
| 			printf("\n"); | 			printf("\n"); | ||||||
| 			input_buffer[write_offset] = 0; | 			append_char(0); // ensure there is a null terminator | ||||||
| 			handle_return(); | 			active_write_input_buffer = !active_write_input_buffer; // flip the active buffer | ||||||
| 			rst_write_offset(); | 			reset_input(); // reset write pointer to 0 | ||||||
|  | 			sem_release(&sem_line); // release sem to allow serial_get_line to resume | ||||||
| 		} | 		} | ||||||
| 		else if (32 <= key && key <= 126) { // printable characters | 		else if (32 <= key && key <= 126) { // printable characters | ||||||
| 			printf("%c", key); | 			printf("%c", key); | ||||||
| 			input_buffer[write_offset] = key; | 			append_char(key); | ||||||
| 			inc_write_offset(); |  | ||||||
| 		} | 		} | ||||||
| 		else if (key == 27) { // escape & escape codes | 		else if (key == 27) { // escape & escape codes | ||||||
| 			while (getchar_timeout_us(0) > 0) {} | 			while (getchar_timeout_us(0) > 0) {} | ||||||
| @@ -90,14 +88,22 @@ void key_pressed_func(void * param) { | |||||||
|  |  | ||||||
| // init serial handler | // init serial handler | ||||||
| void serial_handler_init () { | void serial_handler_init () { | ||||||
| 	input_buffer = malloc(INPUT_BUFFER_SIZE * sizeof(char)); | 	input_buffer_0 = malloc(BUF_SIZE * sizeof(char)); | ||||||
|  | 	input_buffer_1 = malloc(BUF_SIZE * sizeof(char)); | ||||||
|  | 	input_buffers[0] = input_buffer_0; | ||||||
|  | 	input_buffers[1] = input_buffer_1; | ||||||
|  | 	sem_init(&sem_line, 0, 1); | ||||||
| 	async_context_add_when_pending_worker(cyw43_arch_async_context(), &key_pressed_worker); | 	async_context_add_when_pending_worker(cyw43_arch_async_context(), &key_pressed_worker); | ||||||
| 	stdio_set_chars_available_callback(key_pressed_func, cyw43_arch_async_context()); | 	stdio_set_chars_available_callback(key_pressed_func, cyw43_arch_async_context()); | ||||||
| } | } | ||||||
|  |  | ||||||
| void serial_handler_deinit () { | void serial_handler_deinit () { | ||||||
| 	free(input_buffer); | 	free(input_buffer_0); | ||||||
| 	input_buffer = NULL; | 	free(input_buffer_1); | ||||||
|  | 	input_buffer_0 = NULL; | ||||||
|  | 	input_buffer_1 = NULL; | ||||||
|  | 	input_buffers[0] = NULL; | ||||||
|  | 	input_buffers[1] = NULL; | ||||||
| } | } | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
| @@ -1,8 +1,8 @@ | |||||||
| #ifndef SECRET_H | #ifndef SECRET_H | ||||||
| #define SECRET_H | #define SECRET_H | ||||||
|  |  | ||||||
| const char BMC_HOSTNAME[] = "pico_bmc"; | const char BMC_HOSTNAME [] = "pico_bmc"; | ||||||
| const char WIFI_SSID[] = "ssid"; | const char WIFI_SSID [] = "ssid"; | ||||||
| const char WIFI_PASS[] = "pass"; | const char WIFI_PASS [] = "pass"; | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
		Reference in New Issue
	
	Block a user