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_broker.c 101 2015-06-02 15:37:23Z coas-nagasima $
|
---|
40 | */
|
---|
41 |
|
---|
42 | #include <string.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include "wamp.h"
|
---|
45 | #include "wamp_broker.h"
|
---|
46 | #include "main.h"
|
---|
47 | #include "fbs_string.h"
|
---|
48 | #include "kadecot_names.h"
|
---|
49 | #include "kernel_cfg.h"
|
---|
50 |
|
---|
51 | #ifndef _MSC_VER
|
---|
52 | #ifndef strncpy_s
|
---|
53 | #define strncpy_s(dst, dsz, src, sz) strncpy(dst, src, sz)
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifndef strcpy_s
|
---|
57 | #define strcpy_s(s1, s1m, s2) strcpy(s1, s2)
|
---|
58 | #endif
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | static wamp_subscription_t *wamp_broker_new_subscription(wamp_broker_t *broker);
|
---|
62 | static void wamp_broker_delete_subscription(wamp_broker_t *broker,
|
---|
63 | wamp_subscription_t *subscription);
|
---|
64 |
|
---|
65 | void wamp_broker_subscribe(wamp_broker_t *broker)
|
---|
66 | {
|
---|
67 | broker->current_subscription = NULL;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void wamp_broker_subscribe_request_id(wamp_broker_t *broker, const char *value)
|
---|
71 | {
|
---|
72 | broker->requestId = atoi(value);
|
---|
73 | }
|
---|
74 |
|
---|
75 | void wamp_broker_subscribe_options_param(wamp_broker_t *broker, jsonsl_action_t action,
|
---|
76 | struct jsonsl_state_st *state, const char *buf)
|
---|
77 | {
|
---|
78 | }
|
---|
79 |
|
---|
80 | void wamp_broker_subscribe_options(wamp_broker_t *broker)
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | void wamp_broker_subscribe_topic(wamp_broker_t *broker, const char *value)
|
---|
85 | {
|
---|
86 | static const char topic[] = "com.sonycsl.kadecot.echonetlite.topic.";
|
---|
87 | char *pos, *end;
|
---|
88 | wamp_subscription_t *subscription;
|
---|
89 | uint16_t devType;
|
---|
90 | uint8_t epc;
|
---|
91 |
|
---|
92 | if(strncmp(topic, value, sizeof(topic) - 1) != 0)
|
---|
93 | return;
|
---|
94 |
|
---|
95 | subscription = wamp_broker_new_subscription(broker);
|
---|
96 |
|
---|
97 | subscription->deviceName[0] = '\0';
|
---|
98 | strcpy_s(subscription->deviceName, sizeof(subscription->deviceName), &value[sizeof(topic) - 1]);
|
---|
99 | subscription->deviceName[sizeof(subscription->deviceName) - 1] = '\0';
|
---|
100 |
|
---|
101 | end = &subscription->deviceName[sizeof(subscription->deviceName)];
|
---|
102 | for(pos = subscription->deviceName; pos < end; pos++){
|
---|
103 | if(*pos != '.')
|
---|
104 | continue;
|
---|
105 |
|
---|
106 | *pos = '\0';
|
---|
107 | subscription->proprtyName = pos + 1;
|
---|
108 | break;
|
---|
109 | }
|
---|
110 |
|
---|
111 | devType = kadecot_names_get_device_type(subscription->deviceName,
|
---|
112 | strnlen(subscription->deviceName, sizeof(subscription->deviceName)));
|
---|
113 | if(devType == 0){
|
---|
114 | wamp_broker_delete_subscription(broker, subscription);
|
---|
115 | return;
|
---|
116 | }
|
---|
117 |
|
---|
118 | epc = kadecot_names_get_property_code(subscription->proprtyName,
|
---|
119 | strnlen(subscription->proprtyName, subscription->proprtyName - subscription->deviceName), devType);
|
---|
120 | if(epc == 0){
|
---|
121 | wamp_broker_delete_subscription(broker, subscription);
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|
125 | subscription->devType = devType;
|
---|
126 | subscription->epc = epc;
|
---|
127 |
|
---|
128 | broker->current_subscription = subscription;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static void wamp_broker_subscribe_res(wamp_broker_t *broker);
|
---|
132 |
|
---|
133 | void wamp_broker_subscribe_close(struct wamp_state *s)
|
---|
134 | {
|
---|
135 | wamp_broker_t *broker = &s->broker;
|
---|
136 | ER ret;
|
---|
137 |
|
---|
138 | broker->error = internal_error;
|
---|
139 |
|
---|
140 | // send RESULT
|
---|
141 | wamp_broker_subscribe_res(broker);
|
---|
142 |
|
---|
143 | if (broker->error != NULL){
|
---|
144 | ret = wamp_send_error_res(s, WAMP_CODE_SUBSCRIBE, broker->requestId, broker->error);
|
---|
145 | if (ret != E_OK) {
|
---|
146 | syslog(LOG_WARNING, "wamp_broker_subscribe_close() : wamp_send_error_res() result = %d", ret);
|
---|
147 | }
|
---|
148 | broker->error = NULL;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void wamp_broker_subscribe_res(wamp_broker_t *broker)
|
---|
153 | {
|
---|
154 | struct wamp_state *s = broker->s;
|
---|
155 | wamp_subscription_t *subscription = broker->current_subscription;
|
---|
156 | // send SUBSCRIBED or ERROR
|
---|
157 | ECN_FBS_ID buf;
|
---|
158 | ECN_FBS_SSIZE_T pos = 0;
|
---|
159 | ER ret;
|
---|
160 |
|
---|
161 | if ((subscription == NULL) || (subscription->devType == 0) || (subscription->epc == 0))
|
---|
162 | return;
|
---|
163 |
|
---|
164 | ret = _ecn_fbs_cre(1, &buf);
|
---|
165 | if (ret != E_OK) {
|
---|
166 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | ret = fbs_printf(buf, &pos, "[33,%d,%d]", broker->requestId, subscription->subscriptionId);
|
---|
171 | if (ret != E_OK) {
|
---|
172 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
173 | _ecn_fbs_del(buf);
|
---|
174 | return;
|
---|
175 | }
|
---|
176 |
|
---|
177 | ret = main_send_message(buf, s->wbsid);
|
---|
178 | if (ret != E_OK) {
|
---|
179 | syslog(LOG_WARNING, "wamp_broker_subscribe_close() : main_send_message() result = %d", ret);
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | broker->error = NULL;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void wamp_broker_unsubscribe(wamp_broker_t *broker)
|
---|
187 | {
|
---|
188 | broker->current_subscription = NULL;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void wamp_broker_unsubscribe_request_id(wamp_broker_t *broker, const char *value)
|
---|
192 | {
|
---|
193 | broker->requestId = atoi(value);
|
---|
194 | }
|
---|
195 |
|
---|
196 | void wamp_broker_unsubscribe_subscription_id(wamp_broker_t *broker, const char *value)
|
---|
197 | {
|
---|
198 | int subscriptionId = atoi(value);
|
---|
199 | wamp_subscription_t *pos, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
200 |
|
---|
201 | for(pos = broker->subscriptions; pos < end; pos++){
|
---|
202 | if(pos->subscriptionId != subscriptionId)
|
---|
203 | continue;
|
---|
204 |
|
---|
205 | broker->current_subscription = pos;
|
---|
206 | break;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | void wamp_broker_unsubscribe_close(struct wamp_state *s)
|
---|
211 | {
|
---|
212 | wamp_broker_t *broker = &s->broker;
|
---|
213 | wamp_subscription_t *subscription = broker->current_subscription;
|
---|
214 | // send UNSUBSCRIBED or ERROR
|
---|
215 | ECN_FBS_ID buf;
|
---|
216 | ECN_FBS_SSIZE_T pos = 0;
|
---|
217 | ER ret;
|
---|
218 |
|
---|
219 | if(subscription == NULL)
|
---|
220 | return;
|
---|
221 |
|
---|
222 | wamp_broker_delete_subscription(broker, subscription);
|
---|
223 |
|
---|
224 | ret = _ecn_fbs_cre(1, &buf);
|
---|
225 | if (ret != E_OK) {
|
---|
226 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | ret = fbs_printf(buf, &pos, "[35,%d,%d]", broker->requestId, subscription->subscriptionId);
|
---|
231 | if (ret != E_OK) {
|
---|
232 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
233 | _ecn_fbs_del(buf);
|
---|
234 | return;
|
---|
235 | }
|
---|
236 |
|
---|
237 | ret = main_send_message(buf, s->wbsid);
|
---|
238 | if (ret != E_OK) {
|
---|
239 | syslog(LOG_WARNING, "wamp_broker_unsubscribe_close() : main_send_message() result = %d", ret);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void wamp_broker_publish(wamp_broker_t *broker)
|
---|
244 | {
|
---|
245 | }
|
---|
246 |
|
---|
247 | void wamp_broker_publish_request_id(wamp_broker_t *s, const char *value)
|
---|
248 | {
|
---|
249 | }
|
---|
250 |
|
---|
251 | void wamp_broker_publish_options(wamp_broker_t *s, const char *value)
|
---|
252 | {
|
---|
253 | }
|
---|
254 |
|
---|
255 | void wamp_broker_publish_options_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
256 | struct jsonsl_state_st *state, const char *buf)
|
---|
257 | {
|
---|
258 | }
|
---|
259 |
|
---|
260 | void wamp_broker_publish_topic(wamp_broker_t *s, const char *value)
|
---|
261 | {
|
---|
262 | }
|
---|
263 |
|
---|
264 | void wamp_broker_publish_arguments(wamp_broker_t *s, const char *value)
|
---|
265 | {
|
---|
266 | }
|
---|
267 |
|
---|
268 | void wamp_broker_publish_arguments_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
269 | struct jsonsl_state_st *state, const char *buf)
|
---|
270 | {
|
---|
271 | }
|
---|
272 |
|
---|
273 | void wamp_broker_publish_argumentskw(wamp_broker_t *s, const char *value)
|
---|
274 | {
|
---|
275 | }
|
---|
276 |
|
---|
277 | void wamp_broker_publish_argumentskw_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
278 | struct jsonsl_state_st *state, const char *buf)
|
---|
279 | {
|
---|
280 | }
|
---|
281 |
|
---|
282 | void wamp_broker_publish_close(struct wamp_state *s)
|
---|
283 | {
|
---|
284 | }
|
---|
285 |
|
---|
286 | static wamp_subscription_t *wamp_broker_new_subscription(wamp_broker_t *broker)
|
---|
287 | {
|
---|
288 | wamp_subscription_t *pos, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
289 |
|
---|
290 | for(pos = broker->subscriptions; pos < end; pos++){
|
---|
291 | if(pos->subscriptionId != 0)
|
---|
292 | continue;
|
---|
293 |
|
---|
294 | broker->subscriptionId++;
|
---|
295 | if(broker->subscriptionId == 0)
|
---|
296 | broker->subscriptionId++;
|
---|
297 |
|
---|
298 | pos->subscriptionId = broker->subscriptionId;
|
---|
299 | return pos;
|
---|
300 | }
|
---|
301 |
|
---|
302 | return NULL;
|
---|
303 | }
|
---|
304 |
|
---|
305 | static void wamp_broker_delete_subscription(wamp_broker_t *broker,
|
---|
306 | wamp_subscription_t *subscription)
|
---|
307 | {
|
---|
308 | memset(subscription, 0, sizeof(*subscription));
|
---|
309 | }
|
---|
310 |
|
---|
311 | void wamp_broker_publish_inf(wamp_broker_t *broker, uint16_t pubId, int deviceId,
|
---|
312 | uint16_t devType, uint8_t epc, uint8_t pdc, uint8_t *p_edt)
|
---|
313 | {
|
---|
314 | struct wamp_state *s = broker->s;
|
---|
315 | wamp_subscription_t *subscription, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
316 | ECN_FBS_ID buf;
|
---|
317 | ECN_FBS_SSIZE_T pos = 0, start;
|
---|
318 | ER ret;
|
---|
319 | int i;
|
---|
320 |
|
---|
321 | for(subscription = broker->subscriptions; subscription < end; subscription++){
|
---|
322 | if (subscription->subscriptionId == 0)
|
---|
323 | continue;
|
---|
324 |
|
---|
325 | if ((subscription->devType != devType) || (subscription->epc != epc))
|
---|
326 | continue;
|
---|
327 |
|
---|
328 | ret = _ecn_fbs_cre(1, &buf);
|
---|
329 | if (ret != E_OK) {
|
---|
330 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
331 | break;
|
---|
332 | }
|
---|
333 |
|
---|
334 | ret = fbs_printf(buf, &pos, "[36,%d,%d,{\"deviceId\":%d},[],",
|
---|
335 | subscription->subscriptionId, pubId, deviceId);
|
---|
336 | if (ret != E_OK) {
|
---|
337 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
338 | _ecn_fbs_del(buf);
|
---|
339 | break;
|
---|
340 | }
|
---|
341 |
|
---|
342 | ret = fbs_printf(buf, &pos, "{\"propertyName\":\"%s\",\"propertyValue\":[",
|
---|
343 | subscription->proprtyName);
|
---|
344 | if (ret != E_OK) {
|
---|
345 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
346 | _ecn_fbs_del(buf);
|
---|
347 | break;
|
---|
348 | }
|
---|
349 |
|
---|
350 | start = pos;
|
---|
351 | for(i = 0; i < pdc; i++){
|
---|
352 | ret = fbs_printf(buf, &pos, "%d,", p_edt[i]);
|
---|
353 | if (ret != E_OK) {
|
---|
354 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
355 | _ecn_fbs_del(buf);
|
---|
356 | break;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | /* ÅãÌ","ðÁ· */
|
---|
361 | if(start != pos)
|
---|
362 | pos--;
|
---|
363 |
|
---|
364 | ret = fbs_printf(buf, &pos, "]}]");
|
---|
365 | if (ret != E_OK) {
|
---|
366 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
367 | _ecn_fbs_del(buf);
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | ret = main_send_message(buf, s->wbsid);
|
---|
372 | if (ret != E_OK) {
|
---|
373 | syslog(LOG_WARNING, "wamp_broker_publish_inf() : main_send_message() result = %d", ret);
|
---|
374 | }
|
---|
375 | break;
|
---|
376 | }
|
---|
377 | }
|
---|