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 #define LOG_TAG "goog_sensor_environment"
18
19 #include "goog_sensor_environment.h"
20
21 #include "utils/Errors.h"
22 #include "utils/Log.h"
23 #include "utils/RefBase.h"
24
25 namespace android {
26 namespace camera_sensor_listener {
27 namespace {
28
29 using ::android::frameworks::sensorservice::V1_0::ISensorManager;
30 using ::android::frameworks::sensorservice::V1_0::Result;
31 using ::android::hardware::sensors::V1_0::SensorInfo;
32 using ::android::hardware::sensors::V1_0::SensorType;
33
GetHalSensorType(EnvironmentSensorType sensor_type)34 SensorType GetHalSensorType(EnvironmentSensorType sensor_type) {
35 switch (sensor_type) {
36 case EnvironmentSensorType::DEVICE_ORIENTATION:
37 return SensorType::DEVICE_ORIENTATION;
38 case EnvironmentSensorType::LIGHT:
39 return SensorType::LIGHT;
40 case EnvironmentSensorType::PROXIMITY:
41 return SensorType::PROXIMITY;
42 default:
43 LOG_FATAL("Unknown sensor type %d", static_cast<int>(sensor_type));
44 return SensorType::DEVICE_ORIENTATION;
45 }
46 }
47
48 } // namespace
49
GoogSensorEnvironment(EnvironmentSensorType environment_sensor_type,size_t event_queue_size)50 GoogSensorEnvironment::GoogSensorEnvironment(
51 EnvironmentSensorType environment_sensor_type, size_t event_queue_size)
52 : GoogSensorWrapper(event_queue_size),
53 environment_sensor_type_(environment_sensor_type) {
54 ALOGD("%s %d create sensor %s", __func__, __LINE__,
55 GetSensorName(environment_sensor_type));
56 }
57
~GoogSensorEnvironment()58 GoogSensorEnvironment::~GoogSensorEnvironment() {
59 Disable();
60 ALOGD("%s %d destroy sensor %s", __func__, __LINE__, GetSensorName());
61 }
62
Create(EnvironmentSensorType environment_sensor_type,size_t event_queue_size)63 sp<GoogSensorEnvironment> GoogSensorEnvironment::Create(
64 EnvironmentSensorType environment_sensor_type, size_t event_queue_size) {
65 // Check sensor_type validity.
66 int environment_sensor_type_index = static_cast<int>(environment_sensor_type);
67 if (environment_sensor_type_index >=
68 static_cast<int>(EnvironmentSensorType::TOTAL_NUM)) {
69 ALOGE("%s %d unsupported environment sensor type %d", __func__, __LINE__,
70 environment_sensor_type_index);
71 return nullptr;
72 }
73
74 // Create sensor.
75 sp<GoogSensorEnvironment> sensor_ptr =
76 new GoogSensorEnvironment(environment_sensor_type, event_queue_size);
77 if (sensor_ptr == nullptr) {
78 ALOGE("%s %d failed to create GoogSensorEnvironment for %s", __func__,
79 __LINE__, GetSensorName(environment_sensor_type));
80 } else {
81 // Enable sensor.
82 status_t result = sensor_ptr->Enable();
83 if (result != 0) {
84 ALOGE("%s %d failed to enable GoogSensorEnvironment for %s", __func__,
85 __LINE__, sensor_ptr->GetSensorName());
86 } else {
87 ALOGD("%s %d successfully enabled GoogSensorEnvironment for %s", __func__,
88 __LINE__, sensor_ptr->GetSensorName());
89 }
90 }
91 return sensor_ptr;
92 }
93
GetLatestNSensorEvents(int num_sample,std::vector<int64_t> * event_timestamps,std::vector<float> * event_data,std::vector<int64_t> * event_arrival_timestamps) const94 void GoogSensorEnvironment::GetLatestNSensorEvents(
95 int num_sample, std::vector<int64_t>* event_timestamps,
96 std::vector<float>* event_data,
97 std::vector<int64_t>* event_arrival_timestamps) const {
98 event_timestamps->clear();
99 event_data->clear();
100 event_arrival_timestamps->clear();
101
102 if (num_sample < 0) {
103 return;
104 }
105 std::lock_guard<std::mutex> l(event_buffer_lock_);
106 int start_index =
107 std::max(0, static_cast<int>(event_buffer_.size()) - num_sample);
108 auto event = event_buffer_.begin();
109 std::advance(event, start_index);
110 for (; event != event_buffer_.end(); ++event) {
111 event_timestamps->push_back(event->sensor_event.timestamp);
112 event_data->push_back(event->sensor_event.u.scalar);
113 event_arrival_timestamps->push_back(event->event_arrival_time_ns);
114 if (event_arrival_timestamps->size() >= num_sample) {
115 break;
116 }
117 }
118 }
119
GetSensorHandle()120 int32_t GoogSensorEnvironment::GetSensorHandle() {
121 sp<ISensorManager> manager = ISensorManager::getService();
122 if (manager == nullptr) {
123 ALOGE("%s %d Cannot get ISensorManager for sensor %s", __func__, __LINE__,
124 GetSensorName());
125 return -1;
126 }
127 bool found = false;
128 SensorInfo sensor;
129 manager->getDefaultSensor(GetHalSensorType(environment_sensor_type_),
130 [&sensor, &found](const auto& info, auto result) {
131 if (result != Result::OK) {
132 ALOGE("%s %d Cannot find default sensor",
133 __func__, __LINE__);
134 return;
135 }
136 sensor = info;
137 found = true;
138 });
139
140 if (found) {
141 ALOGD("%s %d handle for %s is found.", __func__, __LINE__, GetSensorName());
142 } else {
143 ALOGE("%s %d handle for %s is not found!", __func__, __LINE__,
144 GetSensorName());
145 }
146 return found ? sensor.sensorHandle : -1;
147 }
148
GetSensorName(EnvironmentSensorType sensor_type)149 const char* GoogSensorEnvironment::GetSensorName(
150 EnvironmentSensorType sensor_type) {
151 static constexpr const char* kSensorNames[] = {"device_orientation", "light",
152 "proximity"};
153 return kSensorNames[static_cast<int>(sensor_type)];
154 }
155
156 } // namespace camera_sensor_listener
157 } // namespace android