Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 20 | #include "Sensor.h" |
| 21 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 22 | #include <android/hardware/sensors/2.0/ISensors.h> |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 23 | #include <fmq/MessageQueue.h> |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 24 | #include <hidl/MQDescriptor.h> |
| 25 | #include <hidl/Status.h> |
| 26 | |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace sensors { |
| 32 | namespace V2_0 { |
| 33 | namespace implementation { |
| 34 | |
| 35 | using ::android::sp; |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 36 | using ::android::hardware::EventFlag; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 37 | using ::android::hardware::hidl_array; |
| 38 | using ::android::hardware::hidl_memory; |
| 39 | using ::android::hardware::hidl_string; |
| 40 | using ::android::hardware::hidl_vec; |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 41 | using ::android::hardware::MessageQueue; |
| 42 | using ::android::hardware::MQDescriptor; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 43 | using ::android::hardware::Return; |
| 44 | using ::android::hardware::Void; |
| 45 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 46 | struct Sensors : public ISensors, public ISensorsEventCallback { |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 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 Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 53 | Sensors(); |
| 54 | virtual ~Sensors(); |
| 55 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 56 | // 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 Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 82 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 83 | void postEvents(const std::vector<Event>& events) override; |
| 84 | |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 85 | 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 Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * A map of the available sensors |
| 116 | */ |
| 117 | std::map<int32_t, std::shared_ptr<Sensor>> mSensors; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame^] | 118 | |
| 119 | /** |
| 120 | * Lock to protect writes and reads to the FMQs |
| 121 | */ |
| 122 | std::mutex mLock; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 123 | }; |
| 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 |