Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 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 | |
| 17 | #define LOG_TAG "android.hardware.automotive.evs@1.1-service" |
| 18 | |
| 19 | #include "EvsEnumerator.h" |
| 20 | #include "EvsCamera.h" |
| 21 | #include "EvsDisplay.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace automotive { |
| 26 | namespace evs { |
| 27 | namespace V1_1 { |
| 28 | namespace implementation { |
| 29 | |
| 30 | |
| 31 | // NOTE: All members values are static so that all clients operate on the same state |
| 32 | // That is to say, this is effectively a singleton despite the fact that HIDL |
| 33 | // constructs a new instance for each client. |
| 34 | std::list<EvsEnumerator::CameraRecord> EvsEnumerator::sCameraList; |
| 35 | wp<EvsDisplay> EvsEnumerator::sActiveDisplay; |
| 36 | |
| 37 | |
| 38 | EvsEnumerator::EvsEnumerator() { |
| 39 | ALOGD("EvsEnumerator created"); |
| 40 | |
| 41 | // Add sample camera data to our list of cameras |
| 42 | // In a real driver, this would be expected to can the available hardware |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 43 | sCameraList.emplace_back(EvsCamera::kCameraName_Backup); |
| 44 | sCameraList.emplace_back("LaneView"); |
| 45 | sCameraList.emplace_back("right turn"); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | // Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow. |
| 50 | Return<void> EvsEnumerator::getCameraList(getCameraList_cb _hidl_cb) { |
| 51 | ALOGD("getCameraList"); |
| 52 | |
| 53 | const unsigned numCameras = sCameraList.size(); |
| 54 | |
| 55 | // Build up a packed array of CameraDesc for return |
| 56 | // NOTE: Only has to live until the callback returns |
| 57 | std::vector<CameraDesc_1_0> descriptions; |
| 58 | descriptions.reserve(numCameras); |
| 59 | for (const auto& cam : sCameraList) { |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 60 | descriptions.push_back( cam.desc ); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Encapsulate our camera descriptions in the HIDL vec type |
| 64 | hidl_vec<CameraDesc_1_0> hidlCameras(descriptions); |
| 65 | |
| 66 | // Send back the results |
| 67 | ALOGD("reporting %zu cameras available", hidlCameras.size()); |
| 68 | _hidl_cb(hidlCameras); |
| 69 | |
| 70 | // HIDL convention says we return Void if we sent our result back via callback |
| 71 | return Void(); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | Return<sp<IEvsCamera_1_0>> EvsEnumerator::openCamera(const hidl_string& cameraId) { |
| 76 | ALOGD("openCamera"); |
| 77 | |
| 78 | // Find the named camera |
| 79 | CameraRecord *pRecord = nullptr; |
| 80 | for (auto &&cam : sCameraList) { |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 81 | if (cam.desc.cameraId == cameraId) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 82 | // Found a match! |
| 83 | pRecord = &cam; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Is this a recognized camera id? |
| 89 | if (!pRecord) { |
| 90 | ALOGE("Requested camera %s not found", cameraId.c_str()); |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | // Has this camera already been instantiated by another caller? |
| 95 | sp<EvsCamera> pActiveCamera = pRecord->activeInstance.promote(); |
| 96 | if (pActiveCamera != nullptr) { |
| 97 | ALOGW("Killing previous camera because of new caller"); |
| 98 | closeCamera(pActiveCamera); |
| 99 | } |
| 100 | |
| 101 | // Construct a camera instance for the caller |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 102 | pActiveCamera = new EvsCamera(cameraId.c_str()); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 103 | pRecord->activeInstance = pActiveCamera; |
| 104 | if (pActiveCamera == nullptr) { |
| 105 | ALOGE("Failed to allocate new EvsCamera object for %s\n", cameraId.c_str()); |
| 106 | } |
| 107 | |
| 108 | return pActiveCamera; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | Return<void> EvsEnumerator::closeCamera(const ::android::sp<IEvsCamera_1_0>& pCamera) { |
| 113 | ALOGD("closeCamera"); |
| 114 | |
| 115 | auto pCamera_1_1 = IEvsCamera_1_1::castFrom(pCamera).withDefault(nullptr); |
| 116 | if (pCamera_1_1 == nullptr) { |
| 117 | ALOGE("Ignoring call to closeCamera with null camera ptr"); |
| 118 | return Void(); |
| 119 | } |
| 120 | |
| 121 | // Get the camera id so we can find it in our list |
| 122 | std::string cameraId; |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 123 | pCamera_1_1->getCameraInfo([&cameraId](CameraDesc desc) { |
| 124 | cameraId = desc.cameraId; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 125 | } |
| 126 | ); |
| 127 | |
| 128 | // Find the named camera |
| 129 | CameraRecord *pRecord = nullptr; |
| 130 | for (auto &&cam : sCameraList) { |
Changyeon Jo | 468cc1d | 2019-10-12 05:18:16 -0700 | [diff] [blame^] | 131 | if (cam.desc.cameraId == cameraId) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 132 | // Found a match! |
| 133 | pRecord = &cam; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Is the display being destroyed actually the one we think is active? |
| 139 | if (!pRecord) { |
| 140 | ALOGE("Asked to close a camera who's name isn't recognized"); |
| 141 | } else { |
| 142 | sp<EvsCamera> pActiveCamera = pRecord->activeInstance.promote(); |
| 143 | |
| 144 | if (pActiveCamera == nullptr) { |
| 145 | ALOGE("Somehow a camera is being destroyed when the enumerator didn't know one existed"); |
| 146 | } else if (pActiveCamera != pCamera_1_1) { |
| 147 | // This can happen if the camera was aggressively reopened, orphaning this previous instance |
| 148 | ALOGW("Ignoring close of previously orphaned camera - why did a client steal?"); |
| 149 | } else { |
| 150 | // Drop the active camera |
| 151 | pActiveCamera->forceShutdown(); |
| 152 | pRecord->activeInstance = nullptr; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return Void(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | Return<sp<IEvsDisplay>> EvsEnumerator::openDisplay() { |
| 161 | ALOGD("openDisplay"); |
| 162 | |
| 163 | // If we already have a display active, then we need to shut it down so we can |
| 164 | // give exclusive access to the new caller. |
| 165 | sp<EvsDisplay> pActiveDisplay = sActiveDisplay.promote(); |
| 166 | if (pActiveDisplay != nullptr) { |
| 167 | ALOGW("Killing previous display because of new caller"); |
| 168 | closeDisplay(pActiveDisplay); |
| 169 | } |
| 170 | |
| 171 | // Create a new display interface and return it |
| 172 | pActiveDisplay = new EvsDisplay(); |
| 173 | sActiveDisplay = pActiveDisplay; |
| 174 | |
| 175 | ALOGD("Returning new EvsDisplay object %p", pActiveDisplay.get()); |
| 176 | return pActiveDisplay; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | Return<void> EvsEnumerator::closeDisplay(const ::android::sp<IEvsDisplay>& pDisplay) { |
| 181 | ALOGD("closeDisplay"); |
| 182 | |
| 183 | // Do we still have a display object we think should be active? |
| 184 | sp<EvsDisplay> pActiveDisplay = sActiveDisplay.promote(); |
| 185 | if (pActiveDisplay == nullptr) { |
| 186 | ALOGE("Somehow a display is being destroyed when the enumerator didn't know one existed"); |
| 187 | } else if (sActiveDisplay != pDisplay) { |
| 188 | ALOGW("Ignoring close of previously orphaned display - why did a client steal?"); |
| 189 | } else { |
| 190 | // Drop the active display |
| 191 | pActiveDisplay->forceShutdown(); |
| 192 | sActiveDisplay = nullptr; |
| 193 | } |
| 194 | |
| 195 | return Void(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | Return<DisplayState> EvsEnumerator::getDisplayState() { |
| 200 | ALOGD("getDisplayState"); |
| 201 | |
| 202 | // Do we still have a display object we think should be active? |
| 203 | sp<IEvsDisplay> pActiveDisplay = sActiveDisplay.promote(); |
| 204 | if (pActiveDisplay != nullptr) { |
| 205 | return pActiveDisplay->getDisplayState(); |
| 206 | } else { |
| 207 | return DisplayState::NOT_OPEN; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 212 | } // namespace implementation |
| 213 | } // namespace V1_1 |
| 214 | } // namespace evs |
| 215 | } // namespace automotive |
| 216 | } // namespace hardware |
| 217 | } // namespace android |