Sound Manager
libsoundmanager.hpp
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 #ifndef LIBSOUNDMANAGER_H
18 #define LIBSOUNDMANAGER_H
19 #include <vector>
20 #include <map>
21 #include <string>
22 #include <functional>
23 #include <json-c/json.h>
24 #include <systemd/sd-event.h>
25 extern "C"
26 {
27 #include <afb/afb-binding.h>
28 #include <afb/afb-wsj1.h>
29 #include <afb/afb-ws-client.h>
30 }
31 
33 {
34 public:
37  LibSoundmanager(const LibSoundmanager &) = delete;
38  LibSoundmanager &operator=(const LibSoundmanager &) = delete;
39  int init(int port, const std::string& token);
40 
41  using handler_fun = std::function<void(struct json_object*)>;
42 
43  enum EventType_SM {
44  Event_AsyncSetSourceState = 1 /*arg key: {sourceID, handle, sourceState}*/
45  };
46 
47  /* Method */
48  int registerSource(const std::string& sourceName);
49  int connect(int sourceID, int sinkID);
50  int connect(int sourceID, const std::string& sinkName);
51  int disconnect(int connectionID);
52  int ackSetSourceState(int handle, int error);
53 
54  int call(const std::string& verb, struct json_object* arg);
55  int call(const char* verb, struct json_object* arg);
56  int subscribe(const std::string& event_name);
57  int unsubscribe(const std::string& event_name);
59  void register_callback(
60  void (*event_cb)(const std::string& event, struct json_object* event_contents),
61  void (*reply_cb)(struct json_object* reply_contents),
62  void (*hangup_cb)(void) = nullptr);
63  void register_callback(
64  void (*reply_cb)(struct json_object* reply_contents),
65  void (*hangup_cb)(void) = nullptr);
66 
67 private:
68  int init_event();
69  int initialize_websocket();
70  int dispatch_event(const std::string& event, struct json_object* ev_contents);
71 
72  void (*onEvent)(const std::string& event, struct json_object* event_contents);
73  void (*onReply)(struct json_object* reply);
74  void (*onHangup)(void);
75 
76  struct afb_wsj1* sp_websock;
77  struct afb_wsj1_itf minterface;
78  sd_event* mploop;
79  int mport;
80  std::string mtoken;
81  std::vector<int> msourceIDs;
82  std::map<EventType_SM, handler_fun> handlers;
83  EventType_SM const NumItems = (EventType_SM)(Event_AsyncSetSourceState + 1);
84 
85 public:
86  /* Don't use/ Internal only */
87  void on_hangup(void *closure, struct afb_wsj1 *wsj);
88  void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
89  void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
90  void on_reply(void *closure, struct afb_wsj1_msg *msg);
91 };
92 
93 #endif /* LIBSOUNDMANAGER_H */
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 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
LibSoundmanager & operator=(const LibSoundmanager &)=delete
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)