diff options
author | zheng_wenlong <wenlong_zheng@nexty-ele.com> | 2017-10-23 14:38:56 +0900 |
---|---|---|
committer | zheng_wenlong <wenlong_zheng@nexty-ele.com> | 2017-10-30 11:30:27 +0900 |
commit | 46f7c260dd5e79116e7c6571cacbda1b89495c9e (patch) | |
tree | 3b95e99529d3c32dc4d6f95324ac860fbc11e523 /sample/template/smhandler.cpp | |
parent | 572023336d9573d3d5636d7e61a8dd3ed8878b0d (diff) |
Move sample from agl-service-homescreen-2017
This sample is using libhomescren with homescreen sevice,
So better move inside of libhomescreen.
Related Commit:
https://gerrit.automotivelinux.org/gerrit/#/c/11429/
[Patch Sets 2]
Rebased to c0e6b4d9496eacabeffc4115fad7c1a288de60c8
[Patch Sets 3]
Delete ivi-application-client-protocol.h and some trailing whitespace.
[Patch Sets 4]
Change temlate/README to README.md
[Patch Sets 5]
Rebased to 572023336d9573d3d5636d7e61a8dd3ed8878b0d
[Patch Sets 6]
Add LICENSE.APL-2.0 and LICENSE.MIT to sample.
Modify config.xml license to APL 2.0 + MIT.
BUG-AGL: SPEC-988
Change-Id: Id9a1a2401a9a0a42442ce84025213036a4fccd78
Signed-off-by: zheng_wenlong <wenlong_zheng@nexty-ele.com>
Diffstat (limited to 'sample/template/smhandler.cpp')
-rw-r--r-- | sample/template/smhandler.cpp | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/sample/template/smhandler.cpp b/sample/template/smhandler.cpp new file mode 100644 index 0000000..54b94d8 --- /dev/null +++ b/sample/template/smhandler.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libsmwrapper.h" +using namespace std; + +static int create_json_object(const QJsonObject& obj, struct json_object* jobj); +static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj); +static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj); + +LibSMWrapper::LibSMWrapper(QObject *parent) : + QObject(parent) +{ + /* This is not enabled */ + //libsm = new LibSoundmanager(); +} + +LibSMWrapper::LibSMWrapper(const int port, const QString& token, QObject *parent) : + QObject(parent) +{ + libsm = new LibSoundmanager(port, token.toStdString()); +} + +LibSMWrapper::~LibSMWrapper() +{ + delete libsm; +} + +int LibSMWrapper::call(const QString &verb, const QString &arg) +{ + // translate QJsonObject to struct json_object + struct json_object* jobj = json_object_new_object(); + QJsonDocument jsonDoc = QJsonDocument::fromJson(arg.toUtf8()); + const QJsonObject jsonObj = jsonDoc.object(); + int ret = create_json_object(jsonObj, jobj); + if(ret < 0) + { + return -1; + } + return libsm->call(verb.toStdString().c_str(), jobj); +} + +static int create_json_object(const QJsonObject& obj, struct json_object* jobj) +{ + try{ + for(auto itr = obj.begin(); itr != obj.end();++itr) + { + string key = itr.key().toStdString(); + //const char* key = itr.key().toStdString().c_str(); // Do not code like this. string is removed if size is over 16!! + + bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj); + if(!ret){ + /*This is not implemented*/ + qDebug("JsonArray can't parse for now"); + return -1; + // ToDo + // For now, array may not be inputted for soundmanager + // But use case absolutely exists + /*QJsonArray qarray = itr.value().toArray(); + ret = put_array_to_jobj(key, qarray, jobj);*/ + } + } + } + catch(...){ + qDebug("Json parse error occured"); + return -1; + } + return 0; +} + +static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj) +{ + if(val.isArray()){ + return false; // Array can't input + } + if(val.isString()){ + string value = val.toString().toStdString(); + json_object_object_add(jobj, key, json_object_new_string(value.c_str())); + } + else{ + const int value = val.toInt(); + json_object_object_add(jobj, key, json_object_new_int(value)); + } + return true; +} + +static bool put_array_to_jobj(const char* key, const QJsonArray& qarray, struct json_object* jobj) +{ + // ToDo Fix this !! +/* struct json_object* jarray = json_object_new_array(); + + bool ret; + for(auto jitr = qarray.begin(); jitr != qarray.end(); ++jitr){ + struct json_object* tmp = json_object_new_object(); + ret = put_val_to_jobj(key,jitr,tmp); + if(!ret) + { + put_array_to_jobj(key,jitr,tmp); + } + json_object_array_add(jarray, tmp); + } + json_object_object_add(jobj, key, jarray); + return true;*/ +} + +void LibSMWrapper::wrapper_registerCallback( + void (*event_func)(const string& event, struct json_object* event_contents), + void (*reply_func)(struct json_object* reply_contents)) +{ + libsm->register_callback(event_func, reply_func); +} + +void LibSMWrapper::subscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->subscribe(str); +} + +void LibSMWrapper::unsubscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->unsubscribe(str); +} + +void LibSMWrapper::run_eventloop() +{ + libsm->run_eventloop(); +} + +void LibSMWrapper::print(const QString &str) +{ + qDebug("%s is called", str.toStdString().c_str()); +} + +void LibSMWrapper::emit_event(const QString &event, const QJsonObject &msg) +{ + qDebug("emit smEvent signal @%s", __FUNCTION__); + emit smEvent(event, msg); +} +void LibSMWrapper::emit_reply(const QJsonObject &msg) +{ + qDebug("emit smReply signal @%s", __FUNCTION__); + emit smReply(msg); +}
\ No newline at end of file |