blob: 64ee5c5270e232e67f8f86bcc408eb952f12cc44 [file] [log] [blame]
Brian Stack60fcdcf2018-10-16 14:51:34 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
18#define ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
19
Brian Stack897528d2018-10-23 10:38:03 -070020#include "Sensor.h"
21
Brian Stack60fcdcf2018-10-16 14:51:34 -070022#include <android/hardware/sensors/2.0/ISensors.h>
Brian Stack475d4d42018-10-19 15:58:09 -070023#include <fmq/MessageQueue.h>
Brian Stack60fcdcf2018-10-16 14:51:34 -070024#include <hidl/MQDescriptor.h>
25#include <hidl/Status.h>
26
Brian Stack475d4d42018-10-19 15:58:09 -070027#include <memory>
28
Brian Stack60fcdcf2018-10-16 14:51:34 -070029namespace android {
30namespace hardware {
31namespace sensors {
32namespace V2_0 {
33namespace implementation {
34
35using ::android::sp;
Brian Stack475d4d42018-10-19 15:58:09 -070036using ::android::hardware::EventFlag;
Brian Stack60fcdcf2018-10-16 14:51:34 -070037using ::android::hardware::hidl_array;
38using ::android::hardware::hidl_memory;
39using ::android::hardware::hidl_string;
40using ::android::hardware::hidl_vec;
Brian Stack475d4d42018-10-19 15:58:09 -070041using ::android::hardware::MessageQueue;
42using ::android::hardware::MQDescriptor;
Brian Stack60fcdcf2018-10-16 14:51:34 -070043using ::android::hardware::Return;
44using ::android::hardware::Void;
45
46struct Sensors : public ISensors {
47 using Event = ::android::hardware::sensors::V1_0::Event;
48 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
49 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
50 using Result = ::android::hardware::sensors::V1_0::Result;
51 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
52
Brian Stack475d4d42018-10-19 15:58:09 -070053 Sensors();
54 virtual ~Sensors();
55
Brian Stack60fcdcf2018-10-16 14:51:34 -070056 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
57 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
58
59 Return<Result> setOperationMode(OperationMode mode) override;
60
61 Return<Result> activate(int32_t sensorHandle, bool enabled) override;
62
63 Return<Result> initialize(
64 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
65 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
66 const sp<ISensorsCallback>& sensorsCallback) override;
67
68 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
69 int64_t maxReportLatencyNs) override;
70
71 Return<Result> flush(int32_t sensorHandle) override;
72
73 Return<Result> injectSensorData(const Event& event) override;
74
75 Return<void> registerDirectChannel(const SharedMemInfo& mem,
76 registerDirectChannel_cb _hidl_cb) override;
77
78 Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
79
80 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
81 configDirectReport_cb _hidl_cb) override;
Brian Stack475d4d42018-10-19 15:58:09 -070082
83 private:
84 /**
85 * Utility function to delete the Event Flag
86 */
87 void deleteEventFlag();
88
89 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
90 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
91
92 /**
93 * The Event FMQ where sensor events are written
94 */
95 std::unique_ptr<EventMessageQueue> mEventQueue;
96
97 /**
98 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
99 */
100 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
101
102 /**
103 * Event Flag to signal to the framework when sensor events are available to be read
104 */
105 EventFlag* mEventQueueFlag;
106
107 /**
108 * Callback for asynchronous events, such as dynamic sensor connections.
109 */
110 sp<ISensorsCallback> mCallback;
Brian Stack897528d2018-10-23 10:38:03 -0700111
112 /**
113 * A map of the available sensors
114 */
115 std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700116};
117
118} // namespace implementation
119} // namespace V2_0
120} // namespace sensors
121} // namespace hardware
122} // namespace android
123
124#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H