// vim: ts=4 sw=4 et #include "rilsimulate.h" #include "util/boostthread.h" #include #include "requestqueue.h" #include "waitvariable.h" // this contains the 'low level' ril interface. // like ril.dll + rilgsm.dll // struct rilhandle; enum { RIL_STOP, RIL_REQUEST1, RIL_REQUEST2, }; // this is the object passed in a 'HSRIL' handle. struct rilhandle { rilhandle(pfn_handle_ril_result fn, long param); pfn_handle_ril_result callback; long param; requestqueue q; waitvariable started; waitvariable stopped; boost::thread thrd; int add_request(int req) { return q.add(req); } void get_request(int &id, int &req) { q.get(id, req); } }; // this is the thread object, as passed to boost::thread. // the construction, with pointer to rilhandle, is because boost::thread // makes a copy of the object. // struct rilthread { rilhandle *_h; rilthread(rilhandle*h) : _h(h) { } void operator()() { printf("rilthread %p - notifying start\n", _h); _h->started.set(true); bool stopflag= false; while (!stopflag) { printf("rilthread: waiting for request\n"); int id, req; _h->get_request(id, req); printf("rilthread: processing request(%d,%d)\n", id, req); switch(req) { case RIL_STOP: stopflag=true; break; case RIL_REQUEST1: { long l= 999; _h->callback(0, id, static_cast(&l), sizeof(l), _h->param); } break; case RIL_REQUEST2: { long l= 666; _h->callback(0, id, static_cast(&l), sizeof(l), _h->param); } break; default: printf("rilthread: unknown request\n"); } } printf("rilthread stopped - notifying stop\n"); _h->stopped.set(true); } }; rilhandle::rilhandle(pfn_handle_ril_result fn, long param) : callback(fn), param(param), started(false), stopped(false), thrd(rilthread(this)) { } // these functions are the public interface, equivalent to the // public interface in ril.dll HSRESULT performrequest1(HSRIL hRil, int p1) { rilhandle *h= static_cast(hRil); return h->add_request(RIL_REQUEST1); } HSRESULT performrequest2(HSRIL hRil, int p1, int p2) { rilhandle *h= static_cast(hRil); return h->add_request(RIL_REQUEST2); } HSRIL rilinit(pfn_handle_ril_result fn, long param) { rilhandle *h= new rilhandle(fn, param); printf("handle created, waiting for threadstart\n"); h->started.wait_equal(true); printf("thread started\n"); return static_cast(h); } void rilend(HSRIL hRil) { rilhandle *h= static_cast(hRil); h->add_request(RIL_STOP); printf("waiting for thread end\n"); h->stopped.wait_equal(true); printf("deleting handle\n"); delete h; printf("ril ended\n"); }