-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpd_cgi.cpp
252 lines (207 loc) · 7.48 KB
/
httpd_cgi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/*
* Copyright (c) 2001-2006, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "uip.h"
#include "psock.h"
#include "httpd.h"
#include "httpd_cgi.h"
#include "httpd_fs.h"
#include "pins_cgi.h"
#include <stdio.h>
#include <string.h>
#include <NanodeUIP.h>
#include <Arduino.h>
HTTPD_CGI_CALL(cgi_toggle_pin, "toggle_pin", toggle_pin);
HTTPD_CGI_CALL(cgi_set_pin, "set_pin", set_pin);
HTTPD_CGI_CALL(cgi_buttons_list, "buttons_list", buttons_list);
HTTPD_CGI_CALL(cgi_sensors_list, "sensors_list", sensors_list);
HTTPD_CGI_CALL(cgi_lights_list, "lights_list", lights_list);
static const struct httpd_cgi_call *calls[] = { &cgi_toggle_pin, &cgi_set_pin, &cgi_buttons_list, &cgi_sensors_list, &cgi_lights_list, NULL };
static const pin_def_t* buttons;
static const pin_def_t* lights;
static const pin_def_t* sensors;
extern void cgi_register(const pin_def_t* _buttons,const pin_def_t* _lights,const pin_def_t* _sensors)
{
buttons = _buttons;
lights = _lights;
sensors = _sensors;
}
// These methods assume THIS is a progmem pointer
bool pin_def_t::is_valid_P(void) const
{
return pgm_read_word(&name) != 0;
}
const char* pin_def_t::get_name_P(void) const
{
return reinterpret_cast<const char*>(pgm_read_word(&name));
}
uint8_t pin_def_t::get_number_P(void) const
{
return pgm_read_byte(&number);
}
/*---------------------------------------------------------------------------*/
static PT_THREAD(nullfunction(struct httpd_state *s, char *))
{
PSOCK_BEGIN(&s->sout);
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
extern void log_Pcr(const char* str);
httpd_cgifunction
httpd_cgi(char *name)
{
log_Pcr(name);
const struct httpd_cgi_call **f;
/* Find the matching name in the table, return the function. */
for(f = calls; *f != NULL; ++f)
{
if(strncmp_P((*f)->name, name, strlen((*f)->name)) == 0)
{
return (*f)->function;
}
}
return nullfunction;
}
/*---------------------------------------------------------------------------*/
static const char str_high[] PROGMEM = "HIGH";
static const char str_low[] PROGMEM = "LOW";
static const char str_notfound[] PROGMEM = "not found";
/*---------------------------------------------------------------------------*/
static unsigned short generate_buttons_list(void *arg)
{
//printf_P(PSTR("generate_buttons_list\r\n"));
struct httpd_state *s = (struct httpd_state *)arg;
return snprintf_P((char*)uip_appdata,UIP_APPDATA_SIZE,
PSTR("<li><div class='ui-grid-a'><div class='ui-block-a'>%S</div><div class='ui-block-b'>%S</div></div></li>\r\n"),
buttons[s->count].get_name_P(),
digitalRead(buttons[s->count].get_number_P())?str_high:str_low
);
}
static PT_THREAD(buttons_list(struct httpd_state *s, char *))
{
PSOCK_BEGIN(&s->sout);
for(s->count = 0; buttons[s->count].is_valid_P(); ++s->count) {
PSOCK_GENERATOR_SEND(&s->sout, generate_buttons_list, s);
}
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static unsigned short generate_sensors_list(void *arg)
{
//printf_P(PSTR("generate_sensors_list\r\n"));
struct httpd_state *s = (struct httpd_state *)arg;
return snprintf_P((char*)uip_appdata,UIP_APPDATA_SIZE,
PSTR("<li><div class='ui-grid-a'><div class='ui-block-a'>%S</div><div class='ui-block-b'>%u</div></div></li>\r\n"),
sensors[s->count].get_name_P(),
analogRead(sensors[s->count].get_number_P())
);
}
static PT_THREAD(sensors_list(struct httpd_state *s, char *))
{
PSOCK_BEGIN(&s->sout);
for(s->count = 0; sensors[s->count].is_valid_P(); ++s->count) {
PSOCK_GENERATOR_SEND(&s->sout, generate_sensors_list, s);
}
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static unsigned short generate_lights_list(void *arg)
{
//printf_P(PSTR("generate_lights_list\r\n"));
struct httpd_state *s = (struct httpd_state *)arg;
uint8_t pin = lights[s->count].get_number_P();
return snprintf_P((char*)uip_appdata,UIP_APPDATA_SIZE,
PSTR("<div data-role='fieldcontain'>"
"<label for='light-%u'>%S</label> <select class='light' name='%u' id='light-%u' data-role='slider'>"
"<option value='0'>OFF</option><option value='1'>ON</option>"
"</select>"
"</div>\r\n"),
pin,
lights[s->count].get_name_P(),
pin,
pin
);
}
static PT_THREAD(lights_list(struct httpd_state *s, char *))
{
PSOCK_BEGIN(&s->sout);
for(s->count = 0; lights[s->count].is_valid_P(); ++s->count) {
PSOCK_GENERATOR_SEND(&s->sout, generate_lights_list, s);
}
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static PT_THREAD(toggle_pin(struct httpd_state *s, char *))
{
char* pin_at;
const char* response = str_notfound;
PSOCK_BEGIN(&s->sout);
// Take action
nanode_log(s->filename);
pin_at = strstr(s->filename,"pin=");
if ( pin_at )
{
uint8_t pin = atoi(pin_at+4);
digitalWrite(pin,digitalRead(pin)^HIGH);
response = digitalRead(pin)?str_high:str_low;
}
// Send response
PSOCK_SEND_P(&s->sout,(uint8_t*)response,strlen_P(response));
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static PT_THREAD(set_pin(struct httpd_state *s, char *))
{
char* pin_at;
char* val_at;
const char* response = str_notfound;
PSOCK_BEGIN(&s->sout);
// Take action
nanode_log(s->filename);
pin_at = strstr(s->filename,"pin=");
val_at = strstr(s->filename,"val=");
if ( pin_at && val_at )
{
uint8_t pin = atoi(pin_at+4);
uint8_t val = atoi(val_at+4);
digitalWrite(pin,val);
response = digitalRead(pin)?str_high:str_low;
}
// Send response
PSOCK_SEND_P(&s->sout,(uint8_t*)response,strlen_P(response));
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
/** @} */
// vim:cin:ai:sts=2 sw=2 ft=cpp