From 1363f808f98eccfe113624cfcf3291fd601fbac2 Mon Sep 17 00:00:00 2001 From: Kazumasa Mitsunari Date: Wed, 1 Nov 2017 14:20:44 +0900 Subject: Update document Update ApplicationGuide Change-Id: I8a9c675adeb8997debaa553f0e45f736a2719010 Signed-off-by: Kazumasa Mitsunari --- doc/api-ref/html/libsoundmanager_8cpp_source.html | 40 +++++++++++++---------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'doc/api-ref/html/libsoundmanager_8cpp_source.html') diff --git a/doc/api-ref/html/libsoundmanager_8cpp_source.html b/doc/api-ref/html/libsoundmanager_8cpp_source.html index 43e6d5d..7e145ef 100644 --- a/doc/api-ref/html/libsoundmanager_8cpp_source.html +++ b/doc/api-ref/html/libsoundmanager_8cpp_source.html @@ -5,7 +5,7 @@ -Sound Manager: SoundManagerBinding/libsoundmanager/libsoundmanager.cpp Source File +Sound Manager: R:/SoundManagerBinding/libsoundmanager/libsoundmanager.cpp Source File @@ -83,30 +83,36 @@ $(document).ready(function(){initNavTree('libsoundmanager_8cpp_source.html','');
libsoundmanager.cpp
-Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdarg.h>
18 #include <sys/socket.h>
19 #include <iostream>
20 #include <algorithm>
21 #include <thread>
22 #include <errno.h>
24 
25 #define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
26 #define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
27 
28 using namespace std;
29 
30 static void _DLOG(const char* func, const int line, const char* log, ...);
31 static void _ELOG(const char* func, const int line, const char* log, ...);
32 static bool has_verb(const string& verb);
33 static const char API[] = "soundmanager";
34 
35 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
36 {
37  static_cast<LibSoundmanager*>(closure)->on_hangup(NULL,wsj);
38 }
39 
40 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
41 {
42  /* LibSoundmanager is not called from other process */
43 }
44 
45 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
46 {
47  static_cast<LibSoundmanager*>(closure)->on_event(NULL,event,msg);
48 }
49 
50 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
51 {
52  static_cast<LibSoundmanager*>(closure)->on_reply(NULL,msg);
53 }
54 
55 
70 LibSoundmanager::LibSoundmanager(const int port, const string& token)
71 {
72  int ret;
73  if(port > 0 && token.size() > 0)
74  {
75  mport = port;
76  mtoken = token;
77  }
78  else
79  {
80  ELOG("port and token should be > 0, Initial port and token uses.");
81  }
82 
83  ret = initialize_websocket();
84  if(ret != 0 )
85  {
86  ELOG("Failed to initialize websocket");
87  }
88  else{
89  DLOG("Initialized");
90  }
91 }
92 
94 {
95  if(mploop)
96  {
97  sd_event_unref(mploop);
98  }
99  if(sp_websock != NULL)
100  {
101  free(sp_websock);
102  }
103 }
104 
120  void (*event_cb)(const std::string& event, struct json_object* event_contents),
121  void (*reply_cb)(struct json_object* reply_contents))
122 {
123  onEvent = event_cb;
124  onReply = reply_cb;
125 }
126 
127 int LibSoundmanager::initialize_websocket()
128 {
129  mploop = NULL;
130  onEvent = nullptr;
131  onReply = nullptr;
132  int ret = sd_event_default(&mploop);
133  if(ret < 0)
134  {
135  ELOG("Failed to create event loop");
136  goto END;
137  }
138  /* Initialize interface from websocket */
139 
140  minterface.on_hangup = _on_hangup_static;
141  minterface.on_call = _on_call_static; /* Is this necessary? */
142  minterface.on_event = _on_event_static;
143  muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
144  sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
145  if(sp_websock == NULL)
146  {
147  ELOG("Failed to create websocket connection");
148  goto END;
149  }
150 
151  /* creates the evsrc */
152  //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL);
153 
154  return 0;
155 END:
156  if(mploop)
157  {
158  sd_event_unref(mploop);
159  }
160  return -1;
161 }
162 
163 static void *event_loop_run(void *args)
164 {
165  struct sd_event* loop = (struct sd_event*)(args);
166  DLOG("start eventloop");
167  for(;;)
168  sd_event_run(loop, 30000000);
169 }
170 
183 {
184  if(mploop && sp_websock)
185  {
186  pthread_t thread_id;
187  int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
188  if(ret != 0)
189  {
190  ELOG("Cannot run eventloop due to error:%d", errno);
191  return -1;
192  }
193  else
194  return thread_id;
195  }
196  else
197  {
198  ELOG("Connecting is not established yet");
199  return -1;
200  }
201 }
202 
217 int LibSoundmanager::call(const string& verb, struct json_object* arg)
218 {
219  int ret;
220  if(!sp_websock)
221  {
222  return -1;
223  }
224  if (!has_verb(verb))
225  {
226  ELOG("verb doesn't exit");
227  return -1;
228  }
229  ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
230  if (ret < 0) {
231  ELOG("Failed to call verb:%s",verb.c_str());
232  }
233  return ret;
234 }
235 
236 int LibSoundmanager::call_sync(const string& verb, struct json_object* arg, struct json_object* ret)
237 {
238  /* I haven't implemented yet */
239 }
240 
255 int LibSoundmanager::subscribe(const string& event_name)
256 {
257  if(!sp_websock)
258  {
259  return -1;
260  }
261  struct json_object* j_obj = json_object_new_object();
262  json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
263 
264  int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
265  if (ret < 0) {
266  ELOG("Failed to call verb:%s",__FUNCTION__);
267  }
268  return ret;
269 }
270 
284 int LibSoundmanager::unsubscribe(const string& event_name)
285 {
286  if(!sp_websock)
287  {
288  return -1;
289  }
290  struct json_object* j_obj = json_object_new_object();
291  json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
292 
293  int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
294  if (ret < 0) {
295  ELOG("Failed to call verb:%s",__FUNCTION__);
296  }
297  return ret;
298 }
299 
300 am_Error_e LibSoundmanager::connect(const am_sourceID_t sourceID, const am_sinkID_t sinkID, am_mainConnectionID_t& mainConnectionID)
301 {
302  /*int ret;
303  char *key;
304  rc = asprintf(&key, "%d:%s/%s", ++num, api, "connect");
305  ret = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
306  if(ret < 0)
307  {
308  fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
309 
310  }*/
311  /* open the json scripts */
312  // get mainconnedction ID */
313  //mainConnectionID = xx;
314  return E_OK;
315 }
316 
317 am_Error_e LibSoundmanager::disconnect(const am_mainConnectionID_t mainConnectionID)
318 {
319  return E_OK;
320 }
321 
322 /*const struct afb_wsj1* LibSoundmanager::get_websocket_handler()
323 {
324  if(sp_websock)
325  {
326  return sp_websock;
327  }
328  return nullptr;
329 }
330 
331 const struct sd_event* LibSoundmanager::get_sd_event()
332 {
333  if(mploop)
334  {
335  return mploop;
336  }
337  return nullptr;
338 }*/
339 
340 /************* Callback Function *************/
341 
342 void LibSoundmanager::on_hangup(void *closure, struct afb_wsj1 *wsj)
343 {
344  DLOG("%s called", __FUNCTION__);
345 }
346 
347 void LibSoundmanager::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
348 {
349 }
350 
351 /*
352 * event is like "soundmanager/newMainConnection"
353 * msg is like {"event":"soundmanager\/newMainConnection","data":{"mainConnectionID":3,"sourceID":101,"sinkID":100,"delay":0,"connectionState":4},"jtype":"afb-event"})}
354 * ^key^ ^^^^^^^^^^^^ value ^^^^^^^^^^^^
355 * so you can get
356  event name : struct json_object obj = json_object_object_get(msg,"event")
357 */
358 void LibSoundmanager::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
359 {
360  cout << "ON-EVENT:" << event << "(" << afb_wsj1_msg_object_s(msg) << ")" << endl;
361  if(onEvent != nullptr)
362  {
363  const string ev(event);
364  struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
365  onEvent(ev, ev_contents);
366  }
367 }
368 
369 void LibSoundmanager::on_reply(void *closure, struct afb_wsj1_msg *msg)
370 {
371  cout << "ON-REPLY:" << "(" << afb_wsj1_msg_object_s(msg) << ")" << endl;
372  if(onReply != nullptr)
373  {
374  struct json_object* reply = afb_wsj1_msg_object_j(msg);
375  onReply(reply);
376  }
377 }
378 
379 /* Internal Function in libsoundmanager */
380 
381 static void _ELOG(const char* func, const int line, const char* log, ...)
382 {
383  char *message;
384  va_list args;
385  va_start(args, log);
386  if (log == NULL || vasprintf(&message, log, args) < 0)
387  message = NULL;
388  cout << "[ERROR]" << func << "(" << line << "):" << message << endl;
389  va_end(args);
390  free(message);
391 }
392 
393 static void _DLOG(const char* func, const int line, const char* log, ...)
394 {
395  char *message;
396  va_list args;
397  va_start(args, log);
398  if (log == NULL || vasprintf(&message, log, args) < 0)
399  message = NULL;
400  cout << "[DEBUG]" << func << "(" << line << "):" << message << endl;
401  va_end(args);
402  free(message);
403 }
404 
405 static bool has_verb(const string& verb)
406 {
407  DLOG("verb is %s", verb.c_str());
408  if(find(api_list.begin(), api_list.end(), verb) != api_list.end())
409  return true;
410  else
411  return false;
412 }
const std::vector< std::string > api_list
- +Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdarg.h>
18 #include <sys/socket.h>
19 #include <iostream>
20 #include <algorithm>
21 #include <thread>
22 #include <errno.h>
23 #include <libsoundmanager.hpp>
24 
25 #define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
26 #ifdef DEBUGMODE
27  #define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
28 #else
29  #define DLOG(args,...)
30 #endif
31 static void _DLOG(const char* func, const int line, const char* log, ...);
32 static void _ELOG(const char* func, const int line, const char* log, ...);
33 
34 using namespace std;
35 
36 static bool has_verb(const std::string& verb);
37 static const char API[] = "soundmanager";
38 static int eventIndent(const string& event);
39 
40 static const std::vector<std::string> api_list{
41  std::string("connect"),
42  std::string("disconnect"),
43  std::string("setVolume"),
44  std::string("volumeStep"),
45  std::string("setSinkMuteState"),
46  std::string("getListMainConnections"),
47  std::string("ackConnect"),
48  std::string("ackDisconnect"),
49  std::string("ackSetSourceState"),
50  std::string("registerSource"),
51  std::string("deregisterSource"),
52  std::string("subscribe"),
53  std::string("unsubscribe")
54 };
55 
56 static const std::vector<std::string> event_list{
57  std::string("asyncSetSourceState"),
58  std::string("newMainConnection"),
59  std::string("volumeChanged"),
60  std::string("removedMainConnection"),
61  std::string("sinkMuteStateChanged"),
62  std::string("mainConnectionStateChanged"),
63  std::string("setRoutingReady"),
64  std::string("setRoutingRundown"),
65  std::string("asyncConnect")
66 };
67 
68 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
69 {
70  static_cast<LibSoundmanager*>(closure)->on_hangup(NULL,wsj);
71 }
72 
73 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
74 {
75  /* LibSoundmanager is not called from other process */
76 }
77 
78 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
79 {
80  static_cast<LibSoundmanager*>(closure)->on_event(NULL,event,msg);
81 }
82 
83 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
84 {
85  static_cast<LibSoundmanager*>(closure)->on_reply(NULL,msg);
86 }
87 
88 static void *event_loop_run(void *args)
89 {
90  struct sd_event* loop = (struct sd_event*)(args);
91  DLOG("start eventloop");
92  for(;;)
93  sd_event_run(loop, 30000000);
94 }
95 
97 {
98 }
99 
101 {
102  if(mploop)
103  {
104  sd_event_unref(mploop);
105  }
106  if(sp_websock != NULL)
107  {
108  afb_wsj1_unref(sp_websock);
109  }
110 }
111 
112 
126 int LibSoundmanager::init(int port, const string& token)
127 {
128  int ret;
129  if(port > 0 && token.size() > 0)
130  {
131  mport = port;
132  mtoken = token;
133  }
134  else
135  {
136  ELOG("port and token should be > 0, Initial port and token uses.");
137  return -1;
138  }
139 
140  ret = initialize_websocket();
141  if(ret != 0 )
142  {
143  ELOG("Failed to initialize websocket");
144  return -1;
145  }
146  ret = init_event();
147  if(ret != 0 )
148  {
149  ELOG("Failed to initialize websocket");
150  return -1;
151  }
152  if(ret == -1){
153  ELOG("Failed to create thread");
154  return -1;
155  }
156  return 0;
157 }
158 
159 int LibSoundmanager::initialize_websocket()
160 {
161  mploop = NULL;
162  onEvent = nullptr;
163  onReply = nullptr;
164  int ret = sd_event_default(&mploop);
165  if(ret < 0)
166  {
167  ELOG("Failed to create event loop");
168  goto END;
169  }
170  /* Initialize interface from websocket */
171  {
172  minterface.on_hangup = _on_hangup_static;
173  minterface.on_call = _on_call_static;
174  minterface.on_event = _on_event_static;
175  string muri = "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken;
176  sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
177  }
178  if(sp_websock == NULL)
179  {
180  ELOG("Failed to create websocket connection");
181  goto END;
182  }
183 
184  return 0;
185 END:
186  if(mploop)
187  {
188  sd_event_unref(mploop);
189  }
190  return -1;
191 }
192 
193 int LibSoundmanager::init_event(){
194  /* subscribe most important event for sound right */
195  return subscribe(string("asyncSetSourceState"));
196 }
197 
213  void (*event_cb)(const string& event, struct json_object* event_contents),
214  void (*reply_cb)(struct json_object* reply_contents),
215  void (*hangup_cb)(void))
216 {
217  onEvent = event_cb;
218  onReply = reply_cb;
219  onHangup = hangup_cb;
220 }
221 
237  void (*reply_cb)(struct json_object* reply_contents),
238  void (*hangup_cb)(void))
239 {
240  onReply = reply_cb;
241  onHangup = hangup_cb;
242 }
243 
259 int LibSoundmanager::registerSource(const string& sourceName)
260 {
261  if(!sp_websock)
262  {
263  return -1;
264  }
265  struct json_object* j_obj = json_object_new_object();
266  struct json_object* jsn = json_object_new_string(sourceName.c_str());
267  json_object_object_add(j_obj, "appname", jsn);
268  return this->call(__FUNCTION__, j_obj);
269 }
270 
287 int LibSoundmanager::connect(int sourceID, int sinkID)
288 {
289  if(!sp_websock)
290  {
291  return -1;
292  }
293  struct json_object* j_obj = json_object_new_object();
294  struct json_object* jsource = json_object_new_int(sourceID);
295  struct json_object* jsink = json_object_new_int(sinkID);
296  json_object_object_add(j_obj, "sourceID", jsource);
297  json_object_object_add(j_obj, "sinkID", jsink);
298  return this->call(__FUNCTION__, j_obj);
299 }
300 
319 int LibSoundmanager::connect(int sourceID, const string& sinkName)
320 {
321  if(!sp_websock)
322  {
323  return -1;
324  }
325  struct json_object* j_obj = json_object_new_object();
326  struct json_object* jsource = json_object_new_int(sourceID);
327  //struct json_object* jsink = json_object_new_int(1);
328  struct json_object* jsink = json_object_new_string(sinkName.c_str());
329  json_object_object_add(j_obj, "sourceID", jsource);
330  json_object_object_add(j_obj, "sinkID", jsink);
331  return this->call(__FUNCTION__, j_obj);
332 }
333 
347 int LibSoundmanager::disconnect(int connectionID)
348 {
349  if(!sp_websock)
350  {
351  return -1;
352  }
353  struct json_object* j_obj = json_object_new_object();
354  struct json_object* jconnection = json_object_new_int(connectionID);
355  json_object_object_add(j_obj, "mainConnectionID", jconnection);
356  return this->call(__FUNCTION__, j_obj);
357 }
358 
374 int LibSoundmanager::ackSetSourceState(int handle, int error)
375 {
376  if(!sp_websock)
377  {
378  return -1;
379  }
380  struct json_object* j_obj = json_object_new_object();
381  struct json_object* jhandle = json_object_new_int(handle);
382  struct json_object* jerrno = json_object_new_int(error);
383  json_object_object_add(j_obj, "handle", jhandle);
384  json_object_object_add(j_obj, "error", jerrno);
385  return this->call(__FUNCTION__, j_obj);
386 }
387 
402 int LibSoundmanager::call(const string& verb, struct json_object* arg)
403 {
404  int ret;
405  if(!sp_websock)
406  {
407  return -1;
408  }
409  if (!has_verb(verb))
410  {
411  ELOG("verb doesn't exit");
412  return -1;
413  }
414  ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
415  if (ret < 0) {
416  ELOG("Failed to call verb:%s",verb.c_str());
417  }
418  return ret;
419 }
420 
436 int LibSoundmanager::call(const char* verb, struct json_object* arg)
437 {
438  int ret;
439  if(!sp_websock)
440  {
441  return -1;
442  }
443  if (!has_verb(string(verb)))
444  {
445  ELOG("verb doesn't exit");
446  return -1;
447  }
448  ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
449  if (ret < 0) {
450  ELOG("Failed to call verb:%s",verb);
451  }
452  return ret;
453 }
454 
469 int LibSoundmanager::subscribe(const string& event_name)
470 {
471  if(!sp_websock)
472  {
473  return -1;
474  }
475  struct json_object* j_obj = json_object_new_object();
476  json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
477 
478  int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
479  if (ret < 0) {
480  ELOG("Failed to call verb:%s",__FUNCTION__);
481  }
482  return ret;
483 }
484 
498 int LibSoundmanager::unsubscribe(const string& event_name)
499 {
500  if(!sp_websock)
501  {
502  return -1;
503  }
504  struct json_object* j_obj = json_object_new_object();
505  json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
506 
507  int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
508  if (ret < 0) {
509  ELOG("Failed to call verb:%s",__FUNCTION__);
510  }
511  return ret;
512 }
513 
529 {
530  if (et > 1 && et < NumItems) {
531  this->handlers[et] = std::move(f);
532  }
533 }
534 
535 
536 /************* Callback Function *************/
537 
538 void LibSoundmanager::on_hangup(void *closure, struct afb_wsj1 *wsj)
539 {
540  DLOG("%s called", __FUNCTION__);
541  if(onHangup != nullptr)
542  {
543  onHangup();
544  }
545 }
546 
547 void LibSoundmanager::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
548 {
549 }
550 
551 /*
552 * event is like "soundmanager/newMainConnection"
553 * msg is like {"event":"soundmanager\/newMainConnection","data":{"mainConnectionID":3,"sourceID":101,"sinkID":100,"delay":0,"connectionState":4},"jtype":"afb-event"})}
554 * ^key^ ^^^^^^^^^^^^ value ^^^^^^^^^^^^
555 * so you can get
556  event name : struct json_object obj = json_object_object_get(msg,"event")
557 */
558 void LibSoundmanager::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
559 {
560  /* check event is for us */
561  string ev = string(event);
562  if (ev.find(API) == string::npos) {
563  /* It's not us */
564  return;
565  }
566  struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
567  if(onEvent != nullptr)
568  {
569  onEvent(ev, ev_contents);
570  }
571  else{}
572 
573  dispatch_event(ev, ev_contents);
574 
575  json_object_put(ev_contents);
576 }
577 
578 void LibSoundmanager::on_reply(void *closure, struct afb_wsj1_msg *msg)
579 {
580  struct json_object* reply = afb_wsj1_msg_object_j(msg);
581  /*struct json_object *json_data = json_object_object_get(reply, "response");
582  struct json_object *jverb = json_object_object_get(json_data, "verb");
583  const char* cverb = json_object_get_string(jverb);
584  DLOG("cverb is %s",cverb);
585  string verb = string(cverb);
586  DLOG("verb is %s",verb.c_str());
587 
588  if(verb == "registerSource"){
589  struct json_object *jsourceID = json_object_object_get(json_data, "sourceID");
590  int sourceID = json_object_get_int(jsourceID);
591  msourceIDs.push_back(sourceID);
592  DLOG("my sourceID is created: %d", sourceID);
593  }*/
594  if(onReply != nullptr)
595  {
596  onReply(reply);
597  }
598  json_object_put(reply);
599 }
600 
601 int LibSoundmanager::dispatch_event(const string &event , json_object* event_contents){
602  //dipatch event
603  EventType_SM x;
604 
605  if(event.find(event_list[0].c_str())){
606  x = Event_AsyncSetSourceState;
607  }
608  else{
609  return -1;
610  }
611  auto i = this->handlers.find(x);
612  if(i != handlers.end()){
613  i->second(event_contents);
614  return 0;
615  }
616  return -1;
617 }
618 
619 /* Internal Function in libsoundmanager */
620 
621 static void _ELOG(const char* func, const int line, const char* log, ...)
622 {
623  char *message;
624  va_list args;
625  va_start(args, log);
626  if (log == NULL || vasprintf(&message, log, args) < 0)
627  message = NULL;
628  cout << "[ERROR: soundmanager]" << func << "(" << line << "):" << message << endl;
629  va_end(args);
630  free(message);
631 }
632 
633 static void _DLOG(const char* func, const int line, const char* log, ...)
634 {
635  char *message;
636  va_list args;
637  va_start(args, log);
638  if (log == NULL || vasprintf(&message, log, args) < 0)
639  message = NULL;
640  cout << "[DEBUG: soundmanager]" << func << "(" << line << "):" << message << endl;
641  va_end(args);
642  free(message);
643 }
644 
645 static bool has_verb(const string& verb)
646 {
647  if(find(api_list.begin(), api_list.end(), verb) != api_list.end())
648  return true;
649  else
650  return false;
651 }
+
void register_callback(void(*event_cb)(const std::string &event, struct json_object *event_contents), void(*reply_cb)(struct json_object *reply_contents), void(*hangup_cb)(void)=nullptr)
+
void set_event_handler(enum EventType_SM et, handler_fun f)
+ -
int call_sync(const std::string &verb, struct json_object *arg, struct json_object *ret)
-
int unsubscribe(const std::string &event_name)
-
int call(const std::string &verb, struct json_object *arg)
-
void register_callback(void(*event_cb)(const std::string &event, struct json_object *event_contents), void(*reply_cb)(struct json_object *reply_contents))
+
int init(int port, const std::string &token)
+
int unsubscribe(const std::string &event_name)
+
int disconnect(int connectionID)
+
int call(const std::string &verb, struct json_object *arg)
+
int connect(int sourceID, int sinkID)
+
std::function< void(struct json_object *)> handler_fun
- - -
void on_hangup(void *closure, struct afb_wsj1 *wsj)
-
int subscribe(const std::string &event_name)
-
void on_reply(void *closure, struct afb_wsj1_msg *msg)
- -
void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
-
void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
+ + +
void on_hangup(void *closure, struct afb_wsj1 *wsj)
+
int subscribe(const std::string &event_name)
+
int ackSetSourceState(int handle, int error)
+
void on_reply(void *closure, struct afb_wsj1_msg *msg)
+ +
void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
+
void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
+
int registerSource(const std::string &sourceName)
#define ELOG(args,...)
-
#define DLOG(args,...)
+
#define DLOG(args,...)