* wrapping DLT calls in a new Class because of performance, codesize and lazyness...
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / TelnetServer.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file TelnetServer.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14  *
15  * 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.
16  * 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.
17  * 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>.
18  * 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.
19  * 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.
20  * 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.
21  * 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.
22  *
23  */
24
25 #include "TelnetServer.h"
26 #include <assert.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
30 #include <string.h>
31 #include <netdb.h>
32 #include <config.h>
33 #include <errno.h>
34 #include <sstream>
35 #include <istream>
36 #include <iostream>
37 #include <iterator>
38 #include "DatabaseHandler.h"
39 #include "RoutingSender.h"
40
41 using namespace am;
42
43 TelnetServer* TelnetServer::instance = NULL;
44
45 #define PRINT_BOOL(var) var ? output+="true\t\t" : output+="false\t\t";
46
47 TelnetServer::TelnetServer(SocketHandler *iSocketHandler, CommandSender *iCommandSender, CommandReceiver *iCommandReceiver, RoutingSender *iRoutingSender, RoutingReceiver *iRoutingReceiver, ControlSender *iControlSender, ControlReceiver *iControlReceiver, DatabaseHandler *iDatabasehandler, unsigned int servPort, unsigned int maxConnections) :
48         telnetConnectFiredCB(this, &TelnetServer::connectSocket), //
49         telnetReceiveFiredCB(this, &TelnetServer::receiveData), //
50         telnetDispatchCB(this, &TelnetServer::dispatchData), //
51         telnetCheckCB(this, &TelnetServer::check), //
52         mSocketHandler(iSocketHandler), //
53         mCommandSender(iCommandSender), //
54         mCommandReceiver(iCommandReceiver), //
55         mRoutingSender(iRoutingSender), //
56         mRoutingReceiver(iRoutingReceiver), //
57         mControlSender(iControlSender), //
58         mControlReceiver(iControlReceiver), //
59         mDatabasehandler(iDatabasehandler), //
60         mConnecthandle(), //
61         msgList(), //
62         mListConnections(), //
63         mConnectFD(NULL), //
64         mServerPort(servPort), //
65         mMaxConnections(maxConnections), //
66         mMapCommands(createCommandMap())
67 {
68     assert(mSocketHandler!=NULL);
69     assert(mCommandReceiver!=NULL);
70     assert(mCommandSender!=NULL);
71     assert(mControlSender!=NULL);
72     assert(mControlReceiver!=NULL);
73     assert(mRoutingSender!=NULL);
74     assert(mRoutingReceiver!=NULL);
75     assert(mDatabasehandler!=NULL);
76     assert(servPort!=0);
77     assert(mMaxConnections!=0);
78
79     instance = this;
80
81     int yes = 1;
82     struct sockaddr_in servAddr;
83
84     //setup the port Listener
85     mConnectFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
86     setsockopt(mConnectFD, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
87     memset(&servAddr, 0, sizeof(servAddr));
88     servAddr.sin_family = AF_INET;
89     servAddr.sin_addr.s_addr = INADDR_ANY;
90     servAddr.sin_port = htons(servPort);
91     bind(mConnectFD, (struct sockaddr *) &servAddr, sizeof(servAddr));
92
93     int a = 1;
94     ioctl(mConnectFD, FIONBIO, (char *) &a);
95     setsockopt(mConnectFD, SOL_SOCKET, SO_KEEPALIVE, (char *) &a, sizeof(a));
96
97     short events = 0;
98     events |= POLLIN;
99     mSocketHandler->addFDPoll(mConnectFD, events, NULL, &telnetConnectFiredCB, NULL, NULL, NULL, mConnecthandle);
100 }
101
102 TelnetServer::~TelnetServer()
103 {
104 }
105
106 void TelnetServer::connectSocket(const pollfd pfd, const sh_pollHandle_t handle, void *userData)
107 {
108     (void) handle;
109     (void) userData;
110     //first, accept the connection, create a new filedescriptor
111     struct sockaddr answer;
112     socklen_t len = sizeof(answer);
113     connection_s connection;
114     connection.filedescriptor = accept(pfd.fd, (struct sockaddr*) &answer, &len);
115
116     mListConnections.push_back(connection);
117
118     //set the correct event:
119     short event = 0;
120     event |= POLLIN;
121
122     //aded the filedescriptor to the sockethandler and register the callbacks for receiving the data
123     mSocketHandler->addFDPoll(mListConnections.back().filedescriptor, event, NULL, &telnetReceiveFiredCB, &telnetCheckCB, &telnetDispatchCB, NULL, mListConnections.back().handle);
124 }
125
126 void TelnetServer::receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
127 {
128     (void)handle;
129     (void)userData;
130     //initialize buffer
131     char buffer[100];
132     //read until buffer is full or no more data is there
133     int read = recv(pollfd.fd, buffer, 100, NULL);
134     if (read > 1)
135     {
136         //read the message and store it in a queue - its a telnet connection so data will be sent on enter !
137         std::string msg = std::string(buffer, read);
138         msgList.push(msg);
139     }
140 }
141
142 bool TelnetServer::dispatchData(const sh_pollHandle_t handle, void *userData)
143 {
144     (void)handle;
145     (void)userData;
146     std::vector<connection_s>::iterator iterator = mListConnections.begin();
147     for (; iterator != mListConnections.end(); ++iterator)
148     {
149         if (iterator->handle == handle) break;
150     }
151     if (iterator == mListConnections.end()) return false;
152
153     std::string command;
154     std::vector<std::string> msg;
155     sliceCommand(msgList.front(), command, msg);
156     msgList.pop();
157     mMapCommand_t::iterator commandIter = mMapCommands.find(command);
158     if (commandIter == mMapCommands.end())
159     {
160         send(iterator->filedescriptor, "Command not found!\n", 20, 0);
161     }
162     else
163     {
164         (*commandIter).second(msg, iterator->filedescriptor);
165     }
166
167     //remove the message from the queue and return false if there is no more message to read.
168     if (msgList.size() != 0) return true;
169     return false;
170 }
171
172 bool TelnetServer::check(const sh_pollHandle_t handle, void *userData)
173 {
174     (void)handle;
175     (void)userData;
176     if (msgList.size() != 0) return true;
177     return false;
178 }
179
180 void TelnetServer::listCommand(std::vector<std::string>& msg, int filedescriptor)
181 {
182     instance->listCommandShadow(msg, filedescriptor);
183 }
184
185 void TelnetServer::listCommandShadow(std::vector<std::string> & msg, int filedescriptor)
186 {
187     std::string output;
188     if (msg.empty())
189     {
190         output += "No second parameter given after list, please enter\n";
191     }
192     else if (msg.front().compare("plugins") == 0)
193     {
194         std::vector<std::string> plugins;
195         mRoutingSender->getListPlugins(plugins);
196         std::vector<std::string>::iterator it = plugins.begin();
197         output = "\n\nrouting plugins:\n-------------------------\n";
198         for (; it != plugins.end(); ++it)
199         {
200             output += *it + "\n";
201         }
202     }
203     else if (msg.front().compare("domains") == 0)
204     {
205         output = "domainID\t\tdomainName\t\tbusName\t\tnodeName\t\tearly\t\tstate\t\tcomplete\n";
206         output += "-------------------------------------------------------------------------------------\n";
207         std::vector<am_Domain_s> domainList;
208         mDatabasehandler->getListDomains(domainList);
209         std::vector<am_Domain_s>::iterator it = domainList.begin();
210         for (; it != domainList.end(); ++it)
211         {
212             output += it->domainID + "\t\t";
213             output += it->name + "\t\t";
214             output += it->busname + "\t\t";
215             output += it->nodename + "\t\t";
216             PRINT_BOOL(it->early);
217             switch (it->state)
218             {
219             case DS_CONTROLLED:
220                 output += "DS_CONTROLLED\t\t";
221                 break;
222             case DS_INDEPENDENT_STARTUP:
223                 output += "DS_INDEPENDENT_STARTUP\t\t";
224                 break;
225             case DS_INDEPENDENT_RUNDOWN:
226                 output += "DS_INDEPENDENT_RUNDOWN\t\t";
227                 break;
228             default:
229                 output += "undefined\t\t";
230                 break;
231             }PRINT_BOOL(it->complete);
232         }
233     }
234     else
235     {
236         output = "did not recognize parameter: " + msg.front() + "\n";
237     }
238     send(filedescriptor, output.c_str(), output.size(), 0);
239 }
240
241 void am::TelnetServer::sliceCommand(const std::string & string, std::string & command, std::vector<std::string> & msg)
242 {
243     std::stringstream stream(string);
244     std::istream_iterator<std::string> begin(stream);
245     std::istream_iterator<std::string> end;
246     command = *begin++;
247     msg = std::vector<std::string>(begin, end);
248 }
249
250 TelnetServer::mMapCommand_t TelnetServer::createCommandMap()
251 {
252     mMapCommand_t commands;
253     commands.insert(std::make_pair("list", &TelnetServer::listCommand));
254     return commands;
255 }
256