source: uKadecot/trunk/kadecot/wamp_broker.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: 10.0 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_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
61static wamp_subscription_t *wamp_broker_new_subscription(wamp_broker_t *broker);
62static void wamp_broker_delete_subscription(wamp_broker_t *broker,
63 wamp_subscription_t *subscription);
64
65void wamp_broker_subscribe(wamp_broker_t *broker)
66{
67 broker->current_subscription = NULL;
68}
69
70void wamp_broker_subscribe_request_id(wamp_broker_t *broker, const char *value)
71{
72 broker->requestId = atoi(value);
73}
74
75void 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
80void wamp_broker_subscribe_options(wamp_broker_t *broker)
81{
82}
83
84void 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
131static void wamp_broker_subscribe_res(wamp_broker_t *broker);
132
133void 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
152static 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
186void wamp_broker_unsubscribe(wamp_broker_t *broker)
187{
188 broker->current_subscription = NULL;
189}
190
191void wamp_broker_unsubscribe_request_id(wamp_broker_t *broker, const char *value)
192{
193 broker->requestId = atoi(value);
194}
195
196void 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
210void 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
243void wamp_broker_publish(wamp_broker_t *broker)
244{
245}
246
247void wamp_broker_publish_request_id(wamp_broker_t *s, const char *value)
248{
249}
250
251void wamp_broker_publish_options(wamp_broker_t *s, const char *value)
252{
253}
254
255void 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
260void wamp_broker_publish_topic(wamp_broker_t *s, const char *value)
261{
262}
263
264void wamp_broker_publish_arguments(wamp_broker_t *s, const char *value)
265{
266}
267
268void 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
273void wamp_broker_publish_argumentskw(wamp_broker_t *s, const char *value)
274{
275}
276
277void 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
282void wamp_broker_publish_close(struct wamp_state *s)
283{
284}
285
286static 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
305static void wamp_broker_delete_subscription(wamp_broker_t *broker,
306 wamp_subscription_t *subscription)
307{
308 memset(subscription, 0, sizeof(*subscription));
309}
310
311void 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}
Note: See TracBrowser for help on using the repository browser.