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 DEVICE_GENERIC_SENSOR_PUBLIC_CPP_SENSOR_READING_H_ |
| 6 #define DEVICE_GENERIC_SENSOR_PUBLIC_CPP_SENSOR_READING_H_ |
| 7 |
| 8 #include "device/base/synchronization/one_writer_seqlock.h" |
| 9 #include "device/generic_sensor/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 operator Data() const { return storage_.value; } |
| 28 |
| 29 private: |
| 30 union Storage { |
| 31 int64_t unused; |
| 32 Data value; |
| 33 Storage() { new (&value) Data(); } |
| 34 ~Storage() { value.~Data(); } |
| 35 }; |
| 36 Storage storage_; |
| 37 }; |
| 38 |
| 39 // This structure represents sensor reading data: timestamp and 3 values. |
| 40 struct SensorReading { |
| 41 SensorReading(); |
| 42 ~SensorReading(); |
| 43 SensorReading(const SensorReading& other); |
| 44 SensorReadingField<double> timestamp; |
| 45 SensorReadingField<double> values[3]; |
| 46 }; |
| 47 |
| 48 // This structure represents sensor reading buffer: sensor reading and seqlock |
| 49 // for synchronization. |
| 50 struct SensorReadingSharedBuffer { |
| 51 SensorReadingSharedBuffer(); |
| 52 ~SensorReadingSharedBuffer(); |
| 53 SensorReadingField<OneWriterSeqLock> seqlock; |
| 54 SensorReading reading; |
| 55 |
| 56 // Gets the shared reading buffer offset for the given sensor type. |
| 57 static uint64_t GetOffset(mojom::SensorType type); |
| 58 }; |
| 59 |
| 60 } // namespace device |
| 61 |
| 62 #endif // DEVICE_GENERIC_SENSOR_PUBLIC_CPP_SENSOR_READING_H_ |
OLD | NEW |