|
| 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 | +} |
0 commit comments