blob: ae6e792dbe90781793376410e67809f83e411595 [file] [log] [blame]
Haoxiang Lid2454a52019-06-18 13:23:12 -07001/*
2 * Copyright (C) 2019 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#include <android/hardware/automotive/evs/1.0/IEvsEnumerator.h>
17#include <hidl/HidlTransportSupport.h>
18#include <log/log.h>
19
20#include "ConfigManager.h"
21#include "Utils.h"
22
23namespace android {
24namespace automotive {
25namespace evs {
26namespace support {
27
28using namespace ::android::hardware::automotive::evs::V1_0;
29using ::android::hardware::hidl_vec;
Haoxiang Lid2454a52019-06-18 13:23:12 -070030
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070031vector<string> Utils::sCameraIds;
32
33vector<string> Utils::getRearViewCameraIds() {
34 // If we already get the camera list, re-use it.
35 if (!sCameraIds.empty()) {
36 return sCameraIds;
37 }
38
Changyeon Jo3aa79a12020-02-05 17:09:36 -080039 const char* evsServiceName = "default";
Haoxiang Lid2454a52019-06-18 13:23:12 -070040
41 // Load our configuration information
42 ConfigManager config;
43 if (!config.initialize("/system/etc/automotive/evs_support_lib/camera_config.json")) {
44 ALOGE("Missing or improper configuration for the EVS application. Exiting.");
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070045 return vector<string>();
Haoxiang Lid2454a52019-06-18 13:23:12 -070046 }
47
48 ALOGI("Acquiring EVS Enumerator");
49 sp<IEvsEnumerator> evs = IEvsEnumerator::getService(evsServiceName);
50 if (evs.get() == nullptr) {
51 ALOGE("getService(%s) returned NULL. Exiting.", evsServiceName);
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070052 return vector<string>();
Haoxiang Lid2454a52019-06-18 13:23:12 -070053 }
54
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070055 // static variable cannot be passed into capture, so we create a local
56 // variable instead.
57 vector<string> cameraIds;
Haoxiang Lid2454a52019-06-18 13:23:12 -070058 ALOGD("Requesting camera list");
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070059 evs->getCameraList([&config, &cameraIds](hidl_vec<CameraDesc> cameraList) {
Haoxiang Lid2454a52019-06-18 13:23:12 -070060 ALOGI("Camera list callback received %zu cameras", cameraList.size());
61 for (auto&& cam : cameraList) {
62 ALOGD("Found camera %s", cam.cameraId.c_str());
63
64 // If there are more than one rear-view cameras, return the first
65 // one.
66 for (auto&& info : config.getCameras()) {
67 if (cam.cameraId == info.cameraId) {
68 // We found a match!
69 if (info.function.find("reverse") != std::string::npos) {
70 ALOGD("Camera %s is matched with reverse state",
71 cam.cameraId.c_str());
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070072 cameraIds.emplace_back(cam.cameraId);
Haoxiang Lid2454a52019-06-18 13:23:12 -070073 }
74 }
75 }
76 }
77 });
Haoxiang Li30aa0bc2019-08-30 13:24:59 -070078 sCameraIds = cameraIds;
79 return sCameraIds;
80}
81
82string Utils::getDefaultRearViewCameraId() {
83 auto list = getRearViewCameraIds();
84 if (!list.empty()) {
85 return list[0];
86 } else {
87 return string();
88 }
Haoxiang Lid2454a52019-06-18 13:23:12 -070089}
90
91} // namespace support
92} // namespace evs
93} // namespace automotive
94} // namespace android