[101] | 1 | /*
|
---|
| 2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2015 Cores Co., Ltd. Japan
|
---|
| 5 | *
|
---|
| 6 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
| 7 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
| 8 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
| 9 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
| 10 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
| 11 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
| 12 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
| 13 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
| 14 | gip
|
---|
| 15 | * Ò}j
|
---|
| 16 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
| 17 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
| 18 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
| 19 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
| 20 | * ÆD
|
---|
| 21 | * (a) Äzzɺ¤hL
|
---|
| 22 | gipÒ}j
|
---|
| 23 | AÈÇjÉCãLÌ
|
---|
| 24 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
| 25 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
| 26 | * ñ·é±ÆD
|
---|
| 27 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
| 28 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
| 29 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
| 30 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
| 31 | * ÆÓ·é±ÆD
|
---|
| 32 | *
|
---|
| 33 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
| 34 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
| 35 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
| 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 |
|
---|
| 55 | wamp_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 |
|
---|
| 71 | void 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 |
|
---|
| 77 | void 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 |
|
---|
| 134 | void 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 |
|
---|
| 140 | void 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 |
|
---|
| 215 | void wamp_router_authenticate(wamp_router_t *router)
|
---|
| 216 | {
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void wamp_router_authenticate_signature(wamp_router_t *router, const char *value)
|
---|
| 220 | {
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | void wamp_router_authenticate_extra(wamp_router_t *router, const char *value)
|
---|
| 224 | {
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | void 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 |
|
---|
| 232 | void wamp_router_authenticate_close(struct wamp_state *s)
|
---|
| 233 | {
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | void wamp_router_goodbye(wamp_router_t *router)
|
---|
| 237 | {
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void 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 |
|
---|
| 245 | void wamp_router_goodbye_details(wamp_router_t *router)
|
---|
| 246 | {
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | void wamp_router_goodbye_reason(wamp_router_t *router, const char *value)
|
---|
| 250 | {
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | void 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 | }
|
---|