Ocean
AndroidSensor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_DEVICES_ANDROID_ANDROID_SENSOR_H
9 #define META_OCEAN_DEVICES_ANDROID_ANDROID_SENSOR_H
10 
13 
14 #include "ocean/base/Singleton.h"
15 #include "ocean/base/Thread.h"
16 
17 #include "ocean/devices/Sensor.h"
18 
19 #include <android/sensor.h>
20 
21 namespace Ocean
22 {
23 
24 namespace Devices
25 {
26 
27 namespace Android
28 {
29 
30 /**
31  * This class implements a sensor for the Android library.
32  * The coordinate system of each Android sensor is defined so that the origin of the coordinate system is located in the center of the device.<br>
33  * The x-axis is horizontal and pointing to the right of the device (if the device is held in default orientation).<br>
34  * The y-axis is vertical and pointing to the top of the device.<br>
35  * The z-axis is perpendicular to the screen plane and pointing towards the user (a right handed coordinate system).
36  * @ingroup devicesandroid
37  */
38 class OCEAN_DEVICES_ANDROID_EXPORT AndroidSensor :
39  virtual public AndroidDevice,
40  virtual public Sensor
41 {
42  protected:
43 
44  /**
45  * This class implements the manager for the looper thread.
46  */
47  class LooperManager :
48  public Singleton<LooperManager>,
49  protected Thread
50  {
51  friend class Singleton<LooperManager>;
52 
53  public:
54 
55  /**
56  * Returns the looper of the manager.
57  * @return Looper of the manager, will be valid
58  */
59  ALooper* looper();
60 
61  protected:
62 
63  /**
64  * Creates a new manager object and creates a looper.
65  */
67 
68  /**
69  * Thread run function.
70  * @see Thread::threadRun().
71  */
72  void threadRun() override;
73 
74  protected:
75 
76  /// The looper of this manager.
77  ALooper* looper_ = nullptr;
78  };
79 
80  public:
81 
82  /**
83  * Definition of Android sensor types as defined by the Android API.
84  * The values are defined by the NDK Android API and thus must not changed.
85  */
87  {
88  /// Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.
89  AST_ACCELEROMETER = 1,
90  /// Measures the ambient geomagnetic field for all three physical axes (x, y, z) in microT.
91  AST_MAGNETIC_FIELD = 2,
92  /// Measures degrees of rotation that a device makes around all three physical axes (x, y, z), deprecated since 2.2.
93  AST_ORIENTATION = 3,
94  /// Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).
95  AST_GYROSCOPE = 4,
96  /// Measures the ambient light level (illumination) in lx.
97  AST_LIGHT = 5,
98  /// Measures the ambient air pressure in hPa or mbar.
99  AST_PRESSURE = 6,
100  /// Measures the temperature of the device in degrees Celsius. This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14
101  AST_TEMPERATURE = 7,
102  /// Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear.
103  AST_PROXIMITY = 8,
104  /// Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z).
105  AST_GRAVITY = 9,
106  /// Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.
107  AST_LINEAR_ACCELERATION = 10,
108  /// Measures the orientation of a device by providing the three elements of the device's rotation vector.
109  AST_ROTATION_VECTOR = 11,
110  /// Measures the relative ambient humidity in percent (%).
111  AST_RELATIVE_HUMIDITY = 12,
112  /// Measures the ambient room temperature in degrees Celsius.
113  AST_AMBIENT_TEMPERATURE = 13,
114  AST_MAGNETIC_FIELD_UNCALIBRATED = 14,
115  AST_GAME_ROTATION_VECTOR = 15,
116  AST_GYROSCOPE_UNCALIBRATED = 16,
117  AST_SIGNIFICANT_MOTION = 17,
118  AST_STEP_DETECTOR = 18,
119  AST_STEP_COUNTER = 19,
120  AST_GEOMAGNETIC_ROTATION_VECTOR = 20,
121  AST_HEART_RATE = 21,
122  AST_POSE_6DOF = 28,
123  AST_STATIONARY_DETECT = 29,
124  AST_MOTION_DETECT = 30,
125  AST_HEART_BEAT = 31
126  };
127 
128  public:
129 
130  /**
131  * Starts the device.
132  * @see Device::start().
133  */
134  bool start() override;
135 
136  /**
137  * Pauses the device.
138  * @see Device::pause().
139  */
140  bool pause() override;
141 
142  /**
143  * Stops the device.
144  * @see Device::stop().
145  */
146  bool stop() override;
147 
148  /**
149  * Returns the instance of the sensor manager.
150  * @return The sensor manager instance
151  */
152  static ASensorManager* sensorManager();
153 
154  protected:
155 
156  /**
157  * Creates a new sensor by its name and type.
158  * @param name The name of the sensor
159  * @param type Major and minor device type of the sensor
160  */
161  AndroidSensor(const std::string& name, const DeviceType type);
162 
163  /**
164  * Registers this sensor for the event function.
165  * @param sensorManager The sensor manager which has been used to create the sensor, must be valid
166  */
167  bool registerForEventFunction(ASensorManager* sensorManager);
168 
169  /**
170  * The actual event function of this device.
171  * @return 1 to receive further events, 0 to stop receiving events
172  */
173  virtual int onEventFunction() = 0;
174 
175  private:
176 
177  /**
178  * The static event function.
179  * @param fd File descriptor
180  * @param events The number of events
181  * @param data The event data information, if any
182  * @return 1 to receive further events, 0 to stop receiving events
183  */
184  static inline int onEventFunctionStatic(int fd, int events, void* data);
185 
186  protected:
187 
188  /// The object id of this sensor.
189  ObjectId sensorObjectId_ = invalidObjectId();
190 
191  /// The sensor manager of this device.
192  ASensorManager* sensorManager_ = nullptr;
193 
194  /// The Android NDK sensor of this device.
195  const ASensor* sensor_ = nullptr;
196 
197  /// The Android NDK event queue of this device.
198  ASensorEventQueue* eventQueue_ = nullptr;
199 
200  /// True, if this sensor is started.
201  bool isStarted_ = false;
202 };
203 
204 inline int AndroidSensor::onEventFunctionStatic(int fd, int events, void* data)
205 {
206  if (data == nullptr)
207  {
208  return 0;
209  }
210 
211  AndroidDevice* androidDevice = static_cast<AndroidDevice*>(data);
212 
213  if (androidDevice == nullptr)
214  {
215  return 0;
216  }
217 
218  AndroidSensor* androidSensor = dynamic_cast<AndroidSensor*>(androidDevice);
219  ocean_assert(androidSensor != nullptr);
220 
221  if (androidSensor)
222  {
223  return androidSensor->onEventFunction();
224  }
225 
226  return 0;
227 }
228 
229 }
230 
231 }
232 
233 }
234 
235 #endif // META_OCEAN_DEVICES_ANDROID_ANDROID_SENSOR_H
This class implements a device for the Android library.
Definition: AndroidDevice.h:29
This class implements the manager for the looper thread.
Definition: AndroidSensor.h:50
ALooper * looper()
Returns the looper of the manager.
LooperManager()
Creates a new manager object and creates a looper.
void threadRun() override
Thread run function.
This class implements a sensor for the Android library.
Definition: AndroidSensor.h:41
AndroidSensor(const std::string &name, const DeviceType type)
Creates a new sensor by its name and type.
static ASensorManager * sensorManager()
Returns the instance of the sensor manager.
bool pause() override
Pauses the device.
static int onEventFunctionStatic(int fd, int events, void *data)
The static event function.
Definition: AndroidSensor.h:204
bool start() override
Starts the device.
bool stop() override
Stops the device.
bool registerForEventFunction(ASensorManager *sensorManager)
Registers this sensor for the event function.
AndroidSensorType
Definition of Android sensor types as defined by the Android API.
Definition: AndroidSensor.h:87
virtual int onEventFunction()=0
The actual event function of this device.
Definition of a class holding the major and minor device type.
Definition: devices/Device.h:62
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
This class implements the base class for all sensors.
Definition: Sensor.h:36
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a thread.
Definition: Thread.h:115
const ObjectId invalidObjectId
Definition of an invalid object id.
Definition: rendering/Rendering.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15