1 /*
2  * Copyright (C) 2020 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 #include "SensorsV2_1.h"
18 
19 #include "Sensor.h"
20 
21 namespace android {
22 namespace hardware {
23 namespace sensors {
24 namespace V2_1 {
25 namespace implementation {
26 
27 using V2_X::implementation::ISensorsEventCallback;
28 using V2_X::implementation::OnChangeSensor;
29 
30 class HingeAngleSensor : public OnChangeSensor {
31   public:
HingeAngleSensor(int32_t sensorHandle,ISensorsEventCallback * callback)32     HingeAngleSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
33         : OnChangeSensor(callback) {
34         mSensorInfo.sensorHandle = sensorHandle;
35         mSensorInfo.name = "Hinge Angle Sensor";
36         mSensorInfo.vendor = "Vendor String";
37         mSensorInfo.version = 1;
38         mSensorInfo.type = SensorType::HINGE_ANGLE;
39         mSensorInfo.typeAsString = "";
40         mSensorInfo.maxRange = 360.0f;
41         mSensorInfo.resolution = 1.0f;
42         mSensorInfo.power = 0.001f;
43         mSensorInfo.minDelay = 40 * 1000;  // microseconds
44         mSensorInfo.maxDelay = V2_X::implementation::kDefaultMaxDelayUs;
45         mSensorInfo.fifoReservedEventCount = 0;
46         mSensorInfo.fifoMaxEventCount = 0;
47         mSensorInfo.requiredPermission = "";
48         mSensorInfo.flags = static_cast<uint32_t>(V1_0::SensorFlagBits::ON_CHANGE_MODE |
49                                                   V1_0::SensorFlagBits::WAKE_UP |
50                                                   V1_0::SensorFlagBits::DATA_INJECTION);
51     }
52 };
53 
SensorsV2_1()54 SensorsV2_1::SensorsV2_1() {
55     AddSensor<HingeAngleSensor>();
56 }
57 
58 // Methods from ::android::hardware::sensors::V2_1::ISensors follow.
getSensorsList_2_1(ISensors::getSensorsList_2_1_cb _hidl_cb)59 Return<void> SensorsV2_1::getSensorsList_2_1(ISensors::getSensorsList_2_1_cb _hidl_cb) {
60     std::vector<SensorInfo> sensors;
61     for (const auto& sensor : mSensors) {
62         sensors.push_back(sensor.second->getSensorInfo());
63     }
64 
65     // Call the HIDL callback with the SensorInfo
66     _hidl_cb(sensors);
67 
68     return Void();
69 }
70 
initialize_2_1(const::android::hardware::MQDescriptorSync<V2_1::Event> & eventQueueDescriptor,const::android::hardware::MQDescriptorSync<uint32_t> & wakeLockDescriptor,const sp<V2_1::ISensorsCallback> & sensorsCallback)71 Return<Result> SensorsV2_1::initialize_2_1(
72         const ::android::hardware::MQDescriptorSync<V2_1::Event>& eventQueueDescriptor,
73         const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
74         const sp<V2_1::ISensorsCallback>& sensorsCallback) {
75     auto eventQueue = std::make_unique<MessageQueue<V2_1::Event, kSynchronizedReadWrite>>(
76             eventQueueDescriptor, true /* resetPointers */);
77     std::unique_ptr<EventMessageQueueWrapperBase> wrapper =
78             std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue);
79     mCallbackWrapper = new ISensorsCallbackWrapper(sensorsCallback);
80     return initializeBase(wrapper, wakeLockDescriptor, mCallbackWrapper);
81 }
82 
injectSensorData_2_1(const V2_1::Event & event)83 Return<Result> SensorsV2_1::injectSensorData_2_1(const V2_1::Event& event) {
84     return injectSensorData(convertToOldEvent(event));
85 }
86 
87 }  // namespace implementation
88 }  // namespace V2_1
89 }  // namespace sensors
90 }  // namespace hardware
91 }  // namespace android