blob: fb9b0ba06e7a06851e66ada1dd75073c9ab9d78b [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * Copyright (C) 2008 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 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#define LOG_TAG "CameraService"
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070018#define ATRACE_TAG ATRACE_TAG_CAMERA
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
Ruben Brunkcc776712015-02-17 20:18:47 -080021#include <algorithm>
22#include <climits>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <stdio.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080024#include <cstring>
25#include <ctime>
26#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027#include <sys/types.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080028#include <inttypes.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <pthread.h>
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031#include <android/hardware/ICamera.h>
32#include <android/hardware/ICameraClient.h>
33
Alex Deymo9c2a2c22016-08-25 11:59:14 -070034#include <android-base/macros.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080035#include <android-base/parseint.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080036#include <binder/ActivityManager.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080037#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryBase.h>
41#include <binder/MemoryHeapBase.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080042#include <binder/PermissionController.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080043#include <binder/ProcessInfoService.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080044#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070046#include <cutils/properties.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080047#include <cutils/misc.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080048#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <hardware/hardware.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070050#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080052#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070054#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055#include <utils/Errors.h>
56#include <utils/Log.h>
57#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080058#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080059#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080060#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070061#include <system/camera_metadata.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080062
Ruben Brunkb2119af2014-05-09 19:57:56 -070063#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070064
65#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070066#include "api1/CameraClient.h"
67#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070068#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070069#include "utils/CameraTraces.h"
Emilian Peevbd8c5032018-02-14 23:05:40 +000070#include "utils/TagMonitor.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070071
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080072namespace {
73 const char* kPermissionServiceName = "permission";
74}; // namespace anonymous
75
Mathias Agopian65ab4712010-07-14 17:59:35 -070076namespace android {
77
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080078using binder::Status;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080079using hardware::ICamera;
80using hardware::ICameraClient;
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -070081using hardware::ICameraServiceProxy;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080082using hardware::ICameraServiceListener;
83using hardware::camera::common::V1_0::CameraDeviceStatus;
84using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080085
Mathias Agopian65ab4712010-07-14 17:59:35 -070086// ----------------------------------------------------------------------------
87// Logging support -- this is for debugging only
88// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070089volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070090
Steve Blockb8a80522011-12-20 16:23:08 +000091#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
92#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
94static void setLogLevel(int level) {
95 android_atomic_write(level, &gLogLevel);
96}
97
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080098// Convenience methods for constructing binder::Status objects for error returns
99
100#define STATUS_ERROR(errorCode, errorString) \
101 binder::Status::fromServiceSpecificError(errorCode, \
102 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
103
104#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
105 binder::Status::fromServiceSpecificError(errorCode, \
106 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
107 __VA_ARGS__))
108
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109// ----------------------------------------------------------------------------
110
Svet Ganova453d0d2018-01-11 15:37:58 -0800111static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA");
112
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800113CameraService::CameraService() :
114 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800115 mNumberOfCameras(0),
Emilian Peevf53f66e2017-04-11 14:29:43 +0100116 mSoundRef(0), mInitialized(false) {
Steve Blockdf64d152012-01-04 20:05:49 +0000117 ALOGI("CameraService started (pid=%d)", getpid());
Ruben Brunkcc776712015-02-17 20:18:47 -0800118 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119}
120
Iliyan Malchev8951a972011-04-14 16:55:59 -0700121void CameraService::onFirstRef()
122{
Ruben Brunkcc776712015-02-17 20:18:47 -0800123 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800124
Iliyan Malchev8951a972011-04-14 16:55:59 -0700125 BnCameraService::onFirstRef();
126
Ruben Brunk99e69712015-05-26 17:25:07 -0700127 // Update battery life tracking if service is restarting
128 BatteryNotifier& notifier(BatteryNotifier::getInstance());
129 notifier.noteResetCamera();
130 notifier.noteResetFlashlight();
131
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800132 status_t res = INVALID_OPERATION;
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800133
Emilian Peevf53f66e2017-04-11 14:29:43 +0100134 res = enumerateProviders();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800135 if (res == OK) {
136 mInitialized = true;
137 }
138
139 CameraService::pingCameraServiceProxy();
Svet Ganova453d0d2018-01-11 15:37:58 -0800140
141 mUidPolicy = new UidPolicy(this);
142 mUidPolicy->registerSelf();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800143}
144
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800145status_t CameraService::enumerateProviders() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800146 status_t res;
Emilian Peevaee727d2017-05-04 16:35:48 +0100147
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800148 std::vector<std::string> deviceIds;
149 {
150 Mutex::Autolock l(mServiceLock);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800151
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800152 if (nullptr == mCameraProviderManager.get()) {
153 mCameraProviderManager = new CameraProviderManager();
154 res = mCameraProviderManager->initialize(this);
155 if (res != OK) {
156 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
157 __FUNCTION__, strerror(-res), res);
158 return res;
Emilian Peevaee727d2017-05-04 16:35:48 +0100159 }
160 }
161
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800162
163 // Setup vendor tags before we call get_camera_info the first time
164 // because HAL might need to setup static vendor keys in get_camera_info
165 // TODO: maybe put this into CameraProviderManager::initialize()?
166 mCameraProviderManager->setUpVendorTags();
167
168 if (nullptr == mFlashlight.get()) {
169 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700170 }
171
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800172 res = mFlashlight->findFlashUnits();
173 if (res != OK) {
174 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
175 }
176
177 deviceIds = mCameraProviderManager->getCameraDeviceIds();
178 }
179
180
181 for (auto& cameraId : deviceIds) {
182 String8 id8 = String8(cameraId.c_str());
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700183 onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800184 }
185
186 return OK;
187}
188
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700189sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800190 sp<ICameraServiceProxy> proxyBinder = nullptr;
191#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700192 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800193 // Use checkService because cameraserver normally starts before the
194 // system server and the proxy service. So the long timeout that getService
195 // has before giving up is inappropriate.
196 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800197 if (binder != nullptr) {
198 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700199 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800200#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700201 return proxyBinder;
202}
203
204void CameraService::pingCameraServiceProxy() {
205 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
206 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700207 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700208}
209
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210CameraService::~CameraService() {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800211 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Svet Ganova453d0d2018-01-11 15:37:58 -0800212 mUidPolicy->unregisterSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213}
214
Emilian Peevaee727d2017-05-04 16:35:48 +0100215void CameraService::onNewProviderRegistered() {
216 enumerateProviders();
217}
218
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800219void CameraService::updateCameraNumAndIds() {
220 Mutex::Autolock l(mServiceLock);
221 mNumberOfCameras = mCameraProviderManager->getCameraCount();
222 mNormalDeviceIds =
223 mCameraProviderManager->getAPI1CompatibleCameraDeviceIds();
224}
225
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100226void CameraService::addStates(const String8 id) {
227 std::string cameraId(id.c_str());
228 hardware::camera::common::V1_0::CameraResourceCost cost;
229 status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost);
230 if (res != OK) {
231 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
232 return;
233 }
234 std::set<String8> conflicting;
235 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
236 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
237 }
238
239 {
240 Mutex::Autolock lock(mCameraStatesLock);
241 mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost,
242 conflicting));
243 }
244
245 if (mFlashlight->hasFlashUnit(id)) {
246 mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF);
247 }
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800248
249 updateCameraNumAndIds();
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100250 logDeviceAdded(id, "Device added");
251}
252
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100253void CameraService::removeStates(const String8 id) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800254 updateCameraNumAndIds();
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100255 if (mFlashlight->hasFlashUnit(id)) {
256 mTorchStatusMap.removeItem(id);
257 }
258
259 {
260 Mutex::Autolock lock(mCameraStatesLock);
261 mCameraStates.erase(id);
262 }
263}
264
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800265void CameraService::onDeviceStatusChanged(const String8& id,
266 CameraDeviceStatus newHalStatus) {
267 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
268 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700269
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800270 StatusInternal newStatus = mapToInternal(newHalStatus);
271
Ruben Brunkcc776712015-02-17 20:18:47 -0800272 std::shared_ptr<CameraState> state = getCameraState(id);
273
274 if (state == nullptr) {
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700275 if (newStatus == StatusInternal::PRESENT) {
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100276 ALOGI("%s: Unknown camera ID %s, a new camera is added",
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700277 __FUNCTION__, id.string());
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100278
279 // First add as absent to make sure clients are notified below
280 addStates(id);
281
282 updateStatus(newStatus, id);
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700283 } else {
284 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
285 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700286 return;
287 }
288
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800289 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800290
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800291 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800292 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700293 return;
294 }
295
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800296 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700297 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
298 newStatus));
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800299
300 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
301 // to this device until the status changes
302 updateStatus(StatusInternal::NOT_PRESENT, id);
303
Ruben Brunkcc776712015-02-17 20:18:47 -0800304 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700305 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800306 // Don't do this in updateStatus to avoid deadlock over mServiceLock
307 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700308
Ruben Brunkcc776712015-02-17 20:18:47 -0800309 // Remove cached shim parameters
310 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700311
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700312 // Remove the client from the list of active clients, if there is one
Ruben Brunkcc776712015-02-17 20:18:47 -0800313 clientToDisconnect = removeClientLocked(id);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700314 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800315
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700316 // Disconnect client
317 if (clientToDisconnect.get() != nullptr) {
318 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
319 __FUNCTION__, id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800320 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800321 clientToDisconnect->notifyError(
322 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800323 CaptureResultExtras{});
Ruben Brunkcc776712015-02-17 20:18:47 -0800324 // Ensure not in binder RPC so client disconnect PID checks work correctly
325 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
326 "onDeviceStatusChanged must be called from the camera service process!");
327 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700328 }
329
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100330 removeStates(id);
Ruben Brunkcc776712015-02-17 20:18:47 -0800331 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800332 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700333 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
334 newStatus));
335 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800336 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700337 }
338
Igor Murashkincba2c162013-03-20 15:56:31 -0700339}
340
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800341void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800342 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800343 Mutex::Autolock al(mTorchStatusMutex);
344 onTorchStatusChangedLocked(cameraId, newStatus);
345}
346
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800347void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800348 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800349 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
350 __FUNCTION__, cameraId.string(), newStatus);
351
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800352 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800353 status_t res = getTorchStatusLocked(cameraId, &status);
354 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700355 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
356 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800357 return;
358 }
359 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800360 return;
361 }
362
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800363 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800364 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800365 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
366 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800367 return;
368 }
369
Ruben Brunkcc776712015-02-17 20:18:47 -0800370 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700371 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700372 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700373 auto iter = mTorchUidMap.find(cameraId);
374 if (iter != mTorchUidMap.end()) {
375 int oldUid = iter->second.second;
376 int newUid = iter->second.first;
377 BatteryNotifier& notifier(BatteryNotifier::getInstance());
378 if (oldUid != newUid) {
379 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800380 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700381 notifier.noteFlashlightOff(cameraId, oldUid);
382 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800383 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700384 notifier.noteFlashlightOn(cameraId, newUid);
385 }
386 iter->second.second = newUid;
387 } else {
388 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800389 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700390 notifier.noteFlashlightOn(cameraId, oldUid);
391 } else {
392 notifier.noteFlashlightOff(cameraId, oldUid);
393 }
394 }
395 }
396 }
397
398 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800399 Mutex::Autolock lock(mStatusListenerLock);
400 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800401 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800402 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800403 }
404}
405
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800406Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700407 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100408 Mutex::Autolock l(mServiceLock);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700409 switch (type) {
410 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800411 *numCameras = static_cast<int>(mNormalDeviceIds.size());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800412 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700413 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800414 *numCameras = mNumberOfCameras;
415 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700416 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800417 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700418 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800419 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
420 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700421 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800422 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423}
424
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800425Status CameraService::getCameraInfo(int cameraId,
426 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700427 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100428 Mutex::Autolock l(mServiceLock);
429
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800430 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800431 return STATUS_ERROR(ERROR_DISCONNECTED,
432 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700433 }
434
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800436 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
437 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 }
439
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800440 Status ret = Status::ok();
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800441 status_t err = mCameraProviderManager->getCameraInfo(
442 cameraIdIntToStrLocked(cameraId), cameraInfo);
Emilian Peevf53f66e2017-04-11 14:29:43 +0100443 if (err != OK) {
444 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
445 "Error retrieving camera info from device %d: %s (%d)", cameraId,
446 strerror(-err), err);
Ruben Brunkcc776712015-02-17 20:18:47 -0800447 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100448
Ruben Brunkcc776712015-02-17 20:18:47 -0800449 return ret;
450}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700451
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800452std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) {
453 if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(mNormalDeviceIds.size())) {
454 ALOGE("%s: input id %d invalid: valid range (0, %zu)",
455 __FUNCTION__, cameraIdInt, mNormalDeviceIds.size());
456 return std::string{};
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800457 }
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800458
459 return mNormalDeviceIds[cameraIdInt];
460}
461
462String8 CameraService::cameraIdIntToStr(int cameraIdInt) {
463 Mutex::Autolock lock(mServiceLock);
464 return String8(cameraIdIntToStrLocked(cameraIdInt).c_str());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800465}
466
467Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800468 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700469 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700470 if (!cameraInfo) {
471 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800472 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700473 }
474
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800475 if (!mInitialized) {
476 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800477 return STATUS_ERROR(ERROR_DISCONNECTED,
478 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700479 }
480
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800481 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800482
Emilian Peevf53f66e2017-04-11 14:29:43 +0100483 status_t res = mCameraProviderManager->getCameraCharacteristics(
484 String8(cameraId).string(), cameraInfo);
485 if (res != OK) {
486 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
487 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
488 strerror(-res), res);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700489 }
Zhijun He2b59be82013-09-25 10:14:30 -0700490
491 return ret;
492}
493
Ruben Brunkcc776712015-02-17 20:18:47 -0800494int CameraService::getCallingPid() {
495 return IPCThreadState::self()->getCallingPid();
496}
497
498int CameraService::getCallingUid() {
499 return IPCThreadState::self()->getCallingUid();
500}
501
502String8 CameraService::getFormattedCurrentTime() {
503 time_t now = time(nullptr);
504 char formattedTime[64];
505 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
506 return String8(formattedTime);
507}
508
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800509Status CameraService::getCameraVendorTagDescriptor(
510 /*out*/
511 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700512 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800513 if (!mInitialized) {
514 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800515 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800516 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800517 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
518 if (globalDescriptor != nullptr) {
519 *desc = *(globalDescriptor.get());
520 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800521 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800522}
523
Emilian Peev71c73a22017-03-21 16:35:51 +0000524Status CameraService::getCameraVendorTagCache(
525 /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) {
526 ATRACE_CALL();
527 if (!mInitialized) {
528 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
529 return STATUS_ERROR(ERROR_DISCONNECTED,
530 "Camera subsystem not available");
531 }
532 sp<VendorTagDescriptorCache> globalCache =
533 VendorTagDescriptorCache::getGlobalVendorTagCache();
534 if (globalCache != nullptr) {
535 *cache = *(globalCache.get());
536 }
537 return Status::ok();
538}
539
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800540int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700541 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800542
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800543 int deviceVersion = 0;
544
Emilian Peevf53f66e2017-04-11 14:29:43 +0100545 status_t res;
546 hardware::hidl_version maxVersion{0,0};
547 res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(),
548 &maxVersion);
549 if (res != OK) return -1;
550 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800551
Emilian Peevf53f66e2017-04-11 14:29:43 +0100552 hardware::CameraInfo info;
553 if (facing) {
554 res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800555 if (res != OK) return -1;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100556 *facing = info.facing;
Igor Murashkin634a5152013-02-20 17:15:11 -0800557 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100558
Igor Murashkin634a5152013-02-20 17:15:11 -0800559 return deviceVersion;
560}
561
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800562Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700563 switch(err) {
564 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800566 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
568 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800569 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800570 return STATUS_ERROR(ERROR_DISCONNECTED,
571 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700572 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
574 "Camera HAL encountered error %d: %s",
575 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700576 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800577}
578
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800579Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800580 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800581 int api1CameraId, int facing, int clientPid, uid_t clientUid, int servicePid,
582 bool legacyMode, int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
Ruben Brunkcc776712015-02-17 20:18:47 -0800583 /*out*/sp<BasicClient>* client) {
584
Ruben Brunkcc776712015-02-17 20:18:47 -0800585 if (halVersion < 0 || halVersion == deviceVersion) {
586 // Default path: HAL version is unspecified by caller, create CameraClient
587 // based on device version reported by the HAL.
588 switch(deviceVersion) {
589 case CAMERA_DEVICE_API_VERSION_1_0:
590 if (effectiveApiLevel == API_1) { // Camera1 API route
591 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800592 *client = new CameraClient(cameraService, tmp, packageName,
593 api1CameraId, facing, clientPid, clientUid,
594 getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800595 } else { // Camera2 API route
596 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800598 "Camera device \"%s\" HAL version %d does not support camera2 API",
599 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800600 }
601 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800602 case CAMERA_DEVICE_API_VERSION_3_0:
603 case CAMERA_DEVICE_API_VERSION_3_1:
604 case CAMERA_DEVICE_API_VERSION_3_2:
605 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700606 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800607 if (effectiveApiLevel == API_1) { // Camera1 API route
608 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800609 *client = new Camera2Client(cameraService, tmp, packageName,
610 cameraId, api1CameraId,
611 facing, clientPid, clientUid,
612 servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800613 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800614 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
615 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
616 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800617 facing, clientPid, clientUid, servicePid);
618 }
619 break;
620 default:
621 // Should not be reachable
622 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800623 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800624 "Camera device \"%s\" has unknown HAL version %d",
625 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800626 }
627 } else {
628 // A particular HAL version is requested by caller. Create CameraClient
629 // based on the requested HAL version.
630 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
631 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
632 // Only support higher HAL version device opened as HAL1.0 device.
633 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800634 *client = new CameraClient(cameraService, tmp, packageName,
635 api1CameraId, facing, clientPid, clientUid,
636 servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800637 } else {
638 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
639 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
640 " opened as HAL %x device", halVersion, deviceVersion,
641 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800642 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800643 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
644 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800645 }
646 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800647 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800648}
649
Ruben Brunk6267b532015-04-30 17:44:07 -0700650String8 CameraService::toString(std::set<userid_t> intSet) {
651 String8 s("");
652 bool first = true;
653 for (userid_t i : intSet) {
654 if (first) {
655 s.appendFormat("%d", i);
656 first = false;
657 } else {
658 s.appendFormat(", %d", i);
659 }
660 }
661 return s;
662}
663
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800664int32_t CameraService::mapToInterface(TorchModeStatus status) {
665 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
666 switch (status) {
667 case TorchModeStatus::NOT_AVAILABLE:
668 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
669 break;
670 case TorchModeStatus::AVAILABLE_OFF:
671 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
672 break;
673 case TorchModeStatus::AVAILABLE_ON:
674 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
675 break;
676 default:
677 ALOGW("Unknown new flash status: %d", status);
678 }
679 return serviceStatus;
680}
681
682CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
683 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
684 switch (status) {
685 case CameraDeviceStatus::NOT_PRESENT:
686 serviceStatus = StatusInternal::NOT_PRESENT;
687 break;
688 case CameraDeviceStatus::PRESENT:
689 serviceStatus = StatusInternal::PRESENT;
690 break;
691 case CameraDeviceStatus::ENUMERATING:
692 serviceStatus = StatusInternal::ENUMERATING;
693 break;
694 default:
695 ALOGW("Unknown new HAL device status: %d", status);
696 }
697 return serviceStatus;
698}
699
700int32_t CameraService::mapToInterface(StatusInternal status) {
701 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
702 switch (status) {
703 case StatusInternal::NOT_PRESENT:
704 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
705 break;
706 case StatusInternal::PRESENT:
707 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
708 break;
709 case StatusInternal::ENUMERATING:
710 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
711 break;
712 case StatusInternal::NOT_AVAILABLE:
713 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
714 break;
715 case StatusInternal::UNKNOWN:
716 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
717 break;
718 default:
719 ALOGW("Unknown new internal device status: %d", status);
720 }
721 return serviceStatus;
722}
723
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800724Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800725 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700726
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800727 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800728 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800729 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800730 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800731 if (!(ret = connectHelper<ICameraClient,Client>(
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800732 sp<ICameraClient>{nullptr}, id, cameraId,
733 static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800734 internalPackageName, uid, USE_CALLING_PID,
735 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
736 /*out*/ tmp)
737 ).isOk()) {
738 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700739 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800740 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700741}
742
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800743Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700744 /*out*/
745 CameraParameters* parameters) {
746
747 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
748
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800749 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700750
751 if (parameters == NULL) {
752 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800753 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700754 }
755
Ruben Brunkcc776712015-02-17 20:18:47 -0800756 String8 id = String8::format("%d", cameraId);
757
758 // Check if we already have parameters
759 {
760 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700761 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800762 auto cameraState = getCameraState(id);
763 if (cameraState == nullptr) {
764 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800765 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
766 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800767 }
768 CameraParameters p = cameraState->getShimParams();
769 if (!p.isEmpty()) {
770 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700772 }
773 }
774
Ruben Brunkcc776712015-02-17 20:18:47 -0800775 int64_t token = IPCThreadState::self()->clearCallingIdentity();
776 ret = initializeShimMetadata(cameraId);
777 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800778 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800779 // Error already logged by callee
780 return ret;
781 }
782
783 // Check for parameters again
784 {
785 // Scope for service lock
786 Mutex::Autolock lock(mServiceLock);
787 auto cameraState = getCameraState(id);
788 if (cameraState == nullptr) {
789 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800790 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
791 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700792 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800793 CameraParameters p = cameraState->getShimParams();
794 if (!p.isEmpty()) {
795 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800796 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700797 }
798 }
799
Ruben Brunkcc776712015-02-17 20:18:47 -0800800 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
801 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800802 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700803}
804
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800805// Can camera service trust the caller based on the calling UID?
806static bool isTrustedCallingUid(uid_t uid) {
807 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700808 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800809 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700810 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800811 return true;
812 default:
813 return false;
814 }
815}
816
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800817Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700818 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
819 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500820
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700821#ifdef __BRILLO__
822 UNUSED(clientName8);
823 UNUSED(clientUid);
824 UNUSED(clientPid);
825 UNUSED(originalClientPid);
826#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700827 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
828 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800829 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800830 return allowed;
831 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700832#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -0800833
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800834 int callingPid = getCallingPid();
835
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800836 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800837 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
838 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800839 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
840 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700841 }
842
Ruben Brunkcc776712015-02-17 20:18:47 -0800843 if (getCameraState(cameraId) == nullptr) {
844 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
845 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800846 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
847 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 }
849
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800850 status_t err = checkIfDeviceIsUsable(cameraId);
851 if (err != NO_ERROR) {
852 switch(err) {
853 case -ENODEV:
854 case -EBUSY:
855 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
856 "No camera device with ID \"%s\" currently available", cameraId.string());
857 default:
858 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
859 "Unknown error connecting to ID \"%s\"", cameraId.string());
860 }
861 }
862 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800863}
864
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800865Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700866 const String8& clientName8, int& clientUid, int& clientPid,
867 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800868 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800869 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800871 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800873 clientUid = callingUid;
874 } else if (!isTrustedCallingUid(callingUid)) {
875 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
876 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800877 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
878 "Untrusted caller (calling PID %d, UID %d) trying to "
879 "forward camera access to camera %s for client %s (PID %d, UID %d)",
880 callingPid, callingUid, cameraId.string(),
881 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 }
883
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800884 // Check if we can trust clientPid
885 if (clientPid == USE_CALLING_PID) {
886 clientPid = callingPid;
887 } else if (!isTrustedCallingUid(callingUid)) {
888 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
889 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800890 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
891 "Untrusted caller (calling PID %d, UID %d) trying to "
892 "forward camera access to camera %s for client %s (PID %d, UID %d)",
893 callingPid, callingUid, cameraId.string(),
894 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800895 }
896
897 // If it's not calling from cameraserver, check the permission.
898 if (callingPid != getpid() &&
899 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
900 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800901 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
902 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
903 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800904 }
905
Svet Ganova453d0d2018-01-11 15:37:58 -0800906 // Make sure the UID is in an active state to use the camera
907 if (!mUidPolicy->isUidActive(callingUid)) {
908 ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d",
909 clientPid, clientUid);
910 return STATUS_ERROR_FMT(ERROR_DISABLED,
911 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background",
912 clientName8.string(), clientUid, clientPid, cameraId.string());
913 }
914
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -0700915 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
916 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700917 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -0700918 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919
Ruben Brunk6267b532015-04-30 17:44:07 -0700920 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800921
Ruben Brunka8ca9152015-04-07 14:23:40 -0700922 // Only allow clients who are being used by the current foreground device user, unless calling
923 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -0700924 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
925 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
926 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
927 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800928 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
929 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
930 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -0700931 }
932
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800933 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800934}
935
936status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
937 auto cameraState = getCameraState(cameraId);
938 int callingPid = getCallingPid();
939 if (cameraState == nullptr) {
940 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
941 cameraId.string());
942 return -ENODEV;
943 }
944
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800945 StatusInternal currentStatus = cameraState->getStatus();
946 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800947 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
948 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700949 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800950 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800951 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
952 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700953 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700954 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700955
Ruben Brunkcc776712015-02-17 20:18:47 -0800956 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800957}
958
Ruben Brunkcc776712015-02-17 20:18:47 -0800959void CameraService::finishConnectLocked(const sp<BasicClient>& client,
960 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800961
Ruben Brunkcc776712015-02-17 20:18:47 -0800962 // Make a descriptor for the incoming client
963 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
964 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
965
966 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
967 String8(client->getPackageName()));
968
969 if (evicted.size() > 0) {
970 // This should never happen - clients should already have been removed in disconnect
971 for (auto& i : evicted) {
972 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
973 __FUNCTION__, i->getKey().string());
974 }
975
976 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
977 __FUNCTION__);
978 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -0700979
980 // And register a death notification for the client callback. Do
981 // this last to avoid Binder policy where a nested Binder
982 // transaction might be pre-empted to service the client death
983 // notification if the client process dies before linkToDeath is
984 // invoked.
985 sp<IBinder> remoteCallback = client->getRemote();
986 if (remoteCallback != nullptr) {
987 remoteCallback->linkToDeath(this);
988 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800989}
990
991status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
992 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
993 /*out*/
994 sp<BasicClient>* client,
995 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700996 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -0800997 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -0700998 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -0800999 DescriptorPtr clientDescriptor;
1000 {
1001 if (effectiveApiLevel == API_1) {
1002 // If we are using API1, any existing client for this camera ID with the same remote
1003 // should be returned rather than evicted to allow MediaRecorder to work properly.
1004
1005 auto current = mActiveClientManager.get(cameraId);
1006 if (current != nullptr) {
1007 auto clientSp = current->getValue();
1008 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001009 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1010 ALOGW("CameraService connect called from same client, but with a different"
1011 " API level, evicting prior client...");
1012 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001013 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001014 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001015 *client = clientSp;
1016 return NO_ERROR;
1017 }
1018 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001019 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001020 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001021
Ruben Brunkcc776712015-02-17 20:18:47 -08001022 // Get current active client PIDs
1023 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1024 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001025
Emilian Peev8131a262017-02-01 12:33:43 +00001026 std::vector<int> priorityScores(ownerPids.size());
1027 std::vector<int> states(ownerPids.size());
Igor Murashkine6800ce2013-03-04 17:25:57 -08001028
Emilian Peev8131a262017-02-01 12:33:43 +00001029 // Get priority scores of all active PIDs
1030 status_t err = ProcessInfoService::getProcessStatesScoresFromPids(
1031 ownerPids.size(), &ownerPids[0], /*out*/&states[0],
1032 /*out*/&priorityScores[0]);
1033 if (err != OK) {
1034 ALOGE("%s: Priority score query failed: %d",
1035 __FUNCTION__, err);
1036 return err;
1037 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001038
Ruben Brunkcc776712015-02-17 20:18:47 -08001039 // Update all active clients' priorities
Emilian Peev8131a262017-02-01 12:33:43 +00001040 std::map<int,resource_policy::ClientPriority> pidToPriorityMap;
Ruben Brunkcc776712015-02-17 20:18:47 -08001041 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
Emilian Peev8131a262017-02-01 12:33:43 +00001042 pidToPriorityMap.emplace(ownerPids[i],
1043 resource_policy::ClientPriority(priorityScores[i], states[i]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001044 }
1045 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001046
Ruben Brunkcc776712015-02-17 20:18:47 -08001047 // Get state for the given cameraId
1048 auto state = getCameraState(cameraId);
1049 if (state == nullptr) {
1050 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1051 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001052 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001053 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001054 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001055
1056 // Make descriptor for incoming client
1057 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1058 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1059 state->getConflicting(),
Emilian Peev8131a262017-02-01 12:33:43 +00001060 priorityScores[priorityScores.size() - 1],
1061 clientPid,
1062 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001063
1064 // Find clients that would be evicted
1065 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1066
1067 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1068 // background, so we cannot do evictions
1069 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1070 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1071 " priority).", clientPid);
1072
1073 sp<BasicClient> clientSp = clientDescriptor->getValue();
1074 String8 curTime = getFormattedCurrentTime();
1075 auto incompatibleClients =
1076 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1077
1078 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Emilian Peev8131a262017-02-01 12:33:43 +00001079 "(PID %d, score %d state %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001080 cameraId.string(), packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001081 priorityScores[priorityScores.size() - 1],
1082 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001083
1084 for (auto& i : incompatibleClients) {
1085 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
Emilian Peev8131a262017-02-01 12:33:43 +00001086 "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")",
1087 i->getKey().string(),
1088 String8{i->getValue()->getPackageName()}.string(),
1089 i->getOwnerId(), i->getPriority().getScore(),
1090 i->getPriority().getState());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001091 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001092 PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(),
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001093 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
Emilian Peev8131a262017-02-01 12:33:43 +00001094 i->getPriority().getScore(), i->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001095 }
1096
1097 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001098 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001099 mEventLog.add(msg);
1100
1101 return -EBUSY;
1102 }
1103
1104 for (auto& i : evicted) {
1105 sp<BasicClient> clientSp = i->getValue();
1106 if (clientSp.get() == nullptr) {
1107 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1108
1109 // TODO: Remove this
1110 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1111 __FUNCTION__);
1112 mActiveClientManager.remove(i);
1113 continue;
1114 }
1115
1116 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1117 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001118 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001119
Ruben Brunkcc776712015-02-17 20:18:47 -08001120 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001121 logEvent(String8::format("EVICT device %s client held by package %s (PID"
Emilian Peev8131a262017-02-01 12:33:43 +00001122 " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for"
1123 " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001124 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
Emilian Peev8131a262017-02-01 12:33:43 +00001125 i->getOwnerId(), i->getPriority().getScore(),
1126 i->getPriority().getState(), cameraId.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001127 packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001128 priorityScores[priorityScores.size() - 1],
1129 states[states.size() - 1]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001130
1131 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001133 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001134 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001135 }
1136
Ruben Brunkcc776712015-02-17 20:18:47 -08001137 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1138 // other clients from connecting in mServiceLockWrapper if held
1139 mServiceLock.unlock();
1140
1141 // Clear caller identity temporarily so client disconnect PID checks work correctly
1142 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1143
1144 // Destroy evicted clients
1145 for (auto& i : evictedClients) {
1146 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001147 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001148 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001149
1150 IPCThreadState::self()->restoreCallingIdentity(token);
1151
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001152 for (const auto& i : evictedClients) {
1153 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1154 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1155 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1156 if (ret == TIMED_OUT) {
1157 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1158 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1159 mActiveClientManager.toString().string());
1160 return -EBUSY;
1161 }
1162 if (ret != NO_ERROR) {
1163 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1164 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1165 ret, mActiveClientManager.toString().string());
1166 return ret;
1167 }
1168 }
1169
1170 evictedClients.clear();
1171
Ruben Brunkcc776712015-02-17 20:18:47 -08001172 // Once clients have been disconnected, relock
1173 mServiceLock.lock();
1174
1175 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1176 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1177 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001178 }
1179
Ruben Brunkcc776712015-02-17 20:18:47 -08001180 *partial = clientDescriptor;
1181 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001182}
1183
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001185 const sp<ICameraClient>& cameraClient,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001186 int api1CameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001187 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001188 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001189 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001190 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001191 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001192
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001193 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001194 Status ret = Status::ok();
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001195
1196 String8 id = cameraIdIntToStr(api1CameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001197 sp<Client> client = nullptr;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001198 ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001199 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1200 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1201 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001202
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001204 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001205 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001206 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 }
1208
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001209 *device = client;
1210 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211}
1212
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001213Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001214 const sp<ICameraClient>& cameraClient,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001215 int api1CameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001216 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001217 int clientUid,
1218 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001219 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001220
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001221 ATRACE_CALL();
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001222 String8 id = cameraIdIntToStr(api1CameraId);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001223
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001224 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001225 sp<Client> client = nullptr;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001226 ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, halVersion,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001227 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1228 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1229 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001230
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001231 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001232 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001233 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001234 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001235 }
1236
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001237 *device = client;
1238 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001239}
1240
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001241Status CameraService::connectDevice(
1242 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001243 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001244 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001245 int clientUid,
1246 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001247 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001248
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001249 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001250 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001251 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001252 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001253 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001254 /*api1CameraId*/-1,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001255 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1256 clientUid, USE_CALLING_PID, API_2,
1257 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1258 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001259
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001260 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001261 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001262 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001263 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001264 }
1265
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001266 *device = client;
1267 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001268}
1269
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001270template<class CALLBACK, class CLIENT>
1271Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001272 int api1CameraId, int halVersion, const String16& clientPackageName, int clientUid,
1273 int clientPid, apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001274 /*out*/sp<CLIENT>& device) {
1275 binder::Status ret = binder::Status::ok();
1276
1277 String8 clientName8(clientPackageName);
1278
1279 int originalClientPid = 0;
1280
1281 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1282 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1283 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1284 static_cast<int>(effectiveApiLevel));
1285
1286 sp<CLIENT> client = nullptr;
1287 {
1288 // Acquire mServiceLock and prevent other clients from connecting
1289 std::unique_ptr<AutoConditionLock> lock =
1290 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1291
1292 if (lock == nullptr) {
1293 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1294 , clientPid);
1295 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1296 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1297 cameraId.string(), clientName8.string(), clientPid);
1298 }
1299
1300 // Enforce client permissions and do basic sanity checks
1301 if(!(ret = validateConnectLocked(cameraId, clientName8,
1302 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1303 return ret;
1304 }
1305
1306 // Check the shim parameters after acquiring lock, if they have already been updated and
1307 // we were doing a shim update, return immediately
1308 if (shimUpdateOnly) {
1309 auto cameraState = getCameraState(cameraId);
1310 if (cameraState != nullptr) {
1311 if (!cameraState->getShimParams().isEmpty()) return ret;
1312 }
1313 }
1314
1315 status_t err;
1316
1317 sp<BasicClient> clientTmp = nullptr;
1318 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1319 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1320 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1321 /*out*/&partial)) != NO_ERROR) {
1322 switch (err) {
1323 case -ENODEV:
1324 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1325 "No camera device with ID \"%s\" currently available",
1326 cameraId.string());
1327 case -EBUSY:
1328 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1329 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1330 cameraId.string());
1331 default:
1332 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1333 "Unexpected error %s (%d) opening camera \"%s\"",
1334 strerror(-err), err, cameraId.string());
1335 }
1336 }
1337
1338 if (clientTmp.get() != nullptr) {
1339 // Handle special case for API1 MediaRecorder where the existing client is returned
1340 device = static_cast<CLIENT*>(clientTmp.get());
1341 return ret;
1342 }
1343
1344 // give flashlight a chance to close devices if necessary.
1345 mFlashlight->prepareDeviceOpen(cameraId);
1346
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001347 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001348 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001349 if (facing == -1) {
1350 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1351 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1352 "Unable to get camera device \"%s\" facing", cameraId.string());
1353 }
1354
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001355 sp<BasicClient> tmp = nullptr;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001356 if(!(ret = makeClient(this, cameraCb, clientPackageName,
1357 cameraId, api1CameraId, facing,
1358 clientPid, clientUid, getpid(), legacyMode,
1359 halVersion, deviceVersion, effectiveApiLevel,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001360 /*out*/&tmp)).isOk()) {
1361 return ret;
1362 }
1363 client = static_cast<CLIENT*>(tmp.get());
1364
1365 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1366 __FUNCTION__);
1367
Emilian Peevbd8c5032018-02-14 23:05:40 +00001368 err = client->initialize(mCameraProviderManager, mMonitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001369 if (err != OK) {
1370 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001371 // Errors could be from the HAL module open call or from AppOpsManager
1372 switch(err) {
1373 case BAD_VALUE:
1374 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1375 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1376 case -EBUSY:
1377 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1378 "Camera \"%s\" is already open", cameraId.string());
1379 case -EUSERS:
1380 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1381 "Too many cameras already open, cannot open camera \"%s\"",
1382 cameraId.string());
1383 case PERMISSION_DENIED:
1384 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1385 "No permission to open camera \"%s\"", cameraId.string());
1386 case -EACCES:
1387 return STATUS_ERROR_FMT(ERROR_DISABLED,
1388 "Camera \"%s\" disabled by policy", cameraId.string());
1389 case -ENODEV:
1390 default:
1391 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1392 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1393 strerror(-err), err);
1394 }
1395 }
1396
1397 // Update shim paremeters for legacy clients
1398 if (effectiveApiLevel == API_1) {
1399 // Assume we have always received a Client subclass for API1
1400 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1401 String8 rawParams = shimClient->getParameters();
1402 CameraParameters params(rawParams);
1403
1404 auto cameraState = getCameraState(cameraId);
1405 if (cameraState != nullptr) {
1406 cameraState->setShimParams(params);
1407 } else {
1408 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1409 __FUNCTION__, cameraId.string());
1410 }
1411 }
1412
1413 if (shimUpdateOnly) {
1414 // If only updating legacy shim parameters, immediately disconnect client
1415 mServiceLock.unlock();
1416 client->disconnect();
1417 mServiceLock.lock();
1418 } else {
1419 // Otherwise, add client to active clients list
1420 finishConnectLocked(client, partial);
1421 }
1422 } // lock is destroyed, allow further connect calls
1423
1424 // Important: release the mutex here so the client can call back into the service from its
1425 // destructor (can be at the end of the call)
1426 device = client;
1427 return ret;
1428}
1429
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001431 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001432 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001433
1434 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001435 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001436 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001437 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1438 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001439 }
1440
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001441 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001442 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001443
1444 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001445 auto state = getCameraState(id);
1446 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001447 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001448 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1449 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001450 }
1451
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001452 StatusInternal cameraStatus = state->getStatus();
1453 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001454 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1455 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001456 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1457 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001458 }
1459
1460 {
1461 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001462 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001463 status_t err = getTorchStatusLocked(id, &status);
1464 if (err != OK) {
1465 if (err == NAME_NOT_FOUND) {
1466 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1467 "Camera \"%s\" does not have a flash unit", id.string());
1468 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001469 ALOGE("%s: getting current torch status failed for camera %s",
1470 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001471 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1472 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1473 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001474 }
1475
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001476 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001477 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001478 ALOGE("%s: torch mode of camera %s is not available because "
1479 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1481 "Torch for camera \"%s\" is not available due to an existing camera user",
1482 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001483 } else {
1484 ALOGE("%s: torch mode of camera %s is not available due to "
1485 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001486 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1487 "Torch for camera \"%s\" is not available due to insufficient resources",
1488 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001489 }
1490 }
1491 }
1492
Ruben Brunk99e69712015-05-26 17:25:07 -07001493 {
1494 // Update UID map - this is used in the torch status changed callbacks, so must be done
1495 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001496 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001497 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1498 mTorchUidMap[id].first = uid;
1499 mTorchUidMap[id].second = uid;
1500 } else {
1501 // Set the pending UID
1502 mTorchUidMap[id].first = uid;
1503 }
1504 }
1505
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001506 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001507
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001508 if (err != OK) {
1509 int32_t errorCode;
1510 String8 msg;
1511 switch (err) {
1512 case -ENOSYS:
1513 msg = String8::format("Camera \"%s\" has no flashlight",
1514 id.string());
1515 errorCode = ERROR_ILLEGAL_ARGUMENT;
1516 break;
1517 default:
1518 msg = String8::format(
1519 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1520 id.string(), enabled, strerror(-err), err);
1521 errorCode = ERROR_INVALID_OPERATION;
1522 }
1523 ALOGE("%s: %s", __FUNCTION__, msg.string());
1524 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001525 }
1526
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001527 {
1528 // update the link to client's death
1529 Mutex::Autolock al(mTorchClientMapMutex);
1530 ssize_t index = mTorchClientMap.indexOfKey(id);
1531 if (enabled) {
1532 if (index == NAME_NOT_FOUND) {
1533 mTorchClientMap.add(id, clientBinder);
1534 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001535 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001536 mTorchClientMap.replaceValueAt(index, clientBinder);
1537 }
1538 clientBinder->linkToDeath(this);
1539 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001540 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001541 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001542 }
1543
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001544 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001545}
1546
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001547Status CameraService::notifySystemEvent(int32_t eventId,
1548 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001549 ATRACE_CALL();
1550
Ruben Brunk36597b22015-03-20 22:15:57 -07001551 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001552 case ICameraService::EVENT_USER_SWITCHED: {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07001553 // Try to register for UID policy updates, in case we're recovering
1554 // from a system server crash
1555 mUidPolicy->registerSelf();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001556 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001557 break;
1558 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001559 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001560 default: {
1561 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1562 eventId);
1563 break;
1564 }
1565 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001567}
1568
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001569Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1570 /*out*/
1571 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001572 ATRACE_CALL();
1573
Igor Murashkinbfc99152013-02-27 12:55:20 -08001574 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001575
Ruben Brunk3450ba72015-06-16 11:00:37 -07001576 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001577 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001578 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001579 }
1580
Igor Murashkinbfc99152013-02-27 12:55:20 -08001581 Mutex::Autolock lock(mServiceLock);
1582
Ruben Brunkcc776712015-02-17 20:18:47 -08001583 {
1584 Mutex::Autolock lock(mStatusListenerLock);
1585 for (auto& it : mListenerList) {
1586 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1587 ALOGW("%s: Tried to add listener %p which was already subscribed",
1588 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001589 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001590 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001591 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001592
1593 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001594 }
1595
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001596 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001597 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001598 Mutex::Autolock lock(mCameraStatesLock);
1599 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001600 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001601 }
1602 }
1603
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001604 /*
1605 * Immediately signal current torch status to this listener only
1606 * This may be a subset of all the devices, so don't include it in the response directly
1607 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001608 {
1609 Mutex::Autolock al(mTorchStatusMutex);
1610 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001611 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001612 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001613 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001614 }
1615
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001616 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001617}
Ruben Brunkcc776712015-02-17 20:18:47 -08001618
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001619Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001620 ATRACE_CALL();
1621
Igor Murashkinbfc99152013-02-27 12:55:20 -08001622 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1623
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001624 if (listener == 0) {
1625 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001626 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001627 }
1628
Igor Murashkinbfc99152013-02-27 12:55:20 -08001629 Mutex::Autolock lock(mServiceLock);
1630
Ruben Brunkcc776712015-02-17 20:18:47 -08001631 {
1632 Mutex::Autolock lock(mStatusListenerLock);
1633 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1634 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1635 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001636 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001637 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001638 }
1639 }
1640
1641 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1642 __FUNCTION__, listener.get());
1643
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001644 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001645}
1646
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001647Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001648
1649 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001650 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1651
1652 if (parameters == NULL) {
1653 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001654 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001655 }
1656
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001657 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001658
1659 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001660 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001661 // Error logged by caller
1662 return ret;
1663 }
1664
1665 String8 shimParamsString8 = shimParams.flatten();
1666 String16 shimParamsString16 = String16(shimParamsString8);
1667
1668 *parameters = shimParamsString16;
1669
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001670 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001671}
1672
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001673Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1674 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001675 ATRACE_CALL();
1676
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001677 const String8 id = String8(cameraId);
1678
1679 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001680
1681 switch (apiVersion) {
1682 case API_VERSION_1:
1683 case API_VERSION_2:
1684 break;
1685 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001686 String8 msg = String8::format("Unknown API version %d", apiVersion);
1687 ALOGE("%s: %s", __FUNCTION__, msg.string());
1688 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001689 }
1690
Emilian Peev28ad2ea2017-02-07 16:14:32 +00001691 int deviceVersion = getDeviceVersion(id);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001692 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001693 case CAMERA_DEVICE_API_VERSION_1_0:
1694 case CAMERA_DEVICE_API_VERSION_3_0:
1695 case CAMERA_DEVICE_API_VERSION_3_1:
1696 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001697 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1698 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001699 *isSupported = false;
1700 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001701 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1702 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001703 *isSupported = true;
1704 }
1705 break;
1706 case CAMERA_DEVICE_API_VERSION_3_2:
1707 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001708 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001709 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1710 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001711 *isSupported = true;
1712 break;
1713 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001714 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001715 ALOGE("%s: %s", __FUNCTION__, msg.string());
1716 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001717 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001718 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001719 String8 msg = String8::format("Unknown device version %x for device %s",
1720 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001721 ALOGE("%s: %s", __FUNCTION__, msg.string());
1722 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1723 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001724 }
1725
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001726 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001727}
1728
Ruben Brunkcc776712015-02-17 20:18:47 -08001729void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001730 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001731 for (auto& i : mActiveClientManager.getAll()) {
1732 auto clientSp = i->getValue();
1733 if (clientSp.get() == client) {
1734 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001735 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001736 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001737}
1738
Ruben Brunkcc776712015-02-17 20:18:47 -08001739bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001740 bool ret = false;
1741 {
1742 // Acquire mServiceLock and prevent other clients from connecting
1743 std::unique_ptr<AutoConditionLock> lock =
1744 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001745
Igor Murashkin634a5152013-02-20 17:15:11 -08001746
Ruben Brunkcc776712015-02-17 20:18:47 -08001747 std::vector<sp<BasicClient>> evicted;
1748 for (auto& i : mActiveClientManager.getAll()) {
1749 auto clientSp = i->getValue();
1750 if (clientSp.get() == nullptr) {
1751 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1752 mActiveClientManager.remove(i);
1753 continue;
1754 }
Yin-Chia Yehdbfcb382018-02-16 11:17:36 -08001755 if (remote == clientSp->getRemote()) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001756 mActiveClientManager.remove(i);
1757 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001758
Ruben Brunkcc776712015-02-17 20:18:47 -08001759 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001760 clientSp->notifyError(
1761 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001762 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001763 }
1764 }
1765
Ruben Brunkcc776712015-02-17 20:18:47 -08001766 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1767 // other clients from connecting in mServiceLockWrapper if held
1768 mServiceLock.unlock();
1769
Ruben Brunk36597b22015-03-20 22:15:57 -07001770 // Do not clear caller identity, remote caller should be client proccess
1771
Ruben Brunkcc776712015-02-17 20:18:47 -08001772 for (auto& i : evicted) {
1773 if (i.get() != nullptr) {
1774 i->disconnect();
1775 ret = true;
1776 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001777 }
1778
Ruben Brunkcc776712015-02-17 20:18:47 -08001779 // Reacquire mServiceLock
1780 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001781
Ruben Brunkcc776712015-02-17 20:18:47 -08001782 } // lock is destroyed, allow further connect calls
1783
1784 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001785}
1786
Ruben Brunkcc776712015-02-17 20:18:47 -08001787std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1788 const String8& cameraId) const {
1789 std::shared_ptr<CameraState> state;
1790 {
1791 Mutex::Autolock lock(mCameraStatesLock);
1792 auto iter = mCameraStates.find(cameraId);
1793 if (iter != mCameraStates.end()) {
1794 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795 }
1796 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001797 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798}
1799
Ruben Brunkcc776712015-02-17 20:18:47 -08001800sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1801 // Remove from active clients list
1802 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1803 if (clientDescriptorPtr == nullptr) {
1804 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1805 cameraId.string());
1806 return sp<BasicClient>{nullptr};
1807 }
1808
1809 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001810}
1811
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001812void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001813 // Acquire mServiceLock and prevent other clients from connecting
1814 std::unique_ptr<AutoConditionLock> lock =
1815 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1816
Ruben Brunk6267b532015-04-30 17:44:07 -07001817 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001818 for (size_t i = 0; i < newUserIds.size(); i++) {
1819 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001820 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001821 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001822 return;
1823 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001824 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001825 }
1826
Ruben Brunka8ca9152015-04-07 14:23:40 -07001827
Ruben Brunk6267b532015-04-30 17:44:07 -07001828 if (newAllowedUsers == mAllowedUsers) {
1829 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1830 return;
1831 }
1832
1833 logUserSwitch(mAllowedUsers, newAllowedUsers);
1834
1835 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001836
1837 // Current user has switched, evict all current clients.
1838 std::vector<sp<BasicClient>> evicted;
1839 for (auto& i : mActiveClientManager.getAll()) {
1840 auto clientSp = i->getValue();
1841
1842 if (clientSp.get() == nullptr) {
1843 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1844 continue;
1845 }
1846
Ruben Brunk6267b532015-04-30 17:44:07 -07001847 // Don't evict clients that are still allowed.
1848 uid_t clientUid = clientSp->getClientUid();
1849 userid_t clientUserId = multiuser_get_user_id(clientUid);
1850 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1851 continue;
1852 }
1853
Ruben Brunk36597b22015-03-20 22:15:57 -07001854 evicted.push_back(clientSp);
1855
1856 String8 curTime = getFormattedCurrentTime();
1857
1858 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1859 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001860
Ruben Brunk36597b22015-03-20 22:15:57 -07001861 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001862 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001863 PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due"
1864 " to user switch.", i->getKey().string(),
1865 String8{clientSp->getPackageName()}.string(),
1866 i->getOwnerId(), i->getPriority().getScore(),
1867 i->getPriority().getState()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001868
1869 }
1870
1871 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1872 // blocking other clients from connecting in mServiceLockWrapper if held.
1873 mServiceLock.unlock();
1874
1875 // Clear caller identity temporarily so client disconnect PID checks work correctly
1876 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1877
1878 for (auto& i : evicted) {
1879 i->disconnect();
1880 }
1881
1882 IPCThreadState::self()->restoreCallingIdentity(token);
1883
1884 // Reacquire mServiceLock
1885 mServiceLock.lock();
1886}
Ruben Brunkcc776712015-02-17 20:18:47 -08001887
Ruben Brunka8ca9152015-04-07 14:23:40 -07001888void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001889 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001890 Mutex::Autolock l(mLogLock);
1891 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892}
1893
Ruben Brunka8ca9152015-04-07 14:23:40 -07001894void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001895 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001896 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001897 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001898 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001899}
1900
1901void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001902 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001903 // Log the clients evicted
1904 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001905 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001906}
1907
1908void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001909 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001910 // Log the client rejected
1911 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001912 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001913}
1914
Ruben Brunk6267b532015-04-30 17:44:07 -07001915void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1916 const std::set<userid_t>& newUserIds) {
1917 String8 newUsers = toString(newUserIds);
1918 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001919 if (oldUsers.size() == 0) {
1920 oldUsers = "<None>";
1921 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07001922 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001923 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07001924 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001925}
1926
1927void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1928 // Log the device removal
1929 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1930}
1931
1932void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1933 // Log the device removal
1934 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1935}
1936
1937void CameraService::logClientDied(int clientPid, const char* reason) {
1938 // Log the device removal
1939 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001940}
1941
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001942void CameraService::logServiceError(const char* msg, int errorCode) {
1943 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001944 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001945}
1946
Ruben Brunk36597b22015-03-20 22:15:57 -07001947status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1948 uint32_t flags) {
1949
1950 const int pid = getCallingPid();
1951 const int selfPid = getpid();
1952
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953 // Permission checks
1954 switch (code) {
Svet Ganova453d0d2018-01-11 15:37:58 -08001955 case SHELL_COMMAND_TRANSACTION: {
1956 int in = data.readFileDescriptor();
1957 int out = data.readFileDescriptor();
1958 int err = data.readFileDescriptor();
1959 int argc = data.readInt32();
1960 Vector<String16> args;
1961 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1962 args.add(data.readString16());
1963 }
1964 sp<IBinder> unusedCallback;
1965 sp<IResultReceiver> resultReceiver;
1966 status_t status;
1967 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1968 return status;
1969 }
1970 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1971 return status;
1972 }
1973 status = shellCommand(in, out, err, args);
1974 if (resultReceiver != nullptr) {
1975 resultReceiver->send(status);
1976 }
1977 return NO_ERROR;
1978 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001979 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001980 if (pid != selfPid) {
1981 // Ensure we're being called by system_server, or similar process with
1982 // permissions to notify the camera service about system events
1983 if (!checkCallingPermission(
1984 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1985 const int uid = getCallingUid();
1986 ALOGE("Permission Denial: cannot send updates to camera service about system"
1987 " events from pid=%d, uid=%d", pid, uid);
1988 return PERMISSION_DENIED;
1989 }
1990 }
1991 break;
1992 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001993 }
1994
1995 return BnCameraService::onTransact(code, data, reply, flags);
1996}
1997
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998// We share the media players for shutter and recording sound for all clients.
1999// A reference count is kept to determine when we will actually release the
2000// media players.
2001
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08002002MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08002004 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002005 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006 mp->prepare();
2007 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002008 ALOGE("Failed to load CameraService sounds: %s", file);
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002009 delete mp;
2010 return nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002011 }
2012 return mp;
2013}
2014
2015void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002016 ATRACE_CALL();
2017
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018 Mutex::Autolock lock(mSoundLock);
2019 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2020 if (mSoundRef++) return;
2021
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002022 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg");
2023 if (mSoundPlayer[SOUND_SHUTTER] == nullptr) {
2024 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
2025 }
2026 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg");
2027 if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) {
2028 mSoundPlayer[SOUND_RECORDING_START] =
2029 newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2030 }
2031 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg");
2032 if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) {
2033 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
2034 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035}
2036
2037void CameraService::releaseSound() {
2038 Mutex::Autolock lock(mSoundLock);
2039 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2040 if (--mSoundRef) return;
2041
2042 for (int i = 0; i < NUM_SOUNDS; i++) {
2043 if (mSoundPlayer[i] != 0) {
2044 mSoundPlayer[i]->disconnect();
2045 mSoundPlayer[i].clear();
2046 }
2047 }
2048}
2049
2050void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002051 ATRACE_CALL();
2052
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053 LOG1("playSound(%d)", kind);
2054 Mutex::Autolock lock(mSoundLock);
2055 sp<MediaPlayer> player = mSoundPlayer[kind];
2056 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002057 player->seekTo(0);
2058 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059 }
2060}
2061
2062// ----------------------------------------------------------------------------
2063
2064CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002065 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002066 const String16& clientPackageName,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002067 const String8& cameraIdStr,
2068 int api1CameraId, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002069 int clientPid, uid_t clientUid,
2070 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002071 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002072 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002073 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002074 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002075 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002076 servicePid),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002077 mCameraId(api1CameraId)
Igor Murashkin634a5152013-02-20 17:15:11 -08002078{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002080 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002081
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002082 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002083
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002085
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002086 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087}
2088
Mathias Agopian65ab4712010-07-14 17:59:35 -07002089// tear down the client
2090CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002091 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002092 mDestructionStarted = true;
2093
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002094 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002095 // unconditionally disconnect. function is idempotent
2096 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097}
2098
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002099sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2100
Igor Murashkin634a5152013-02-20 17:15:11 -08002101CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002102 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002103 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002104 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002105 int clientPid, uid_t clientUid,
2106 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002107 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2108 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2109 mServicePid(servicePid),
2110 mDisconnected(false),
2111 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002112{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002113 if (sCameraService == nullptr) {
2114 sCameraService = cameraService;
2115 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002116 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002117 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002118
2119 // In some cases the calling code has no access to the package it runs under.
2120 // For example, NDK camera API.
2121 // In this case we will get the packages for the calling UID and pick the first one
2122 // for attributing the app op. This will work correctly for runtime permissions
2123 // as for legacy apps we will toggle the app op for all packages in the UID.
2124 // The caveat is that the operation may be attributed to the wrong package and
2125 // stats based on app ops may be slightly off.
2126 if (mClientPackageName.size() <= 0) {
2127 sp<IServiceManager> sm = defaultServiceManager();
2128 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2129 if (binder == 0) {
2130 ALOGE("Cannot get permission service");
2131 // Leave mClientPackageName unchanged (empty) and the further interaction
2132 // with camera will fail in BasicClient::startCameraOps
2133 return;
2134 }
2135
2136 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2137 Vector<String16> packages;
2138
2139 permCtrl->getPackagesForUid(mClientUid, packages);
2140
2141 if (packages.isEmpty()) {
2142 ALOGE("No packages for calling UID");
2143 // Leave mClientPackageName unchanged (empty) and the further interaction
2144 // with camera will fail in BasicClient::startCameraOps
2145 return;
2146 }
2147 mClientPackageName = packages[0];
2148 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002149}
2150
2151CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002152 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002153 mDestructionStarted = true;
2154}
2155
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002156binder::Status CameraService::BasicClient::disconnect() {
2157 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002158 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002159 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002160 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002161 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002162
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002163 sCameraService->removeByClient(this);
2164 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002165 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002166
2167 sp<IBinder> remote = getRemote();
2168 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002169 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002170 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002171
2172 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002173 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002174 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2175 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2176 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002177
Igor Murashkincba2c162013-03-20 15:56:31 -07002178 // client shouldn't be able to call into us anymore
2179 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002180
2181 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002182}
2183
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002184status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2185 // No dumping of clients directly over Binder,
2186 // must go through CameraService::dump
2187 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2188 IPCThreadState::self()->getCallingUid(), NULL, 0);
2189 return OK;
2190}
2191
Ruben Brunkcc776712015-02-17 20:18:47 -08002192String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002193 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002194}
2195
2196
2197int CameraService::BasicClient::getClientPid() const {
2198 return mClientPid;
2199}
2200
Ruben Brunk6267b532015-04-30 17:44:07 -07002201uid_t CameraService::BasicClient::getClientUid() const {
2202 return mClientUid;
2203}
2204
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002205bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2206 // Defaults to API2.
2207 return level == API_2;
2208}
2209
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002210status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002211 ATRACE_CALL();
2212
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002213 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002214 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002215 mOpsCallback = new OpsCallback(this);
2216
Igor Murashkine6800ce2013-03-04 17:25:57 -08002217 {
2218 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002219 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002220 }
2221
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002222 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002223 mClientPackageName, mOpsCallback);
Svet Ganov1d519102018-02-26 10:48:14 -08002224 res = mAppOpsManager.startOpNoThrow(AppOpsManager::OP_CAMERA,
2225 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002226
Svetoslav28e8ef72015-05-11 19:21:31 -07002227 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002228 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2229 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002230 return PERMISSION_DENIED;
2231 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002232
Svetoslav28e8ef72015-05-11 19:21:31 -07002233 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002234 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2235 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002236 // Return the same error as for device policy manager rejection
2237 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002238 }
2239
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002240 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002241
2242 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002243 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002244
Emilian Peev573291c2018-02-10 02:10:56 +00002245 int apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1;
2246 if (canCastToApiClient(API_2)) {
2247 apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2;
2248 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002249 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002250 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
Emilian Peev573291c2018-02-10 02:10:56 +00002251 mCameraIdStr, mCameraFacing, mClientPackageName, apiLevel);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002252
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002253 return OK;
2254}
2255
2256status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002257 ATRACE_CALL();
2258
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002259 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002260 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002261 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002262 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002263 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002264 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002265
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002266 // This function is called when a client disconnects. This should
2267 // release the camera, but actually only if it was in a proper
2268 // functional state, i.e. with status NOT_AVAILABLE
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002269 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002270 StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002271
Ruben Brunkcc776712015-02-17 20:18:47 -08002272 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002273 sCameraService->updateStatus(StatusInternal::PRESENT,
2274 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002275
Emilian Peev573291c2018-02-10 02:10:56 +00002276 int apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1;
2277 if (canCastToApiClient(API_2)) {
2278 apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2;
2279 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002280 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002281 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
Emilian Peev573291c2018-02-10 02:10:56 +00002282 mCameraIdStr, mCameraFacing, mClientPackageName, apiLevel);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002283 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002284 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002285 if (mOpsCallback != NULL) {
2286 mAppOpsManager.stopWatchingMode(mOpsCallback);
2287 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002288 mOpsCallback.clear();
2289
2290 return OK;
2291}
2292
2293void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002294 ATRACE_CALL();
2295
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002296 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002297 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002298
2299 if (op != AppOpsManager::OP_CAMERA) {
2300 ALOGW("Unexpected app ops notification received: %d", op);
2301 return;
2302 }
2303
2304 int32_t res;
2305 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002306 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002307 ALOGV("checkOp returns: %d, %s ", res,
2308 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2309 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2310 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2311 "UNKNOWN");
2312
2313 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002314 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002315 myName.string());
Svet Ganova453d0d2018-01-11 15:37:58 -08002316 block();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002317 }
2318}
2319
Svet Ganova453d0d2018-01-11 15:37:58 -08002320void CameraService::BasicClient::block() {
2321 ATRACE_CALL();
2322
2323 // Reset the client PID to allow server-initiated disconnect,
2324 // and to prevent further calls by client.
2325 mClientPid = getCallingPid();
2326 CaptureResultExtras resultExtras; // a dummy result (invalid)
2327 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras);
2328 disconnect();
2329}
2330
Mathias Agopian65ab4712010-07-14 17:59:35 -07002331// ----------------------------------------------------------------------------
2332
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002333void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002334 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002335 (void) errorCode;
2336 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002337 if (mRemoteCallback != NULL) {
2338 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2339 } else {
2340 ALOGE("mRemoteCallback is NULL!!");
2341 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002342}
2343
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002344// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002345binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002346 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002347 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002348}
2349
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002350bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2351 return level == API_1;
2352}
2353
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002354CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2355 mClient(client) {
2356}
2357
2358void CameraService::Client::OpsCallback::opChanged(int32_t op,
2359 const String16& packageName) {
2360 sp<BasicClient> client = mClient.promote();
2361 if (client != NULL) {
2362 client->opChanged(op, packageName);
2363 }
2364}
2365
Mathias Agopian65ab4712010-07-14 17:59:35 -07002366// ----------------------------------------------------------------------------
Svet Ganova453d0d2018-01-11 15:37:58 -08002367// UidPolicy
2368// ----------------------------------------------------------------------------
2369
2370void CameraService::UidPolicy::registerSelf() {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002371 Mutex::Autolock _l(mUidLock);
2372
Svet Ganova453d0d2018-01-11 15:37:58 -08002373 ActivityManager am;
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002374 if (mRegistered) return;
Svet Ganova453d0d2018-01-11 15:37:58 -08002375 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
2376 | ActivityManager::UID_OBSERVER_IDLE
2377 | ActivityManager::UID_OBSERVER_ACTIVE,
2378 ActivityManager::PROCESS_STATE_UNKNOWN,
2379 String16("cameraserver"));
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002380 status_t res = am.linkToDeath(this);
2381 if (res == OK) {
2382 mRegistered = true;
2383 ALOGV("UidPolicy: Registered with ActivityManager");
2384 }
Svet Ganova453d0d2018-01-11 15:37:58 -08002385}
2386
2387void CameraService::UidPolicy::unregisterSelf() {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002388 Mutex::Autolock _l(mUidLock);
2389
Svet Ganova453d0d2018-01-11 15:37:58 -08002390 ActivityManager am;
2391 am.unregisterUidObserver(this);
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002392 am.unlinkToDeath(this);
2393 mRegistered = false;
2394 mActiveUids.clear();
2395 ALOGV("UidPolicy: Unregistered with ActivityManager");
Svet Ganova453d0d2018-01-11 15:37:58 -08002396}
2397
2398void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) {
2399 onUidIdle(uid, disabled);
2400}
2401
2402void CameraService::UidPolicy::onUidActive(uid_t uid) {
2403 Mutex::Autolock _l(mUidLock);
2404 mActiveUids.insert(uid);
2405}
2406
2407void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) {
2408 bool deleted = false;
2409 {
2410 Mutex::Autolock _l(mUidLock);
2411 if (mActiveUids.erase(uid) > 0) {
2412 deleted = true;
2413 }
2414 }
2415 if (deleted) {
2416 sp<CameraService> service = mService.promote();
2417 if (service != nullptr) {
2418 service->blockClientsForUid(uid);
2419 }
2420 }
2421}
2422
2423bool CameraService::UidPolicy::isUidActive(uid_t uid) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002424 Mutex::Autolock _l(mUidLock);
2425 return isUidActiveLocked(uid);
2426}
2427
2428bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid) {
2429 // Non-app UIDs are considered always active
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002430 // If activity manager is unreachable, assume everything is active
2431 if (uid < FIRST_APPLICATION_UID || !mRegistered) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002432 return true;
2433 }
2434 auto it = mOverrideUids.find(uid);
2435 if (it != mOverrideUids.end()) {
2436 return it->second;
2437 }
2438 return mActiveUids.find(uid) != mActiveUids.end();
2439}
2440
2441void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, bool active) {
2442 updateOverrideUid(uid, active, true);
2443}
2444
2445void CameraService::UidPolicy::removeOverrideUid(uid_t uid) {
2446 updateOverrideUid(uid, false, false);
2447}
2448
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002449void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) {
2450 Mutex::Autolock _l(mUidLock);
2451 ALOGV("UidPolicy: ActivityManager has died");
2452 mRegistered = false;
2453 mActiveUids.clear();
2454}
2455
Svet Ganova453d0d2018-01-11 15:37:58 -08002456void CameraService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
2457 bool wasActive = false;
2458 bool isActive = false;
2459 {
2460 Mutex::Autolock _l(mUidLock);
2461 wasActive = isUidActiveLocked(uid);
2462 mOverrideUids.erase(uid);
2463 if (insert) {
2464 mOverrideUids.insert(std::pair<uid_t, bool>(uid, active));
2465 }
2466 isActive = isUidActiveLocked(uid);
2467 }
2468 if (wasActive != isActive && !isActive) {
2469 sp<CameraService> service = mService.promote();
2470 if (service != nullptr) {
2471 service->blockClientsForUid(uid);
2472 }
2473 }
2474}
2475
2476// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002477// CameraState
2478// ----------------------------------------------------------------------------
2479
2480CameraService::CameraState::CameraState(const String8& id, int cost,
2481 const std::set<String8>& conflicting) : mId(id),
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002482 mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002483
2484CameraService::CameraState::~CameraState() {}
2485
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002486CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002487 Mutex::Autolock lock(mStatusLock);
2488 return mStatus;
2489}
2490
2491CameraParameters CameraService::CameraState::getShimParams() const {
2492 return mShimParams;
2493}
2494
2495void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2496 mShimParams = params;
2497}
2498
2499int CameraService::CameraState::getCost() const {
2500 return mCost;
2501}
2502
2503std::set<String8> CameraService::CameraState::getConflicting() const {
2504 return mConflicting;
2505}
2506
2507String8 CameraService::CameraState::getId() const {
2508 return mId;
2509}
2510
2511// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002512// ClientEventListener
2513// ----------------------------------------------------------------------------
2514
2515void CameraService::ClientEventListener::onClientAdded(
2516 const resource_policy::ClientDescriptor<String8,
2517 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002518 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002519 if (basicClient.get() != nullptr) {
2520 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2521 notifier.noteStartCamera(descriptor.getKey(),
2522 static_cast<int>(basicClient->getClientUid()));
2523 }
2524}
2525
2526void CameraService::ClientEventListener::onClientRemoved(
2527 const resource_policy::ClientDescriptor<String8,
2528 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002529 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002530 if (basicClient.get() != nullptr) {
2531 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2532 notifier.noteStopCamera(descriptor.getKey(),
2533 static_cast<int>(basicClient->getClientUid()));
2534 }
2535}
2536
2537
2538// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002539// CameraClientManager
2540// ----------------------------------------------------------------------------
2541
Ruben Brunk99e69712015-05-26 17:25:07 -07002542CameraService::CameraClientManager::CameraClientManager() {
2543 setListener(std::make_shared<ClientEventListener>());
2544}
2545
Ruben Brunkcc776712015-02-17 20:18:47 -08002546CameraService::CameraClientManager::~CameraClientManager() {}
2547
2548sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2549 const String8& id) const {
2550 auto descriptor = get(id);
2551 if (descriptor == nullptr) {
2552 return sp<BasicClient>{nullptr};
2553 }
2554 return descriptor->getValue();
2555}
2556
2557String8 CameraService::CameraClientManager::toString() const {
2558 auto all = getAll();
2559 String8 ret("[");
2560 bool hasAny = false;
2561 for (auto& i : all) {
2562 hasAny = true;
2563 String8 key = i->getKey();
2564 int32_t cost = i->getCost();
2565 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00002566 int32_t score = i->getPriority().getScore();
2567 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08002568 auto conflicting = i->getConflicting();
2569 auto clientSp = i->getValue();
2570 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002571 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002572 if (clientSp.get() != nullptr) {
2573 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002574 uid_t clientUid = clientSp->getClientUid();
2575 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002576 }
Emilian Peev8131a262017-02-01 12:33:43 +00002577 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
2578 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002579
Ruben Brunk6267b532015-04-30 17:44:07 -07002580 if (clientSp.get() != nullptr) {
2581 ret.appendFormat("User Id: %d, ", clientUserId);
2582 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002583 if (packageName.size() != 0) {
2584 ret.appendFormat("Client Package Name: %s", packageName.string());
2585 }
2586
2587 ret.append(", Conflicting Client Devices: {");
2588 for (auto& j : conflicting) {
2589 ret.appendFormat("%s, ", j.string());
2590 }
2591 ret.append("})");
2592 }
2593 if (hasAny) ret.append("\n");
2594 ret.append("]\n");
2595 return ret;
2596}
2597
2598CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2599 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00002600 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
2601 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002602
2603 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Emilian Peev8131a262017-02-01 12:33:43 +00002604 key, value, cost, conflictingKeys, score, ownerId, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002605}
2606
2607CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2608 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2609 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00002610 partial->getConflicting(), partial->getPriority().getScore(),
2611 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08002612}
2613
2614// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002615
2616static const int kDumpLockRetries = 50;
2617static const int kDumpLockSleep = 60000;
2618
2619static bool tryLock(Mutex& mutex)
2620{
2621 bool locked = false;
2622 for (int i = 0; i < kDumpLockRetries; ++i) {
2623 if (mutex.tryLock() == NO_ERROR) {
2624 locked = true;
2625 break;
2626 }
2627 usleep(kDumpLockSleep);
2628 }
2629 return locked;
2630}
2631
2632status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002633 ATRACE_CALL();
2634
Mathias Agopian65ab4712010-07-14 17:59:35 -07002635 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002636 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002637 getCallingPid(),
2638 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002639 return NO_ERROR;
2640 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002641 bool locked = tryLock(mServiceLock);
2642 // failed to lock - CameraService is probably deadlocked
2643 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002644 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002645 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002646
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002647 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002648 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002649
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002650 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002651 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002652
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002653 if (locked) mServiceLock.unlock();
2654 return NO_ERROR;
2655 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002656 dprintf(fd, "\n== Service global info: ==\n\n");
2657 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002658 dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size());
2659 for (size_t i = 0; i < mNormalDeviceIds.size(); i++) {
2660 dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str());
2661 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002662 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002663 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2664 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002665
2666 dumpEventLog(fd);
2667
2668 bool stateLocked = tryLock(mCameraStatesLock);
2669 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002670 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002671 }
2672
Emilian Peevbd8c5032018-02-14 23:05:40 +00002673 int argSize = args.size();
2674 for (int i = 0; i < argSize; i++) {
2675 if (args[i] == TagMonitor::kMonitorOption) {
2676 if (i + 1 < argSize) {
2677 mMonitorTags = String8(args[i + 1]);
2678 }
2679 break;
2680 }
2681 }
2682
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002683 for (auto& state : mCameraStates) {
2684 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002685
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002686 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002687
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002688 CameraParameters p = state.second->getShimParams();
2689 if (!p.isEmpty()) {
2690 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2691 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002692 }
2693
2694 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08002695 if (clientDescriptor != nullptr) {
2696 dprintf(fd, " Device %s is open. Client instance dump:\n",
2697 cameraId.string());
2698 dprintf(fd, " Client priority score: %d state: %d\n",
2699 clientDescriptor->getPriority().getScore(),
2700 clientDescriptor->getPriority().getState());
2701 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
2702
2703 auto client = clientDescriptor->getValue();
2704 dprintf(fd, " Client package: %s\n",
2705 String8(client->getPackageName()).string());
2706
2707 client->dumpClient(fd, args);
2708 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002709 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002710 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002711 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002712
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002713 }
2714
2715 if (stateLocked) mCameraStatesLock.unlock();
2716
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002717 if (locked) mServiceLock.unlock();
2718
Emilian Peevf53f66e2017-04-11 14:29:43 +01002719 mCameraProviderManager->dump(fd, args);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002720
2721 dprintf(fd, "\n== Vendor tags: ==\n\n");
2722
2723 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2724 if (desc == NULL) {
Emilian Peev71c73a22017-03-21 16:35:51 +00002725 sp<VendorTagDescriptorCache> cache =
2726 VendorTagDescriptorCache::getGlobalVendorTagCache();
2727 if (cache == NULL) {
2728 dprintf(fd, "No vendor tags.\n");
2729 } else {
2730 cache->dump(fd, /*verbosity*/2, /*indentation*/2);
2731 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002732 } else {
2733 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2734 }
2735
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002736 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002737 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002738 camera3::CameraTraces::dump(fd, args);
2739
2740 // Process dump arguments, if any
2741 int n = args.size();
2742 String16 verboseOption("-v");
2743 String16 unreachableOption("--unreachable");
2744 for (int i = 0; i < n; i++) {
2745 if (args[i] == verboseOption) {
2746 // change logging level
2747 if (i + 1 >= n) continue;
2748 String8 levelStr(args[i+1]);
2749 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002750 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002751 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002752 } else if (args[i] == unreachableOption) {
2753 // Dump memory analysis
2754 // TODO - should limit be an argument parameter?
2755 UnreachableMemoryInfo info;
2756 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2757 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002758 dprintf(fd, "\n== Unable to dump unreachable memory. "
2759 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002760 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002761 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002762 std::string s = info.ToString(/*log_contents*/ true);
2763 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002764 }
2765 }
2766 }
2767 return NO_ERROR;
2768}
2769
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002770void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002771 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002772
2773 Mutex::Autolock l(mLogLock);
2774 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002775 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002776 }
2777
2778 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002779 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002780 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002781 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002782 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002783 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002784}
2785
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002786void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002787 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002788 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2789 if (mTorchClientMap[i] == who) {
2790 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002791 String8 cameraId = mTorchClientMap.keyAt(i);
2792 status_t res = mFlashlight->setTorchMode(cameraId, false);
2793 if (res) {
2794 ALOGE("%s: torch client died but couldn't turn off torch: "
2795 "%s (%d)", __FUNCTION__, strerror(-res), res);
2796 return;
2797 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002798 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002799 break;
2800 }
2801 }
2802}
2803
Ruben Brunkcc776712015-02-17 20:18:47 -08002804/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002805
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002806 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002807 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2808 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002809 */
Yin-Chia Yehdbfcb382018-02-16 11:17:36 -08002810 // PID here is approximate and can be wrong.
Ruben Brunka8ca9152015-04-07 14:23:40 -07002811 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2812
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002813 // check torch client
2814 handleTorchClientBinderDied(who);
2815
2816 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002817 if(!evictClientIdByRemote(who)) {
2818 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002819 return;
2820 }
2821
Ruben Brunkcc776712015-02-17 20:18:47 -08002822 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2823 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002824}
2825
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002826void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002827 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002828}
2829
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002830void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2831 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002832 // Do not lock mServiceLock here or can get into a deadlock from
2833 // connect() -> disconnect -> updateStatus
2834
2835 auto state = getCameraState(cameraId);
2836
2837 if (state == nullptr) {
2838 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2839 cameraId.string());
2840 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002841 }
2842
Ruben Brunkcc776712015-02-17 20:18:47 -08002843 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2844 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2845 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002846 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002847
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002848 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002849 // Update torch status if it has a flash unit.
2850 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002851 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002852 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2853 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002854 TorchModeStatus newTorchStatus =
2855 status == StatusInternal::PRESENT ?
2856 TorchModeStatus::AVAILABLE_OFF :
2857 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002858 if (torchStatus != newTorchStatus) {
2859 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2860 }
2861 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002862 }
2863
2864 Mutex::Autolock lock(mStatusListenerLock);
2865
2866 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002867 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08002868 }
2869 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002870}
2871
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002872template<class Func>
2873void CameraService::CameraState::updateStatus(StatusInternal status,
2874 const String8& cameraId,
2875 std::initializer_list<StatusInternal> rejectSourceStates,
2876 Func onStatusUpdatedLocked) {
2877 Mutex::Autolock lock(mStatusLock);
2878 StatusInternal oldStatus = mStatus;
2879 mStatus = status;
2880
2881 if (oldStatus == status) {
2882 return;
2883 }
2884
2885 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
2886 cameraId.string(), oldStatus, status);
2887
2888 if (oldStatus == StatusInternal::NOT_PRESENT &&
2889 (status != StatusInternal::PRESENT &&
2890 status != StatusInternal::ENUMERATING)) {
2891
2892 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
2893 __FUNCTION__);
2894 mStatus = oldStatus;
2895 return;
2896 }
2897
2898 /**
2899 * Sometimes we want to conditionally do a transition.
2900 * For example if a client disconnects, we want to go to PRESENT
2901 * only if we weren't already in NOT_PRESENT or ENUMERATING.
2902 */
2903 for (auto& rejectStatus : rejectSourceStates) {
2904 if (oldStatus == rejectStatus) {
2905 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
2906 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
2907 mStatus = oldStatus;
2908 return;
2909 }
2910 }
2911
2912 onStatusUpdatedLocked(cameraId, status);
2913}
2914
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07002915void CameraService::updateProxyDeviceState(int newState,
Emilian Peev573291c2018-02-10 02:10:56 +00002916 const String8& cameraId, int facing, const String16& clientName, int apiLevel) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002917 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2918 if (proxyBinder == nullptr) return;
2919 String16 id(cameraId);
Emilian Peev573291c2018-02-10 02:10:56 +00002920 proxyBinder->notifyCameraState(id, newState, facing, clientName, apiLevel);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002921}
2922
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002923status_t CameraService::getTorchStatusLocked(
2924 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002925 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002926 if (!status) {
2927 return BAD_VALUE;
2928 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002929 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2930 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002931 // invalid camera ID or the camera doesn't have a flash unit
2932 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002933 }
2934
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002935 *status = mTorchStatusMap.valueAt(index);
2936 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002937}
2938
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002939status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002940 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002941 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2942 if (index == NAME_NOT_FOUND) {
2943 return BAD_VALUE;
2944 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002945 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002946
2947 return OK;
2948}
2949
Svet Ganova453d0d2018-01-11 15:37:58 -08002950void CameraService::blockClientsForUid(uid_t uid) {
2951 const auto clients = mActiveClientManager.getAll();
2952 for (auto& current : clients) {
2953 if (current != nullptr) {
2954 const auto basicClient = current->getValue();
2955 if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) {
2956 basicClient->block();
2957 }
2958 }
2959 }
2960}
2961
2962// NOTE: This is a remote API - make sure all args are validated
2963status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) {
2964 if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) {
2965 return PERMISSION_DENIED;
2966 }
2967 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
2968 return BAD_VALUE;
2969 }
2970 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
2971 return handleSetUidState(args, err);
2972 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
2973 return handleResetUidState(args, err);
2974 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
2975 return handleGetUidState(args, out, err);
2976 } else if (args.size() == 1 && args[0] == String16("help")) {
2977 printHelp(out);
2978 return NO_ERROR;
2979 }
2980 printHelp(err);
2981 return BAD_VALUE;
2982}
2983
2984status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) {
2985 PermissionController pc;
2986 int uid = pc.getPackageUid(args[1], 0);
2987 if (uid <= 0) {
2988 ALOGE("Unknown package: '%s'", String8(args[1]).string());
2989 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
2990 return BAD_VALUE;
2991 }
2992 bool active = false;
2993 if (args[2] == String16("active")) {
2994 active = true;
2995 } else if ((args[2] != String16("idle"))) {
2996 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
2997 return BAD_VALUE;
2998 }
2999 mUidPolicy->addOverrideUid(uid, active);
3000 return NO_ERROR;
3001}
3002
3003status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) {
3004 PermissionController pc;
3005 int uid = pc.getPackageUid(args[1], 0);
3006 if (uid < 0) {
3007 ALOGE("Unknown package: '%s'", String8(args[1]).string());
3008 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
3009 return BAD_VALUE;
3010 }
3011 mUidPolicy->removeOverrideUid(uid);
3012 return NO_ERROR;
3013}
3014
3015status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) {
3016 PermissionController pc;
3017 int uid = pc.getPackageUid(args[1], 0);
3018 if (uid <= 0) {
3019 ALOGE("Unknown package: '%s'", String8(args[1]).string());
3020 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
3021 return BAD_VALUE;
3022 }
3023 if (mUidPolicy->isUidActive(uid)) {
3024 return dprintf(out, "active\n");
3025 } else {
3026 return dprintf(out, "idle\n");
3027 }
3028}
3029
3030status_t CameraService::printHelp(int out) {
3031 return dprintf(out, "Camera service commands:\n"
3032 " get-uid-state <PACKAGE> gets the uid state\n"
3033 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
3034 " reset-uid-state <PACKAGE> clears the uid state override\n"
3035 " help print this message\n");
3036}
3037
Mathias Agopian65ab4712010-07-14 17:59:35 -07003038}; // namespace android