| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_H_ | |
| 6 #define SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_H_ | |
| 7 | |
| 8 #include "device/base/synchronization/one_writer_seqlock.h" | |
| 9 #include "services/device/public/interfaces/sensor.mojom.h" | |
| 10 | |
| 11 namespace device { | |
| 12 | |
| 13 // This class is guarantied to have a fixed size of 64 bits on every platform. | |
| 14 // It is introduce to simplify sensors shared buffer memory calculation. | |
| 15 template <typename Data> | |
| 16 class SensorReadingField { | |
| 17 public: | |
| 18 static_assert(sizeof(Data) <= sizeof(int64_t), | |
| 19 "The field size must be <= 64 bits."); | |
| 20 SensorReadingField() = default; | |
| 21 SensorReadingField(Data value) { storage_.value = value; } | |
| 22 SensorReadingField& operator=(Data value) { | |
| 23 storage_.value = value; | |
| 24 return *this; | |
| 25 } | |
| 26 Data& value() { return storage_.value; } | |
| 27 const Data& value() const { return storage_.value; } | |
| 28 | |
| 29 operator Data() const { return storage_.value; } | |
| 30 | |
| 31 private: | |
| 32 union Storage { | |
| 33 int64_t unused; | |
| 34 Data value; | |
| 35 Storage() { new (&value) Data(); } | |
| 36 ~Storage() { value.~Data(); } | |
| 37 }; | |
| 38 Storage storage_; | |
| 39 }; | |
| 40 | |
| 41 // This structure represents sensor reading data: timestamp and 4 values. | |
| 42 struct SensorReading { | |
| 43 SensorReading(); | |
| 44 ~SensorReading(); | |
| 45 SensorReading(const SensorReading& other); | |
| 46 SensorReadingField<double> timestamp; | |
| 47 constexpr static int kValuesCount = 4; | |
| 48 // AMBIENT_LIGHT: | |
| 49 // values[0]: ambient light level in SI lux units. | |
| 50 // | |
| 51 // PROXIMITY: | |
| 52 // values[0]: proximity sensor distance measured in centimeters. | |
| 53 // | |
| 54 // ACCELEROMETER: | |
| 55 // values[0]: acceleration minus Gx on the x-axis. | |
| 56 // values[1]: acceleration minus Gy on the y-axis. | |
| 57 // values[2]: acceleration minus Gz on the z-axis. | |
| 58 // | |
| 59 // LINEAR_ACCELERATION: | |
| 60 // values[0]: acceleration on the x-axis. | |
| 61 // values[1]: acceleration on the y-axis. | |
| 62 // values[2]: acceleration on the z-axis. | |
| 63 // | |
| 64 // GYROSCOPE: | |
| 65 // values[0]: angular speed around the x-axis. | |
| 66 // values[1]: angular speed around the y-axis. | |
| 67 // values[2]: angular speed around the z-axis. | |
| 68 // | |
| 69 // MAGNETOMETER: | |
| 70 // values[0]: ambient magnetic field in the x-axis in micro-Tesla (uT). | |
| 71 // values[1]: ambient magnetic field in the y-axis in micro-Tesla (uT). | |
| 72 // values[2]: ambient magnetic field in the z-axis in micro-Tesla (uT). | |
| 73 // | |
| 74 // PRESSURE: | |
| 75 // values[0]: atmospheric pressure in hPa (millibar). | |
| 76 // | |
| 77 // ABSOLUTE_ORIENTATION: | |
| 78 // values[0]: x value of a quaternion representing the orientation of the | |
| 79 // device in 3D space. | |
| 80 // values[1]: y value of a quaternion representing the orientation of the | |
| 81 // device in 3D space. | |
| 82 // values[2]: z value of a quaternion representing the orientation of the | |
| 83 // device in 3D space. | |
| 84 // values[3]: w value of a quaternion representing the orientation of the | |
| 85 // device in 3D space. | |
| 86 // | |
| 87 // RELATIVE_ORIENTATION: | |
| 88 // (Identical to ABSOLUTE_ORIENTATION except that it doesn't use the | |
| 89 // geomagnetic field.) | |
| 90 // values[0]: x value of a quaternion representing the orientation of the | |
| 91 // device in 3D space. | |
| 92 // values[1]: y value of a quaternion representing the orientation of the | |
| 93 // device in 3D space. | |
| 94 // values[2]: z value of a quaternion representing the orientation of the | |
| 95 // device in 3D space. | |
| 96 // values[3]: w value of a quaternion representing the orientation of the | |
| 97 // device in 3D space. | |
| 98 SensorReadingField<double> values[kValuesCount]; | |
| 99 }; | |
| 100 | |
| 101 // This structure represents sensor reading buffer: sensor reading and seqlock | |
| 102 // for synchronization. | |
| 103 struct SensorReadingSharedBuffer { | |
| 104 SensorReadingSharedBuffer(); | |
| 105 ~SensorReadingSharedBuffer(); | |
| 106 SensorReadingField<OneWriterSeqLock> seqlock; | |
| 107 SensorReading reading; | |
| 108 | |
| 109 // Gets the shared reading buffer offset for the given sensor type. | |
| 110 static uint64_t GetOffset(mojom::SensorType type); | |
| 111 }; | |
| 112 | |
| 113 } // namespace device | |
| 114 | |
| 115 #endif // SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_H_ | |
| OLD | NEW |