From 1557448bf9b5dbb5c78a9f94fd9d7da664d85365 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 14 Feb 2024 13:33:20 -0800 Subject: [PATCH] improve power route handler and error handling, add test sim for bmc power sw --- cgi.h | 20 ++++++--- handlers.h | 14 ++++-- hw/BMC_PW_SW_Sim.asc | 61 +++++++++++++++++++++++++++ main.c | 3 ++ www/error_missing_requested_state.ssi | 4 ++ www/error_requested_state_invalid.ssi | 4 ++ 6 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 hw/BMC_PW_SW_Sim.asc create mode 100644 www/error_missing_requested_state.ssi create mode 100644 www/error_requested_state_invalid.ssi diff --git a/cgi.h b/cgi.h index 8d9dddc..395f2e9 100644 --- a/cgi.h +++ b/cgi.h @@ -5,19 +5,29 @@ #include "pico/cyw43_arch.h" #include "handlers.h" -const char * cgi_power_handler (int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) { +char parseRequestedState (char * requested_state) { + +} + +const char * cgi_power_handler (int iIndex, int iNumParams, char * pcParam [], char * pcValue []) { // Check if an request for power has been made (/power?requested_state=x) if (strcmp(pcParam[0] , "requested_state") == 0){ - // Look at the argument to check if LED is to be turned on (x=1) or off (x=0) - if(strcmp(pcValue[0], "0") == 0) { + if (strcmp(pcValue[0], "0") == 0) { bmc_power_handler(false); + return "/power.ssi"; } - else if(strcmp(pcValue[0], "1") == 0) { + else if (strcmp(pcValue[0], "1") == 0) { bmc_power_handler(true); + return "/power.ssi"; + } + else { + return "/error_requested_state_invalid.ssi"; } } // Send the index page back to the user - return "/power.ssi"; + else { + return "/error_missing_requested_state.ssi"; + } } const char * cgi_status_handler (int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) { diff --git a/handlers.h b/handlers.h index e384aca..c82933f 100644 --- a/handlers.h +++ b/handlers.h @@ -7,6 +7,7 @@ #define STATE_UPDATE_REPEAT_DELAY_MS 100 bool current_state = false; +struct repeating_timer * state_update_timer = NULL; int64_t pw_sw_on_async (alarm_id_t id, void * user_data) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); @@ -29,10 +30,15 @@ bool bmc_power_handler (bool requested_state) { } } -struct repeating_timer * bmc_handler_init () { - struct repeating_timer * timer = malloc(sizeof(struct repeating_timer)); - add_repeating_timer_ms(STATE_UPDATE_REPEAT_DELAY_MS, update_current_state_async, NULL, timer); - return timer; +void bmc_handler_init () { + state_update_timer = malloc(sizeof(struct repeating_timer)); + add_repeating_timer_ms(STATE_UPDATE_REPEAT_DELAY_MS, update_current_state_async, NULL, state_update_timer); +} + +void bmc_handler_deinit () { + cancel_repeating_timer(state_update_timer); + free(state_update_timer); + state_update_timer = NULL; } #endif \ No newline at end of file diff --git a/hw/BMC_PW_SW_Sim.asc b/hw/BMC_PW_SW_Sim.asc new file mode 100644 index 0000000..82ba557 --- /dev/null +++ b/hw/BMC_PW_SW_Sim.asc @@ -0,0 +1,61 @@ +Version 4 +SHEET 1 1792 680 +WIRE 240 -208 176 -208 +WIRE 448 -208 320 -208 +WIRE 1040 -208 448 -208 +WIRE 448 -64 448 -208 +WIRE 528 -64 496 -64 +WIRE 560 -64 528 -64 +WIRE 1040 -64 1040 -208 +WIRE 496 -48 496 -64 +WIRE 176 0 176 -208 +WIRE 496 16 496 0 +WIRE 560 16 496 16 +WIRE 1152 16 1088 16 +WIRE 1280 16 1232 16 +WIRE 1312 16 1280 16 +WIRE 176 144 176 80 +WIRE 448 144 448 16 +WIRE 448 144 176 144 +WIRE 1040 144 1040 32 +WIRE 1040 144 448 144 +WIRE 176 176 176 144 +WIRE 1312 176 1312 96 +FLAG 176 176 0 +FLAG 448 -208 PW_SW +FLAG 528 -64 SIG_SW +FLAG 1280 16 SIG_BMC +FLAG 1312 176 0 +SYMBOL voltage 176 -16 R0 +WINDOW 123 0 0 Left 0 +WINDOW 39 0 0 Left 0 +SYMATTR InstName V1 +SYMATTR Value 5 +SYMBOL res 336 -224 R90 +WINDOW 0 0 56 VBottom 2 +WINDOW 3 32 56 VTop 2 +SYMATTR InstName R1 +SYMATTR Value 10k +SYMBOL sw 448 32 R180 +SYMATTR InstName S1 +SYMATTR Value SW1 +SYMBOL voltage 560 -80 R0 +WINDOW 123 0 0 Left 0 +WINDOW 39 0 0 Left 0 +SYMATTR InstName V2 +SYMATTR Value PULSE(0 1 0.1 0 0 0.1 0.5 2) +SYMBOL voltage 1312 0 R0 +WINDOW 123 0 0 Left 0 +WINDOW 39 0 0 Left 0 +SYMATTR InstName V4 +SYMATTR Value PULSE(0 3.3 0.3 0 0 0.1 0.3 2) +SYMBOL nmos 1088 -64 M0 +SYMATTR InstName M1 +SYMATTR Value Si7336ADP +SYMBOL res 1248 0 R90 +WINDOW 0 0 56 VBottom 2 +WINDOW 3 32 56 VTop 2 +SYMATTR InstName R2 +SYMATTR Value 1k +TEXT 472 -120 Left 2 !.model SW1 sw(Vh=0 Vt=0.5) +TEXT 176 -312 Left 2 !.tran 1 diff --git a/main.c b/main.c index 0580dc7..fd29148 100644 --- a/main.c +++ b/main.c @@ -32,5 +32,8 @@ int main() { cgi_init(); printf("CGI Handler initialised\n"); + bmc_handler_init(); + printf("BMC handler initialized\n"); + while(1); } \ No newline at end of file diff --git a/www/error_missing_requested_state.ssi b/www/error_missing_requested_state.ssi new file mode 100644 index 0000000..fb75280 --- /dev/null +++ b/www/error_missing_requested_state.ssi @@ -0,0 +1,4 @@ +{ + "error": true, + "description": "Missing search parameter requested_state" +} \ No newline at end of file diff --git a/www/error_requested_state_invalid.ssi b/www/error_requested_state_invalid.ssi new file mode 100644 index 0000000..7cb9fd7 --- /dev/null +++ b/www/error_requested_state_invalid.ssi @@ -0,0 +1,4 @@ +{ + "error": true, + "description": "Invalid requested_state must be 0 or 1" +} \ No newline at end of file