Skip to content

Commit 33b8175

Browse files
Gerhard-Viennakrzmaz
authored andcommitted
Added new files
1 parent e176dde commit 33b8175

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

src/cgi.h

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) 2023 Gerhard Schiller ([email protected])
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
* https://www.gnu.org/licenses/gpl-3.0.txt
5+
*
6+
* This code is based on parts of the file: "LwIP/LwIP_HTTP_Server_Raw/Src/httpd_cg_ssi.c"
7+
*
8+
*
9+
* For details see the copyright notice below.
10+
*
11+
* Note: this STMicroelectronics software component has no LICENSE file
12+
* in its root directory.
13+
*
14+
*/
15+
16+
17+
/**
18+
******************************************************************************
19+
* @file LwIP/LwIP_HTTP_Server_Raw/Src/httpd_cg_ssi.c
20+
* @author MCD Application Team
21+
* @brief Webserver SSI and CGI handlers
22+
******************************************************************************
23+
* @attention
24+
*
25+
* Copyright (c) 2017 STMicroelectronics.
26+
* All rights reserved.
27+
*
28+
* This software is licensed under terms that can be found in the LICENSE file
29+
* in the root directory of this software component.
30+
* If no LICENSE file comes with this software, it is provided AS-IS.
31+
*
32+
******************************************************************************
33+
*/
34+
#include "lwip/apps/httpd.h"
35+
#include "pico/cyw43_arch.h"
36+
#include "lwipopts.h"
37+
38+
39+
#include <string.h>
40+
#include <stdlib.h>
41+
42+
// GPIOs for Leds
43+
#define LED1 18
44+
#define LED2 19
45+
#define LED3 20
46+
#define LED4 21
47+
48+
49+
/* CGI handler for LED control */
50+
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
51+
/* CGI handler for LED control with feedback*/
52+
const char * LEDS_CGI_Handler2(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
53+
54+
/* Html request for "/leds.cgi" will start LEDS_CGI_Handler */
55+
const tCGI LEDS_CGI={"/leds.cgi", LEDS_CGI_Handler};
56+
/* Html request for "/leds2.cgi" will start LEDS_CGI_Handler2 */
57+
const tCGI LEDS_CGI2={"/leds2.cgi", LEDS_CGI_Handler2};
58+
59+
/* Cgi call table, two CGI used */
60+
tCGI CGI_TAB[2];
61+
62+
void LED_On(int led){
63+
printf("GPIO%d on\n", led);
64+
gpio_put(led, 1);
65+
}
66+
67+
void LED_Off(int led){
68+
printf("GPIO%d off\n", led);
69+
gpio_put(led, 0);
70+
}
71+
72+
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
73+
{
74+
int i=0;
75+
76+
/* First SSI handler iIndex = 0 (see CGI_TAB[] in cgi_init() )*/
77+
if (iIndex==0)
78+
{
79+
/* All leds off */
80+
LED_Off(LED1);
81+
LED_Off(LED2);
82+
LED_Off(LED3);
83+
LED_Off(LED4);
84+
85+
/* Check cgi parameter : application GET /leds.cgi?led=2&led=4 */
86+
for (i=0; i<iNumParams; i++){
87+
/* check parameter "led" */
88+
if (strcmp(pcParam[i] , "led") == 0){
89+
/* switch led1 ON if 1 */
90+
if(strcmp(pcValue[i], "1") == 0)
91+
LED_On(LED1);
92+
93+
/* switch led2 ON if 2 */
94+
else if(strcmp(pcValue[i], "2") == 0)
95+
LED_On(LED2);
96+
97+
/* switch led3 ON if 3 */
98+
else if(strcmp(pcValue[i], "3") == 0)
99+
LED_On(LED3);
100+
101+
/* switch led4 ON if 4 */
102+
else if(strcmp(pcValue[i], "4") == 0)
103+
LED_On(LED4);
104+
}
105+
}
106+
}
107+
/* uri to send after cgi call*/
108+
return "/cgi.html";
109+
}
110+
111+
const char * LEDS_CGI_Handler2(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
112+
{
113+
int i=0;
114+
115+
/* Second SSI handler iIndex = 1 (see CGI_TAB[] in cgi_init() )*/
116+
if (iIndex==1)
117+
{
118+
/* All leds off */
119+
LED_Off(LED1);
120+
LED_Off(LED2);
121+
LED_Off(LED3);
122+
LED_Off(LED4);
123+
124+
/* Check cgi parameter : application GET /leds.cgi?led=2&led=4 */
125+
for (i=0; i<iNumParams; i++){
126+
/* check parameter "led" */
127+
if (strcmp(pcParam[i] , "led") == 0){
128+
/* switch led1 ON if 1 */
129+
if(strcmp(pcValue[i], "1") == 0)
130+
LED_On(LED1);
131+
132+
/* switch led2 ON if 2 */
133+
else if(strcmp(pcValue[i], "2") == 0)
134+
LED_On(LED2);
135+
136+
/* switch led3 ON if 3 */
137+
else if(strcmp(pcValue[i], "3") == 0)
138+
LED_On(LED3);
139+
140+
/* switch led4 ON if 4 */
141+
else if(strcmp(pcValue[i], "4") == 0)
142+
LED_On(LED4);
143+
}
144+
}
145+
}
146+
/* uri to send after cgi call*/
147+
return "/ssi_cgi.shtml";
148+
}
149+
150+
void cgi_init(void)
151+
{
152+
/* configure CGI handler */
153+
CGI_TAB[0] = LEDS_CGI;
154+
CGI_TAB[1] = LEDS_CGI2;
155+
http_set_cgi_handlers(CGI_TAB, 2);
156+
157+
for(int i = LED1; i <= LED4; i++){
158+
gpio_init(i);
159+
gpio_set_dir(i, GPIO_OUT);
160+
gpio_put(i, 0);
161+
}
162+
}

src/fs/cgi.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head> <title>Pico W CGI-PAGE</title> </head>
4+
<body> <h1>Pico W</h1>
5+
<h3>CGI-PAGE</h3>
6+
7+
<span>
8+
This page allows you to control four leds: LED1, LED2, LED3 and LED4 connected to GPIO18 - 21<br><br>
9+
To turn a led on/off check/uncheck its corresponding checkbox.<br>
10+
Then click on the "Send" button to submit the changes.<br>
11+
</span>
12+
<br>
13+
<form method="get" action="/leds.cgi">
14+
<input value="1" name="led" type="checkbox">LED1<br>
15+
<input value="2" name="led" type="checkbox">LED2<br>
16+
<input value="3" name="led" type="checkbox">LED3<br>
17+
<input value="4" name="led" type="checkbox">LED4<br>
18+
<br>
19+
<input value="Send" type="submit">
20+
</form>
21+
<br>
22+
<br>
23+
<a href="/index.html">Back</a>
24+
</body>
25+
</html>

src/fs/ssi_cgi.shtml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head> <title>Pico W CGI-SSI-PAGE</title> </head>
4+
<body> <h1>Pico W</h1>
5+
<h3>CGI with Feedback (ssi) PAGE</h3>
6+
7+
<span>
8+
This page allows you to control four leds: LED1, LED2, LED3 and LED4 connected to GPIO18 - 21<br><br>
9+
To turn a led on/off check/uncheck its corresponding checkbox.<br>
10+
Then click on the "Send" button to submit the changes.<br>
11+
</span>
12+
<br>
13+
<form method="get" action="/leds2.cgi">
14+
<input value="1" name="led" type="checkbox" <!--#state1-->>
15+
<span style=<!--#bg1-->>LED1</span><br>
16+
<input value="2" name="led" type="checkbox" <!--#state2-->>
17+
<span style=<!--#bg2-->>LED2</span><br>
18+
<input value="3" name="led" type="checkbox" <!--#state3-->>
19+
<span style=<!--#bg3-->>LED3</span><br>
20+
<input value="4" name="led" type="checkbox" <!--#state4-->>
21+
<span style=<!--#bg4-->>LED4</span><br>
22+
<input value="Send" type="submit">
23+
</form>
24+
<br>
25+
<br>
26+
<a href="/index.html">Back</a>
27+
</body>
28+
</html>
29+

0 commit comments

Comments
 (0)