source: uKadecot/trunk/kadecot/wamp_router.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: 7.3 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: wamp_router.c 101 2015-06-02 15:37:23Z coas-nagasima $
40 */
41
42#include <string.h>
43#include "wamp.h"
44#include "wamp_router.h"
45#include "main.h"
46#include "fbs_string.h"
47#include "kernel_cfg.h"
48
49#ifndef _MSC_VER
50#ifndef strncpy_s
51#define strncpy_s(dst, dsz, src, sz) strncpy(dst, src, sz)
52#endif
53#endif
54
55wamp_session_t *wamp_router_hello(wamp_router_t *router)
56{
57 wamp_session_t *result = NULL, *pos, *end = &router->sessions[sizeof(router->sessions) / sizeof(router->sessions[0])];
58
59 for(pos = router->sessions; pos < end; pos++) {
60 if(pos->router != NULL)
61 continue;
62
63 result = pos;
64 result->router = router;
65 router->current_session = result;
66 }
67
68 return result;
69}
70
71void wamp_router_hello_realm(wamp_router_t *router, const char *value)
72{
73 wamp_session_t *s = router->current_session;
74 strncpy_s(s->realm, sizeof(s->realm), value, sizeof(s->realm));
75}
76
77void wamp_router_hello_details_param(wamp_router_t *router, jsonsl_action_t action,
78 struct jsonsl_state_st *state, const char *buf)
79{
80 static const char roles[] = "roles";
81 static const char publisher[] = "publisher";
82 static const char subscriber[] = "subscriber";
83 static const char caller[] = "caller";
84 static const char callee[] = "callee";
85 wamp_session_t *s = router->current_session;
86
87 switch (state->level) {
88 case 3:
89 switch (action){
90 case JSONSL_ACTION_POP:
91 switch (state->type)
92 {
93 case JSONSL_T_HKEY:
94 if(strncmp(roles, router->s->jsn_buf, sizeof(roles) - 1) == 0){
95 s->roles |= WAMP_ROUTER_ROLES_IN_GETTING;
96 }
97 break;
98 default:
99 break;
100 }
101 break;
102 }
103 break;
104 case 4:
105 switch (action){
106 case JSONSL_ACTION_POP:
107 switch (state->type)
108 {
109 case JSONSL_T_HKEY:
110 if((s->roles & WAMP_ROUTER_ROLES_IN_GETTING) !=0 ){
111 if(strncmp(publisher, router->s->jsn_buf, sizeof(publisher) - 1) == 0){
112 s->roles |= WAMP_ROUTER_ROLES_PUBLISHER;
113 }
114 else if(strncmp(subscriber, router->s->jsn_buf, sizeof(subscriber) - 1) == 0){
115 s->roles |= WAMP_ROUTER_ROLES_SUBSCRIBER;
116 }
117 else if(strncmp(caller, router->s->jsn_buf, sizeof(caller) - 1) == 0){
118 s->roles |= WAMP_ROUTER_ROLES_CALLER;
119 }
120 else if(strncmp(callee, router->s->jsn_buf, sizeof(callee) - 1) == 0){
121 s->roles |= WAMP_ROUTER_ROLES_CALLEE;
122 }
123 }
124 break;
125 default:
126 break;
127 }
128 break;
129 }
130 break;
131 }
132}
133
134void wamp_router_hello_details(wamp_router_t *router)
135{
136 wamp_session_t *s = router->current_session;
137 s->roles &= ~WAMP_ROUTER_ROLES_IN_GETTING;
138}
139
140void wamp_router_hello_close(struct wamp_state *s)
141{
142 wamp_router_t *router = &s->router;
143 wamp_session_t *session = router->current_session;
144 ECN_FBS_ID buf;
145 ECN_FBS_SSIZE_T pos = 0, start;
146 ER ret;
147
148 router->session_id++;
149 if(router->session_id == 0){
150 router->session_id++;
151 }
152 session->id = router->session_id;
153
154 // send WELLCOM or ABORT
155 _ecn_fbs_cre(16, &buf);
156
157 ret = fbs_printf(buf, &pos, "[2,%d,{\"roles\":{", session->id);
158 if (ret != E_OK) {
159 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
160 _ecn_fbs_del(buf);
161 return;
162 }
163
164 start = pos;
165 if(session->roles & WAMP_ROUTER_ROLES_PUBLISHER){
166 ret = fbs_printf(buf, &pos, "\"publisher\":{},");
167 if (ret != E_OK) {
168 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
169 _ecn_fbs_del(buf);
170 return;
171 }
172 }
173 if(session->roles & WAMP_ROUTER_ROLES_SUBSCRIBER){
174 ret = fbs_printf(buf, &pos, "\"subscriber\":{},");
175 if (ret != E_OK) {
176 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
177 _ecn_fbs_del(buf);
178 return;
179 }
180 }
181 if(session->roles & WAMP_ROUTER_ROLES_CALLER){
182 ret = fbs_printf(buf, &pos, "\"caller\":{},");
183 if (ret != E_OK) {
184 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
185 _ecn_fbs_del(buf);
186 return;
187 }
188 }
189 if(session->roles & WAMP_ROUTER_ROLES_CALLEE){
190 ret = fbs_printf(buf, &pos, "\"callee\":{},");
191 if (ret != E_OK) {
192 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
193 _ecn_fbs_del(buf);
194 return;
195 }
196 }
197
198 /* ÅŒã‚Ì","‚ðÁ‚· */
199 if(start != pos)
200 pos--;
201
202 ret = fbs_printf(buf, &pos, "}}]");
203 if (ret != E_OK) {
204 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
205 _ecn_fbs_del(buf);
206 return;
207 }
208
209 ret = main_send_message(buf, s->wbsid);
210 if (ret != E_OK) {
211 syslog(LOG_WARNING, "wamp_router_hello_close() : main_send_message() result = %d", ret);
212 }
213}
214
215void wamp_router_authenticate(wamp_router_t *router)
216{
217}
218
219void wamp_router_authenticate_signature(wamp_router_t *router, const char *value)
220{
221}
222
223void wamp_router_authenticate_extra(wamp_router_t *router, const char *value)
224{
225}
226
227void wamp_router_authenticate_extra_param(wamp_router_t *router, jsonsl_action_t action,
228 struct jsonsl_state_st *state, const char *buf)
229{
230}
231
232void wamp_router_authenticate_close(struct wamp_state *s)
233{
234}
235
236void wamp_router_goodbye(wamp_router_t *router)
237{
238}
239
240void wamp_router_goodbye_details_param(wamp_router_t *router, jsonsl_action_t action,
241 struct jsonsl_state_st *state, const char *buf)
242{
243}
244
245void wamp_router_goodbye_details(wamp_router_t *router)
246{
247}
248
249void wamp_router_goodbye_reason(wamp_router_t *router, const char *value)
250{
251}
252
253void wamp_router_goodbye_close(struct wamp_state *s)
254{
255 wamp_router_t *router = &s->router;
256 wamp_session_t *session = router->current_session;
257 ECN_FBS_ID buf;
258 ECN_FBS_SSIZE_T pos = 0;
259 ER ret;
260
261 if(session == NULL)
262 return;
263
264 // send GOODBYE
265 ret = _ecn_fbs_cre(1, &buf);
266 if (ret != E_OK) {
267 syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
268 return;
269 }
270
271 ret = fbs_printf(buf, &pos, "[6,{},\"wamp.error.goodbye_and_out\"]");
272 if (ret != E_OK) {
273 syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
274 _ecn_fbs_del(buf);
275 return;
276 }
277
278 ret = main_send_message(buf, s->wbsid);
279 if (ret != E_OK) {
280 syslog(LOG_WARNING, "wamp_router_goodbye_close() : main_send_message() result = %d", ret);
281 }
282
283 memset(session, 0, sizeof(session));
284}
Note: See TracBrowser for help on using the repository browser.