source: uKadecot/trunk/uip/apps/webserver/websocket.c@ 101

Last change on this file since 101 was 101, checked in by coas-nagasima, 8 years ago

TOPPERS/uKadecotのソースコードを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/plain
File size: 6.9 KB
Line 
1/*
2 * TOPPERS ECHONET Lite Communication Middleware
3 *
4 * Copyright (C) 2015 Cores Co., Ltd. Japan
5 *
6 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
7 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
8 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
9 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
10 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
11 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
12 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
13 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
14ƒƒ“ƒgi—˜—p
15 * ŽÒƒ}ƒjƒ…
16ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
17 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
18 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
19 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
20 * ‚ƁD
21 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
22ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
23ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
24 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
25 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
26 * •ñ‚·‚邱‚ƁD
27 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
28 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
29 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
30 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
31 * –Ɛӂ·‚邱‚ƁD
32 *
33 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
34 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
35 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
36 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
37 * ‚̐ӔC‚𕉂í‚È‚¢D
38 *
39 * @(#) $Id: websocket.c 101 2015-06-02 15:37:23Z coas-nagasima $
40 */
41
42#include <string.h>
43#include "websocket.h"
44#include "uip.h"
45#include "kernel.h"
46#include "kernel_cfg.h"
47
48static ECN_FBS_ID cur_out_msg;
49
50void websocket_init(struct websocket *ws, ID wbsid)
51{
52 memset(ws, 0, sizeof(*ws));
53 ws->wbsid = wbsid;
54}
55
56void websocket_destroy(struct websocket *ws)
57{
58 if ((cur_out_msg.ptr != NULL) && (((ID *)cur_out_msg.ptr->_gap)[0] == ws->wbsid))
59 {
60 _ecn_fbs_del(cur_out_msg);
61 memset(&cur_out_msg, 0, sizeof(cur_out_msg));
62 }
63
64 if (ws->cur_in_msg.ptr != NULL) {
65 _ecn_fbs_del(ws->cur_in_msg);
66 memset(&ws->cur_in_msg, 0, sizeof(ws->cur_in_msg));
67 }
68
69 ws->wbsid = 0;
70}
71
72int websocket_input(struct websocket *ws, const void *data, int len)
73{
74 ws_state_t *s = &ws->rstate;
75 const u8_t *pos = (const u8_t *)data, *end = &((const u8_t *)data)[len];
76
77 for(; pos < end; pos++){
78 switch (s->state)
79 {
80 // FIN RSV1-3 OPECODE
81 case 0:
82 s->fin = (*pos & 0x80) != 0;
83 s->opecode = (enum opecode_t)(*pos & 0x0F);
84 s->state = 1;
85 break;
86 // MASK Payload len
87 case 1:
88 // MASK=0‚ÍNG
89 if((*pos & 0x80) == 0)
90 return -1/*MASK_ERROR*/;
91
92 switch(*pos & 0x7F){
93 case 127:
94 s->payload_len = 0;
95 s->state = 2/*7+64bit*/;
96 break;
97 case 126:
98 s->payload_len = 0;
99 s->state = 8/*7+16bit*/;
100 break;
101 default:
102 s->payload_len = *pos & 0x7F;
103 s->state = 10/*7bit*/;
104 break;
105 }
106 break;
107 // 64bit Extended payload length
108 case 2: case 3: case 4: case 5: case 6: case 7:
109 // 16bit Extended payload length
110 case 8: case 9:
111 s->payload_len = (s->payload_len << 8) + *pos;
112 s->state++;
113 break;
114 // Masking-key
115 case 10: case 11: case 12:
116 s->masking_key[s->state - 10] = *pos;
117 s->state++;
118 break;
119 case 13:
120 s->masking_key[3] = *pos;
121
122 s->data_pos = 0;
123 if (s->payload_len <= 0) {
124 s->state = 0;
125 }
126 else {
127 _ecn_fbs_cre(1, &ws->cur_in_msg);
128 s->state++;
129 }
130 break;
131 // Payload buffer
132 default:
133 _ecn_fbs_poke(ws->cur_in_msg, s->data_pos, *pos ^ s->masking_key[s->data_pos % 4]);
134
135 s->data_pos++;
136 if (s->data_pos >= s->payload_len) {
137 ER ret;
138 s->state = 0;
139 ((ID *)ws->cur_in_msg.ptr->_gap)[0] = ws->wbsid;
140 ret = psnd_dtq(ecn_api_mailboxid, (intptr_t)ws->cur_in_msg.ptr);
141 if (ret != E_OK) {
142 syslog(LOG_WARNING, "websocket_input() : psnd_dtq(%d) result = %d", ecn_api_mailboxid, ret);
143 _ecn_fbs_del(ws->cur_in_msg);
144 }
145 memset(&ws->cur_in_msg, 0, sizeof(ws->cur_in_msg));
146 }
147 break;
148 }
149 }
150
151 return 0;
152}
153
154bool_t websocket_newdata(struct websocket *ws)
155{
156 ws_state_t *s = &ws->wstate;
157 struct websocket *mws;
158 ER ret;
159 ECN_FBS_ID data;
160 ID wbsid;
161
162 if(s->state != 0)
163 return true;
164
165 if (cur_out_msg.ptr != NULL)
166 return (((ID *)cur_out_msg.ptr->_gap)[0] == ws->wbsid);
167
168 for (;;) {
169 ret = prcv_dtq(WEBSOCKET_MBXID, (intptr_t *)&data.ptr);
170 if (ret == E_TMOUT)
171 return false;
172
173 if (ret != E_OK) {
174 TOPPERS_assert_abort();
175 return false;
176 }
177
178 wbsid = ((ID *)data.ptr->_gap)[0];
179 mws = websocket_getws(wbsid);
180 if ((mws != NULL) && (mws->wbsid != 0))
181 break;
182
183 _ecn_fbs_del(cur_out_msg);
184 }
185
186 cur_out_msg.ptr = data.ptr;
187
188 return (wbsid == ws->wbsid);
189}
190
191int websocket_output(struct websocket *ws, void *data, int len)
192{
193 ws_state_t *s = &ws->wstate;
194 u8_t *pos = (u8_t *)data, *end = &((u8_t *)data)[len];
195
196 for(; pos < end; pos++){
197 switch (s->state)
198 {
199 // FIN RSV1-3 OPECODE
200 case 0:
201 if ((cur_out_msg.ptr == NULL) || (((ID *)cur_out_msg.ptr->_gap)[0] != ws->wbsid))
202 return (intptr_t)pos - (intptr_t)data;
203
204 s->payload_len = _ecn_fbs_get_datalen(cur_out_msg);
205 if(s->payload_len == 0){
206 goto next;
207 }
208
209 s->fin = 1;
210 s->opecode = text_frame;
211 s->masked = 0;//
212 s->masking_key[0] = 0;//0x01;
213 s->masking_key[1] = 0;//0x23;
214 s->masking_key[2] = 0;//0x45;
215 s->masking_key[3] = 0;//0x67;
216
217 *pos = ((s->fin != 0) ? 0x80 : 0)
218 | ((char)s->opecode & 0x0F);
219 s->state = 1;
220 break;
221 // MASK Payload len
222 case 1:
223 if(s->payload_len < 126){
224 *pos = ((s->masked != 0) ? 0x80 : 0) | s->payload_len;
225 if (s->masked != 0) {
226 s->state = 10/*7bit*/;
227 }
228 else{
229 goto payload_check;
230 }
231 }
232 else if(s->payload_len < 0x10000){
233 *pos = ((s->masked != 0) ? 0x80 : 0) | 126;
234 s->state = 8/*7+16bit*/;
235 }
236 else{
237 *pos = ((s->masked != 0) ? 0x80 : 0) | 127;
238 s->state = 2/*7+64bit*/;
239 }
240 break;
241 // 64bit Extended payload length
242 case 2: case 3: case 4: case 5: case 6: case 7:
243 // 16bit Extended payload length
244 case 8: case 9:
245 *pos = (s->payload_len >> (8 * (9 - s->state)));
246 s->state++;
247 if((s->state == 10) && (s->masked == 0)){
248 goto payload_check;
249 }
250 break;
251 // Masking-key
252 case 10: case 11: case 12:
253 *pos = s->masking_key[s->state - 10];
254 s->state++;
255 break;
256 case 13:
257 *pos = s->masking_key[3];
258 s->state++;
259 payload_check:
260 s->data_pos = 0;
261 if (s->data_pos < s->payload_len)
262 s->state = 14;
263 else
264 goto next;
265 break;
266 // Payload buffer
267 default:
268 *pos = _ecn_fbs_peek(cur_out_msg, s->data_pos) ^ s->masking_key[s->data_pos % 4];
269
270 s->data_pos++;
271 if(s->data_pos >= s->payload_len){
272 goto next;
273 }
274 break;
275 next:
276 _ecn_fbs_del(cur_out_msg);
277 memset(&cur_out_msg, 0, sizeof(cur_out_msg));
278 s->state = 0;
279 websocket_newdata(ws);
280 break;
281 }
282 }
283
284 return (intptr_t)pos - (intptr_t)data;
285}
Note: See TracBrowser for help on using the repository browser.