1 /*
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 
23 namespace android {
24 namespace automotive {
25 namespace evs {
26 namespace support {
27 
28 using namespace ::android::hardware::automotive::evs::V1_0;
29 using ::android::hardware::hidl_vec;
30 
31 vector<string> Utils::sCameraIds;
32 
getRearViewCameraIds()33 vector<string> Utils::getRearViewCameraIds() {
34     // If we already get the camera list, re-use it.
35     if (!sCameraIds.empty()) {
36         return sCameraIds;
37     }
38 
39     const char* evsServiceName = "default";
40 
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.");
45         return vector<string>();
46     }
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);
52         return vector<string>();
53     }
54 
55     // static variable cannot be passed into capture, so we create a local
56     // variable instead.
57     vector<string> cameraIds;
58     ALOGD("Requesting camera list");
59     evs->getCameraList([&config, &cameraIds](hidl_vec<CameraDesc> cameraList) {
60         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());
72                         cameraIds.emplace_back(cam.cameraId);
73                     }
74                 }
75             }
76         }
77     });
78     sCameraIds = cameraIds;
79     return sCameraIds;
80 }
81 
getDefaultRearViewCameraId()82 string Utils::getDefaultRearViewCameraId() {
83     auto list = getRearViewCameraIds();
84     if (!list.empty()) {
85         return list[0];
86     } else {
87         return string();
88     }
89 }
90 
91 }  // namespace support
92 }  // namespace evs
93 }  // namespace automotive
94 }  // namespace android
95