2 * Copyright (C) 2011, BMW AG
4 * GeniviAudioMananger AudioManagerDaemon
6 * \file DBusWrapper.cpp
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
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
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.
25 #include "SocketHandler.h"
28 #include <sys/fcntl.h>
29 #include <sys/errno.h>
39 //todo: implement time correction if timer was interrupted by call
41 DLT_IMPORT_CONTEXT(AudioManager)
45 SocketHandler::SocketHandler()
50 mLastInsertedHandle(0),
51 mLastInsertedPollHandle(0),
52 mRecreatePollfds(true),
60 SocketHandler::~SocketHandler()
65 //todo: maybe have some: give me more time returned?
67 * start the block listening for filedescriptors. This is the mainloop.
69 void SocketHandler::start_listenting()
72 std::list<int16_t>hitList;
77 //prepare the signalmask
79 sigemptyset(&sigmask);
80 sigaddset(&sigmask, SIGINT);
81 sigaddset(&sigmask, SIGQUIT);
82 sigaddset(&sigmask, SIGTERM);
83 sigaddset(&sigmask, SIGHUP);
84 sigaddset(&sigmask, SIGQUIT);
86 while (!gDispatchDone)
88 //first we go through the registered filedescriptors and check if someone needs preparation:
89 mListPoll_t::iterator prepIter=mListPoll.begin();
90 shPollPrepare *prep=NULL;
91 for(;prepIter!=mListPoll.end();++prepIter)
93 if((prep=prepIter->prepareCB)!=NULL) prep->Call(prepIter->handle,prepIter->userData);
98 mfdPollingArray.clear();
99 //there was a change in the setup, so we need to recreate the fdarray from the list
100 std::for_each(mListPoll.begin(),mListPoll.end(),CopyPollfd(mfdPollingArray));
101 mRecreatePollfds=false;
104 //block until something is on a filedescriptor
108 if((pollStatus=ppoll(&mfdPollingArray.front(),mfdPollingArray.size(),insertTime(buffertime),&sigmask))<0)
112 //a signal was received, that means it's time to go...
117 DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("SocketHandler::start_listenting ppoll returned with error"),DLT_INT(errno));
123 //sigprocmask (SIG_SETMASK, &mask, &oldmask);
124 if((pollStatus=poll(&mfdPollingArray.front(),mfdPollingArray.size(),timespec2ms(mTimeout)))<0)
129 //a signal was received, that means it's time to go...
130 //todo: add things to do here before going to sleep
133 DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("SocketHandler::start_listenting poll returned with error"),DLT_INT(errno));
136 //sigprocmask (SIG_SETMASK, &oldmask, NULL);
139 if (pollStatus!=0) //only check filedescriptors if there was a change
141 //todo: here could be a timer that makes sure naughty plugins return!
143 //get all indexes of the fired events and save them int hitList
145 std::vector<pollfd>::iterator it=mfdPollingArray.begin();
148 it=std::find_if(it,mfdPollingArray.end(),onlyFiredEvents);
149 if (it!=mfdPollingArray.end()) hitList.push_back(std::distance(mfdPollingArray.begin(), it++));
151 } while (it!=mfdPollingArray.end());
153 //stage 1, call firedCB for all matched events, but only if callback is not zero!
154 std::list<int16_t>::iterator hListIt=hitList.begin();
155 for(;hListIt!=hitList.end();++hListIt)
157 shPollFired* fire=NULL;
158 if ((fire=mListPoll.at(*hListIt).firedCB)!=NULL) fire->Call(mfdPollingArray.at(*hListIt),mListPoll.at(*hListIt).handle,mListPoll.at(*hListIt).userData);
161 //stage 2, lets ask around if some dispatching is necessary, if not, they are taken from the hitlist
162 hListIt=hitList.begin();
163 for(;hListIt!=hitList.end();++hListIt)
165 shPollCheck* check=NULL;
166 if ((check=mListPoll.at(*hListIt).checkCB)!=NULL)
168 if (!check->Call(mListPoll.at(*hListIt).handle,mListPoll.at(*hListIt).userData))
170 hListIt=hitList.erase(hListIt);
175 //stage 3, the ones left need to dispatch, we do this as long as there is something to dispatch..
178 hListIt=hitList.begin();
179 for(;hListIt!=hitList.end();++hListIt)
181 shPollDispatch *dispatch=NULL;
182 if((dispatch=mListPoll.at(*hListIt).dispatchCB)!=NULL)
184 if (!dispatch->Call(mListPoll.at(*hListIt).handle,mListPoll.at(*hListIt).userData))
186 hListIt=hitList.erase(hListIt);
189 else //there is no dispatch function, so we just remove the file from the list...
191 hListIt=hitList.erase(hListIt);
194 } while (!hitList.empty());
199 //this was a timer event, we need to take care about the timers
208 void SocketHandler::stop_listening()
214 * Adds a filedescriptor to the polling loop
215 * @param fd this is a valid filedescriptor
216 * @param event the event flags
217 * @param callback the callback that shall be called if the filedescriptor poll succeeded
218 * @return E_OK if the descriptor was added, E_NON_EXISTENT if the fd is not valid
220 am_Error_e SocketHandler::addFDPoll(const int fd,const int16_t event, shPollPrepare *prepare,shPollFired *fired,shPollCheck *check,shPollDispatch *dispatch, void* userData,sh_pollHandle_t& handle)
222 if (!fdIsValid(fd)) return E_NON_EXISTENT;
225 pollData.pollfdValue.fd=fd;
226 pollData.handle=++mLastInsertedPollHandle;
227 pollData.pollfdValue.events=event;
228 pollData.pollfdValue.revents=0;
229 pollData.userData=userData;
230 pollData.prepareCB=prepare;
231 pollData.firedCB=fired;
232 pollData.checkCB=check;
233 pollData.dispatchCB=dispatch;
235 //add new data to the list
236 mListPoll.push_back(pollData);
238 mRecreatePollfds=true;
240 handle=pollData.handle;
245 * removes a filedescriptor from the poll loop
246 * @param fd the filedescriptor to be removed
247 * @return E_OK in case of sucess, E_NON_EXISTENT or E_UNKNOWN if the fd in not registered
249 am_Error_e SocketHandler::removeFDPoll(const sh_pollHandle_t handle)
251 mListPoll_t::iterator iterator=mListPoll.begin();
253 for (;iterator!=mListPoll.end();++iterator)
255 if(iterator->handle==handle)
257 iterator=mListPoll.erase(iterator);
258 mRecreatePollfds=true;
266 * adds a timer to the list of timers. The callback will be fired when the timer is up.
267 * This is not a high precise timer, it is very coarse. It is meant to be used for timeouts when waiting
268 * for an answer via a filedescriptor.
269 * One time timer. If you need again a timer, you need to add a new timer in the callback of the old one.
270 * @param timeouts time until the callback is fired
271 * @param callback the callback
272 * @param handle the handle that is created for the timer is returned. Can be used to remove the timer
273 * @return E_OK in case of success
275 am_Error_e SocketHandler::addTimer(const timespec timeouts,shTimerCallBack*& callback,sh_timerHandle_t& handle, void * userData)
277 assert(!((timeouts.tv_sec==0) && (timeouts.tv_nsec==0)));
278 assert(callback!=NULL);
282 //create a new handle for the timer
283 handle=++mLastInsertedHandle; //todo: overflow ruling !
284 timerItem.handle=handle;
285 timerItem.countdown=timeouts;
286 timerItem.timeout=timeouts;
287 timerItem.callback=callback;
288 timerItem.userData=userData;
290 //add timer to the list
291 mListActiveTimer.push_back(timerItem);
292 mListTimer.push_back(timerItem);
294 //very important: sort the list so that the smallest value is front
295 mListActiveTimer.sort(compareCountdown);
296 mTimeout=mListActiveTimer.front().countdown;
301 * removes a timer from the list of timers
302 * @param handle the handle to the timer
303 * @return E_OK in case of success, E_UNKNOWN if timer was not found.
305 am_Error_e SocketHandler::removeTimer(const sh_timerHandle_t handle)
309 //stop the current timer
312 std::list<timer_s>::iterator it=mListTimer.begin();
313 for(;it!=mListTimer.end();++it)
315 if(it->handle==handle)
317 it=mListTimer.erase(it);
325 * restarts a timer and updates with a new interval
326 * @param handle handle to the timer
327 * @param timeouts new timout time
328 * @return E_OK on success, E_NON_EXISTENT if the handle was not found
330 am_Error_e SocketHandler::restartTimer(const sh_timerHandle_t handle, const timespec timeouts)
333 std::list<timer_s>::iterator it=mListTimer.begin();
334 for(;it!=mListTimer.end();++it)
336 if (it->handle==handle)
343 if (timeouts.tv_nsec!=-1 && timeouts.tv_sec!=-1)
345 timerItem.timeout=timeouts;
348 mListActiveTimer.push_back(timerItem);
350 //very important: sort the list so that the smallest value is front
351 mListActiveTimer.sort(compareCountdown);
352 mTimeout=mListActiveTimer.front().countdown;
356 am_Error_e SocketHandler::stopTimer(const sh_timerHandle_t handle)
358 //go through the list and remove the timer with the handle
359 std::list<timer_s>::iterator it=mListActiveTimer.begin();
360 for(;it!=mListActiveTimer.end();++it)
362 if(it->handle==handle)
364 it=mListActiveTimer.erase(it);
365 if (!mListActiveTimer.empty())
367 mTimeout=mListActiveTimer.front().countdown;
377 return E_NON_EXISTENT;
381 * updates the eventFlags of a poll
382 * @param fd the filedescriptor of the poll
383 * @param event the event flags
384 * @return E_OK on succsess, E_NON_EXISTENT if fd was not found
386 am_Error_e SocketHandler::updateEventFlags(const sh_pollHandle_t handle, const int16_t events)
388 mListPoll_t::iterator iterator=mListPoll.begin();
390 for (;iterator!=mListPoll.end();++iterator)
392 if(iterator->handle==handle)
394 iterator->pollfdValue.events=events;
395 mRecreatePollfds=true;
403 * checks if a filedescriptor is valid
404 * @param fd the filedescriptor
405 * @return true if the fd is valid
407 bool SocketHandler::fdIsValid(const int fd) const
409 return (fcntl(fd, F_GETFL) != -1 || errno != EBADF);
413 * whenever a timer is up, this function needs to be called.
414 * Removes the fired timer, calls the callback and resets mTimeout
416 void SocketHandler::timerUp()
418 //first fire the event
419 mListActiveTimer.front().callback->Call(mListActiveTimer.front().handle,mListActiveTimer.front().userData);
421 //then remove the first timer, the one who fired
422 mListActiveTimer.pop_front();
423 if(!mListActiveTimer.empty())
425 //substract the old value from all timers in the list
426 std::for_each(mListActiveTimer.begin(),mListActiveTimer.end(),SubstractTime(mTimeout));
427 mTimeout=mListActiveTimer.front().countdown;
439 void SocketHandler::initTimer()
441 if(!mListActiveTimer.empty())
443 mTimeout=mListActiveTimer.front().countdown;
454 * convert timespec to milliseconds
455 * @param time time in timespec
456 * @return time in milliseconds
458 inline int SocketHandler::timespec2ms(const timespec & time)
460 return (time.tv_nsec == -1 && time.tv_sec == -1) ? -1 : time.tv_sec * 1000 + time.tv_nsec / 1000000;
463 inline timespec* am::SocketHandler::insertTime(timespec& buffertime)
465 buffertime.tv_nsec=mTimeout.tv_nsec;
466 buffertime.tv_sec=mTimeout.tv_sec;
467 return (mTimeout.tv_nsec == -1 && mTimeout.tv_sec == -1) ? NULL : &buffertime;
471 * functor to easy substract from each countdown
472 * @param t value to substract from
474 void SocketHandler::SubstractTime::operator ()(timer_s & t) const
477 if((val = t.countdown.tv_nsec - param.tv_nsec) < 0){
478 t.countdown.tv_nsec = 1000000000 + val;
479 t.countdown.tv_sec--;
481 t.countdown.tv_nsec = val;
483 (t.countdown.tv_sec - param.tv_sec) < 0 ? 0 : (t.countdown.tv_sec -= param.tv_sec);
486 void SocketHandler::CopyPollfd::operator ()(const sh_poll_s & row)
488 pollfd temp=row.pollfdValue;
490 mArray.push_back(temp);