blob: f543935c0195aac95d65f8aa6d1ada44b98f64e9 [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
Brian Stack237abc62018-10-23 11:09:59 -070046struct Sensors : public ISensors, public ISensorsEventCallback {
Brian Stack60fcdcf2018-10-16 14:51:34 -070047 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
Brian Stack237abc62018-10-23 11:09:59 -070083 void postEvents(const std::vector<Event>& events) override;
84
Brian Stack475d4d42018-10-19 15:58:09 -070085 private:
86 /**
87 * Utility function to delete the Event Flag
88 */
89 void deleteEventFlag();
90
91 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
92 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
93
94 /**
95 * The Event FMQ where sensor events are written
96 */
97 std::unique_ptr<EventMessageQueue> mEventQueue;
98
99 /**
100 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
101 */
102 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
103
104 /**
105 * Event Flag to signal to the framework when sensor events are available to be read
106 */
107 EventFlag* mEventQueueFlag;
108
109 /**
110 * Callback for asynchronous events, such as dynamic sensor connections.
111 */
112 sp<ISensorsCallback> mCallback;
Brian Stack897528d2018-10-23 10:38:03 -0700113
114 /**
115 * A map of the available sensors
116 */
117 std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
Brian Stack237abc62018-10-23 11:09:59 -0700118
119 /**
120 * Lock to protect writes and reads to the FMQs
121 */
122 std::mutex mLock;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700123};
124
125} // namespace implementation
126} // namespace V2_0
127} // namespace sensors
128} // namespace hardware
129} // namespace android
130
131#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H