-
-
Notifications
You must be signed in to change notification settings - Fork 14
Added cgi #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added cgi #10
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
cmake_minimum_required(VERSION 3.19) | ||
cmake_minimum_required(VERSION 3.18) | ||
|
||
find_package(Perl) | ||
if(NOT PERL_FOUND) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright (c) 2023 Gerhard Schiller ([email protected]) | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
* https://www.gnu.org/licenses/gpl-3.0.txt | ||
* | ||
* This code is based on parts of the file: "LwIP/LwIP_HTTP_Server_Raw/Src/httpd_cg_ssi.c" | ||
* | ||
* | ||
* For details see the copyright notice below. | ||
* | ||
* Note: this STMicroelectronics software component has no LICENSE file | ||
* in its root directory. | ||
* | ||
*/ | ||
|
||
|
||
/** | ||
****************************************************************************** | ||
* @file LwIP/LwIP_HTTP_Server_Raw/Src/httpd_cg_ssi.c | ||
* @author MCD Application Team | ||
* @brief Webserver SSI and CGI handlers | ||
****************************************************************************** | ||
* @attention | ||
* | ||
* Copyright (c) 2017 STMicroelectronics. | ||
* All rights reserved. | ||
* | ||
* This software is licensed under terms that can be found in the LICENSE file | ||
* in the root directory of this software component. | ||
* If no LICENSE file comes with this software, it is provided AS-IS. | ||
* | ||
****************************************************************************** | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really in favor of adding the file with a specific author name and a different license. What if me or someone else wants to modify/extend it later? It makes maintenance far more complex. There is a common LICENSE file in the root of this repository (it's MIT though). If you're worried about the STMicroelectronics copyright, why not derive the example implementation from the lwIP example like the other examples in this repository? |
||
#include "lwip/apps/httpd.h" | ||
#include "pico/cyw43_arch.h" | ||
#include "lwipopts.h" | ||
|
||
|
||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
// GPIOs for Leds | ||
#define LED1 18 | ||
#define LED2 19 | ||
#define LED3 20 | ||
#define LED4 21 | ||
|
||
|
||
/* CGI handler for LED control */ | ||
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]); | ||
/* CGI handler for LED control with feedback*/ | ||
const char * LEDS_CGI_Handler2(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]); | ||
|
||
/* Html request for "/leds.cgi" will start LEDS_CGI_Handler */ | ||
const tCGI LEDS_CGI={"/leds.cgi", LEDS_CGI_Handler}; | ||
/* Html request for "/leds2.cgi" will start LEDS_CGI_Handler2 */ | ||
const tCGI LEDS_CGI2={"/leds2.cgi", LEDS_CGI_Handler2}; | ||
|
||
/* Cgi call table, two CGI used */ | ||
tCGI CGI_TAB[2]; | ||
|
||
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); | ||
} | ||
|
||
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) | ||
{ | ||
int i=0; | ||
|
||
/* First SSI handler iIndex = 0 (see CGI_TAB[] in cgi_init() )*/ | ||
if (iIndex==0) | ||
{ | ||
/* All leds off */ | ||
LED_Off(LED1); | ||
LED_Off(LED2); | ||
LED_Off(LED3); | ||
LED_Off(LED4); | ||
|
||
/* Check cgi parameter : application GET /leds.cgi?led=2&led=4 */ | ||
for (i=0; i<iNumParams; i++){ | ||
/* check parameter "led" */ | ||
if (strcmp(pcParam[i] , "led") == 0){ | ||
/* switch led1 ON if 1 */ | ||
if(strcmp(pcValue[i], "1") == 0) | ||
LED_On(LED1); | ||
|
||
/* switch led2 ON if 2 */ | ||
else if(strcmp(pcValue[i], "2") == 0) | ||
LED_On(LED2); | ||
|
||
/* switch led3 ON if 3 */ | ||
else if(strcmp(pcValue[i], "3") == 0) | ||
LED_On(LED3); | ||
|
||
/* switch led4 ON if 4 */ | ||
else if(strcmp(pcValue[i], "4") == 0) | ||
LED_On(LED4); | ||
} | ||
} | ||
} | ||
/* uri to send after cgi call*/ | ||
return "/cgi.html"; | ||
} | ||
|
||
const char * LEDS_CGI_Handler2(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) | ||
{ | ||
int i=0; | ||
|
||
/* Second SSI handler iIndex = 1 (see CGI_TAB[] in cgi_init() )*/ | ||
if (iIndex==1) | ||
{ | ||
/* All leds off */ | ||
LED_Off(LED1); | ||
LED_Off(LED2); | ||
LED_Off(LED3); | ||
LED_Off(LED4); | ||
|
||
/* Check cgi parameter : application GET /leds.cgi?led=2&led=4 */ | ||
for (i=0; i<iNumParams; i++){ | ||
/* check parameter "led" */ | ||
if (strcmp(pcParam[i] , "led") == 0){ | ||
/* switch led1 ON if 1 */ | ||
if(strcmp(pcValue[i], "1") == 0) | ||
LED_On(LED1); | ||
|
||
/* switch led2 ON if 2 */ | ||
else if(strcmp(pcValue[i], "2") == 0) | ||
LED_On(LED2); | ||
|
||
/* switch led3 ON if 3 */ | ||
else if(strcmp(pcValue[i], "3") == 0) | ||
LED_On(LED3); | ||
|
||
/* switch led4 ON if 4 */ | ||
else if(strcmp(pcValue[i], "4") == 0) | ||
LED_On(LED4); | ||
} | ||
} | ||
} | ||
/* uri to send after cgi call*/ | ||
return "/ssi_cgi.shtml"; | ||
} | ||
|
||
void cgi_init(void) | ||
{ | ||
/* configure CGI handler */ | ||
CGI_TAB[0] = LEDS_CGI; | ||
CGI_TAB[1] = LEDS_CGI2; | ||
http_set_cgi_handlers(CGI_TAB, 2); | ||
|
||
for(int i = LED1; i <= LED4; i++){ | ||
gpio_init(i); | ||
gpio_set_dir(i, GPIO_OUT); | ||
gpio_put(i, 0); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> <title>Pico W CGI-PAGE</title> </head> | ||
<body> <h1>Pico W</h1> | ||
<h3>CGI-PAGE</h3> | ||
|
||
<span> | ||
This page allows you to control four leds: LED1, LED2, LED3 and LED4 connected to GPIO18 - 21<br><br> | ||
To turn a led on/off check/uncheck its corresponding checkbox.<br> | ||
Then click on the "Send" button to submit the changes.<br> | ||
</span> | ||
<br> | ||
<form method="get" action="/leds.cgi"> | ||
<input value="1" name="led" type="checkbox">LED1<br> | ||
<input value="2" name="led" type="checkbox">LED2<br> | ||
<input value="3" name="led" type="checkbox">LED3<br> | ||
<input value="4" name="led" type="checkbox">LED4<br> | ||
<br> | ||
<input value="Send" type="submit"> | ||
</form> | ||
<br> | ||
<br> | ||
<a href="/index.html">Back</a> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> <title>Pico W</title> </head> | ||
<body> <h1>Pico W</h1> | ||
<head> <title>Pico W SSI-PAGE</title> </head> | ||
<body> | ||
<h1>Pico W</h1> | ||
<h3>SSI-PAGE</h3> | ||
<p><!--#Hello--> Times <!--#counter-->!</p> | ||
<p>GPIO26 voltage is: <!--#GPIO-->!</p> | ||
<br> | ||
<br> | ||
<a href="/index.html">Back</a> | ||
</body> | ||
</html> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> <title>Pico W CGI-SSI-PAGE</title> </head> | ||
<body> <h1>Pico W</h1> | ||
<h3>CGI with Feedback (ssi) PAGE</h3> | ||
|
||
<span> | ||
This page allows you to control four leds: LED1, LED2, LED3 and LED4 connected to GPIO18 - 21<br><br> | ||
To turn a led on/off check/uncheck its corresponding checkbox.<br> | ||
Then click on the "Send" button to submit the changes.<br> | ||
</span> | ||
<br> | ||
<form method="get" action="/leds2.cgi"> | ||
<input value="1" name="led" type="checkbox" <!--#state1-->> | ||
<span style=<!--#bg1-->>LED1</span><br> | ||
<input value="2" name="led" type="checkbox" <!--#state2-->> | ||
<span style=<!--#bg2-->>LED2</span><br> | ||
<input value="3" name="led" type="checkbox" <!--#state3-->> | ||
<span style=<!--#bg3-->>LED3</span><br> | ||
<input value="4" name="led" type="checkbox" <!--#state4-->> | ||
<span style=<!--#bg4-->>LED4</span><br> | ||
<input value="Send" type="submit"> | ||
</form> | ||
<br> | ||
<br> | ||
<a href="/index.html">Back</a> | ||
</body> | ||
</html> | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,11 +6,13 @@ | |||||
#include "pico/cyw43_arch.h" | ||||||
|
||||||
#include "lwipopts.h" | ||||||
#include "cgi.h" | ||||||
#include "ssi.h" | ||||||
|
||||||
void run_server() { | ||||||
httpd_init(); | ||||||
ssi_init(); | ||||||
cgi_init(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit-pick: please fix the indent here
Suggested change
|
||||||
printf("Http server initialized.\n"); | ||||||
// infinite loop for now | ||||||
for (;;) {} | ||||||
|
@@ -23,9 +25,6 @@ int main() { | |||||
printf("failed to initialise\n"); | ||||||
return 1; | ||||||
} | ||||||
// turn on LED to distinguish from BOOTSEL mode | ||||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); | ||||||
|
||||||
cyw43_arch_enable_sta_mode(); | ||||||
// this seems to be the best be can do using the predefined `cyw43_pm_value` macro: | ||||||
// cyw43_wifi_pm(&cyw43_state, CYW43_PERFORMANCE_PM); | ||||||
|
@@ -43,6 +42,9 @@ int main() { | |||||
auto ip_addr = cyw43_state.netif[CYW43_ITF_STA].ip_addr.addr; | ||||||
printf("IP Address: %lu.%lu.%lu.%lu\n", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, ip_addr >> 24); | ||||||
} | ||||||
|
||||||
// turn on LED to signal connected | ||||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); | ||||||
|
||||||
|
||||||
run_server(); | ||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.