Skip to content

Commit 0c4d38d

Browse files
Gerhard-Viennakrzmaz
authored andcommitted
Rewrote the cgi part
1 parent 33b8175 commit 0c4d38d

File tree

6 files changed

+155
-151
lines changed

6 files changed

+155
-151
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ cmake/pico_sdk_import.cmake
66
# clangd stuff
77
.cache
88
compile_commands.json
9+
10+
#kate backups
11+
*~
12+

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ file(RENAME fsdata.c my_fsdata.c)
2020

2121
add_executable(${PROGRAM_NAME}
2222
main.cpp
23+
cgi.cpp
2324
)
2425
target_compile_definitions(${PROGRAM_NAME} PRIVATE
2526
WIFI_SSID=\"${WIFI_SSID}\"

src/cgi.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include "lwip/apps/httpd.h"
2+
#include "pico/cyw43_arch.h"
3+
#include "lwipopts.h"
4+
#include "cgi.h"
5+
6+
7+
static const tCGI cgi_handlers[] = {
8+
{
9+
/* Html request for "/leds.cgi" will start cgi_handler_basic */
10+
"/leds.cgi", cgi_handler_basic
11+
},
12+
{
13+
/* Html request for "/leds2.cgi" will start cgi_handler_extended */
14+
"/leds_ext.cgi", cgi_handler_extended
15+
}
16+
};
17+
18+
19+
20+
/* cgi-handler triggered by a request for "/leds.cgi" */
21+
const char *
22+
cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
23+
{
24+
int i=0;
25+
26+
/* We use this handler only for one page request ("/leds.cgi")
27+
* and it is at position 0 in the tCGI array (see above).
28+
* So iIndex should be 0.
29+
*/
30+
LWIP_ASSERT("cgi_handler_basic called with wrong index", iIndex == 0);
31+
32+
/* All leds off */
33+
Led_Off(LED1);
34+
Led_Off(LED2);
35+
Led_Off(LED3);
36+
Led_Off(LED4);
37+
38+
/* Check the query string.
39+
* A request to turn LED2 and LED4 on would look like: "/leds.cgi?led=2&led=4"
40+
*/
41+
for (i = 0; i < iNumParams; i++){
42+
/* check if parameter is "led" */
43+
if (strcmp(pcParam[i] , "led") == 0){
44+
/* look ar argument to find which led to turn on */
45+
if(strcmp(pcValue[i], "1") == 0)
46+
Led_On(LED1);
47+
else if(strcmp(pcValue[i], "2") == 0)
48+
Led_On(LED2);
49+
else if(strcmp(pcValue[i], "3") == 0)
50+
Led_On(LED3);
51+
else if(strcmp(pcValue[i], "4") == 0)
52+
Led_On(LED4);
53+
}
54+
}
55+
56+
/* Our response to the "SUBMIT" is to simply send the same page again*/
57+
return "/cgi.html";
58+
}
59+
60+
/* cgi-handler triggered by a request for "/leds_ext.cgi".
61+
*
62+
* It is almost identical to cgi_handler_basic().
63+
* Both handlers could be easily implemented in one function -
64+
* distinguish them by looking at the iIndex parameter.
65+
* I left it this way to show how to implement two (or more)
66+
* enirely different handlers.
67+
*/
68+
const char *
69+
cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
70+
{
71+
int i=0;
72+
73+
/* We use this handler only for one request ("/leds_ext.cgi")
74+
* and it is at position 1 in the tCGI array (see above).
75+
* So iIndex should be 1.
76+
*/
77+
LWIP_ASSERT("cgi_handler_basic called with wrong index", iIndex == 1);
78+
79+
/* All leds off */
80+
Led_Off(LED1);
81+
Led_Off(LED2);
82+
Led_Off(LED3);
83+
Led_Off(LED4);
84+
85+
/* Check the query string.
86+
* A request to turn LED2 and LED4 on would look like: "/leds.cgi?led=2&led=4"
87+
*/
88+
for (i = 0; i < iNumParams; i++){
89+
/* check if parameter is "led" */
90+
if (strcmp(pcParam[i] , "led") == 0){
91+
/* look ar argument to find which led to turn on */
92+
if(strcmp(pcValue[i], "1") == 0)
93+
Led_On(LED1);
94+
else if(strcmp(pcValue[i], "2") == 0)
95+
Led_On(LED2);
96+
else if(strcmp(pcValue[i], "3") == 0)
97+
Led_On(LED3);
98+
else if(strcmp(pcValue[i], "4") == 0)
99+
Led_On(LED4);
100+
}
101+
}
102+
103+
/* Our response to the "SUBMIT" is to send "/ssi_cgi.shtml".
104+
* The extension ".shtml" tells the server to insert some values
105+
* which show the user what has been done in response.
106+
*/
107+
return "/ssi_cgi.shtml";
108+
}
109+
110+
/* initialize the CGI handler */
111+
void
112+
cgi_init(void)
113+
{
114+
http_set_cgi_handlers(cgi_handlers, 2);
115+
116+
for(int i = LED1; i <= LED4; i++){
117+
gpio_init(i);
118+
gpio_set_dir(i, GPIO_OUT);
119+
gpio_put(i, 0);
120+
}
121+
}
122+
123+
/* led control and debugging info */
124+
void
125+
Led_On(int led)
126+
{
127+
printf("GPIO%d on\n", led);
128+
gpio_put(led, 1);
129+
}
130+
131+
void
132+
Led_Off(int led)
133+
{
134+
printf("GPIO%d off\n", led);
135+
gpio_put(led, 0);
136+
}
137+
138+

src/cgi.h

Lines changed: 10 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,23 @@
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>
1+
#ifndef __CGI_H__
2+
#define __CGI_H__
413

424
// GPIOs for Leds
435
#define LED1 18
446
#define LED2 19
457
#define LED3 20
468
#define LED4 21
479

10+
/* initialize the CGI handler */
11+
void cgi_init(void);
4812

4913
/* CGI handler for LED control */
50-
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
14+
const char * cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
5115
/* 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);
16+
const char * cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
13917

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-
}
14918

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);
19+
/* led control and debugging info */
20+
void Led_On(int led);
21+
void Led_Off(int led);
15622

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-
}
23+
#endif // __CGI_H__

src/fs/ssi_cgi.shtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Then click on the "Send" button to submit the changes.<br>
1111
</span>
1212
<br>
13-
<form method="get" action="/leds2.cgi">
13+
<form method="get" action="/leds_ext.cgi">
1414
<input value="1" name="led" type="checkbox" <!--#state1-->>
1515
<span style=<!--#bg1-->>LED1</span><br>
1616
<input value="2" name="led" type="checkbox" <!--#state2-->>

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
void run_server() {
1313
httpd_init();
1414
ssi_init();
15-
cgi_init();
15+
cgi_init();
1616
printf("Http server initialized.\n");
1717
// infinite loop for now
1818
for (;;) {}

0 commit comments

Comments
 (0)