2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "qlibsoundmanager.h"
18 #include <QJsonDocument>
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);
25 static void cbEvent_static(const std::string& event, struct json_object* event_contents)
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);
35 static void cbReply_static(struct json_object* replyContents)
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();
47 QLibSoundmanager::QLibSoundmanager(QObject *parent) :
50 /* This is not enabled */
51 libsm = new LibSoundmanager();
54 QLibSoundmanager::~QLibSoundmanager()
59 int QLibSoundmanager::init(int port, const QString& token)
64 string ctoken = token.toStdString();
65 int rc = libsm->init(port, ctoken);
71 libsm->register_callback(
77 int QLibSoundmanager::call(const QString &verb, const QJsonObject &arg)
79 // translate QJsonObject to struct json_object
80 struct json_object* jobj = json_object_new_object();
81 int ret = create_json_object(arg, jobj);
86 return libsm->call(verb.toStdString().c_str(), jobj);
89 int QLibSoundmanager::connect(int sourceID, const QString& sinkName){
90 string str = sinkName.toStdString();
91 return libsm->connect(sourceID, str);
93 int QLibSoundmanager::disconnect(int connectionID){
94 return libsm->disconnect(connectionID);
96 int QLibSoundmanager::ackSetSourceState(int handle, int errorcode){
97 return libsm->ackSetSourceState(handle, errorcode);
99 int QLibSoundmanager::registerSource(const QString& name){
100 string str = name.toStdString();
101 return libsm->registerSource(str);
104 static int create_json_object(const QJsonObject& obj, struct json_object* jobj)
107 for(auto itr = obj.begin(); itr != obj.end();++itr)
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!!
112 bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj);
114 /*This is not implemented*/
115 qDebug("JsonArray can't parse for now");
121 qDebug("Json parse error occured");
127 static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj)
130 return false; // Array can't input
133 string value = val.toString().toStdString();
134 json_object_object_add(jobj, key, json_object_new_string(value.c_str()));
137 const int value = val.toInt();
138 json_object_object_add(jobj, key, json_object_new_int(value));
144 void QLibSoundmanager::subscribe(const QString &event_name)
146 std::string str = event_name.toStdString();
147 libsm->subscribe(str);
150 void QLibSoundmanager::unsubscribe(const QString &event_name)
152 std::string str = event_name.toStdString();
153 libsm->unsubscribe(str);