[101] | 1 | /*
|
---|
| 2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2015 Cores Co., Ltd. Japan
|
---|
| 5 | *
|
---|
| 6 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 7 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 8 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 9 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 10 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 11 | * スコード中に含まれていること.
|
---|
| 12 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 13 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 14 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 15 | * の無保証規定を掲載すること.
|
---|
| 16 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 17 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 18 | * と.
|
---|
| 19 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 20 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 21 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 22 | * 報告すること.
|
---|
| 23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 25 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 26 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 27 | * 免責すること.
|
---|
| 28 | *
|
---|
| 29 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 30 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 31 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 32 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 33 | * の責任を負わない.
|
---|
| 34 | *
|
---|
| 35 | * @(#) $Id: wamp_broker.c 108 2015-06-11 09:15:46Z coas-nagasima $
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <string.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include "wamp.h"
|
---|
| 41 | #include "wamp_broker.h"
|
---|
| 42 | #include "main.h"
|
---|
| 43 | #include "fbs_string.h"
|
---|
| 44 | #include "kadecot_names.h"
|
---|
| 45 | #include "kernel_cfg.h"
|
---|
| 46 |
|
---|
| 47 | #ifndef _MSC_VER
|
---|
| 48 | #ifndef strncpy_s
|
---|
| 49 | #define strncpy_s(dst, dsz, src, sz) strncpy(dst, src, sz)
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
| 52 | #ifndef strcpy_s
|
---|
| 53 | #define strcpy_s(s1, s1m, s2) strcpy(s1, s2)
|
---|
| 54 | #endif
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
| 57 | static wamp_subscription_t *wamp_broker_new_subscription(wamp_broker_t *broker);
|
---|
| 58 | static void wamp_broker_delete_subscription(wamp_broker_t *broker,
|
---|
| 59 | wamp_subscription_t *subscription);
|
---|
| 60 |
|
---|
| 61 | void wamp_broker_subscribe(wamp_broker_t *broker)
|
---|
| 62 | {
|
---|
| 63 | broker->current_subscription = NULL;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void wamp_broker_subscribe_request_id(wamp_broker_t *broker, const char *value)
|
---|
| 67 | {
|
---|
| 68 | broker->requestId = atoi(value);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void wamp_broker_subscribe_options_param(wamp_broker_t *broker, jsonsl_action_t action,
|
---|
| 72 | struct jsonsl_state_st *state, const char *buf)
|
---|
| 73 | {
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void wamp_broker_subscribe_options(wamp_broker_t *broker)
|
---|
| 77 | {
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void wamp_broker_subscribe_topic(wamp_broker_t *broker, const char *value)
|
---|
| 81 | {
|
---|
| 82 | static const char topic[] = "com.sonycsl.kadecot.echonetlite.topic.";
|
---|
| 83 | char *pos, *end;
|
---|
| 84 | wamp_subscription_t *subscription;
|
---|
| 85 | uint16_t devType;
|
---|
| 86 | uint8_t epc;
|
---|
| 87 |
|
---|
| 88 | if(strncmp(topic, value, sizeof(topic) - 1) != 0)
|
---|
| 89 | return;
|
---|
| 90 |
|
---|
| 91 | subscription = wamp_broker_new_subscription(broker);
|
---|
| 92 |
|
---|
| 93 | subscription->deviceName[0] = '\0';
|
---|
| 94 | strcpy_s(subscription->deviceName, sizeof(subscription->deviceName), &value[sizeof(topic) - 1]);
|
---|
| 95 | subscription->deviceName[sizeof(subscription->deviceName) - 1] = '\0';
|
---|
| 96 |
|
---|
| 97 | end = &subscription->deviceName[sizeof(subscription->deviceName)];
|
---|
| 98 | for(pos = subscription->deviceName; pos < end; pos++){
|
---|
| 99 | if(*pos != '.')
|
---|
| 100 | continue;
|
---|
| 101 |
|
---|
| 102 | *pos = '\0';
|
---|
| 103 | subscription->proprtyName = pos + 1;
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | devType = kadecot_names_get_device_type(subscription->deviceName,
|
---|
| 108 | strnlen(subscription->deviceName, sizeof(subscription->deviceName)));
|
---|
| 109 | if(devType == 0){
|
---|
| 110 | wamp_broker_delete_subscription(broker, subscription);
|
---|
| 111 | return;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | epc = kadecot_names_get_property_code(subscription->proprtyName,
|
---|
| 115 | strnlen(subscription->proprtyName, subscription->proprtyName - subscription->deviceName), devType);
|
---|
| 116 | if(epc == 0){
|
---|
| 117 | wamp_broker_delete_subscription(broker, subscription);
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | subscription->devType = devType;
|
---|
| 122 | subscription->epc = epc;
|
---|
| 123 |
|
---|
| 124 | broker->current_subscription = subscription;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | static void wamp_broker_subscribe_res(wamp_broker_t *broker);
|
---|
| 128 |
|
---|
| 129 | void wamp_broker_subscribe_close(struct wamp_state *s)
|
---|
| 130 | {
|
---|
| 131 | wamp_broker_t *broker = &s->broker;
|
---|
| 132 | ER ret;
|
---|
| 133 |
|
---|
| 134 | broker->error = internal_error;
|
---|
| 135 |
|
---|
| 136 | // send RESULT
|
---|
| 137 | wamp_broker_subscribe_res(broker);
|
---|
| 138 |
|
---|
| 139 | if (broker->error != NULL){
|
---|
| 140 | ret = wamp_send_error_res(s, WAMP_CODE_SUBSCRIBE, broker->requestId, broker->error);
|
---|
| 141 | if (ret != E_OK) {
|
---|
| 142 | syslog(LOG_WARNING, "wamp_broker_subscribe_close() : wamp_send_error_res() result = %d", ret);
|
---|
| 143 | }
|
---|
| 144 | broker->error = NULL;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | static void wamp_broker_subscribe_res(wamp_broker_t *broker)
|
---|
| 149 | {
|
---|
| 150 | struct wamp_state *s = broker->s;
|
---|
| 151 | wamp_subscription_t *subscription = broker->current_subscription;
|
---|
| 152 | // send SUBSCRIBED or ERROR
|
---|
| 153 | ECN_FBS_ID buf;
|
---|
| 154 | ECN_FBS_SSIZE_T pos = 0;
|
---|
| 155 | ER ret;
|
---|
| 156 |
|
---|
| 157 | if ((subscription == NULL) || (subscription->devType == 0) || (subscription->epc == 0))
|
---|
| 158 | return;
|
---|
| 159 |
|
---|
| 160 | ret = _ecn_fbs_cre(1, &buf);
|
---|
| 161 | if (ret != E_OK) {
|
---|
| 162 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | ret = fbs_printf(buf, &pos, "[33,%d,%d]", broker->requestId, subscription->subscriptionId);
|
---|
| 167 | if (ret != E_OK) {
|
---|
| 168 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
| 169 | _ecn_fbs_del(buf);
|
---|
| 170 | return;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | ret = main_send_message(buf, s->wbsid);
|
---|
| 174 | if (ret != E_OK) {
|
---|
| 175 | syslog(LOG_WARNING, "wamp_broker_subscribe_close() : main_send_message() result = %d", ret);
|
---|
| 176 | return;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | broker->error = NULL;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void wamp_broker_unsubscribe(wamp_broker_t *broker)
|
---|
| 183 | {
|
---|
| 184 | broker->current_subscription = NULL;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | void wamp_broker_unsubscribe_request_id(wamp_broker_t *broker, const char *value)
|
---|
| 188 | {
|
---|
| 189 | broker->requestId = atoi(value);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | void wamp_broker_unsubscribe_subscription_id(wamp_broker_t *broker, const char *value)
|
---|
| 193 | {
|
---|
| 194 | int subscriptionId = atoi(value);
|
---|
| 195 | wamp_subscription_t *pos, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
| 196 |
|
---|
| 197 | for(pos = broker->subscriptions; pos < end; pos++){
|
---|
| 198 | if(pos->subscriptionId != subscriptionId)
|
---|
| 199 | continue;
|
---|
| 200 |
|
---|
| 201 | broker->current_subscription = pos;
|
---|
| 202 | break;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | void wamp_broker_unsubscribe_close(struct wamp_state *s)
|
---|
| 207 | {
|
---|
| 208 | wamp_broker_t *broker = &s->broker;
|
---|
| 209 | wamp_subscription_t *subscription = broker->current_subscription;
|
---|
| 210 | // send UNSUBSCRIBED or ERROR
|
---|
| 211 | ECN_FBS_ID buf;
|
---|
| 212 | ECN_FBS_SSIZE_T pos = 0;
|
---|
| 213 | ER ret;
|
---|
| 214 |
|
---|
| 215 | if(subscription == NULL)
|
---|
| 216 | return;
|
---|
| 217 |
|
---|
| 218 | wamp_broker_delete_subscription(broker, subscription);
|
---|
| 219 |
|
---|
| 220 | ret = _ecn_fbs_cre(1, &buf);
|
---|
| 221 | if (ret != E_OK) {
|
---|
| 222 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
| 223 | return;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | ret = fbs_printf(buf, &pos, "[35,%d,%d]", broker->requestId, subscription->subscriptionId);
|
---|
| 227 | if (ret != E_OK) {
|
---|
| 228 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
| 229 | _ecn_fbs_del(buf);
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | ret = main_send_message(buf, s->wbsid);
|
---|
| 234 | if (ret != E_OK) {
|
---|
| 235 | syslog(LOG_WARNING, "wamp_broker_unsubscribe_close() : main_send_message() result = %d", ret);
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | void wamp_broker_publish(wamp_broker_t *broker)
|
---|
| 240 | {
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void wamp_broker_publish_request_id(wamp_broker_t *s, const char *value)
|
---|
| 244 | {
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | void wamp_broker_publish_options(wamp_broker_t *s, const char *value)
|
---|
| 248 | {
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | void wamp_broker_publish_options_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
| 252 | struct jsonsl_state_st *state, const char *buf)
|
---|
| 253 | {
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void wamp_broker_publish_topic(wamp_broker_t *s, const char *value)
|
---|
| 257 | {
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | void wamp_broker_publish_arguments(wamp_broker_t *s, const char *value)
|
---|
| 261 | {
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | void wamp_broker_publish_arguments_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
| 265 | struct jsonsl_state_st *state, const char *buf)
|
---|
| 266 | {
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | void wamp_broker_publish_argumentskw(wamp_broker_t *s, const char *value)
|
---|
| 270 | {
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | void wamp_broker_publish_argumentskw_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
| 274 | struct jsonsl_state_st *state, const char *buf)
|
---|
| 275 | {
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | void wamp_broker_publish_close(struct wamp_state *s)
|
---|
| 279 | {
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | static wamp_subscription_t *wamp_broker_new_subscription(wamp_broker_t *broker)
|
---|
| 283 | {
|
---|
| 284 | wamp_subscription_t *pos, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
| 285 |
|
---|
| 286 | for(pos = broker->subscriptions; pos < end; pos++){
|
---|
| 287 | if(pos->subscriptionId != 0)
|
---|
| 288 | continue;
|
---|
| 289 |
|
---|
| 290 | broker->subscriptionId++;
|
---|
| 291 | if(broker->subscriptionId == 0)
|
---|
| 292 | broker->subscriptionId++;
|
---|
| 293 |
|
---|
| 294 | pos->subscriptionId = broker->subscriptionId;
|
---|
| 295 | return pos;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | return NULL;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | static void wamp_broker_delete_subscription(wamp_broker_t *broker,
|
---|
| 302 | wamp_subscription_t *subscription)
|
---|
| 303 | {
|
---|
| 304 | memset(subscription, 0, sizeof(*subscription));
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | void wamp_broker_publish_inf(wamp_broker_t *broker, uint16_t pubId, int deviceId,
|
---|
| 308 | uint16_t devType, uint8_t epc, uint8_t pdc, uint8_t *p_edt)
|
---|
| 309 | {
|
---|
| 310 | struct wamp_state *s = broker->s;
|
---|
| 311 | wamp_subscription_t *subscription, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
| 312 | ECN_FBS_ID buf;
|
---|
| 313 | ECN_FBS_SSIZE_T pos = 0, start;
|
---|
| 314 | ER ret;
|
---|
| 315 | int i;
|
---|
| 316 |
|
---|
| 317 | for(subscription = broker->subscriptions; subscription < end; subscription++){
|
---|
| 318 | if (subscription->subscriptionId == 0)
|
---|
| 319 | continue;
|
---|
| 320 |
|
---|
| 321 | if ((subscription->devType != devType) || (subscription->epc != epc))
|
---|
| 322 | continue;
|
---|
| 323 |
|
---|
| 324 | ret = _ecn_fbs_cre(1, &buf);
|
---|
| 325 | if (ret != E_OK) {
|
---|
| 326 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
| 327 | break;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | ret = fbs_printf(buf, &pos, "[36,%d,%d,{\"deviceId\":%d},[],",
|
---|
| 331 | subscription->subscriptionId, pubId, deviceId);
|
---|
| 332 | if (ret != E_OK) {
|
---|
| 333 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
| 334 | _ecn_fbs_del(buf);
|
---|
| 335 | break;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | ret = fbs_printf(buf, &pos, "{\"propertyName\":\"%s\",\"propertyValue\":[",
|
---|
| 339 | subscription->proprtyName);
|
---|
| 340 | if (ret != E_OK) {
|
---|
| 341 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
| 342 | _ecn_fbs_del(buf);
|
---|
| 343 | break;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | start = pos;
|
---|
| 347 | for(i = 0; i < pdc; i++){
|
---|
| 348 | ret = fbs_printf(buf, &pos, "%d,", p_edt[i]);
|
---|
| 349 | if (ret != E_OK) {
|
---|
| 350 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
| 351 | _ecn_fbs_del(buf);
|
---|
| 352 | break;
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | /* 最後の","を消す */
|
---|
| 357 | if(start != pos)
|
---|
| 358 | pos--;
|
---|
| 359 |
|
---|
| 360 | ret = fbs_printf(buf, &pos, "]}]");
|
---|
| 361 | if (ret != E_OK) {
|
---|
| 362 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
| 363 | _ecn_fbs_del(buf);
|
---|
| 364 | break;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | ret = main_send_message(buf, s->wbsid);
|
---|
| 368 | if (ret != E_OK) {
|
---|
| 369 | syslog(LOG_WARNING, "wamp_broker_publish_inf() : main_send_message() result = %d", ret);
|
---|
| 370 | }
|
---|
| 371 | break;
|
---|
| 372 | }
|
---|
| 373 | }
|
---|