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     }
50 };
51 
SensorsV2_1()52 SensorsV2_1::SensorsV2_1() {
53     AddSensor<HingeAngleSensor>();
54 }
55 
56 // Methods from ::android::hardware::sensors::V2_1::ISensors follow.
getSensorsList_2_1(ISensors::getSensorsList_2_1_cb _hidl_cb)57 Return<void> SensorsV2_1::getSensorsList_2_1(ISensors::getSensorsList_2_1_cb _hidl_cb) {
58     std::vector<SensorInfo> sensors;
59     for (const auto& sensor : mSensors) {
60         sensors.push_back(sensor.second->getSensorInfo());
61     }
62 
63     // Call the HIDL callback with the SensorInfo
64     _hidl_cb(sensors);
65 
66     return Void();
67 }
68 
initialize_2_1(const::android::hardware::MQDescriptorSync<V2_1::Event> & eventQueueDescriptor,const::android::hardware::MQDescriptorSync<uint32_t> & wakeLockDescriptor,const sp<V2_1::ISensorsCallback> & sensorsCallback)69 Return<Result> SensorsV2_1::initialize_2_1(
70         const ::android::hardware::MQDescriptorSync<V2_1::Event>& eventQueueDescriptor,
71         const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
72         const sp<V2_1::ISensorsCallback>& sensorsCallback) {
73     auto eventQueue = std::make_unique<MessageQueue<V2_1::Event, kSynchronizedReadWrite>>(
74             eventQueueDescriptor, true /* resetPointers */);
75     std::unique_ptr<EventMessageQueueWrapperBase> wrapper =
76             std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue);
77     mCallbackWrapper = new ISensorsCallbackWrapper(sensorsCallback);
78     return initializeBase(wrapper, wakeLockDescriptor, mCallbackWrapper);
79 }
80 
injectSensorData_2_1(const V2_1::Event & event)81 Return<Result> SensorsV2_1::injectSensorData_2_1(const V2_1::Event& event) {
82     return injectSensorData(convertToOldEvent(event));
83 }
84 
85 }  // namespace implementation
86 }  // namespace V2_1
87 }  // namespace sensors
88 }  // namespace hardware
89 }  // namespace android