improve power route handler and error handling,

add test sim for bmc power sw
This commit is contained in:
Arthur Lu 2024-02-14 13:33:20 -08:00
parent 426c04116f
commit 1557448bf9
6 changed files with 97 additions and 9 deletions

20
cgi.h
View File

@ -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[]) {

View File

@ -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

61
hw/BMC_PW_SW_Sim.asc Normal file
View File

@ -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

3
main.c
View File

@ -32,5 +32,8 @@ int main() {
cgi_init();
printf("CGI Handler initialised\n");
bmc_handler_init();
printf("BMC handler initialized\n");
while(1);
}

View File

@ -0,0 +1,4 @@
{
"error": true,
"description": "Missing search parameter requested_state"
}

View File

@ -0,0 +1,4 @@
{
"error": true,
"description": "Invalid requested_state must be 0 or 1"
}