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