-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_if.c
137 lines (111 loc) · 2.95 KB
/
web_if.c
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
#include "web_if.h"
#include "uart.h"
prog_char webif_404_reply[] =
"HTTP/1.0 404 Not Found\r\n"
"Content-Type: text/html; charset=windows-1251\r\n"
"Server: ATmega16\r\n"
"\r\n"
"<pre>Page not found\r\n\r\n"
"<a href='/'>Home page</a></pre>\r\n";
prog_char webif_200_header[] =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html; charset=windows-1251\r\n"
"Server: ATmega16\r\n"
"\r\n";
#define LED_DDR DDRD
#define LED_PORT PORTD
#define LED_BIT (1<<PD0)
#define led_on() { LED_PORT &= ~LED_BIT; led_state = 1; }
#define led_off() { LED_PORT |= LED_BIT; led_state = 0; }
uint8_t led_state;
uint8_t lang_ru;
void fill_buf_p(char **buf, const prog_char *pstr)
{
char c;
while((c = pgm_read_byte(pstr)))
{
*((*buf)++) = c;
pstr++;
}
}
void webif_init()
{
LED_DDR |= LED_BIT;
led_off();
}
void webif_data(uint8_t id, eth_frame_t *frame, uint16_t len)
{
ip_packet_t *ip = (void*)(frame->data);
tcp_packet_t *tcp = (void*)(ip->data);
char *req = (void*)tcp_get_data(tcp);
char *buf = (void*)(tcp->data), *buf_ptr = buf;
char *url, *p, *params, *name, *value;
if(!len) return;
if( (memcmp_P(req, PSTR("GET "), 4) == 0) &&
((p = strchr(req + 4, ' ')) != 0) )
{
url = req + 4;
*p = 0;
if((params = strchr(url, '?')))
*(params++) = 0;
if(strcmp_P(url, PSTR("/")) == 0)
{
if(params==NULL) {
send_Uart_str(" No params!");
send_Uart(13);
}else {
send_Uart_str(" With params!");
send_Uart(13);
}
while(params)
{
if((p = strchr(params, '&')))
*(p++) = 0;
name = params;
if((value = strchr(name, '=')))
*(value++) = 0;
if( (strcmp_P(name, PSTR("led")) == 0 ) && value )
{
if(strcmp_P(value, PSTR("on")) == 0)
led_on()
else if(strcmp_P(value, PSTR("off")) == 0)
led_off()
}
else if( (strcmp_P(name, PSTR("lang")) == 0) && value )
{
if(strcmp_P(value, PSTR("en")) == 0)
lang_ru = 0;
else if(strcmp_P(value, PSTR("ru")) == 0)
lang_ru = 1;
}
params = p;
}
fill_buf_p(&buf_ptr, webif_200_header);
fill_buf_p(&buf_ptr, PSTR("<pre>"));
if(!lang_ru)
{
fill_buf_p(&buf_ptr, PSTR("<p align='right'>[<b>EN</b> | "
"<a href='/?lang=ru'>RU</a>]</p>"));
}
else
{
fill_buf_p(&buf_ptr, PSTR("<p align='right'>[<a href='/?lang=en'>EN</a> | "
"<b>RU</b>]</p>"));
}
if((!led_state)&&(!lang_ru))
fill_buf_p(&buf_ptr, PSTR("Led is OFF. Turn <a href='/?led=on'>on</a>."));
else if(led_state &&(!lang_ru))
fill_buf_p(&buf_ptr, PSTR("Led is ON. Turn <a href='/?led=off'>off</a>."));
else if((!led_state)&&(lang_ru))
fill_buf_p(&buf_ptr, PSTR("Ñâåòîäèîä âûêëþ÷åí. <a href='/?led=on'>Âêëþ÷èòü</a>."));
else if(led_state &&(lang_ru))
fill_buf_p(&buf_ptr, PSTR("Ñâåòîäèîä âêëþ÷åí. <a href='/?led=off'>Âûêëþ÷èòü</a>."));
fill_buf_p(&buf_ptr, PSTR("</pre>"));
}
else
{
fill_buf_p(&buf_ptr, webif_404_reply);
}
}
tcp_send(id, frame, buf_ptr-buf, 1);
}