以下程序生成的HTML網頁上面有一個form表單,但暫時不支持對錶單提交的POST數據進行解析。
【類型定義】
typedef struct
{
uint8_t step;
char buf[20];
char len_s[4];
uint8_t bufptr;
} uip_tcp_appstate_t;
【程序代碼】
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include "uip/uip.h"
const char html_0[] PROGMEM = "HTTP/1.1 200 OK\r\nContent-Length: ";
const char html_1[] PROGMEM = "\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\n\r\n";
const char html_2[] PROGMEM = "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>AVR Page</title>\n</head>\n\n<body>\n<form id=\"form1\" name=\"form1\" method=\"post\">\nThis is a page from a server just made of AVR MCU.<br>\n<input name=\"textfield\" type=\"text\" id=\"textfield\" value=\"";
const char html_3[] PROGMEM = "\">\n<input type=\"submit\" value=\"Submit\">\n</form>\n</body>\n</html>\n";
void send_current_str(void)
{
uint8_t len = strlen(uip_conn->appstate.buf + uip_conn->appstate.bufptr);
uip_send(uip_conn->appstate.buf + uip_conn->appstate.bufptr, len);
uip_conn->appstate.bufptr += len + 1;
}
void uip_appcall(void)
{
if (uip_connected())
{
uip_conn->appstate.step = 0;
uip_conn->appstate.bufptr = 1 + sprintf_P(uip_conn->appstate.buf, PSTR("PINA=%d"), PINA);
sprintf_P(uip_conn->appstate.len_s, PSTR("%d"), strlen_P(html_2) + strlen_P(html_3) + uip_conn->appstate.bufptr - 1);
uip_conn->appstate.bufptr = 0;
}
if (uip_acked() || uip_newdata() || uip_rexmit())
{
switch (uip_conn->appstate.step)
{
case 0:
uip_send_P(html_0);
break;
case 1:
uip_send(uip_conn->appstate.len_s, strlen(uip_conn->appstate.len_s));
break;
case 2:
uip_send_P(html_1);
break;
case 3:
uip_send_P(html_2);
break;
case 4:
send_current_str();
break;
case 5:
uip_send_P(html_3);
break;
default:
uip_close();
}
if ((uip_newdata() || uip_acked()) && !uip_closed())
uip_conn->appstate.step++;
}
}