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 "libsmwrapper.h"
20 static int create_json_object(const QJsonObject& obj, struct json_object* jobj);
21 static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj);
22 static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj);
24 LibSMWrapper::LibSMWrapper(QObject *parent) :
27 /* This is not enabled */
28 //libsm = new LibSoundmanager();
31 LibSMWrapper::LibSMWrapper(const int port, const QString& token, QObject *parent) :
34 libsm = new LibSoundmanager(port, token.toStdString());
37 LibSMWrapper::~LibSMWrapper()
42 int LibSMWrapper::call(const QString &verb, const QString &arg)
44 // translate QJsonObject to struct json_object
45 struct json_object* jobj = json_object_new_object();
46 QJsonDocument jsonDoc = QJsonDocument::fromJson(arg.toUtf8());
47 const QJsonObject jsonObj = jsonDoc.object();
48 int ret = create_json_object(jsonObj, jobj);
53 return libsm->call(verb.toStdString().c_str(), jobj);
56 static int create_json_object(const QJsonObject& obj, struct json_object* jobj)
59 for(auto itr = obj.begin(); itr != obj.end();++itr)
61 string key = itr.key().toStdString();
62 //const char* key = itr.key().toStdString().c_str(); // Do not code like this. string is removed if size is over 16!!
64 bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj);
66 /*This is not implemented*/
67 qDebug("JsonArray can't parse for now");
70 // For now, array may not be inputted for soundmanager
71 // But use case absolutely exists
72 /*QJsonArray qarray = itr.value().toArray();
73 ret = put_array_to_jobj(key, qarray, jobj);*/
78 qDebug("Json parse error occured");
84 static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj)
87 return false; // Array can't input
90 string value = val.toString().toStdString();
91 json_object_object_add(jobj, key, json_object_new_string(value.c_str()));
94 const int value = val.toInt();
95 json_object_object_add(jobj, key, json_object_new_int(value));
100 static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj)
103 /* struct json_object* jarray = json_object_new_array();
106 for(auto jitr = qarray.begin(); jitr != qarray.end(); ++jitr){
107 struct json_object* tmp = json_object_new_object();
108 ret = put_val_to_jobj(key,jitr,tmp);
111 put_array_to_jobj(key,jitr,tmp);
113 json_object_array_add(jarray, tmp);
115 json_object_object_add(jobj, key, jarray);
119 void LibSMWrapper::wrapper_registerCallback(
120 void (*event_func)(const string& event, struct json_object* event_contents),
121 void (*reply_func)(struct json_object* reply_contents))
123 libsm->register_callback(event_func, reply_func);
126 void LibSMWrapper::subscribe(const QString event_name)
128 std::string str = event_name.toStdString();
129 libsm->subscribe(str);
132 void LibSMWrapper::unsubscribe(const QString event_name)
134 std::string str = event_name.toStdString();
135 libsm->unsubscribe(str);
138 void LibSMWrapper::run_eventloop()
140 libsm->run_eventloop();
143 void LibSMWrapper::print(const QString &str)
145 qDebug("%s is called", str.toStdString().c_str());
148 void LibSMWrapper::emit_event(const QString &event, const QJsonObject &msg)
150 qDebug("emit smEvent signal @%s", __FUNCTION__);
151 emit smEvent(event, msg);
153 void LibSMWrapper::emit_reply(const QJsonObject &msg)
155 qDebug("emit smReply signal @%s", __FUNCTION__);