#include #include "rilsimulate.h" #include "rilmonitor.h" #include #include "util/boostthread.h" typedef std::map FuncCalls; // this object contains the static callback function, as passed to rilinit. // also, it manages a list of outstanding requests, and returns answers to the correct callers. class rilmonitor_impl { private: FuncCalls _funccalls; boost::mutex _fnmutex; // protects access to _funccalls public: HSRIL _hril; public: rilmonitor_impl(); ~rilmonitor_impl(); // ril framework bool process_request(requesthandler_ptr rq); // registered with RIL_Initialize static void handle_ril_result(long dwCode, HSRESULT hrCmdID, const void *lpData, long cbdata, long dwParam); void HandleResult(long dwCode, HSRESULT hrCmdID, const void *lpData, long cbdata); }; // ... i am attempting to have the update function part of the request, // so i don't need to declare the request as public, and seperatly declare an update fn as private. // // NOTE: c0x++ will support templates of local types, the requesthandler structs can then be moved // inside the request function class request1handler : public requesthandler { private: long &_level; HSRIL _hril; int _param; public: request1handler(long&level, HSRIL hril, int param) : _level(level), _hril(hril), _param(param) { } virtual void handle(long dwCode, const void *data, size_t size) { printf("mon:fromthread - request1handler::handle\n"); _level= *static_cast(data); notify(); } virtual bool request(int& cmdid) { HSRESULT hr= performrequest1(_hril, _param); if (hr<0) return false; cmdid=static_cast(hr); return true; } }; requesthandler_ptr rilmonitor::request_value1(long &level, int param) { requesthandler_ptr rq= requesthandler_ptr(new request1handler(level, _->_hril, param)); if (!_->process_request(rq)) { return requesthandler_ptr(); } return rq; } class request2handler : public requesthandler { private: long &_level; HSRIL _hril; int _p1; int _p2; public: request2handler(long&level, HSRIL hril, int p1, int p2) : _level(level), _hril(hril), _p1(p1), _p2(p2) { } virtual void handle(long dwCode, const void *data, size_t size) { printf("mon:fromthread - request2handler::handle\n"); _level= *static_cast(data); notify(); } virtual bool request(int& cmdid) { HSRESULT hr= performrequest2(_hril, _p1, _p2); if (hr<0) return false; cmdid=static_cast(hr); return true; } }; requesthandler_ptr rilmonitor::request_value2(long &level, int p1, int p2) { requesthandler_ptr rq= requesthandler_ptr(new request2handler(level, _->_hril, p1, p2)); if (!_->process_request(rq)) { return requesthandler_ptr(); } return rq; } // ---------------------------------------------------------------- // implementation // ---------------------------------------------------------------- rilmonitor_impl::rilmonitor_impl() { _hril= rilinit(rilmonitor_impl::handle_ril_result, reinterpret_cast(this)); } rilmonitor_impl::~rilmonitor_impl() { rilend(_hril); } rilmonitor::rilmonitor() { _= new rilmonitor_impl(); } rilmonitor::~rilmonitor() { delete _; _= NULL; } // ril framework bool rilmonitor_impl::process_request(requesthandler_ptr rq) { boost::mutex::scoped_lock lock(_fnmutex); int cmdid; if (!rq->request(cmdid)) return false; _funccalls.insert(FuncCalls::value_type(cmdid, rq)); return true; } void rilmonitor_impl::handle_ril_result(long dwCode, HSRESULT hrCmdID, const void *lpData, long cbdata, long dwParam) { ((rilmonitor_impl*)dwParam)->HandleResult(dwCode, hrCmdID, lpData, cbdata); } void rilmonitor_impl::HandleResult(long dwCode, HSRESULT hrCmdID, const void *lpData, long cbdata) { printf("mon:handle result\n"); requesthandler_ptr rq; { boost::mutex::scoped_lock lock(_fnmutex); FuncCalls::iterator i= _funccalls.find(static_cast(hrCmdID)); if (i==_funccalls.end()) { printf("mon:unknown cmdid %d\n", hrCmdID); return; } rq= (*i).second; _funccalls.erase(i); } printf("mon:handling result for hr=%d\n", hrCmdID); rq->handle(dwCode, lpData, cbdata); }