update on dbus
[profile/ivi/genivi/genivi-audio-manager.git] / PluginRoutingInterfaceDbus / AudiomanagerInterface.cpp
1 /**
2  *
3  * Copyright (C) 2011, BMW AG
4  *
5  * PluginDBus
6  *
7  * \file DBusInterface.cpp
8  *
9  * \date 20.05.2011
10  * \author Christian Müller (christian.ei.mueller@bmw.de)
11  *
12  * \section License
13  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
14  * Copyright (C) 2011, BMW AG – Christian Müller  Christian.ei.mueller@bmw.de
15  *
16  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
17  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
18  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
19  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
20  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
21  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
22  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
23  */
24
25 #include "headers.h"
26 #include <iostream>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <errno.h>
32
33 class AudioManagerInterface;
34
35
36 AudioManagerInterface* AudioManagerInterface::m_reference = NULL;
37
38 static DBUSMessageHandler* g_pDbusMessage;
39
40 static MethodTable manager_methods[] =
41 {
42         { "peekDomain",        "s",             "u",            &AudioManagerInterface::peekDomain },
43         { "registerSource",    "sss",           "u",            &AudioManagerInterface::registerSource },
44         { "registerSink",      "sss",           "u",            &AudioManagerInterface::registerSink },
45         { "registerDomain",    "sssb",          "u",            &AudioManagerInterface::registerDomain },
46         { "registerGateway",   "sssss",         "u",            &AudioManagerInterface::registerGateway },
47         { "",                  "",              "",     NULL }
48 };
49
50 static SignalTable manager_signals[] = {
51         { "signal_systemReady",         ""},
52         { "",                           ""}
53 };
54
55 static DBusObjectPathVTable vtable =
56 {
57         NULL,AudioManagerInterface::receive_callback,NULL, NULL, NULL, NULL
58 };
59
60
61
62 AudioManagerInterface::AudioManagerInterface(RoutingReceiveInterface* r_interface, dbusRoothandler* roothandler) : m_audioman(r_interface),m_Introspection(new DBUSIntrospection(manager_methods, manager_signals,std::string(MY_NODE))),m_roothandler(roothandler) {
63 }
64
65 bool AudioManagerInterface::startup_interface()
66 {
67         DLT_LOG(DBusPlugin, DLT_LOG_INFO, DLT_STRING("Starting up dbus connector"));
68     g_pDbusMessage = new DBUSMessageHandler(&vtable,m_roothandler->returnConnection(),this);
69     return true;
70 }
71
72 void AudioManagerInterface::stop()
73 {
74         DLT_LOG(DBusPlugin, DLT_LOG_INFO, DLT_STRING("Stopped dbus connector"));
75     delete g_pDbusMessage;
76 }
77
78 void AudioManagerInterface::peekDomain(DBusConnection* conn, DBusMessage* msg) {
79         (void)conn; // TODO: remove, only prevents warning
80         g_pDbusMessage->initReceive(msg);
81         char* name = g_pDbusMessage->getString();
82         domain_t domain = m_audioman->peekDomain(name);
83         g_pDbusMessage->initReply(msg);
84         g_pDbusMessage->append((dbus_uint32_t)domain);
85         g_pDbusMessage->closeReply();
86 }
87
88 void AudioManagerInterface::registerSource(DBusConnection* conn, DBusMessage* msg) {
89         (void)conn; // TODO: remove, only prevents warning
90         g_pDbusMessage->initReceive(msg);
91         char* name = g_pDbusMessage->getString();
92         char* audioclass = g_pDbusMessage->getString();
93         char* domain = g_pDbusMessage->getString();
94         source_t source=m_audioman->registerSource(name, audioclass, domain);
95         g_pDbusMessage->initReply(msg);
96         g_pDbusMessage->append((dbus_uint32_t)source);
97         g_pDbusMessage->closeReply();
98 }
99 void AudioManagerInterface::registerSink(DBusConnection* conn, DBusMessage* msg) {
100         (void)conn; // TODO: remove, only prevents warning
101         g_pDbusMessage->initReceive(msg);
102         char* name = g_pDbusMessage->getString();
103         char* audioclass = g_pDbusMessage->getString();
104         char* domain = g_pDbusMessage->getString();
105         sink_t sink=m_audioman->registerSink(name, audioclass, domain);
106         g_pDbusMessage->initReply(msg);
107         g_pDbusMessage->append((dbus_uint32_t)sink);
108         g_pDbusMessage->closeReply();
109 }
110
111 void AudioManagerInterface::registerDomain(DBusConnection* conn, DBusMessage* msg) {
112         char busname[40];
113         strcpy(busname, BUS_NAME);
114         (void)conn; // TODO: remove, only prevents warning
115         g_pDbusMessage->initReceive(msg);
116         char* name = g_pDbusMessage->getString();
117         char* node = g_pDbusMessage->getString();
118         bool earlymode = g_pDbusMessage->getString();
119         domain_t domain=m_reference->m_audioman->registerDomain(name, busname, node, earlymode);
120         g_pDbusMessage->initReply(msg);
121         g_pDbusMessage->append((dbus_uint32_t)domain);
122         g_pDbusMessage->closeReply();
123
124 }
125 void AudioManagerInterface::registerGateway(DBusConnection* conn, DBusMessage* msg) {
126         (void)conn; // TODO: remove, only prevents warning
127         g_pDbusMessage->initReceive(msg);
128         char* name = g_pDbusMessage->getString();
129         char* sink = g_pDbusMessage->getString();
130         char* source = g_pDbusMessage->getString();
131         char* domainSource = g_pDbusMessage->getString();
132         char* domainSink = g_pDbusMessage->getString();
133         char* controlDomain = g_pDbusMessage->getString();
134         domain_t domain=m_audioman->registerGateway(name, sink, source, domainSource, domainSink, controlDomain);
135         g_pDbusMessage->initReply(msg);
136         g_pDbusMessage->append((dbus_uint32_t)domain);
137         g_pDbusMessage->closeReply();
138         emit_systemReady();
139 }
140 void AudioManagerInterface::emit_systemReady() {
141         g_pDbusMessage->sendSignal("signal_systemReady");
142 }
143
144 DBusHandlerResult AudioManagerInterface::receive_callback(DBusConnection *conn, DBusMessage *msg, void *user_data){
145         m_reference=(AudioManagerInterface*) user_data;
146         DLT_LOG(DBusPlugin, DLT_LOG_INFO, DLT_STRING("message received"));
147
148     string nodeString =std::string(DBUS_SERVICE_ROOT)+"/"+std::string(MY_NODE);
149
150         if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) {
151                 m_reference->m_Introspection->process(conn,msg);
152                 return DBUS_HANDLER_RESULT_HANDLED;
153         }
154
155         bool found=false;
156         int i = 0;
157
158         while (!found && strcmp(manager_methods[i].name, "") != 0) {
159                 if (dbus_message_is_method_call(msg,DBUS_SERVICE_SERVICE,manager_methods[i].name)) {
160                         MethodTable entry = manager_methods[i];
161                         DLT_LOG(DBusPlugin, DLT_LOG_INFO, DLT_STRING("got call for method:"),DLT_STRING(entry.name));
162                         CallBackMethod m = entry.function;
163                         (m_reference->*m)(conn, msg);
164                         found=true;
165                         return DBUS_HANDLER_RESULT_HANDLED;
166                 }
167                 i++;
168         }
169
170         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
171 }
172
173
174