update on dbus
[profile/ivi/genivi/genivi-audio-manager.git] / PluginCommandInterfaceDbus / CommandDbusReceive.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 DLT_IMPORT_CONTEXT(DBusCommandPlugin);
34
35 class CommandDbusReceive;
36
37
38 CommandDbusReceive* CommandDbusReceive::m_reference = NULL;
39
40 static DBUSMessageHandler* g_pDbusMessage;
41
42 static MethodTable manager_methods[] =
43 {
44         { "connect",                    "uu",           "u",            &CommandDbusReceive::connect },
45         { "disconnect",                         "uu",           "u",            &CommandDbusReceive::disconnect },
46         { "getListConnections",     "",                 "a(ii)",        &CommandDbusReceive::getListConnections },
47         { "getListSinks",               "",             "a(si)",        &CommandDbusReceive::getListSinks },
48         { "getListSources",             "",             "a(si)",        &CommandDbusReceive::getListSources },
49         { "interruptRequest",           "ss",           "u",            &CommandDbusReceive::interruptRequest },
50         { "interruptResume",            "s",            "u",            &CommandDbusReceive::interruptResume },
51         { "setVolume",                  "ss",       "u",                &CommandDbusReceive::setVolume },
52         { "",                  "",      "",     NULL }
53 };
54
55 static SignalTable manager_signals[] = {
56         { "signal_connectionChanged",   ""},
57         { "signal_numberOfSinksChanged",        ""},
58         { "signal_numberOfSourcesChanged",      ""},
59         { "",                           ""}
60 };
61
62 static DBusObjectPathVTable vtable =
63 {
64         NULL,CommandDbusReceive::receive_callback,NULL, NULL, NULL, NULL
65 };
66
67
68
69 CommandDbusReceive::CommandDbusReceive(CommandReceiveInterface* r_interface, dbusRoothandler* roothandler): m_audioman(r_interface),m_Introspection(new DBUSIntrospection(manager_methods, manager_signals,std::string(MY_NODE))),m_roothandler(roothandler) {
70 }
71
72 bool CommandDbusReceive::startup_interface() {
73         DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Starting up dbus connector"));
74     g_pDbusMessage = new DBUSMessageHandler(&vtable,m_roothandler->returnConnection(),this);
75     return true;
76 }
77
78 void CommandDbusReceive::stop() {
79         DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Stopped dbus communication"));
80     delete g_pDbusMessage;
81 }
82
83 void CommandDbusReceive::connect(DBusConnection* conn, DBusMessage* msg) {
84         (void)conn; // TODO: remove, only prevents warning
85         g_pDbusMessage->initReceive(msg);
86         source_t source = g_pDbusMessage->getUInt();
87         sink_t sink = g_pDbusMessage->getUInt();
88         connection_t connect=m_audioman->connect(source, sink);
89         g_pDbusMessage->initReply(msg);
90         g_pDbusMessage->append((unsigned int)connect);
91         g_pDbusMessage->closeReply();
92
93 }
94 void CommandDbusReceive::disconnect(DBusConnection* conn, DBusMessage* msg) {
95         (void)conn; // TODO: remove, only prevents warning
96         g_pDbusMessage->initReceive(msg);
97         source_t source = g_pDbusMessage->getUInt();
98         sink_t sink = g_pDbusMessage->getUInt();
99         connection_t connect=m_audioman->disconnect(source, sink);
100         g_pDbusMessage->initReply(msg);
101         g_pDbusMessage->append((unsigned int)connect);
102         g_pDbusMessage->closeReply();
103 }
104
105 void CommandDbusReceive::getListConnections(DBusConnection* conn, DBusMessage* msg) {
106         (void)conn; // TODO: remove, only prevents warning
107         std::list<ConnectionType> list=m_audioman->getListConnections();
108         g_pDbusMessage->initReply(msg);
109         g_pDbusMessage->append(list);
110         g_pDbusMessage->closeReply();
111
112 }
113
114 void CommandDbusReceive::getListSinks(DBusConnection* conn, DBusMessage* msg) {
115         (void)conn; // TODO: remove, only prevents warning
116         std::list<SinkType> list=m_audioman->getListSinks();
117         g_pDbusMessage->initReply(msg);
118         g_pDbusMessage->append(list);
119         g_pDbusMessage->closeReply();
120
121 }
122
123 void CommandDbusReceive::getListSources(DBusConnection* conn, DBusMessage* msg) {
124         (void)conn; // TODO: remove, only prevents warning
125         std::list<SourceType> list=m_audioman->getListSources();
126         g_pDbusMessage->initReply(msg);
127         g_pDbusMessage->append(list);
128         g_pDbusMessage->closeReply();
129
130 }
131
132 void CommandDbusReceive::interruptRequest(DBusConnection* conn, DBusMessage* msg) {
133         (void)conn; // TODO: remove, only prevents warning
134         g_pDbusMessage->initReceive(msg);
135         char* source = g_pDbusMessage->getString();
136         char* sink = g_pDbusMessage->getString();
137     genInt_t interrupt=m_audioman->interruptRequest(source, sink);
138         g_pDbusMessage->initReply(msg);
139         g_pDbusMessage->append((unsigned int)interrupt);
140         g_pDbusMessage->closeReply();
141 }
142
143 void CommandDbusReceive::interruptResume(DBusConnection* conn, DBusMessage* msg) {
144         (void)conn; // TODO: remove, only prevents warning
145         g_pDbusMessage->initReceive(msg);
146         interrupt_t interrupt = g_pDbusMessage->getUInt();
147         interrupt_t returnVal=m_audioman->interruptResume(interrupt);
148         g_pDbusMessage->initReply(msg);
149         g_pDbusMessage->append((unsigned int)returnVal);
150         g_pDbusMessage->closeReply();
151 }
152
153 void CommandDbusReceive::setVolume(DBusConnection* conn, DBusMessage* msg) {
154         (void)conn; // TODO: remove, only prevents warning
155         g_pDbusMessage->initReceive(msg);
156         sink_t sink = g_pDbusMessage->getUInt();
157         volume_t volume = g_pDbusMessage->getUInt();
158         volume_t returnVolume=m_audioman->setVolume(sink,volume);
159         g_pDbusMessage->initReply(msg);
160         g_pDbusMessage->append((unsigned int)returnVolume);
161         g_pDbusMessage->closeReply();
162 }
163
164 void CommandDbusReceive::emitSignalConnectionsChanged() {
165         g_pDbusMessage->sendSignal(SIG_CONNECTION_CHANGED);
166 }
167
168 void CommandDbusReceive::emitSignalNumberofSinksChanged() {
169         g_pDbusMessage->sendSignal(SIG_NUM_SINKS_CHANGED);
170 }
171
172 void CommandDbusReceive::emitSignalNumberofSourcesChanged() {
173         g_pDbusMessage->sendSignal(SIG_NUM_SOURCES_CHANGED);
174 }
175
176 DBusHandlerResult CommandDbusReceive::receive_callback (DBusConnection *conn,DBusMessage *msg,void *user_data) {
177         m_reference=(CommandDbusReceive*) user_data;
178         DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("message received"));
179
180     string nodeString =std::string(DBUS_SERVICE_ROOT)+"/"+std::string(MY_NODE);
181
182         if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) {
183                 m_reference->m_Introspection->process(conn,msg);
184                 return DBUS_HANDLER_RESULT_HANDLED;
185         }
186
187         bool found=false;
188         int i = 0;
189
190         const char *n = dbus_message_get_member(msg);
191         DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("got call for method:"),DLT_STRING(n));
192         while (!found && strcmp(manager_methods[i].name, "") != 0) {
193                 if (dbus_message_is_method_call(msg,DBUS_SERVICE_SERVICE,manager_methods[i].name)) {
194                         MethodTable entry = manager_methods[i];
195                         DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("got call for method:"),DLT_STRING(entry.name));
196                         CallBackMethod m = entry.function;
197                         (m_reference->*m)(conn, msg);
198                         found=true;
199                         return DBUS_HANDLER_RESULT_HANDLED;
200                 }
201                 i++;
202         }
203
204         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
205 }
206
207