OSDN Git Service

update libMiMic
[mimic/MiMicSDK.git] / projects / example / test.httppost / src / sketch.c
1 /**\r
2  * POSTリクエスト受信サンプルです。\r
3  * 受信したPOSTリクエストをエコーバックします。\r
4  */\r
5 #include "boot/sketch.h"\r
6 #include "NyLPC_net.h"\r
7 #include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h"\r
8 #include "NyLPC_http.h"\r
9 #include "NyLPC_utils.h"\r
10 \r
11 \r
12 const struct NyLPC_TiNetInterface_Interface* netif;\r
13 struct MyHttpd{\r
14         NyLPC_TcHttpd_t super;\r
15 }httpd;\r
16 \r
17 #define NUM_OF_FSDATA 4\r
18 extern struct NyLPC_TRomFileData fsdata[];\r
19 \r
20 \r
21 void handler(NyLPC_TcHttpdConnection_t* i_connection)\r
22 {\r
23         NyLPC_TcModUrl_t mod;\r
24         char url[64];\r
25         int l;\r
26 \r
27 \r
28         //SHORT URL\r
29         NyLPC_cModUrl_initialize(&mod);\r
30         if(!NyLPC_cModUrl_execute(&mod,i_connection,url,64)){\r
31                 NyLPC_cModUrl_finalize(&mod);\r
32                 return;\r
33         }\r
34         if(NyLPC_cModUrl_getMethod(&mod)==NyLPC_THttpMethodType_POST){\r
35                 NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,"text/html",NULL);\r
36                 NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"<html><h1>Your post message</h1><textarea>");\r
37                 //NyLPC_TInt16 NyLPC_cModUrl_readBody(NyLPC_TcModUrl_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection,void* i_buf,NyLPC_TInt16 i_buf_size)\r
38                 l=NyLPC_cModUrl_readBody(&mod,i_connection,url,10);\r
39                 while(l>0){\r
40                         NyLPC_cHttpdConnection_sendResponseBody(i_connection,url,l);\r
41                         l=NyLPC_cModUrl_readBody(&mod,i_connection,url,10);\r
42                 }\r
43                 NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"</textarea></html>");\r
44                 NyLPC_cModUrl_finalize(&mod);\r
45         }\r
46         return;\r
47 }\r
48 void setup(void)\r
49 {\r
50         //uipサービス初期化。いろいろ利用可能に。\r
51         NyLPC_cNet_initialize(&net);\r
52 \r
53         NyLPC_cHttpd_initialize(&httpd.super,80);\r
54         httpd.super.function.onRequest=handler;\r
55 \r
56 }\r
57 \r
58 \r
59 void loop(void)\r
60 {\r
61         NyLPC_TcNetConfig_t c2;\r
62         //192.168.0.39\r
63         NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE);\r
64         NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39);\r
65         NyLPC_cNetConfig_setGateway(&c2,192,168,128,254);\r
66         NyLPC_cNet_start(&net,&c2);\r
67         NyLPC_cHttpd_loop(&httpd.super);\r
68         for(;;);\r
69 }\r
70 \r
71 \r