Test for Mediaplayer of HMI Framework at dab version
[apps/mediaplayer.git] / app / qlibsoundmanager.cpp
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 "qlibsoundmanager.h"
18 #include <QJsonDocument>
19 using namespace std;
20
21 static int create_json_object(const QJsonObject& obj, struct json_object* jobj);
22 static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj);
23 QLibSoundmanager* me;
24
25 static void cbEvent_static(const std::string& event, struct json_object* event_contents)
26 {
27     const QString event_name = QString(event.c_str());
28     QString str = QString(json_object_get_string(event_contents));
29     QJsonParseError error;
30     QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
31     const QJsonObject jobj = jdoc.object();
32     emit me->event(event_name, jobj);
33 }
34
35 static void cbReply_static(struct json_object* replyContents)
36 {
37     if(me == nullptr){
38         return;
39     }
40     QString str = QString(json_object_get_string(replyContents));
41     QJsonParseError error;
42     QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
43     QJsonObject jobj = jdoc.object();
44     emit me->reply(jobj);
45 }
46
47 QLibSoundmanager::QLibSoundmanager(QObject *parent) :
48     QObject(parent)
49 {
50     /* This is not enabled */
51     libsm = new LibSoundmanager();
52 }
53
54 QLibSoundmanager::~QLibSoundmanager()
55 {
56     delete libsm;
57 }
58
59 int QLibSoundmanager::init(int port, const QString& token)
60 {
61     if(libsm == nullptr){
62         return -1;
63     }
64     string ctoken = token.toStdString();
65     int rc = libsm->init(port, ctoken);
66     if(rc != 0){
67         return rc;
68     }
69     me = this;
70
71     libsm->register_callback(
72         cbEvent_static,
73         cbReply_static);
74     return rc;
75 }
76
77 int QLibSoundmanager::call(const QString &verb, const QJsonObject &arg)
78 {
79     // translate QJsonObject to struct json_object
80     struct json_object* jobj = json_object_new_object();
81     int ret = create_json_object(arg, jobj);
82     if(ret < 0)
83     {
84         return -1;
85     }
86     return libsm->call(verb.toStdString().c_str(), jobj);
87 }
88
89 int QLibSoundmanager::connect(int sourceID, const QString& sinkName){
90     string str = sinkName.toStdString();
91     return libsm->connect(sourceID, str);
92 }
93 int QLibSoundmanager::disconnect(int connectionID){
94     return libsm->disconnect(connectionID);
95 }
96 int QLibSoundmanager::ackSetSourceState(int handle, int errorcode){
97     return libsm->ackSetSourceState(handle, errorcode);
98 }
99 int QLibSoundmanager::registerSource(const QString& name){
100     string str = name.toStdString();
101     return libsm->registerSource(str);
102 }
103
104 static int create_json_object(const QJsonObject& obj, struct json_object* jobj)
105 {
106     try{
107         for(auto itr = obj.begin(); itr != obj.end();++itr)
108         {
109             string key = itr.key().toStdString();
110             //const char* key = itr.key().toStdString().c_str(); // Do not code like this. string is removed if size is over 16!!
111
112             bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj);
113             if(!ret){
114                 /*This is not implemented*/
115                 qDebug("JsonArray can't parse for now");
116                 return -1;
117             }
118         }
119     }
120     catch(...){
121         qDebug("Json parse error occured");
122         return -1;
123     }
124     return 0;
125 }
126
127 static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj)
128 {
129     if(val.isArray()){
130         return false;  // Array can't input
131     }
132     if(val.isString()){
133         string value = val.toString().toStdString();
134         json_object_object_add(jobj, key, json_object_new_string(value.c_str()));
135     }
136     else{
137         const int value = val.toInt();     
138         json_object_object_add(jobj, key, json_object_new_int(value));   
139     }
140     return true;
141 }
142
143
144 void QLibSoundmanager::subscribe(const QString &event_name)
145 {
146     std::string str = event_name.toStdString();
147     libsm->subscribe(str);
148 }
149
150 void QLibSoundmanager::unsubscribe(const QString &event_name)
151 {
152     std::string str = event_name.toStdString();
153     libsm->unsubscribe(str);
154 }