summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt40
-rw-r--r--LICENSE54
-rw-r--r--libsoundmanager.pc.in11
-rw-r--r--src/CMakeLists.txt50
-rw-r--r--src/libsoundmanager.cpp640
-rw-r--r--src/libsoundmanager.hpp92
6 files changed, 887 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..8c9b2ce
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,40 @@
+#
+# 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.
+#
+
+project(libsoundmanager CXX)
+
+cmake_minimum_required(VERSION 2.8)
+
+set(PROJECT_NAME "Soundmanager client library")
+set(PROJECT_VERSION "1.0")
+set(PROJECT_PRETTY_NAME "libsm")
+set(PROJECT_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
+set(PROJECT_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
+
+# get pkgconfig
+INCLUDE(FindPkgConfig)
+link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined)
+
+set(CMAKE_BUILD_TYPE Debug)
+add_definitions(-DDEBUGMODE)
+
+add_subdirectory(src)
+
+#generate configure file
+configure_file(libsoundmanager.pc.in libsoundmanager.pc @ONLY)
+INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libsoundmanager.pc
+ DESTINATION
+ ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..31c692a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,54 @@
+Apache License
+
+Version 2.0, January 2004
+
+http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
+
+"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
+
+"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
+
+"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
+
+"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
+
+"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
+
+"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
+
+"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
+
+"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
+
+"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
+
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
diff --git a/libsoundmanager.pc.in b/libsoundmanager.pc.in
new file mode 100644
index 0000000..0ab1481
--- /dev/null
+++ b/libsoundmanager.pc.in
@@ -0,0 +1,11 @@
+includedir=@PROJECT_INCLUDEDIR@
+libdir=@PROJECT_LIBDIR@
+
+Name: @PROJECT_PRETTY_NAME@
+Description: @PROJECT_DESCRIPTION@
+Version: @PROJECT_VERSION@
+URL: @PROJECT_URL@
+
+Requires: json-c libafbwsc
+Cflags: -I${includedir}
+Libs: -L${libdir} -lsoundmanager
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..1a34347
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,50 @@
+#
+# 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.
+#
+
+
+cmake_minimum_required(VERSION 2.8)
+
+set(TARGET_LIBSM soundmanager)
+add_definitions("-std=c++11")
+
+pkg_check_modules(libsm_depends json-c libafbwsc libsystemd)
+set(libsm_sources libsoundmanager.cpp)
+
+link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined)
+
+add_library(${TARGET_LIBSM} SHARED ${libsm_sources})
+target_compile_options(${TARGET_LIBSM} PUBLIC ${libsm_depends_CFLAGS})
+
+if(DEFINED DEBUGMODE)
+ target_compile_options(${TARGET_LIBSM} PRIVATE -g -O0)
+else(DEFINED DEBUGMODE)
+ target_compile_options(${TARGET_LIBSM} PRIVATE -g -O2)
+endif(DEFINED DEBUGMODE)
+
+include_directories(${TARGET_LIBSM} ${libsm_depends_INCLUDE_DIRS})
+target_link_libraries(${TARGET_LIBSM} afbwsc ${link_libraries} ${libsm_depends_LIBRARIES})
+
+if(DEFINED CMAKE_INSTALL_LIBDIR)
+ INSTALL(TARGETS ${TARGET_LIBSM}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
+endif(DEFINED CMAKE_INSTALL_LIBDIR)
+if(DEFINED CMAKE_INSTALL_INCLUDEDIR)
+ INSTALL(FILES libsoundmanager.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+endif(DEFINED CMAKE_INSTALL_INCLUDEDIR)
+
+#####################
+#add_subdirectory(test)
+
diff --git a/src/libsoundmanager.cpp b/src/libsoundmanager.cpp
new file mode 100644
index 0000000..6ddd09b
--- /dev/null
+++ b/src/libsoundmanager.cpp
@@ -0,0 +1,640 @@
+/*
+ * 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 <stdarg.h>
+#include <sys/socket.h>
+#include <iostream>
+#include <algorithm>
+#include "libsoundmanager.hpp"
+
+#define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
+#ifdef DEBUGMODE
+ #define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
+#else
+ #define DLOG(args,...)
+#endif
+static void _DLOG(const char* func, const int line, const char* log, ...);
+static void _ELOG(const char* func, const int line, const char* log, ...);
+
+using namespace std;
+
+static bool has_verb(const std::string& verb);
+static const char API[] = "soundmanager";
+static int eventIndent(const string& event);
+
+static const std::vector<std::string> api_list{
+ std::string("connect"),
+ std::string("disconnect"),
+ std::string("setVolume"),
+ std::string("volumeStep"),
+ std::string("setSinkMuteState"),
+ std::string("getListMainConnections"),
+ std::string("ackConnect"),
+ std::string("ackDisconnect"),
+ std::string("ackSetSourceState"),
+ std::string("registerSource"),
+ std::string("deregisterSource"),
+ std::string("subscribe"),
+ std::string("unsubscribe")
+};
+
+static const std::vector<std::string> event_list{
+ std::string("asyncSetSourceState"),
+ std::string("newMainConnection"),
+ std::string("volumeChanged"),
+ std::string("removedMainConnection"),
+ std::string("sinkMuteStateChanged"),
+ std::string("mainConnectionStateChanged"),
+ std::string("setRoutingReady"),
+ std::string("setRoutingRundown"),
+ std::string("asyncConnect")
+};
+
+static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
+{
+ static_cast<LibSoundmanager*>(closure)->on_hangup(NULL,wsj);
+}
+
+static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
+{
+ /* LibSoundmanager is not called from other process */
+}
+
+static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
+{
+ static_cast<LibSoundmanager*>(closure)->on_event(NULL,event,msg);
+}
+
+static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
+{
+ static_cast<LibSoundmanager*>(closure)->on_reply(NULL,msg);
+}
+
+LibSoundmanager::LibSoundmanager()
+{
+}
+
+LibSoundmanager::~LibSoundmanager()
+{
+ if(mploop)
+ {
+ sd_event_unref(mploop);
+ }
+ if(sp_websock != NULL)
+ {
+ afb_wsj1_unref(sp_websock);
+ }
+}
+
+
+/**
+ * This function is initialization function
+ *
+ * #### Parameters
+ * - port [in] : This argument should be specified to the port number to be used for websocket
+ * - token [in] : This argument should be specified to the token to be used for websocket
+ *
+ * #### Return
+ * Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ *
+ */
+int LibSoundmanager::init(int port, const string& token)
+{
+ int ret;
+ if(port > 0 && token.size() > 0)
+ {
+ mport = port;
+ mtoken = token;
+ }
+ else
+ {
+ ELOG("port and token should be > 0, Initial port and token uses.");
+ return -1;
+ }
+
+ ret = initialize_websocket();
+ if(ret != 0 )
+ {
+ ELOG("Failed to initialize websocket");
+ return -1;
+ }
+ ret = init_event();
+ if(ret != 0 )
+ {
+ ELOG("Failed to initialize websocket");
+ return -1;
+ }
+ return 0;
+}
+
+int LibSoundmanager::initialize_websocket()
+{
+ mploop = NULL;
+ onEvent = nullptr;
+ onReply = nullptr;
+ int ret = sd_event_default(&mploop);
+ if(ret < 0)
+ {
+ ELOG("Failed to create event loop");
+ goto END;
+ }
+ /* Initialize interface from websocket */
+ {
+ minterface.on_hangup = _on_hangup_static;
+ minterface.on_call = _on_call_static;
+ minterface.on_event = _on_event_static;
+ string muri = "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken;
+ sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
+ }
+ if(sp_websock == NULL)
+ {
+ ELOG("Failed to create websocket connection");
+ goto END;
+ }
+
+ return 0;
+END:
+ if(mploop)
+ {
+ sd_event_unref(mploop);
+ }
+ return -1;
+}
+
+int LibSoundmanager::init_event(){
+ /* subscribe most important event for sound right */
+ return subscribe(string("asyncSetSourceState"));
+}
+
+/**
+ * This function registers callback function for reply/event message from sound manager
+ *
+ * #### Parameters
+ * - event_cb [in] : This argument should be specified to the callback for subscribed event
+
+
+
+ * - reply_cb [in] : This argument should be specified to the reply callback for call function
+ * - hangup_cb [in] : This argument should be specified to the hangup callback for call function. nullptr is defaulty set.
+ *
+ * #### Return
+ *
+ * #### Note
+ * Event callback is invoked by sound manager for event you subscribed.
+ * If you would like to get event, please call subscribe function before/after this function
+ */
+void LibSoundmanager::register_callback(
+ void (*event_cb)(const string& event, struct json_object* event_contents),
+ void (*reply_cb)(struct json_object* reply_contents),
+ void (*hangup_cb)(void))
+{
+ onEvent = event_cb;
+ onReply = reply_cb;
+ onHangup = hangup_cb;
+}
+
+/**
+ * This function is overload of register_callback. This registers callback function for reply/event message from sound manager
+ *
+ * #### Parameters
+ * - reply_cb [in] : This argument should be specified to the reply callback for call function
+ * - hangup_cb [in] : This argument should be specified to the hangup callback for call function. nullptr is defaulty set.
+ *
+ * #### Return
+ *
+ * #### Note
+ * Event callback is invoked by sound manager for event you subscribed.
+ * This function for convinience for user uses set_event_handler
+ * If you would like to get event, please call subscribe function before/after this function
+ */
+void LibSoundmanager::register_callback(
+ void (*reply_cb)(struct json_object* reply_contents),
+ void (*hangup_cb)(void))
+{
+ onReply = reply_cb;
+ onHangup = hangup_cb;
+}
+
+/**
+ * This function calls registerSource of Audio Manager via WebSocket
+ * registerSource is registration as source for policy management
+ *
+ * #### Parameters
+ * - sourceName [in] : This argument should be specified to the source name (e.g. "MediaPlayer")
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * This function must be called to get source ID
+ * mainConnectionID is returned by async reply function
+ *
+ */
+int LibSoundmanager::registerSource(const string& sourceName)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jsn = json_object_new_string(sourceName.c_str());
+ json_object_object_add(j_obj, "appname", jsn);
+ return this->call(__FUNCTION__, j_obj);
+}
+
+/**
+ * This function calls connect of Audio Manager via WebSocket
+ * connect is to get sound right
+ *
+ * #### Parameters
+ * - sourceID [in] : This argument should be specified to the sourceID as int. This parameter is returned value of registerSource
+ * - sinkID [in] : This argument should be specified to the sinkID as int. ID is specified by AudioManager
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * This function must be called to get source right
+ * connectionID is
+ *
+ */
+int LibSoundmanager::connect(int sourceID, int sinkID)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jsource = json_object_new_int(sourceID);
+ struct json_object* jsink = json_object_new_int(sinkID);
+ json_object_object_add(j_obj, "sourceID", jsource);
+ json_object_object_add(j_obj, "sinkID", jsink);
+ return this->call(__FUNCTION__, j_obj);
+}
+
+/**
+ * This function calls the connect of Audio Manager via WebSocket
+ * This function is overload of connect
+ * Instead of sinkID as the number, abstract sinkName will be supported.
+ * For now, "default" is only supported
+ *
+ * #### Parameters
+ * - sourceID [in] : This argument should be specified to the sourceID as int. This parameter is returned value of registerSource
+ * - sinkName [in] : This argument should be specified to the sinkID as int. ID is aliased by SoundManager (e.g: "default")
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * For now, aliase(hardware abstraction) like "DriverZone:Speaker" is under hard consideration
+ * Just "default" is usable.
+ *
+ */
+int LibSoundmanager::connect(int sourceID, const string& sinkName)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jsource = json_object_new_int(sourceID);
+ //struct json_object* jsink = json_object_new_int(1);
+ struct json_object* jsink = json_object_new_string(sinkName.c_str());
+ json_object_object_add(j_obj, "sourceID", jsource);
+ json_object_object_add(j_obj, "sinkID", jsink);
+ return this->call(__FUNCTION__, j_obj);
+}
+
+/**
+ * This function calls the disconnect of Audio Manager via WebSocket
+ *
+ * #### Parameters
+ * - connectionID [in] : This parameter is returned value of connect
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ *
+ *
+ */
+int LibSoundmanager::disconnect(int connectionID)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jconnection = json_object_new_int(connectionID);
+ json_object_object_add(j_obj, "mainConnectionID", jconnection);
+ return this->call(__FUNCTION__, j_obj);
+}
+
+/**
+ * This function calls the ackSetSourceState of Audio Manager via WebSocket
+ *
+ * #### Parameters
+ * - sourceID [in] : This parameter is returned value of ackSetSourceState
+ * - handle [in] : This parameter is returned value of ackSetSourceState
+ * - errno [in] : If you have some errors, input ohter than 0. 0 means acknowledge
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * This function must be called when application get asyncSetSourceState event
+ * Input handle number attached in asyncSetSourceState and error number(0 is acknowledge)
+ */
+int LibSoundmanager::ackSetSourceState(int handle, int error)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ struct json_object* jhandle = json_object_new_int(handle);
+ struct json_object* jerrno = json_object_new_int(error);
+ json_object_object_add(j_obj, "handle", jhandle);
+ json_object_object_add(j_obj, "error", jerrno);
+ return this->call(__FUNCTION__, j_obj);
+}
+
+/**
+ * This function calls the API of Audio Manager via WebSocket
+ *
+ * #### Parameters
+ * - verb [in] : This argument should be specified to the API name (e.g. "connect")
+ * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * To call Audio Manager's APIs, the application should set its function name, arguments to JSON format.
+ *
+ */
+int LibSoundmanager::call(const string& verb, struct json_object* arg)
+{
+ int ret;
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ if (!has_verb(verb))
+ {
+ ELOG("verb doesn't exit");
+ return -1;
+ }
+ ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",verb.c_str());
+ }
+ return ret;
+}
+
+/**
+ * This function calls the API of Audio Manager via WebSocket
+ * This function is overload function of "call"
+ *
+ * #### Parameters
+ * - verb [in] : This argument should be specified to the API name (e.g. "connect")
+ * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * To call Audio Manager's APIs, the application should set its function name, arguments to JSON format.
+ *
+ */
+int LibSoundmanager::call(const char* verb, struct json_object* arg)
+{
+ int ret;
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ if (!has_verb(string(verb)))
+ {
+ ELOG("verb doesn't exit");
+ return -1;
+ }
+ ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",verb);
+ }
+ return ret;
+}
+
+/**
+ * Register callback function for each event
+ *
+ * #### Parameters
+ * - event_name [in] : This argument should be specified to the event name
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * This function enables to get an event to your callback function.
+ * Regarding the list of event name, please refer to CommandSender API and RountingSender API.
+ *
+ */
+int LibSoundmanager::subscribe(const string& event_name)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
+
+ int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",__FUNCTION__);
+ }
+ return ret;
+}
+
+/**
+ * Unregister callback function for each event
+ *
+ * #### Parameters
+ * - event_name [in] : This argument should be specified to the event name
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * This function disables to get an event to your callback function.
+ *
+ */
+int LibSoundmanager::unsubscribe(const string& event_name)
+{
+ if(!sp_websock)
+ {
+ return -1;
+ }
+ struct json_object* j_obj = json_object_new_object();
+ json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str()));
+
+ int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
+ if (ret < 0) {
+ ELOG("Failed to call verb:%s",__FUNCTION__);
+ }
+ return ret;
+}
+
+/**
+ * This function calls the ackSetSourceState of Audio Manager via WebSocket
+ *
+ * #### Parameters
+ * - EventType_AsyncSetSourceState [in] : This parameter is EventType of soundmanager
+ * - handler_func [in] : This parameter is callback function
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of transmission error.
+ *
+ * #### Note
+ * This function must be called when application get asyncSetSourceState event
+ * Input handle number attached in asyncSetSourceState and error number(0 is acknowledge)
+ */
+void LibSoundmanager::set_event_handler(enum EventType_SM et, handler_fun f)
+{
+ if (et > 1 && et < NumItems) {
+ this->handlers[et] = std::move(f);
+ }
+}
+
+
+/************* Callback Function *************/
+
+void LibSoundmanager::on_hangup(void *closure, struct afb_wsj1 *wsj)
+{
+ DLOG("%s called", __FUNCTION__);
+ if(onHangup != nullptr)
+ {
+ onHangup();
+ }
+}
+
+void LibSoundmanager::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
+{
+}
+
+/*
+* event is like "soundmanager/newMainConnection"
+* msg is like {"event":"soundmanager\/newMainConnection","data":{"mainConnectionID":3,"sourceID":101,"sinkID":100,"delay":0,"connectionState":4},"jtype":"afb-event"})}
+* ^key^ ^^^^^^^^^^^^ value ^^^^^^^^^^^^
+* so you can get
+ event name : struct json_object obj = json_object_object_get(msg,"event")
+*/
+void LibSoundmanager::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
+{
+ /* check event is for us */
+ string ev = string(event);
+ if (ev.find(API) == string::npos) {
+ /* It's not us */
+ return;
+ }
+ struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
+ if(onEvent != nullptr)
+ {
+ onEvent(ev, ev_contents);
+ }
+ else{}
+
+ dispatch_event(ev, ev_contents);
+
+ json_object_put(ev_contents);
+}
+
+void LibSoundmanager::on_reply(void *closure, struct afb_wsj1_msg *msg)
+{
+ struct json_object* reply = afb_wsj1_msg_object_j(msg);
+ /*struct json_object *json_data = json_object_object_get(reply, "response");
+ struct json_object *jverb = json_object_object_get(json_data, "verb");
+ const char* cverb = json_object_get_string(jverb);
+ DLOG("cverb is %s",cverb);
+ string verb = string(cverb);
+ DLOG("verb is %s",verb.c_str());
+
+ if(verb == "registerSource"){
+ struct json_object *jsourceID = json_object_object_get(json_data, "sourceID");
+ int sourceID = json_object_get_int(jsourceID);
+ msourceIDs.push_back(sourceID);
+ DLOG("my sourceID is created: %d", sourceID);
+ }*/
+ if(onReply != nullptr)
+ {
+ onReply(reply);
+ }
+ json_object_put(reply);
+}
+
+int LibSoundmanager::dispatch_event(const string &event , json_object* event_contents){
+ //dipatch event
+ EventType_SM x;
+
+ if(event.find(event_list[0].c_str())){
+ x = Event_AsyncSetSourceState;
+ }
+ else{
+ return -1;
+ }
+ auto i = this->handlers.find(x);
+ if(i != handlers.end()){
+ i->second(event_contents);
+ return 0;
+ }
+ return -1;
+}
+
+/* Internal Function in libsoundmanager */
+
+static void _ELOG(const char* func, const int line, const char* log, ...)
+{
+ char *message;
+ va_list args;
+ va_start(args, log);
+ if (log == NULL || vasprintf(&message, log, args) < 0)
+ message = NULL;
+ cout << "[ERROR: soundmanager]" << func << "(" << line << "):" << message << endl;
+ va_end(args);
+ free(message);
+}
+
+static void _DLOG(const char* func, const int line, const char* log, ...)
+{
+ char *message;
+ va_list args;
+ va_start(args, log);
+ if (log == NULL || vasprintf(&message, log, args) < 0)
+ message = NULL;
+ cout << "[DEBUG: soundmanager]" << func << "(" << line << "):" << message << endl;
+ va_end(args);
+ free(message);
+}
+
+static bool has_verb(const string& verb)
+{
+ if(find(api_list.begin(), api_list.end(), verb) != api_list.end())
+ return true;
+ else
+ return false;
+}
diff --git a/src/libsoundmanager.hpp b/src/libsoundmanager.hpp
new file mode 100644
index 0000000..8b81204
--- /dev/null
+++ b/src/libsoundmanager.hpp
@@ -0,0 +1,92 @@
+/*
+ * 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 LIBSOUNDMANAGER_H
+#define LIBSOUNDMANAGER_H
+#include <vector>
+#include <map>
+#include <string>
+#include <functional>
+#include <json-c/json.h>
+#include <systemd/sd-event.h>
+extern "C"
+{
+#include <afb/afb-wsj1.h>
+#include <afb/afb-ws-client.h>
+}
+
+class LibSoundmanager
+{
+public:
+ LibSoundmanager();
+ ~LibSoundmanager();
+ LibSoundmanager(const LibSoundmanager &) = delete;
+ LibSoundmanager &operator=(const LibSoundmanager &) = delete;
+ int init(int port, const std::string& token);
+
+ using handler_fun = std::function<void(struct json_object*)>;
+
+ enum EventType_SM {
+ Event_AsyncSetSourceState = 1 /*arg key: {sourceID, handle, sourceState}*/
+ };
+
+ /* Method */
+ int registerSource(const std::string& sourceName);
+ int connect(int sourceID, int sinkID);
+ int connect(int sourceID, const std::string& sinkName);
+ int disconnect(int connectionID);
+ int ackSetSourceState(int handle, int error);
+
+ int call(const std::string& verb, struct json_object* arg);
+ int call(const char* verb, struct json_object* arg);
+ int subscribe(const std::string& event_name);
+ int unsubscribe(const std::string& event_name);
+ void set_event_handler(enum EventType_SM et, handler_fun f);
+ void register_callback(
+ void (*event_cb)(const std::string& event, struct json_object* event_contents),
+ void (*reply_cb)(struct json_object* reply_contents),
+ void (*hangup_cb)(void) = nullptr);
+ void register_callback(
+ void (*reply_cb)(struct json_object* reply_contents),
+ void (*hangup_cb)(void) = nullptr);
+
+private:
+ int init_event();
+ int initialize_websocket();
+ int dispatch_event(const std::string& event, struct json_object* ev_contents);
+
+ void (*onEvent)(const std::string& event, struct json_object* event_contents);
+ void (*onReply)(struct json_object* reply);
+ void (*onHangup)(void);
+
+ struct afb_wsj1* sp_websock;
+ struct afb_wsj1_itf minterface;
+ sd_event* mploop;
+ int mport;
+ std::string mtoken;
+ std::vector<int> msourceIDs;
+ std::map<EventType_SM, handler_fun> handlers;
+ EventType_SM const NumItems = (EventType_SM)(Event_AsyncSetSourceState + 1);
+
+public:
+ /* Don't use/ Internal only */
+ void on_hangup(void *closure, struct afb_wsj1 *wsj);
+ void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
+ void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
+ void on_reply(void *closure, struct afb_wsj1_msg *msg);
+};
+
+#endif /* LIBSOUNDMANAGER_H */