blob: eb6cfe7eaa9a383e9ee8dfecc16b3c95c7bca688 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_H_
#define SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_H_
#include "device/base/synchronization/one_writer_seqlock.h"
#include "services/device/public/mojom/sensor.mojom.h"
namespace device {
// This class is guaranteed to have a fixed size of 64 bits on every platform.
// It is introduce to simplify sensors shared buffer memory calculation.
template <typename Data>
class SensorReadingField {
public:
static_assert(sizeof(Data) <= sizeof(int64_t),
"The field size must be <= 64 bits.");
SensorReadingField() = default;
SensorReadingField(Data value) { storage_.value = value; }
SensorReadingField& operator=(Data value) {
storage_.value = value;
return *this;
}
Data& value() { return storage_.value; }
const Data& value() const { return storage_.value; }
operator Data() const { return storage_.value; }
private:
union Storage {
int64_t unused;
Data value;
Storage() { new (&value) Data(); }
~Storage() { value.~Data(); }
};
Storage storage_;
};
struct SensorReadingBase {
SensorReadingBase();
~SensorReadingBase();
SensorReadingField<double> timestamp;
};
// Represents raw sensor reading data: timestamp and 4 values.
struct SensorReadingRaw : public SensorReadingBase {
SensorReadingRaw();
~SensorReadingRaw();
constexpr static size_t kValuesCount = 4;
SensorReadingField<double> values[kValuesCount];
};
// Represents a single data value.
struct SensorReadingSingle : public SensorReadingBase {
SensorReadingSingle();
~SensorReadingSingle();
SensorReadingField<double> value;
};
// Represents a vector in 3d coordinate system.
struct SensorReadingXYZ : public SensorReadingBase {
SensorReadingXYZ();
~SensorReadingXYZ();
SensorReadingField<double> x;
SensorReadingField<double> y;
SensorReadingField<double> z;
};
// Represents quaternion.
struct SensorReadingQuat : public SensorReadingXYZ {
SensorReadingQuat();
~SensorReadingQuat();
SensorReadingField<double> w;
};
// A common type to represent sensor reading.
// For every implemented sensor type, the reading is stored as described below:
//
// AMBIENT_LIGHT:
// als.value: ambient light level in SI lux units.
//
// PROXIMITY:
// proximity.value: proximity sensor distance measured in centimeters.
//
// ACCELEROMETER:
// accel.x: acceleration minus Gx on the x-axis in SI meters per second
// squared (m/s^2) units. It corresponds to Accelerometer.x in the W3C
// Accelerometer Specification.
// accel.y: acceleration minus Gy on the y-axis in SI meters per second
// squared (m/s^2) units. It corresponds to Accelerometer.y in the W3C
// Accelerometer Specification.
// accel.z: acceleration minus Gz on the z-axis in SI meters per second
// squared (m/s^2) units. It corresponds to Accelerometer.y in the W3C
// Accelerometer Specification.
//
// LINEAR_ACCELERATION:
// accel.x: acceleration on the x-axis in SI meters per second squared
// (m/s^2) units. It corresponds to LinearAccelerationSensor.x in the W3C
// Accelerometer Specification.
// accel.y: acceleration on the y-axis in SI meters per second squared
// (m/s^2) units. It corresponds to LinearAccelerationSensor.y in the W3C
// Accelerometer Specification.
// accel.z: acceleration on the z-axis in SI meters per second squared
// (m/s^2) units. It corresponds to LinearAccelerationSensor.z in the W3C
// Accelerometer Specification.
//
// GYROSCOPE:
// gyro.x: angular speed around the x-axis in radians/second. It corresponds to
// Gyroscope.x in the W3C Gyroscope Specification.
// gyro.y: angular speed around the y-axis in radians/second. It corresponds to
// Gyroscope.y in the W3C Gyroscope Specification.
// gyro.z: angular speed around the z-axis in radians/second. It corresponds to
// Gyroscope.z in the W3C Gyroscope Specification.
//
// MAGNETOMETER:
// magn.x: ambient magnetic field in the x-axis in micro-Tesla (uT).
// magn.y: ambient magnetic field in the y-axis in micro-Tesla (uT).
// magn.z: ambient magnetic field in the z-axis in micro-Tesla (uT).
//
// PRESSURE:
// pressure.value: atmospheric pressure in hPa (millibar).
//
// ABSOLUTE_ORIENTATION_EULER_ANGLES:
// orientation_euler.x: x-axis angle in degrees representing the orientation of
// the device in 3D space. It corresponds to the beta value in the W3C
// DeviceOrientation Event Specification. This value is in [-180, 180).
// orientation_euler.y: y-axis angle in degrees representing the orientation of
// the device in 3D space. It corresponds to the gamma value in the W3C
// DeviceOrientation Event Specification. This value is in [-90, 90).
// orientation_euler.z: z-axis angle in degrees representing the orientation of
// the device in 3D space. It corresponds to the alpha value in the W3C
// DeviceOrientation Event Specification. This value is in [0, 360).
//
// ABSOLUTE_ORIENTATION_QUATERNION:
// orientation_quat.x: x value of a quaternion representing the orientation of
// the device in 3D space.
// orientation_quat.y: y value of a quaternion representing the orientation of
// the device in 3D space.
// orientation_quat.z: z value of a quaternion representing the orientation of
// the device in 3D space.
// orientation_quat.w: w value of a quaternion representing the orientation of
// the device in 3D space.
//
// RELATIVE_ORIENTATION_EULER_ANGLES:
// (Identical to ABSOLUTE_ORIENTATION_EULER_ANGLES except that it doesn't use
// the geomagnetic field.)
// orientation_euler.x: x-axis angle in degrees representing the orientation of
// the device in 3D space. It corresponds to the beta value in the W3C
// DeviceOrientation Event Specification. This value is in [-180, 180).
// orientation_euler.y: y-axis angle in degrees representing the orientation of
// the device in 3D space. It corresponds to the gamma value in the W3C
// DeviceOrientation Event Specification. This value is in [-90, 90).
// orientation_euler.z: z-axis angle in degrees representing the orientation of
// the device in 3D space. It corresponds to the alpha value in the W3C
// DeviceOrientation Event Specification. This value is in [0, 360).
//
// RELATIVE_ORIENTATION_QUATERNION:
// (Identical to ABSOLUTE_ORIENTATION_QUATERNION except that it doesn't use
// the geomagnetic field.)
// orientation_quat.x: x value of a quaternion representing the orientation of
// the device in 3D space.
// orientation_quat.y: y value of a quaternionrepresenting the orientation of
// the device in 3D space.
// orientation_quat.z: z value of a quaternion representing the orientation of
// the device in 3D space.
// orientation_quat.w: w value of a quaternion representing the orientation of
// the device in 3D space.
union SensorReading {
SensorReadingRaw raw;
SensorReadingSingle als; // AMBIENT_LIGHT
SensorReadingSingle proximity; // PROXIMITY
SensorReadingSingle pressure; // PRESSURE
SensorReadingXYZ accel; // ACCELEROMETER, LINEAR_ACCELERATION
SensorReadingXYZ gyro; // GYROSCOPE
SensorReadingXYZ magn; // MAGNETOMETER
SensorReadingQuat orientation_quat; // ABSOLUTE_ORIENTATION_QUATERNION,
// RELATIVE_ORIENTATION_QUATERNION
SensorReadingXYZ orientation_euler; // ABSOLUTE_ORIENTATION_EULER_ANGLES,
// RELATIVE_ORIENTATION_EULER_ANGLES
double timestamp() const { return raw.timestamp; }
SensorReading();
SensorReading(const SensorReading&);
~SensorReading();
SensorReading& operator=(const SensorReading&);
};
static_assert(sizeof(SensorReading) == sizeof(SensorReadingRaw),
"Check SensorReading size.");
// This structure represents sensor reading buffer: sensor reading and seqlock
// for synchronization.
struct SensorReadingSharedBuffer {
SensorReadingSharedBuffer();
~SensorReadingSharedBuffer();
SensorReadingField<OneWriterSeqLock> seqlock;
SensorReading reading;
// Gets the shared reading buffer offset for the given sensor type.
static uint64_t GetOffset(mojom::SensorType type);
};
} // namespace device
#endif // SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_H_