• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define LOG_TAG "SensorClient"
2 #include <private/dvr/sensor_client.h>
3 
4 #include <log/log.h>
5 #include <poll.h>
6 
7 #include <pdx/default_transport/client_channel_factory.h>
8 #include <private/dvr/sensor-ipc.h>
9 
10 using android::pdx::Transaction;
11 
12 namespace android {
13 namespace dvr {
14 
SensorClient(int sensor_type)15 SensorClient::SensorClient(int sensor_type)
16     : BASE(pdx::default_transport::ClientChannelFactory::Create(
17           DVR_SENSOR_SERVICE_CLIENT)),
18       sensor_type_(sensor_type) {}
19 
~SensorClient()20 SensorClient::~SensorClient() {}
21 
StartSensor()22 int SensorClient::StartSensor() {
23   Transaction trans{*this};
24   auto status = trans.Send<int>(DVR_SENSOR_START, &sensor_type_,
25                                 sizeof(sensor_type_), nullptr, 0);
26   ALOGE_IF(!status, "startSensor() failed because: %s\n",
27            status.GetErrorMessage().c_str());
28   return ReturnStatusOrError(status);
29 }
30 
StopSensor()31 int SensorClient::StopSensor() {
32   Transaction trans{*this};
33   auto status = trans.Send<int>(DVR_SENSOR_STOP);
34   ALOGE_IF(!status, "stopSensor() failed because: %s\n",
35            status.GetErrorMessage().c_str());
36   return ReturnStatusOrError(status);
37 }
38 
Poll(sensors_event_t * events,int max_events)39 int SensorClient::Poll(sensors_event_t* events, int max_events) {
40   int num_events = 0;
41   struct iovec rvec[] = {
42       {.iov_base = &num_events, .iov_len = sizeof(int)},
43       {.iov_base = events, .iov_len = max_events * sizeof(sensors_event_t)},
44   };
45   Transaction trans{*this};
46   auto status = trans.SendVector<int>(DVR_SENSOR_POLL, nullptr, rvec);
47   ALOGE_IF(!status, "Sensor poll() failed because: %s\n",
48            status.GetErrorMessage().c_str());
49   return !status ? -status.error() : num_events;
50 }
51 
52 }  // namespace dvr
53 }  // namespace android
54 
55 // Entrypoints to simplify using the library when programmatically dynamicly
56 // loading it.
57 // Allows us to call this library without linking it, as, for instance,
58 // when compiling GVR in Google3.
59 // NOTE(segal): It's kind of a hack.
60 
dvrStartSensor(int type)61 extern "C" uint64_t dvrStartSensor(int type) {
62   android::dvr::SensorClient* service =
63       android::dvr::SensorClient::Create(type).release();
64   service->StartSensor();
65   return (uint64_t)service;
66 }
67 
dvrStopSensor(uint64_t service)68 extern "C" void dvrStopSensor(uint64_t service) {
69   android::dvr::SensorClient* iss =
70       reinterpret_cast<android::dvr::SensorClient*>(service);
71   iss->StopSensor();
72   delete iss;
73 }
74 
dvrPollSensor(uint64_t service,int max_count,sensors_event_t * events)75 extern "C" int dvrPollSensor(uint64_t service, int max_count,
76                              sensors_event_t* events) {
77   return reinterpret_cast<android::dvr::SensorClient*>(service)->Poll(
78       events, max_count);
79 }
80