httpd-cgi.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright (c) 2001-2006, Adam Dunkels.
00016  * All rights reserved.
00017  *
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions
00020  * are met:
00021  * 1. Redistributions of source code must retain the above copyright
00022  *    notice, this list of conditions and the following disclaimer.
00023  * 2. Redistributions in binary form must reproduce the above copyright
00024  *    notice, this list of conditions and the following disclaimer in the
00025  *    documentation and/or other materials provided with the distribution.
00026  * 3. The name of the author may not be used to endorse or promote
00027  *    products derived from this software without specific prior
00028  *    written permission.
00029  *
00030  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00031  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00032  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00034  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00036  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00037  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00038  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00039  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00040  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00041  *
00042  * This file is part of the uIP TCP/IP stack.
00043  *
00044  * $Id: httpd-cgi.c,v 1.2 2006/06/11 21:46:37 adam Exp $
00045  *
00046  */
00047 
00048 #include "uip.h"
00049 #include "psock.h"
00050 #include "httpd.h"
00051 #include "httpd-cgi.h"
00052 #include "httpd-fs.h"
00053 #inclide "http_func.h"
00054 
00055 #include "../mprintf/mprintf.h"
00056 #include <string.h>
00057 #include <stdio.h>
00058 
00059 HTTPD_CGI_CALL(temp, "tag-temp", cgitemperature );
00060 HTTPD_CGI_CALL(setp, "tag-setpoint", cgisetpoint );
00061 HTTPD_CGI_CALL(estad, "tag-estado", cgiestado );
00062 
00063 
00064 static const struct httpd_cgi_call *calls[] = { &temp, &setp, &estad, NULL };
00065 
00066 /*---------------------------------------------------------------------------*/
00067 static
00068 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
00069 {
00070   PSOCK_BEGIN(&s->sout);
00071   PSOCK_END(&s->sout);
00072 }
00073 /*---------------------------------------------------------------------------*/
00074 httpd_cgifunction
00075 httpd_cgi(char *name)
00076 {
00077   const struct httpd_cgi_call **f;
00078 
00079   /* Find the matching name in the table, return the function. */
00080   for(f = calls; *f != NULL; ++f) {
00081     if(strncmp((*f)->name, name, strlen((*f)->name)) == 0) {
00082       return (*f)->function;
00083     }
00084   }
00085   return nullfunction;
00086 }
00087 extern unsigned char temp0, temp1, setpoint, ac_auto_on, ac_modo;
00088 
00089 static unsigned short cgi_gettemp( void *arg )
00090 {
00091         sprintf( uip_appdata, "%3d,%02d", (int)temp1, (int)temp0);
00092 
00093         return strlen( uip_appdata );
00094 }
00095 
00096 static PT_THREAD(cgitemperature(struct httpd_state *s, char *ptr))
00097 {
00098   PSOCK_BEGIN(&s->sout);
00099   PSOCK_GENERATOR_SEND(&s->sout, cgi_gettemp, NULL);
00100   PSOCK_END(&s->sout);
00101 }
00102 
00103 static unsigned short cgi_getsetp( void *arg )
00104 {
00105         sprintf( uip_appdata, "%d", (int)setpoint);
00106 
00107         return strlen( uip_appdata );
00108 }
00109 
00110 static PT_THREAD(cgisetpoint(struct httpd_state *s, char *ptr))
00111 {
00112   PSOCK_BEGIN(&s->sout);
00113   PSOCK_GENERATOR_SEND(&s->sout, cgi_getsetp, NULL);
00114   PSOCK_END(&s->sout);
00115 }
00116 
00117 static unsigned short cgi_getestado( void *arg )
00118 {
00119         switch(ac_modo)
00120         {
00121         case 0:
00122                 strcpy( uip_appdata, "<option>ON</option> <option>AUTO</option> <option selected>OFF</option>");
00123                 break;
00124         case 1:
00125                 strcpy( uip_appdata, "<option>ON</option> <option selected>AUTO</option> <option>OFF</option>");
00126                 break;
00127         case 2:
00128                 strcpy( uip_appdata, "<option selected>ON</option> <option>AUTO</option> <option>OFF</option>");
00129                 break;
00130         default:
00131                 strcpy( uip_appdata, "<option>ON</option> <option>AUTO</option> <option selected>OFF</option>");
00132                 break;
00133         }
00134         return strlen( uip_appdata );
00135 }
00136 
00137 static PT_THREAD(cgiestado(struct httpd_state *s, char *ptr))
00138 {
00139   PSOCK_BEGIN(&s->sout);
00140   PSOCK_GENERATOR_SEND(&s->sout, cgi_getestado, NULL);
00141   PSOCK_END(&s->sout);
00142 }
00143 
00144 #define PASSWORD "sisca"
00145 
00146 void cgiProcessFormInput( char *pcInputString, int xInputLength )
00147 {
00148 char *c, *d, *e, *pcText;
00149 char tac_modo, tsetpoint;
00150 
00151         c = strstr( pcInputString, "?" );
00152     if( c )
00153     {
00154         printf("\n\n");
00155         *c = 0x00; //terminate at the filename
00156         c++;
00157         e = strstr( c, " " );
00158         if( e != NULL )
00159         {
00160             *e = 0x00; //terminate the whole string
00161         }
00162         d = strstr( c, "stpt=");
00163         if (d) {
00164         c=d;
00165         c += strlen("stpt=");
00166         d = strchr( c, '&');
00167         *d = '\0';
00168         tsetpoint = atoi(c);
00169         printf("--[%s]--",c);
00170         c=d+1;
00171         }
00172 
00173         d = strstr( c, "estd=");
00174         if(d) {
00175         c=d;
00176         c += strlen("estd=");
00177         d = strchr( c, '&');
00178         *d = '\0';
00179         if(strstr(c, "ON")) tac_modo=2;
00180         if(strstr(c, "OFF")) tac_modo=0;
00181         if(strstr(c, "AUTO")) tac_modo=1;
00182         printf("--[%s]--",c);
00183         c=d+1; }
00184         d = strstr( c, "psswd=");
00185         if (d)
00186         {
00187                         c=d;
00188                         c += strlen("psswd=");
00189                         printf("--[%s]--",c);
00190                         if(strstr(c,PASSWORD))
00191                         {
00192                                 setpoint = tsetpoint;
00193                                 ac_modo = tac_modo;
00194                                 printf("\n Password ok");
00195                         }
00196                         else
00197                         {
00198                                 tsetpoint = setpoint;
00199                                 tac_modo = ac_modo;
00200                                 printf("\n Wrong Password");
00201                         }
00202         }
00203 
00204         printf("\n Setpoint: %d",setpoint);
00205         printf("\n Modo de op: %d\n",ac_modo);
00206     }
00207 }
00208 
00209 
00210 
00211 
00212