Add sound manager initial source code
[staging/soundmanager.git] / sample / radio / app / libsmwrapper.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 "libsmwrapper.h"
18 using namespace std;
19
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);
23
24 LibSMWrapper::LibSMWrapper(QObject *parent) :
25     QObject(parent)
26 {
27     /* This is not enabled */
28     //libsm = new LibSoundmanager();
29 }
30
31 LibSMWrapper::LibSMWrapper(const int port, const QString& token, QObject *parent) :
32     QObject(parent)
33 {
34     libsm = new LibSoundmanager(port, token.toStdString());
35 }
36
37 LibSMWrapper::~LibSMWrapper()
38 {
39     delete libsm;
40 }
41
42 int LibSMWrapper::call(const QString &verb, const QString &arg)
43 {
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);
49     if(ret < 0)
50     {
51         return -1;
52     }
53     return libsm->call(verb.toStdString().c_str(), jobj);
54 }
55
56 static int create_json_object(const QJsonObject& obj, struct json_object* jobj)
57 {
58     try{
59         for(auto itr = obj.begin(); itr != obj.end();++itr)
60         {
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!!
63
64             bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj);
65             if(!ret){
66                 /*This is not implemented*/
67                 qDebug("JsonArray can't parse for now");
68                 return -1;
69                 // ToDo 
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);*/
74             }
75         }
76     }
77     catch(...){
78         qDebug("Json parse error occured");
79         return -1;
80     }
81     return 0;
82 }
83
84 static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj)
85 {
86     if(val.isArray()){
87         return false;  // Array can't input
88     }
89     if(val.isString()){
90         string value = val.toString().toStdString();
91         json_object_object_add(jobj, key, json_object_new_string(value.c_str()));
92     }
93     else{
94         const int value = val.toInt();     
95         json_object_object_add(jobj, key, json_object_new_int(value));   
96     }
97     return true;
98 }
99
100 static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj)
101 {
102     // ToDo Fix this !!
103 /*    struct json_object* jarray = json_object_new_array();
104     
105     bool ret;
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);
109         if(!ret)
110         {
111             put_array_to_jobj(key,jitr,tmp);
112         }
113         json_object_array_add(jarray, tmp);
114     }
115     json_object_object_add(jobj, key, jarray);
116     return true;*/
117 }
118
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))
122 {
123     libsm->register_callback(event_func, reply_func);
124 }
125
126 void LibSMWrapper::subscribe(const QString event_name)
127 {
128     std::string str = event_name.toStdString();
129     libsm->subscribe(str);
130 }
131
132 void LibSMWrapper::unsubscribe(const QString event_name)
133 {
134     std::string str = event_name.toStdString();
135     libsm->unsubscribe(str);
136 }
137
138 void LibSMWrapper::run_eventloop()
139 {
140     libsm->run_eventloop();
141 }
142
143 void LibSMWrapper::print(const QString &str)
144 {
145     qDebug("%s is called", str.toStdString().c_str());
146 }
147
148 void LibSMWrapper::emit_event(const QString &event, const QJsonObject &msg)
149 {
150     qDebug("emit smEvent signal @%s", __FUNCTION__);
151     emit smEvent(event, msg);
152 }
153 void LibSMWrapper::emit_reply(const QJsonObject &msg)
154 {
155     qDebug("emit smReply signal @%s", __FUNCTION__);    
156     emit smReply(msg);
157 }