Add sound manager initial source code
[staging/soundmanager.git] / include / libsoundmanager / libsoundmanager.hpp
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 <audiomanagertypes.h>
20 #include <vector>
21 #include <map>
22 #include <string>
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
32 using namespace am;
33
34 const std::vector<std::string> api_list{
35         std::string("connect"),
36     std::string("disconnect"),
37         std::string("setVolume"),
38         std::string("volumeStep"),
39         std::string("setSinkMuteState"),
40         std::string("getListMainConnections"),
41     std::string("ackConnect"),
42         std::string("ackDisconnect"),
43     std::string("ackSetSourceState"),    
44         std::string("registerSource"),
45         std::string("registerDomain"),
46         std::string("deregisterSource"),
47     std::string("subscribe"),
48     std::string("unsubscribe")
49 };
50
51 const std::vector<std::string> event_list{
52     std::string("newMainConnection"),
53     std::string("volumeChanged"),
54         std::string("removedMainConnection"),
55         std::string("sinkMuteStateChanged"),
56         std::string("mainConnectionStateChanged"),
57         std::string("setRoutingReady"),
58         std::string("setRoutingRundown"),
59         std::string("asyncConnect"),
60         std::string("asyncSetSourceState")
61 };
62
63 class LibSoundmanager
64 {
65 private:
66     LibSoundmanager() = delete;
67 public:
68     LibSoundmanager(const int port, const std::string& token);
69     //LibSoundmanager(const std::string& uri);  
70     ~LibSoundmanager();
71     
72     /* Method */
73     void register_callback(
74         void (*event_cb)(const std::string& event, struct json_object* event_contents), 
75         void (*reply_cb)(struct json_object* reply_contents),
76         void (*hangup_cb)(void) = nullptr);
77     int run_eventloop();
78     int call(const std::string& verb, struct json_object* arg);
79     int call(const char* verb, struct json_object* arg);
80     int subscribe(const std::string& event_name);
81     int unsubscribe(const std::string& event_name);
82     /*const struct afb_wsj1* get_websocket_handler();
83     const struct sd_event* get_sd_event();*/
84
85 private:
86     int initialize_websocket();
87     void (*onEvent)(const std::string& event, struct json_object* event_contents);
88     void (*onReply)(struct json_object* reply);
89     void (*onHangup)(void);
90
91     struct afb_wsj1* sp_websock;
92     struct afb_wsj1_itf minterface;
93     sd_event* mploop;
94     std::string muri;
95     int mport = 12345;
96     std::string mtoken = "123456";
97
98     am_Error_e connect(const am_sourceID_t sourceID, const am_sinkID_t sinkID, am::am_mainConnectionID_t& mainConnectionID);
99     am_Error_e disconnect(const am_mainConnectionID_t mainConnectionID);
100
101 public:
102     /* Don't use/ Internal only */
103     void on_hangup(void *closure, struct afb_wsj1 *wsj);
104     void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
105     void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
106     void on_reply(void *closure, struct afb_wsj1_msg *msg);
107 };
108 #endif /* LIBSOUNDMANAGER_H */