/** * Copyright (C) 2012, BMW AG * * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012 * * \copyright * 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, * 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, * subject to the following conditions: * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * 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. * 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 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * \file CAmSocketHandler.h * For further information see http://www.genivi.org/. */ #ifndef SOCKETHANDLER_H_ #define SOCKETHANDLER_H_ #include "audiomanagertypes.h" #include #include #include #include #include #include #include //todo: remove me namespace am { #define MAX_NS 1000000000L typedef uint16_t sh_timerHandle_t; //! class TAmShPollFired: public IAmShPollFired { private: TClass* mInstance; void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData); public: TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) {}; virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) { (*mInstance.*mFunction)(pollfd, handle, userData); }; }; template class TAmShPollCheck: public IAmShPollCheck { private: TClass* mInstance; bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData); public: TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) {}; virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); }; }; CAmSocketHandler(); ~CAmSocketHandler(); am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle); am_Error_e removeFDPoll(const sh_pollHandle_t handle); am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events); am_Error_e addTimer(const timespec timeouts, IAmShTimerCallBack* callback, sh_timerHandle_t& handle, void* userData); am_Error_e removeTimer(const sh_timerHandle_t handle); am_Error_e restartTimer(const sh_timerHandle_t handle); am_Error_e updateTimer(const sh_timerHandle_t handle, const timespec timeouts); am_Error_e stopTimer(const sh_timerHandle_t handle); void start_listenting(); void stop_listening(); void exit_mainloop(); void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) { (void) pollfd; (void) handle; (void) userData; }; bool checkerCallback(const sh_pollHandle_t handle, void* userData) { (void) handle; (void) userData; return (false); }; TAmShPollFired receiverCallbackT; TAmShPollCheck checkerCallbackT; private: static CAmSocketHandler* mInstance; int mPipe[2]; int mDispatchDone; //this starts / stops the mainloop struct sh_timer_s //! rListTimerIter; //! mListPollfd_t; //! mListPoll_t; //!= MAX_NS) { result.tv_sec++; result.tv_nsec = result.tv_nsec - MAX_NS; } return (result); } /** * comapares timespec values * @param a * @param b * @return */ inline int timespecCompare(const timespec& a, const timespec& b) { //less if (a.tv_sec < b.tv_sec) return (-1); //greater else if (a.tv_sec > b.tv_sec) return (1); //less else if (a.tv_nsec < b.tv_nsec) return (-1); //greater else if (a.tv_nsec > b.tv_nsec) return (1); //equal else return (0); } /** * functor to return all fired events * @param a * @return */ inline static bool eventFired(const pollfd& a) { return (a.revents == 0 ? false : true); } /** * functor to help find the items that do not need dispatching * @param a * @return */ inline static bool noDispatching(const sh_poll_s& a) { //remove from list of there is no checkCB if (!a.checkCB) return (true); return (!a.checkCB->Call(a.handle, a.userData)); } /** * checks if dispatching is already finished * @param a * @return */ inline static bool dispatchingFinished(const sh_poll_s& a) { //remove from list of there is no dispatchCB if (!a.dispatchCB) return (true); return (!a.dispatchCB->Call(a.handle, a.userData)); } class CAmShCopyPollfd //!< functor to copy filedescriptors into the poll array { private: mListPollfd_t& mArray; public: CAmShCopyPollfd(mListPollfd_t& dest) : mArray(dest) { } void operator()(const sh_poll_s& row) { pollfd temp = row.pollfdValue; temp.revents = 0; mArray.push_back(temp); } }; class CAmShCallFire //!< functor to call the firecallbacks { public: CAmShCallFire() { } ; void operator()(sh_poll_s& row) { row.firedCB->Call(row.pollfdValue, row.handle, row.userData); } }; class CAmShCallPrep //!< functor to call the preparation callbacks { public: CAmShCallPrep() { } ; void operator()(sh_poll_s& row) { if (row.prepareCB) row.prepareCB->Call(row.handle, row.userData); } }; class CAmShCallTimer //!Call(row.handle, row.userData); } }; class CAmShCountdownUp //! mListTimer; //! mListActiveTimer; //!::iterator it(mListActiveTimer.begin()); // for(;it!=mListActiveTimer.end();++it) // { // std::cout<< "Handle " << it->handle << "sec " << it->countdown.tv_sec << "nsec " << it->countdown.tv_nsec< class TAmShTimerCallBack: public IAmShTimerCallBack { private: TClass* mInstance; void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData); public: TAmShTimerCallBack(TClass* instance, void (TClass::*function)(sh_timerHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) { } ; virtual void Call(sh_timerHandle_t handle, void* userData) { (*mInstance.*mFunction)(handle, userData); } }; /** * template for a callback */ template class TAmShPollPrepare: public IAmShPollPrepare { private: TClass* mInstance; void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData); public: TAmShPollPrepare(TClass* instance, void (TClass::*function)(const sh_timerHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) { } ; virtual void Call(const sh_timerHandle_t handle, void* userData) { (*mInstance.*mFunction)(handle, userData); } ; }; /**make private, not public * template for a callback */ template class TAmShPollFired: public IAmShPollFired { private: TClass* mInstance; void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData); public: TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) { } ; virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) { (*mInstance.*mFunction)(pollfd, handle, userData); } ; }; /** * template for a callback */ template class TAmShPollCheck: public IAmShPollCheck { private: TClass* mInstance; bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData); public: TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) { } ; virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); } ; }; /** * template for a callback */ template class TAmShPollDispatch: public IAmShPollDispatch { private: TClass* mInstance; bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData); public: TAmShPollDispatch(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // mFunction(function) { } ; virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); } ; }; } /* namespace am */ #endif /* SOCKETHANDLER_H_ */