diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/qlibsoundmanager.cpp | 158 | ||||
-rw-r--r-- | src/qlibsoundmanager.h | 56 | ||||
-rw-r--r-- | src/src.pro | 41 |
3 files changed, 255 insertions, 0 deletions
diff --git a/src/qlibsoundmanager.cpp b/src/qlibsoundmanager.cpp new file mode 100644 index 0000000..13e0a3f --- /dev/null +++ b/src/qlibsoundmanager.cpp @@ -0,0 +1,158 @@ +/* + * 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 "qlibsoundmanager.h" +#include <QJsonDocument> +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); +QLibSoundmanager* me; + +static void cbEvent_static(const std::string& event, struct json_object* event_contents) +{ + const QString event_name = QString(event.c_str()); + QString str = QString(json_object_get_string(event_contents)); + QJsonParseError error; + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error); + const QJsonObject jobj = jdoc.object(); + emit me->event(event_name, jobj); +} + +static void cbReply_static(struct json_object* replyContents) +{ + if(me == nullptr){ + return; + } + QString str = QString(json_object_get_string(replyContents)); + QJsonParseError error; + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error); + QJsonObject jobj = jdoc.object(); + emit me->reply(jobj); +} + +QLibSoundmanager::QLibSoundmanager(QObject *parent) : + QObject(parent) +{ + libsm = new LibSoundmanager(); +} + +QLibSoundmanager::~QLibSoundmanager() +{ + delete libsm; +} + +int QLibSoundmanager::init(int port, const QString& token) +{ + if(libsm == nullptr){ + return -1; + } + string ctoken = token.toStdString(); + int rc = libsm->init(port, ctoken); + if(rc != 0){ + return rc; + } + me = this; + + libsm->register_callback( + cbEvent_static, + cbReply_static); + return rc; +} + +int QLibSoundmanager::call(const QString &verb, const QJsonObject &arg) +{ + // translate QJsonObject to struct json_object + struct json_object* jobj = json_object_new_object(); + int ret = create_json_object(arg, jobj); + if(ret < 0) + { + return -1; + } + return libsm->call(verb.toStdString().c_str(), jobj); +} + +int QLibSoundmanager::connect(int sourceID, const QString& sinkName){ + string str = sinkName.toStdString(); + return libsm->connect(sourceID, str); +} + +int QLibSoundmanager::connect(int sourceID, int sinkID){ + return libsm->connect(sourceID, sinkID); +} + +int QLibSoundmanager::disconnect(int connectionID){ + return libsm->disconnect(connectionID); +} +int QLibSoundmanager::ackSetSourceState(int handle, int errorcode){ + return libsm->ackSetSourceState(handle, errorcode); +} +int QLibSoundmanager::registerSource(const QString& name){ + string str = name.toStdString(); + return libsm->registerSource(str); +} + +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; + } + } + } + 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; // FIXME: 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; +} + + +void QLibSoundmanager::subscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->subscribe(str); +} + +void QLibSoundmanager::unsubscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->unsubscribe(str); +} diff --git a/src/qlibsoundmanager.h b/src/qlibsoundmanager.h new file mode 100644 index 0000000..523173c --- /dev/null +++ b/src/qlibsoundmanager.h @@ -0,0 +1,56 @@ +/* + * 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. + */ +#ifndef QLIBSOUNDMANAGER_H +#define QLIBSOUNDMANAGER_H + + #include <QObject> + #include <QVariant> + #include <QtCore/QJsonObject> + #include <libsoundmanager.hpp> + #include <QString> + #include <string> + +// Note: This wrapper library will be integrated to libqtappfw + +class QLibSoundmanager : public QObject +{ + Q_OBJECT +public: // method + explicit QLibSoundmanager(QObject *parent = nullptr); + ~QLibSoundmanager(); + int init(int port, const QString& token); + void subscribe(const QString event_name); + void unsubscribe(const QString event_name); + +public: + + Q_INVOKABLE int call(const QString &verb, const QJsonObject &arg); + Q_INVOKABLE int connect(int sourceID, const QString& sinkName); + Q_INVOKABLE int connect(int sourceID, int sinkID); + Q_INVOKABLE int disconnect(int connectionID); + Q_INVOKABLE int ackSetSourceState(int handle, int errorcode); + Q_INVOKABLE int registerSource(const QString& name); + +signals: + void reply(const QVariant &msg); + void event(const QVariant &event, const QVariant &msg); + +private: + LibSoundmanager* libsm; +}; + + +#endif /*QLIBSOUNDMANAGER_H*/ diff --git a/src/src.pro b/src/src.pro new file mode 100644 index 0000000..b2726fe --- /dev/null +++ b/src/src.pro @@ -0,0 +1,41 @@ +# +# 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. + +TEMPLATE = lib +VERSION = 0.1.0 +TARGET = qtSoundmanagerWrapper + +HEADERS = qlibsoundmanager.h +SOURCES = qlibsoundmanager.cpp + +headers.path = /usr/include +headers.files = qlibsoundmanager.h + +target.path = /usr/lib + +CONFIG += link_pkgconfig create_pc create_prl no_install_prl + +PKGCONFIG += libsoundmanager + +QMAKE_PKGCONFIG_NAME = qlibsoundmanager +QMAKE_PKGCONFIG_FILE = $${QMAKE_PKGCONFIG_NAME} +QMAKE_PKGCONFIG_VERSION = $${VERSION} +QMAKE_PKGCONFIG_DESCRIPTION = A wrapper interface for libsoundmanager for Qt +QMAKE_PKGCONFIG_LIBDIR = ${prefix}/lib +QMAKE_PKGCONFIG_INCDIR = ${prefix}/include +QMAKE_PKGCONFIG_REQUIRES = libsoundmanager +QMAKE_PKGCONFIG_DESTDIR = pkgconfig + +INSTALLS += target headers |