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 148 2016-01-08 05:35:52Z 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 | if(subscription == NULL)
|
---|
93 | return;
|
---|
94 |
|
---|
95 | subscription->deviceName[0] = '\0';
|
---|
96 | strcpy_s(subscription->deviceName, sizeof(subscription->deviceName), &value[sizeof(topic) - 1]);
|
---|
97 | subscription->deviceName[sizeof(subscription->deviceName) - 1] = '\0';
|
---|
98 |
|
---|
99 | end = &subscription->deviceName[sizeof(subscription->deviceName)];
|
---|
100 | for(pos = subscription->deviceName; pos < end; pos++){
|
---|
101 | if(*pos != '.')
|
---|
102 | continue;
|
---|
103 |
|
---|
104 | *pos = '\0';
|
---|
105 | subscription->proprtyName = pos + 1;
|
---|
106 | break;
|
---|
107 | }
|
---|
108 |
|
---|
109 | devType = kadecot_names_get_device_type(subscription->deviceName,
|
---|
110 | strnlen(subscription->deviceName, sizeof(subscription->deviceName)));
|
---|
111 | if(devType == 0){
|
---|
112 | wamp_broker_delete_subscription(broker, subscription);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | epc = kadecot_names_get_property_code(subscription->proprtyName,
|
---|
117 | strnlen(subscription->proprtyName, subscription->proprtyName - subscription->deviceName), devType);
|
---|
118 | if(epc == 0){
|
---|
119 | wamp_broker_delete_subscription(broker, subscription);
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | subscription->devType = devType;
|
---|
124 | subscription->epc = epc;
|
---|
125 |
|
---|
126 | broker->current_subscription = subscription;
|
---|
127 | }
|
---|
128 |
|
---|
129 | static void wamp_broker_subscribe_res(wamp_broker_t *broker);
|
---|
130 |
|
---|
131 | void wamp_broker_subscribe_close(struct wamp_state *s)
|
---|
132 | {
|
---|
133 | wamp_broker_t *broker = &s->broker;
|
---|
134 | ER ret;
|
---|
135 |
|
---|
136 | broker->error = internal_error;
|
---|
137 |
|
---|
138 | // send RESULT
|
---|
139 | wamp_broker_subscribe_res(broker);
|
---|
140 |
|
---|
141 | if (broker->error != NULL){
|
---|
142 | ret = wamp_send_error_res(s, WAMP_CODE_SUBSCRIBE, broker->requestId, broker->error);
|
---|
143 | if (ret != E_OK) {
|
---|
144 | syslog(LOG_WARNING, "wamp_broker_subscribe_close() : wamp_send_error_res() result = %d", ret);
|
---|
145 | }
|
---|
146 | broker->error = NULL;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void wamp_broker_subscribe_res(wamp_broker_t *broker)
|
---|
151 | {
|
---|
152 | struct wamp_state *s = broker->s;
|
---|
153 | wamp_subscription_t *subscription = broker->current_subscription;
|
---|
154 | // send SUBSCRIBED or ERROR
|
---|
155 | ECN_FBS_ID buf;
|
---|
156 | ECN_FBS_SSIZE_T pos = 0;
|
---|
157 | ER ret;
|
---|
158 |
|
---|
159 | if ((subscription == NULL) || (subscription->devType == 0) || (subscription->epc == 0))
|
---|
160 | return;
|
---|
161 |
|
---|
162 | ret = _ecn_fbs_cre(1, &buf);
|
---|
163 | if (ret != E_OK) {
|
---|
164 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | ret = fbs_printf(buf, &pos, "[33,%d,%d]", broker->requestId, subscription->subscriptionId);
|
---|
169 | if (ret != E_OK) {
|
---|
170 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
171 | _ecn_fbs_del(buf);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 |
|
---|
175 | ret = main_send_message(buf, s->wbsid);
|
---|
176 | if (ret != E_OK) {
|
---|
177 | syslog(LOG_WARNING, "wamp_broker_subscribe_close() : main_send_message() result = %d", ret);
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | broker->error = NULL;
|
---|
182 | }
|
---|
183 |
|
---|
184 | void wamp_broker_unsubscribe(wamp_broker_t *broker)
|
---|
185 | {
|
---|
186 | broker->current_subscription = NULL;
|
---|
187 | }
|
---|
188 |
|
---|
189 | void wamp_broker_unsubscribe_request_id(wamp_broker_t *broker, const char *value)
|
---|
190 | {
|
---|
191 | broker->requestId = atoi(value);
|
---|
192 | }
|
---|
193 |
|
---|
194 | void wamp_broker_unsubscribe_subscription_id(wamp_broker_t *broker, const char *value)
|
---|
195 | {
|
---|
196 | int subscriptionId = atoi(value);
|
---|
197 | wamp_subscription_t *pos, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
198 |
|
---|
199 | for(pos = broker->subscriptions; pos < end; pos++){
|
---|
200 | if(pos->subscriptionId != subscriptionId)
|
---|
201 | continue;
|
---|
202 |
|
---|
203 | broker->current_subscription = pos;
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | void wamp_broker_unsubscribe_close(struct wamp_state *s)
|
---|
209 | {
|
---|
210 | wamp_broker_t *broker = &s->broker;
|
---|
211 | wamp_subscription_t *subscription = broker->current_subscription;
|
---|
212 | // send UNSUBSCRIBED or ERROR
|
---|
213 | ECN_FBS_ID buf;
|
---|
214 | ECN_FBS_SSIZE_T pos = 0;
|
---|
215 | ER ret;
|
---|
216 |
|
---|
217 | if(subscription == NULL)
|
---|
218 | return;
|
---|
219 |
|
---|
220 | wamp_broker_delete_subscription(broker, subscription);
|
---|
221 |
|
---|
222 | ret = _ecn_fbs_cre(1, &buf);
|
---|
223 | if (ret != E_OK) {
|
---|
224 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
225 | return;
|
---|
226 | }
|
---|
227 |
|
---|
228 | ret = fbs_printf(buf, &pos, "[35,%d,%d]", broker->requestId, subscription->subscriptionId);
|
---|
229 | if (ret != E_OK) {
|
---|
230 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
231 | _ecn_fbs_del(buf);
|
---|
232 | return;
|
---|
233 | }
|
---|
234 |
|
---|
235 | ret = main_send_message(buf, s->wbsid);
|
---|
236 | if (ret != E_OK) {
|
---|
237 | syslog(LOG_WARNING, "wamp_broker_unsubscribe_close() : main_send_message() result = %d", ret);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | void wamp_broker_publish(wamp_broker_t *broker)
|
---|
242 | {
|
---|
243 | }
|
---|
244 |
|
---|
245 | void wamp_broker_publish_request_id(wamp_broker_t *s, const char *value)
|
---|
246 | {
|
---|
247 | }
|
---|
248 |
|
---|
249 | void wamp_broker_publish_options(wamp_broker_t *s, const char *value)
|
---|
250 | {
|
---|
251 | }
|
---|
252 |
|
---|
253 | void wamp_broker_publish_options_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
254 | struct jsonsl_state_st *state, const char *buf)
|
---|
255 | {
|
---|
256 | }
|
---|
257 |
|
---|
258 | void wamp_broker_publish_topic(wamp_broker_t *s, const char *value)
|
---|
259 | {
|
---|
260 | }
|
---|
261 |
|
---|
262 | void wamp_broker_publish_arguments(wamp_broker_t *s, const char *value)
|
---|
263 | {
|
---|
264 | }
|
---|
265 |
|
---|
266 | void wamp_broker_publish_arguments_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
267 | struct jsonsl_state_st *state, const char *buf)
|
---|
268 | {
|
---|
269 | }
|
---|
270 |
|
---|
271 | void wamp_broker_publish_argumentskw(wamp_broker_t *s, const char *value)
|
---|
272 | {
|
---|
273 | }
|
---|
274 |
|
---|
275 | void wamp_broker_publish_argumentskw_param(wamp_broker_t *s, jsonsl_action_t action,
|
---|
276 | struct jsonsl_state_st *state, const char *buf)
|
---|
277 | {
|
---|
278 | }
|
---|
279 |
|
---|
280 | void wamp_broker_publish_close(struct wamp_state *s)
|
---|
281 | {
|
---|
282 | }
|
---|
283 |
|
---|
284 | static wamp_subscription_t *wamp_broker_new_subscription(wamp_broker_t *broker)
|
---|
285 | {
|
---|
286 | wamp_subscription_t *pos, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
287 |
|
---|
288 | for(pos = broker->subscriptions; pos < end; pos++){
|
---|
289 | if(pos->subscriptionId != 0)
|
---|
290 | continue;
|
---|
291 |
|
---|
292 | broker->subscriptionId++;
|
---|
293 | if(broker->subscriptionId == 0)
|
---|
294 | broker->subscriptionId++;
|
---|
295 |
|
---|
296 | pos->subscriptionId = broker->subscriptionId;
|
---|
297 | return pos;
|
---|
298 | }
|
---|
299 |
|
---|
300 | return NULL;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static void wamp_broker_delete_subscription(wamp_broker_t *broker,
|
---|
304 | wamp_subscription_t *subscription)
|
---|
305 | {
|
---|
306 | memset(subscription, 0, sizeof(*subscription));
|
---|
307 | }
|
---|
308 |
|
---|
309 | void wamp_broker_publish_inf(wamp_broker_t *broker, uint16_t pubId, int deviceId,
|
---|
310 | uint16_t devType, uint8_t epc, uint8_t pdc, uint8_t *p_edt)
|
---|
311 | {
|
---|
312 | struct wamp_state *s = broker->s;
|
---|
313 | wamp_subscription_t *subscription, *end = &broker->subscriptions[sizeof(broker->subscriptions) / sizeof(broker->subscriptions[0])];
|
---|
314 | ECN_FBS_ID buf;
|
---|
315 | ECN_FBS_SSIZE_T pos = 0, start;
|
---|
316 | ER ret;
|
---|
317 | int i;
|
---|
318 |
|
---|
319 | for(subscription = broker->subscriptions; subscription < end; subscription++){
|
---|
320 | if (subscription->subscriptionId == 0)
|
---|
321 | continue;
|
---|
322 |
|
---|
323 | if ((subscription->devType != devType) || (subscription->epc != epc))
|
---|
324 | continue;
|
---|
325 |
|
---|
326 | ret = _ecn_fbs_cre(1, &buf);
|
---|
327 | if (ret != E_OK) {
|
---|
328 | syslog(LOG_WARNING, "_ecn_fbs_cre() result = %d", ret);
|
---|
329 | break;
|
---|
330 | }
|
---|
331 |
|
---|
332 | ret = fbs_printf(buf, &pos, "[36,%d,%d,{\"deviceId\":%d},[],",
|
---|
333 | subscription->subscriptionId, pubId, deviceId);
|
---|
334 | if (ret != E_OK) {
|
---|
335 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
336 | _ecn_fbs_del(buf);
|
---|
337 | break;
|
---|
338 | }
|
---|
339 |
|
---|
340 | ret = fbs_printf(buf, &pos, "{\"propertyName\":\"%s\",\"propertyValue\":[",
|
---|
341 | subscription->proprtyName);
|
---|
342 | if (ret != E_OK) {
|
---|
343 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
344 | _ecn_fbs_del(buf);
|
---|
345 | break;
|
---|
346 | }
|
---|
347 |
|
---|
348 | start = pos;
|
---|
349 | for(i = 0; i < pdc; i++){
|
---|
350 | ret = fbs_printf(buf, &pos, "%d,", p_edt[i]);
|
---|
351 | if (ret != E_OK) {
|
---|
352 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
353 | _ecn_fbs_del(buf);
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* 最後の","を消す */
|
---|
359 | if(start != pos)
|
---|
360 | pos--;
|
---|
361 |
|
---|
362 | ret = fbs_printf(buf, &pos, "]}]");
|
---|
363 | if (ret != E_OK) {
|
---|
364 | syslog(LOG_WARNING, "fbs_printf() result = %d", ret);
|
---|
365 | _ecn_fbs_del(buf);
|
---|
366 | break;
|
---|
367 | }
|
---|
368 |
|
---|
369 | ret = main_send_message(buf, s->wbsid);
|
---|
370 | if (ret != E_OK) {
|
---|
371 | syslog(LOG_WARNING, "wamp_broker_publish_inf() : main_send_message() result = %d", ret);
|
---|
372 | }
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | }
|
---|