OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ | 5 #ifndef DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ |
6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ | 6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" |
15 #include "device/generic_sensor/public/interfaces/sensor.mojom.h" | 16 #include "device/generic_sensor/public/interfaces/sensor.mojom.h" |
16 #include "mojo/public/cpp/system/buffer.h" | 17 #include "mojo/public/cpp/system/buffer.h" |
17 | 18 |
18 namespace device { | 19 namespace device { |
19 | 20 |
20 class PlatformSensorProvider; | 21 class PlatformSensorProvider; |
21 class PlatformSensorConfiguration; | 22 class PlatformSensorConfiguration; |
22 | 23 |
23 // Base class for the sensors provided by the platform. Concrete instances of | 24 // Base class for the sensors provided by the platform. Concrete instances of |
24 // this class are created by platform specific PlatformSensorProvider. | 25 // this class are created by platform specific PlatformSensorProvider. |
(...skipping 18 matching lines...) Expand all Loading... |
43 bool StartListening(Client* client, | 44 bool StartListening(Client* client, |
44 const PlatformSensorConfiguration& config); | 45 const PlatformSensorConfiguration& config); |
45 bool StopListening(Client* client, const PlatformSensorConfiguration& config); | 46 bool StopListening(Client* client, const PlatformSensorConfiguration& config); |
46 | 47 |
47 void UpdateSensor(); | 48 void UpdateSensor(); |
48 | 49 |
49 void AddClient(Client*); | 50 void AddClient(Client*); |
50 void RemoveClient(Client*); | 51 void RemoveClient(Client*); |
51 | 52 |
52 protected: | 53 protected: |
| 54 using ReadingBuffer = SharedMemorySeqLockBuffer<mojom::SensorReading>; |
| 55 |
| 56 public: |
| 57 static const size_t kReadingBufferSize = sizeof(ReadingBuffer); |
| 58 |
| 59 protected: |
53 virtual ~PlatformSensor(); | 60 virtual ~PlatformSensor(); |
54 PlatformSensor(mojom::SensorType type, | 61 PlatformSensor(mojom::SensorType type, |
55 mojo::ScopedSharedBufferMapping mapping, | 62 mojo::ScopedSharedBufferMapping mapping, |
56 PlatformSensorProvider* provider); | 63 PlatformSensorProvider* provider); |
57 | 64 |
58 using ConfigMap = std::map<Client*, std::list<PlatformSensorConfiguration>>; | 65 using ConfigMap = std::map<Client*, std::list<PlatformSensorConfiguration>>; |
59 | 66 |
60 virtual bool UpdateSensorInternal(const ConfigMap& configurations); | 67 virtual bool UpdateSensorInternal(const ConfigMap& configurations); |
61 virtual bool StartSensor( | 68 virtual bool StartSensor( |
62 const PlatformSensorConfiguration& configuration) = 0; | 69 const PlatformSensorConfiguration& configuration) = 0; |
63 virtual void StopSensor() = 0; | 70 virtual void StopSensor() = 0; |
64 virtual bool CheckSensorConfiguration( | 71 virtual bool CheckSensorConfiguration( |
65 const PlatformSensorConfiguration& configuration) = 0; | 72 const PlatformSensorConfiguration& configuration) = 0; |
66 | 73 |
| 74 // Updates shared buffer with new sensor reading data. |
| 75 // Note: this method is thread-safe. |
| 76 void UpdateSensorReading(const mojom::SensorReading& reading); |
| 77 |
67 void NotifySensorReadingChanged(); | 78 void NotifySensorReadingChanged(); |
68 void NotifySensorError(); | 79 void NotifySensorError(); |
69 | 80 |
70 mojo::ScopedSharedBufferMapping shared_buffer_mapping_; | |
71 | |
72 // For testing purposes. | 81 // For testing purposes. |
73 const ConfigMap& config_map() const { return config_map_; } | 82 const ConfigMap& config_map() const { return config_map_; } |
74 | 83 |
75 private: | 84 private: |
76 friend class base::RefCountedThreadSafe<PlatformSensor>; | 85 friend class base::RefCountedThreadSafe<PlatformSensor>; |
77 | 86 mojo::ScopedSharedBufferMapping shared_buffer_mapping_; |
78 mojom::SensorType type_; | 87 mojom::SensorType type_; |
79 base::ObserverList<Client, true> clients_; | 88 base::ObserverList<Client, true> clients_; |
80 ConfigMap config_map_; | 89 ConfigMap config_map_; |
81 PlatformSensorProvider* provider_; | 90 PlatformSensorProvider* provider_; |
82 base::WeakPtrFactory<PlatformSensor> weak_factory_; | 91 base::WeakPtrFactory<PlatformSensor> weak_factory_; |
83 DISALLOW_COPY_AND_ASSIGN(PlatformSensor); | 92 DISALLOW_COPY_AND_ASSIGN(PlatformSensor); |
84 }; | 93 }; |
85 | 94 |
86 } // namespace device | 95 } // namespace device |
87 | 96 |
88 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ | 97 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ |
OLD | NEW |