diff --git a/.gitignore b/.gitignore index bc9e916..d9fe7f5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,7 @@ cmake/pico_sdk_import.cmake # clangd stuff .cache compile_commands.json + +#kate backups +*~ + diff --git a/CMakeLists.txt b/CMakeLists.txt index 18bf22d..9856d5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.19) +cmake_minimum_required(VERSION 3.18) find_package(Perl) if(NOT PERL_FOUND) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c44f51a..9864242 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,12 +14,13 @@ execute_process(COMMAND WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} ECHO_OUTPUT_VARIABLE ECHO_ERROR_VARIABLE - COMMAND_ERROR_IS_FATAL ANY ) file(RENAME fsdata.c my_fsdata.c) add_executable(${PROGRAM_NAME} main.cpp + ssi.cpp + cgi.cpp ) target_compile_definitions(${PROGRAM_NAME} PRIVATE WIFI_SSID=\"${WIFI_SSID}\" @@ -37,7 +38,7 @@ target_link_libraries(${PROGRAM_NAME} ) pico_enable_stdio_usb(${PROGRAM_NAME} TRUE) -pico_enable_stdio_uart(${PROGRAM_NAME} FALSE) +pico_enable_stdio_uart(${PROGRAM_NAME} TRUE) suppress_tinyusb_warnings() pico_add_extra_outputs(${PROGRAM_NAME}) diff --git a/src/cgi.cpp b/src/cgi.cpp new file mode 100644 index 0000000..10fb034 --- /dev/null +++ b/src/cgi.cpp @@ -0,0 +1,138 @@ +#include "lwip/apps/httpd.h" +#include "pico/cyw43_arch.h" +#include "lwipopts.h" +#include "cgi.h" + + +static const tCGI cgi_handlers[] = { + { + /* Html request for "/leds.cgi" will start cgi_handler_basic */ + "/leds.cgi", cgi_handler_basic + }, + { + /* Html request for "/leds2.cgi" will start cgi_handler_extended */ + "/leds_ext.cgi", cgi_handler_extended + } +}; + + + +/* cgi-handler triggered by a request for "/leds.cgi" */ +const char * +cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) +{ + int i=0; + + /* We use this handler for one page request only: "/leds.cgi" + * and it is at position 0 in the tCGI array (see above). + * So iIndex should be 0. + */ + printf("cgi_handler_basic called with index %d\n", iIndex); + + /* All leds off */ + Led_Off(LED1); + Led_Off(LED2); + Led_Off(LED3); + Led_Off(LED4); + + /* Check the query string. + * A request to turn LED2 and LED4 on would look like: "/leds.cgi?led=2&led=4" + */ + for (i = 0; i < iNumParams; i++){ + /* check if parameter is "led" */ + if (strcmp(pcParam[i] , "led") == 0){ + /* look ar argument to find which led to turn on */ + if(strcmp(pcValue[i], "1") == 0) + Led_On(LED1); + else if(strcmp(pcValue[i], "2") == 0) + Led_On(LED2); + else if(strcmp(pcValue[i], "3") == 0) + Led_On(LED3); + else if(strcmp(pcValue[i], "4") == 0) + Led_On(LED4); + } + } + + /* Our response to the "SUBMIT" is to simply send the same page again*/ + return "/cgi.html"; +} + +/* cgi-handler triggered by a request for "/leds_ext.cgi". + * + * It is almost identical to cgi_handler_basic(). + * Both handlers could be easily implemented in one function - + * distinguish them by looking at the iIndex parameter. + * I left it this way to show how to implement two (or more) + * enirely different handlers. + */ +const char * +cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) +{ + int i=0; + + /* We use this handler for one page request only: "/leds_ext.cgi" + * and it is at position 1 in the tCGI array (see above). + * So iIndex should be 1. + */ + printf("cgi_handler_extended called with index %d\n", iIndex); + + /* All leds off */ + Led_Off(LED1); + Led_Off(LED2); + Led_Off(LED3); + Led_Off(LED4); + + /* Check the query string. + * A request to turn LED2 and LED4 on would look like: "/leds.cgi?led=2&led=4" + */ + for (i = 0; i < iNumParams; i++){ + /* check if parameter is "led" */ + if (strcmp(pcParam[i] , "led") == 0){ + /* look ar argument to find which led to turn on */ + if(strcmp(pcValue[i], "1") == 0) + Led_On(LED1); + else if(strcmp(pcValue[i], "2") == 0) + Led_On(LED2); + else if(strcmp(pcValue[i], "3") == 0) + Led_On(LED3); + else if(strcmp(pcValue[i], "4") == 0) + Led_On(LED4); + } + } + + /* Our response to the "SUBMIT" is to send "/ssi_cgi.shtml". + * The extension ".shtml" tells the server to insert some values + * which show the user what has been done in response. + */ + return "/ssi_cgi.shtml"; +} + +/* initialize the CGI handler */ +void +cgi_init(void) +{ + http_set_cgi_handlers(cgi_handlers, 2); + + for(int i = LED1; i <= LED4; i++){ + gpio_init(i); + gpio_set_dir(i, GPIO_OUT); + gpio_put(i, 0); + } +} + +/* led control and debugging info */ +void +Led_On(int led) +{ + printf("GPIO%d on\n", led); + gpio_put(led, 1); +} + +void +Led_Off(int led) +{ + printf("GPIO%d off\n", led); + gpio_put(led, 0); +} + + diff --git a/src/cgi.h b/src/cgi.h new file mode 100644 index 0000000..de1f8cf --- /dev/null +++ b/src/cgi.h @@ -0,0 +1,23 @@ +#ifndef __CGI_H__ +#define __CGI_H__ + +// GPIOs for Leds +#define LED1 18 +#define LED2 19 +#define LED3 20 +#define LED4 21 + +/* initialize the CGI handler */ +void cgi_init(void); + +/* CGI handler for LED control */ +const char * cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]); +/* CGI handler for LED control with feedback*/ +const char * cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]); + + +/* led control and debugging info */ +void Led_On(int led); +void Led_Off(int led); + +#endif // __CGI_H__ diff --git a/src/fs/cgi.html b/src/fs/cgi.html new file mode 100644 index 0000000..ef3c8a4 --- /dev/null +++ b/src/fs/cgi.html @@ -0,0 +1,25 @@ + + +
Hello World
- -