-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
365 lines (324 loc) · 8.55 KB
/
main.c
File metadata and controls
365 lines (324 loc) · 8.55 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (c) 2009 Braiden Kindt
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include "w5100.h"
#include "uart.h"
#include "httpd.h"
#include "lg.h"
#include "socket.h"
#include "mcp9800.h"
#include "debug.h"
#include "diskio.h"
#include "spi.h"
#include "pff.h"
#include "ratelimit.h"
#define RATE_LIMIT_DEFAULT 20
#define RATE_LIMIT_POWER 3
#define TEMP 1
#define FAN 2
#define ERR_RATE_TOO_FAST 2
w5100_config_t W5100_CONFIG PROGMEM = {
{192,168,0,1},
{255,255,255,0},
{'b','r','a','i','d','n'},
{192,168,0,5}
};
#ifdef SUPPORT_RAW_CMD
uint8_t atoi16(char *str)
{
uint8_t result = 0;
for(; *str; str++) {
result <<= 4;
if (*str >= '0' && *str <= '9') {
result += *str - '0';
} else if (*str >= 'a' && *str <= 'f') {
result += *str - 'a' + 10;
} else if (*str >= 'A' && *str <= 'F') {
result += *str - 'A' + 10;
}
}
return result;
}
uint8_t do_raw_cmd(uint8_t sock, uint8_t status, char *buffer, uint8_t add_len_and_sum)
{
if (status == HTTPD_READ_QUERY_STRING) {
status = httpd_read_path(sock, buffer);
}
uint8_t len = strlen(buffer);
if (status != HTTPD_READ_DONE) {
return 0;
} else if (!add_len_and_sum && (len < 9 || len > 10)) {
return 0;
} else if (add_len_and_sum && len != 6) {
return 0;
}
// use part of the receive buffer as our opcode array
// were assuming buffer is big enough, no bounds checking
uint8_t *opcode = ((uint8_t*)buffer) + len;
// convert char arrary into binary opcode
uint8_t n;
for (n = (len + 1) / 2 - 1; n != 0xFF ; n--) {
uint8_t is_one_byte = buffer[n * 2 + 1] == 0;
buffer[n * 2 + 2] = 0;
opcode[n] = atoi16(&(buffer[n * 2]));
if (is_one_byte) {
opcode[n] <<= 4;
}
}
if (add_len_and_sum) {
// generate the checksum nibble
if (len % 2) {
opcode[len / 2] |= lg_checksum(opcode, (len + 1) / 2);
} else {
opcode[len / 2] = lg_checksum(opcode, (len +1) / 2) << 4;
}
// add the bit length to start of opcode
*--opcode = len * 4 + 4;
}
#ifdef DEBUG
log("cmd=");
for (n = 0; n < 5; n++) {
log_uint8(opcode[n]);
}
log("\n");
#endif
// send the op code
if (ratelimit(RATE_LIMIT_DEFAULT)) {
lg_send(opcode);
return 1;
} else {
return ERR_RATE_TOO_FAST;
}
}
#endif
uint8_t do_on_cmd(uint8_t sock, uint8_t status, char *buffer)
{
uint8_t temp = 70;
uint8_t fan = 1;
if (status == HTTPD_READ_QUERY_STRING) {
do {
uint8_t param = 0;
uint8_t value;
status = httpd_read_path(sock, buffer);
if (status == HTTPD_READ_QUERY_VALUE) {
// the param name is in buffer, next read will
// given the value.
if (strcmp_P(buffer, PSTR("temp")) == 0) {
param = TEMP;
} else if (strcmp_P(buffer, PSTR("fan")) == 0) {
param = FAN;
}
status = httpd_read_path(sock, buffer);
value = atoi(buffer);
if (param == FAN) {
fan = value;
} else if (param == TEMP) {
temp = value;
}
}
} while (status);
} else if (status != HTTPD_READ_DONE) {
return 0; // 404, "on" was refrences as a directory
}
log("fan=");
log_uint8(fan);
log(", temp=");
log_uint8(temp);
log("\n");
uint8_t *cmd = (uint8_t*) buffer;
lg_create_cmd(temp, fan, cmd);
#ifdef DEBUG
log("cmd=");
for (fan = 0; fan < 5; fan++) {
log_uint8(cmd[fan]);
}
log("\n");
#endif
if (ratelimit(RATE_LIMIT_DEFAULT)) {
lg_send(cmd);
return 1;
} else {
return ERR_RATE_TOO_FAST;
}
}
uint8_t ratelimit_lg_cmd(uint8_t cmd, uint8_t limit)
{
if (ratelimit(limit)) {
lg_cmd(cmd);
return 0;
} else {
return ERR_RATE_TOO_FAST;
}
}
void cgi_handler(uint8_t sock, uint8_t method, char *buffer)
{
uint8_t status = httpd_read_path(sock, buffer);
if (status == HTTPD_READ_PATH && strcmp_P(buffer, PSTR("ac")) == 0)
{
// found a directory named 'ac', read the command
status = httpd_read_path(sock, buffer);
if (status == HTTPD_READ_DONE || status == HTTPD_READ_QUERY_STRING) {
// found a 'file', map to command
if (strcmp_P(buffer, PSTR("power")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_POWER, RATE_LIMIT_POWER);
} else if (strcmp_P(buffer, PSTR("temp-down")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_TEMP_DOWN, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("temp-up")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_TEMP_UP, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("energy")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_ENERGY_SAVE, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("mode")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_MODE, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("timer")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_TIMER, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("speed")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_FAN_SPEED, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("off")) == 0) {
status = ratelimit_lg_cmd(LG_CMD_POWER_OFF, RATE_LIMIT_DEFAULT);
} else if (strcmp_P(buffer, PSTR("on")) == 0) {
// parse the 'on' command
if((status = do_on_cmd(sock, status, buffer)) == 0) {
goto NOTFOUND;
}
#ifdef SUPPORT_RAW_CMD
} else if (strcmp_P(buffer, PSTR("cmd")) == 0) {
if (!do_raw_cmd(sock, status, buffer, 1)) {
goto ERR;
}
} else if (strcmp_P(buffer, PSTR("raw")) == 0) {
if (!do_raw_cmd(sock, status, buffer, 0)) {
goto ERR;
}
#endif
} else {
goto NOTFOUND;
}
if (status != ERR_RATE_TOO_FAST) {
goto OK;
} else {
goto ERR;
}
}
#ifdef SUPPORT_MCP9800
} else if (status == HTTPD_READ_PATH && strcmp_P(buffer, PSTR("temp")) == 0) {
status = httpd_read_path(sock, buffer);
if (status == HTTPD_READ_DONE || status == HTTPD_READ_QUERY_STRING) {
if (strcmp_P(buffer, PSTR("raw")) == 0) {
uint8_t temp;
httpd_write_response(sock, HTTPD_OK);
sock_write_P(sock, PSTR("Content-Type: text/plain\r\n\r\n"), 28);
temp = mcp9800_get_temp();
itoa(temp, buffer, 16);
sock_write( sock, buffer, strlen(buffer));
return;
}
}
#endif
}
NOTFOUND:
httpd_write_response(sock, HTTPD_NOTFOUND);
return;
ERR:
httpd_write_response(sock, HTTPD_ERR);
return;
OK:
httpd_write_response(sock, HTTPD_OK);
sock_write_P(sock, PSTR("Content-Type: text/plain\r\n\r\nOK"), 30);
return;
}
int main()
{
FATFS *fs = NULL;
wdt_enable(WDTO_8S);
_delay_ms(2);
uart_init();
lg_init();
spi_init();
w5100_init(&W5100_CONFIG);
ratelimit_init();
//enable cd card CS
DDRD |= _BV(PIN3);
PORTD |= _BV(PIN3);
#ifdef SUPPORT_MMC
FATFS tmp;
// try to init micro SD
if (disk_initialize() != STA_NOINIT) {
// once initial inti is done enable full speed SPI
if(pf_mount(&tmp) == FR_OK) {
fs = &tmp;
} else {
log("pf_mount FAILED\n");
}
} else {
log("disk_init FAILED\n");
}
#endif
spi_fullspeed();
httpd_init();
#ifdef SUPPORT_MCP9800
mcp9800_init();
#endif
sei();
#ifdef DEBUG
uint8_t n;
log("cfg=");
for (n=0; n<6 ; n++) {
log_uint8(w5100_mem_read(W5100_COMMON_REG, W5100_SHAR0+n));
if (n<5) log_char(':');
}
log_char(' ');
for (n=0; n<4; n++) {
log_int(w5100_mem_read(W5100_COMMON_REG, W5100_SIPR0+n),1,10);
if (n<3) log_char('.');
}
log_char('/');
for (n=0; n<4; n++) {
log_int(w5100_mem_read(W5100_COMMON_REG, W5100_SUBR0+n),1,10);
if (n<3) log_char('.');
}
log_char(' ');
for (n=0; n<4; n++) {
log_int(w5100_mem_read(W5100_COMMON_REG, W5100_GAR0+n),1,10);
if (n<3) log_char('.');
}
log("\n");
#endif
uint16_t loop = 0;
while (1) {
wdt_reset();
httpd_loop(cgi_handler);
if (++loop % 0x400 == 0) {
sock_dump_netstat();
}
}
return 0;
}