1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sensors-impl/Sensors.h"
#include <aidl/android/hardware/common/fmq/SynchronizedReadWrite.h>
using ::aidl::android::hardware::common::fmq::MQDescriptor;
using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
using ::aidl::android::hardware::sensors::Event;
using ::aidl::android::hardware::sensors::ISensors;
using ::aidl::android::hardware::sensors::ISensorsCallback;
using ::aidl::android::hardware::sensors::SensorInfo;
using ::ndk::ScopedAStatus;
namespace aidl {
namespace android {
namespace hardware {
namespace sensors {
ScopedAStatus Sensors::activate(int32_t in_sensorHandle, bool in_enabled) {
auto sensor = mSensors.find(in_sensorHandle);
if (sensor != mSensors.end()) {
sensor->second->activate(in_enabled);
return ScopedAStatus::ok();
}
return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
ScopedAStatus Sensors::batch(int32_t in_sensorHandle, int64_t in_samplingPeriodNs,
int64_t /* in_maxReportLatencyNs */) {
auto sensor = mSensors.find(in_sensorHandle);
if (sensor != mSensors.end()) {
sensor->second->batch(in_samplingPeriodNs);
return ScopedAStatus::ok();
}
return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
ScopedAStatus Sensors::configDirectReport(int32_t /* in_sensorHandle */,
int32_t /* in_channelHandle */,
ISensors::RateLevel /* in_rate */,
int32_t* _aidl_return) {
*_aidl_return = EX_UNSUPPORTED_OPERATION;
return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
ScopedAStatus Sensors::flush(int32_t in_sensorHandle) {
auto sensor = mSensors.find(in_sensorHandle);
if (sensor != mSensors.end()) {
return sensor->second->flush();
}
return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
ScopedAStatus Sensors::getSensorsList(std::vector<SensorInfo>* _aidl_return) {
for (const auto& sensor : mSensors) {
_aidl_return->push_back(sensor.second->getSensorInfo());
}
return ScopedAStatus::ok();
}
ScopedAStatus Sensors::initialize(
const MQDescriptor<Event, SynchronizedReadWrite>& in_eventQueueDescriptor,
const MQDescriptor<int32_t, SynchronizedReadWrite>& in_wakeLockDescriptor,
const std::shared_ptr<::aidl::android::hardware::sensors::ISensorsCallback>&
in_sensorsCallback) {
ScopedAStatus result = ScopedAStatus::ok();
mEventQueue = std::make_unique<AidlMessageQueue<Event, SynchronizedReadWrite>>(
in_eventQueueDescriptor, true /* resetPointers */);
// Ensure that all sensors are disabled.
for (auto sensor : mSensors) {
sensor.second->activate(false);
}
// Stop the Wake Lock thread if it is currently running
if (mReadWakeLockQueueRun.load()) {
mReadWakeLockQueueRun = false;
mWakeLockThread.join();
}
// Save a reference to the callback
mCallback = in_sensorsCallback;
// Ensure that any existing EventFlag is properly deleted
deleteEventFlag();
// Create the EventFlag that is used to signal to the framework that sensor events have been
// written to the Event FMQ
if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
// Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
// events have been successfully read and handled by the framework.
mWakeLockQueue = std::make_unique<AidlMessageQueue<int32_t, SynchronizedReadWrite>>(
in_wakeLockDescriptor, true /* resetPointers */);
if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
// Start the thread to read events from the Wake Lock FMQ
mReadWakeLockQueueRun = true;
mWakeLockThread = std::thread(startReadWakeLockThread, this);
return result;
}
ScopedAStatus Sensors::injectSensorData(const Event& in_event) {
auto sensor = mSensors.find(in_event.sensorHandle);
if (sensor != mSensors.end()) {
return sensor->second->injectEvent(in_event);
}
return ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(ERROR_BAD_VALUE));
}
ScopedAStatus Sensors::registerDirectChannel(const ISensors::SharedMemInfo& /* in_mem */,
int32_t* _aidl_return) {
*_aidl_return = EX_UNSUPPORTED_OPERATION;
return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
ScopedAStatus Sensors::setOperationMode(OperationMode in_mode) {
for (auto sensor : mSensors) {
sensor.second->setOperationMode(in_mode);
}
return ScopedAStatus::ok();
}
ScopedAStatus Sensors::unregisterDirectChannel(int32_t /* in_channelHandle */) {
return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
} // namespace sensors
} // namespace hardware
} // namespace android
} // namespace aidl
|