2 * Copyright (C) 2012, BMW AG
4 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
13 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 * \file CAmSocketHandler.h
16 * For further information see http://www.genivi.org/.
19 #ifndef SOCKETHANDLER_H_
20 #define SOCKETHANDLER_H_
22 #include "audiomanagertypes.h"
23 #include <sys/socket.h>
34 static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop the mainloop
36 typedef uint16_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler
37 typedef uint16_t sh_pollHandle_t; //!<this is a handle for a filedescriptor to be used with the SocketHandler
39 class IAmShPollPrepare;
42 class IAmShPollDispatch;
43 class IAmShTimerCallBack;
46 * The am::CAmSocketHandler implements a mainloop for the AudioManager. Plugins and different parts of the AudioManager add their filedescriptors to the handler
47 * to get called on communication of the filedescriptors.\n
48 * More information can be found here : \ref mainl
50 class CAmSocketHandler
56 am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle);
57 am_Error_e removeFDPoll(const sh_pollHandle_t handle);
58 am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
59 am_Error_e addTimer(const timespec timeouts, IAmShTimerCallBack*& callback, sh_timerHandle_t& handle, void* userData);
60 am_Error_e removeTimer(const sh_timerHandle_t handle);
61 am_Error_e restartTimer(const sh_timerHandle_t handle, const timespec timeouts);
62 am_Error_e stopTimer(const sh_timerHandle_t handle);
63 void start_listenting();
64 void stop_listening();
66 struct sh_timer_s //!<struct that holds information of timers
68 sh_timerHandle_t handle; //!<the handle of the timer
69 timespec countdown; //!<the countdown, this value is decreased every time the timer is up
70 timespec timeout; //!<the original timer value
71 IAmShTimerCallBack* callback; //!<the callbackfunction
72 void * userData; //!<saves a void pointer together with the rest.
75 class CAmShSubstractTime //!<functor to easy substract from each countdown value
80 CAmShSubstractTime(timespec param) :
84 void operator()(sh_timer_s& t) const;
87 struct sh_poll_s //!<struct that holds information about polls
89 sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
90 IAmShPollPrepare *prepareCB;
91 IAmShPollFired *firedCB;
92 IAmShPollCheck *checkCB;
93 IAmShPollDispatch *dispatchCB;
94 pollfd pollfdValue; //!<the array for polling the filedescriptors
95 void *userData; //!<userdata saved together with the callback.
98 typedef std::vector<pollfd> mListPollfd_t; //!<vector of filedescriptors
99 typedef std::vector<sh_poll_s> mListPoll_t; //!<list for the callbacks
101 class CAmShCopyPollfd
104 mListPollfd_t& mArray;
106 CAmShCopyPollfd(mListPollfd_t& dest) :
110 void operator()(const sh_poll_s& row);
113 bool fdIsValid(const int fd) const;
116 int timespec2ms(const timespec& time);
117 timespec* insertTime(timespec& buffertime);
118 static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b)
120 return ((a.countdown.tv_sec == b.countdown.tv_sec) ? (a.countdown.tv_nsec < b.countdown.tv_nsec) : (a.countdown.tv_sec < b.countdown.tv_sec));
123 static bool onlyFiredEvents(const pollfd& a)
125 return (a.revents == 0 ? false : true);
128 //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
129 mListPollfd_t mfdPollingArray;
130 mListPoll_t mListPoll;
131 std::list<sh_timer_s> mListTimer; //!<list of all timers
132 std::list<sh_timer_s> mListActiveTimer; //!<list of all currently active timers
133 sh_timerHandle_t mNextTimer;
134 sh_timerHandle_t mLastInsertedHandle;
135 sh_pollHandle_t mLastInsertedPollHandle;
136 bool mRecreatePollfds;
141 * prototype for the timer callback
143 class IAmShTimerCallBack
146 virtual void Call(const sh_timerHandle_t handle, void* userData)=0;
147 virtual ~IAmShTimerCallBack(){};
151 * prototype for poll prepared callback
153 class IAmShPollPrepare
156 virtual void Call(const sh_pollHandle_t handle, void* userData)=0;
157 virtual ~IAmShPollPrepare(){};
161 * prototype for poll fired callback
166 virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0;
167 virtual ~ IAmShPollFired(){};
171 * prototype for poll check callback
176 virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
177 virtual ~ IAmShPollCheck(){};
181 * prototype for dispatch callback
183 class IAmShPollDispatch
186 virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
187 virtual ~ IAmShPollDispatch(){};
191 * template to create the functor for a class
193 template<class TClass> class TAmShTimerCallBack: public IAmShTimerCallBack
197 void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
200 TAmShTimerCallBack(TClass* instance, void (TClass::*function)(sh_timerHandle_t handle, void* userData)) :
201 mInstance(instance), //
202 mFunction(function){};
204 virtual void Call(sh_timerHandle_t handle, void* userData)
206 (*mInstance.*mFunction)(handle, userData);
211 * template for a callback
213 template<class TClass> class TAmShPollPrepare: public IAmShPollPrepare
217 void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
220 TAmShPollPrepare(TClass* instance, void (TClass::*function)(const sh_timerHandle_t handle, void* userData)) :
221 mInstance(instance), //
222 mFunction(function){};
224 virtual void Call(const sh_timerHandle_t handle, void* userData)
226 (*mInstance.*mFunction)(handle, userData);
232 * template for a callback
234 template<class TClass> class TAmShPollFired: public IAmShPollFired
238 void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
241 TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
242 mInstance(instance), //
243 mFunction(function){};
245 virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
247 (*mInstance.*mFunction)(pollfd, handle, userData);
253 * template for a callback
255 template<class TClass> class TAmShPollCheck: public IAmShPollCheck
259 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
262 TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
263 mInstance(instance), //
264 mFunction(function){};
266 virtual bool Call(const sh_pollHandle_t handle, void* userData)
268 return ((*mInstance.*mFunction)(handle, userData));
274 * template for a callback
276 template<class TClass> class TAmShPollDispatch: public IAmShPollDispatch
280 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
283 TAmShPollDispatch(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
284 mInstance(instance), //
285 mFunction(function){};
287 virtual bool Call(const sh_pollHandle_t handle, void* userData)
289 return ((*mInstance.*mFunction)(handle, userData));
294 #endif /* SOCKETHANDLER_H_ */