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 sockethandler implements a mainloop for the audiomanager. Plugins and different parts of the audiomanager add their filedescriptors to the handler to get called
47 * on communication of the filedescriptors.
49 class CAmSocketHandler
55 am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle);
56 am_Error_e removeFDPoll(const sh_pollHandle_t handle);
57 am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
58 am_Error_e addTimer(const timespec timeouts, IAmShTimerCallBack*& callback, sh_timerHandle_t& handle, void* userData);
59 am_Error_e removeTimer(const sh_timerHandle_t handle);
60 am_Error_e restartTimer(const sh_timerHandle_t handle, const timespec timeouts);
61 am_Error_e stopTimer(const sh_timerHandle_t handle);
62 void start_listenting();
63 void stop_listening();
65 struct sh_timer_s //!<struct that holds information of timers
67 sh_timerHandle_t handle; //!<the handle of the timer
68 timespec countdown; //!<the countdown, this value is decreased every time the timer is up
69 timespec timeout; //!<the original timer value
70 IAmShTimerCallBack* callback; //!<the callbackfunction
71 void * userData; //!<saves a void pointer together with the rest.
74 class CAmShSubstractTime //!<functor to easy substract from each countdown value
79 CAmShSubstractTime(timespec param) :
83 void operator()(sh_timer_s& t) const;
86 struct sh_poll_s //!<struct that holds information about polls
88 sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
89 IAmShPollPrepare *prepareCB;
90 IAmShPollFired *firedCB;
91 IAmShPollCheck *checkCB;
92 IAmShPollDispatch *dispatchCB;
93 pollfd pollfdValue; //!<the array for polling the filedescriptors
94 void *userData; //!<userdata saved together with the callback.
97 typedef std::vector<pollfd> mListPollfd_t; //!<vector of filedescriptors
98 typedef std::vector<sh_poll_s> mListPoll_t; //!<list for the callbacks
100 class CAmShCopyPollfd
103 mListPollfd_t& mArray;
105 CAmShCopyPollfd(mListPollfd_t& dest) :
109 void operator()(const sh_poll_s& row);
112 bool fdIsValid(const int fd) const;
115 int timespec2ms(const timespec& time);
116 timespec* insertTime(timespec& buffertime);
117 static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b)
119 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));
122 static bool onlyFiredEvents(const pollfd& a)
124 return (a.revents == 0 ? false : true);
127 //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
128 mListPollfd_t mfdPollingArray;
129 mListPoll_t mListPoll;
130 std::list<sh_timer_s> mListTimer; //!<list of all timers
131 std::list<sh_timer_s> mListActiveTimer; //!<list of all currently active timers
132 sh_timerHandle_t mNextTimer;
133 sh_timerHandle_t mLastInsertedHandle;
134 sh_pollHandle_t mLastInsertedPollHandle;
135 bool mRecreatePollfds;
140 * prototype for the timer callback
142 class IAmShTimerCallBack
145 virtual void Call(const sh_timerHandle_t handle, void* userData)=0;
146 virtual ~IAmShTimerCallBack(){};
150 * prototype for poll prepared callback
152 class IAmShPollPrepare
155 virtual void Call(const sh_pollHandle_t handle, void* userData)=0;
156 virtual ~IAmShPollPrepare(){};
160 * prototype for poll fired callback
165 virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0;
166 virtual ~ IAmShPollFired(){};
170 * prototype for poll check callback
175 virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
176 virtual ~ IAmShPollCheck(){};
180 * prototype for dispatch callback
182 class IAmShPollDispatch
185 virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
186 virtual ~ IAmShPollDispatch(){};
190 * template to create the functor for a class
192 template<class TClass> class TAmShTimerCallBack: public IAmShTimerCallBack
196 void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
199 TAmShTimerCallBack(TClass* instance, void (TClass::*function)(sh_timerHandle_t handle, void* userData)) :
200 mInstance(instance), //
201 mFunction(function){};
203 virtual void Call(sh_timerHandle_t handle, void* userData)
205 (*mInstance.*mFunction)(handle, userData);
210 * template for a callback
212 template<class TClass> class TAmShPollPrepare: public IAmShPollPrepare
216 void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
219 TAmShPollPrepare(TClass* instance, void (TClass::*function)(const sh_timerHandle_t handle, void* userData)) :
220 mInstance(instance), //
221 mFunction(function){};
223 virtual void Call(const sh_timerHandle_t handle, void* userData)
225 (*mInstance.*mFunction)(handle, userData);
231 * template for a callback
233 template<class TClass> class TAmShPollFired: public IAmShPollFired
237 void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
240 TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
241 mInstance(instance), //
242 mFunction(function){};
244 virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
246 (*mInstance.*mFunction)(pollfd, handle, userData);
252 * template for a callback
254 template<class TClass> class TAmShPollCheck: public IAmShPollCheck
258 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
261 TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
262 mInstance(instance), //
263 mFunction(function){};
265 virtual bool Call(const sh_pollHandle_t handle, void* userData)
267 return ((*mInstance.*mFunction)(handle, userData));
273 * template for a callback
275 template<class TClass> class TAmShPollDispatch: public IAmShPollDispatch
279 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
282 TAmShPollDispatch(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
283 mInstance(instance), //
284 mFunction(function){};
286 virtual bool Call(const sh_pollHandle_t handle, void* userData)
288 return ((*mInstance.*mFunction)(handle, userData));
293 #endif /* SOCKETHANDLER_H_ */