* formatting all the source code with eclipse source code style
[profile/ivi/genivi/genivi-audio-manager.git] / includes / SocketHandler.h
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file SocketHandler.h
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 #ifndef SOCKETHANDLER_H_
26 #define SOCKETHANDLER_H_
27
28 #include <audiomanagertypes.h>
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 #include <stdint.h>
32 #include <sys/poll.h>
33 #include <list>
34 #include <map>
35 #include <signal.h>
36
37 namespace am
38 {
39
40 static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop the mainloop
41
42 typedef uint16_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler
43 typedef uint16_t sh_pollHandle_t; //!<this is a handle for a filedescriptor to be used with the SocketHandler
44
45 class shPollPrepare;
46 class shPollCheck;
47 class shPollFired;
48 class shPollDispatch;
49 class shTimerCallBack;
50
51 class SocketHandler
52 {
53 public:
54     SocketHandler();
55     virtual ~SocketHandler();
56
57     am_Error_e addFDPoll(const int fd, const short event, shPollPrepare *prepare, shPollFired *fired, shPollCheck *check, shPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle);
58     am_Error_e removeFDPoll(const sh_pollHandle_t handle);
59     am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
60     am_Error_e addTimer(const timespec timeouts, shTimerCallBack*& callback, sh_timerHandle_t& handle, void* userData);
61     am_Error_e removeTimer(const sh_timerHandle_t handle);
62     am_Error_e restartTimer(const sh_timerHandle_t handle, const timespec timeouts);
63     am_Error_e stopTimer(const sh_timerHandle_t handle);
64     void start_listenting();
65     void stop_listening();
66 private:
67     struct timer_s //!<struct that holds information of timers
68     {
69         sh_timerHandle_t handle; //!<the handle of the timer
70         timespec countdown; //!<the countdown, this value is decreased every time the timer is up
71         timespec timeout; //!<the original timer value
72         shTimerCallBack* callback; //!<the callbackfunction
73         void * userData; //!<saves a void pointer together with the rest.
74     };
75
76     class SubstractTime //!<functor to easy substract from each countdown value
77     {
78     private:
79         timespec param;
80     public:
81         SubstractTime(timespec param) :
82                 param(param){}
83         void operator()(timer_s& t) const;
84     };
85
86     struct sh_poll_s //!<struct that holds information about polls
87     {
88         sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
89         shPollPrepare *prepareCB;
90         shPollFired *firedCB;
91         shPollCheck *checkCB;
92         shPollDispatch *dispatchCB;
93         pollfd pollfdValue; //!<the array for polling the filedescriptors
94         void *userData; //!<userdata saved together with the callback.
95     };
96
97     typedef std::vector<pollfd> mPollfd_t; //!<vector of filedescriptors
98     typedef std::vector<sh_poll_s> mListPoll_t; //!<list for the callbacks
99
100     class CopyPollfd
101     {
102     private:
103         mPollfd_t& mArray;
104     public:
105         CopyPollfd(mPollfd_t& dest) :
106                 mArray(dest){}
107         void operator()(const sh_poll_s& row);
108     };
109
110     bool fdIsValid(const int fd) const;
111     void initTimer();
112     void timerUp();
113     int timespec2ms(const timespec& time);
114     timespec* insertTime(timespec& buffertime);
115     static bool compareCountdown(const timer_s& a, const timer_s& b)
116     {
117         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);
118     }
119
120     static bool onlyFiredEvents(const pollfd& a)
121     {
122         return a.revents == 0 ? false : true;
123     }
124
125     //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
126     mPollfd_t mfdPollingArray;
127     mListPoll_t mListPoll;
128     std::list<timer_s> mListTimer; //!<list of all timers
129     std::list<timer_s> mListActiveTimer; //!<list of all currently active timers
130     sh_timerHandle_t mNextTimer;
131     sh_timerHandle_t mLastInsertedHandle;
132     sh_pollHandle_t mLastInsertedPollHandle;
133     timespec mTimeout;
134     bool mRecreatePollfds;
135 };
136
137 /**
138  * classic functor for the BasicTimerCallback
139  */
140 class shTimerCallBack
141 {
142 public:
143     virtual void Call(const sh_timerHandle_t handle, void* userData)=0;
144     virtual ~shTimerCallBack(){};
145 };
146
147 class shPollPrepare
148 {
149 public:
150     virtual void Call(const sh_pollHandle_t handle, void* userData)=0;
151     virtual ~shPollPrepare(){};
152 };
153
154 class shPollFired
155 {
156 public:
157     virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0;
158     virtual ~ shPollFired(){};
159 };
160
161 class shPollCheck
162 {
163 public:
164     virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
165     virtual ~ shPollCheck(){};
166 };
167
168 class shPollDispatch
169 {
170 public:
171     virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
172     virtual ~ shPollDispatch() {};
173 };
174
175 /**
176  * template to create the functor for a class
177  */
178 template<class TClass> class shTimerCallBack_T: public shTimerCallBack
179 {
180 private:
181     TClass* mInstance;
182     void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
183
184 public:
185     shTimerCallBack_T(TClass* instance, void(TClass::*function)(sh_timerHandle_t handle, void* userData)) :
186             mInstance(instance),//
187             mFunction(function) {};
188
189     virtual void Call(sh_timerHandle_t handle, void* userData)
190     {
191         (*mInstance.*mFunction)(handle, userData);
192     }
193 };
194
195 /**
196  * template to create the functor for a class
197  */
198 template<class TClass> class shPollPrepare_T: public shPollPrepare
199 {
200 private:
201     TClass* mInstance;
202     void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
203
204 public:
205     shPollPrepare_T(TClass* instance, void(TClass::*function)(const sh_timerHandle_t handle, void* userData)) :
206             mInstance(instance), //
207             mFunction(function){};
208
209     virtual void Call(const sh_timerHandle_t handle, void* userData)
210     {
211         (*mInstance.*mFunction)(handle, userData);
212     };
213 };
214
215 template<class TClass> class shPollFired_T: public shPollFired
216 {
217 private:
218     TClass* mInstance;
219     void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
220
221 public:
222     shPollFired_T(TClass* instance, void(TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
223             mInstance(instance), //
224             mFunction(function){};
225
226     virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
227     {
228         (*mInstance.*mFunction)(pollfd, handle, userData);
229     };
230 };
231
232 template<class TClass> class shPollCheck_T: public shPollCheck
233 {
234 private:
235     TClass* mInstance;
236     bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
237
238 public:
239     shPollCheck_T(TClass* instance, bool(TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
240             mInstance(instance), //
241             mFunction(function){};
242
243     virtual bool Call(const sh_pollHandle_t handle, void* userData)
244     {
245         return (*mInstance.*mFunction)(handle, userData);
246     };
247 };
248
249 template<class TClass> class shPollDispatch_T: public shPollDispatch
250 {
251 private:
252     TClass* mInstance;
253     bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
254
255 public:
256     shPollDispatch_T(TClass* instance, bool(TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
257             mInstance(instance), //
258             mFunction(function) {};
259
260     virtual bool Call(const sh_pollHandle_t handle, void* userData)
261     {
262         return (*mInstance.*mFunction)(handle, userData);
263     };
264 };
265 } /* namespace am */
266 #endif /* SOCKETHANDLER_H_ */