blob: 9ed6d737f335de6cd6796628a0ca4531bd3b2ac1 [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
Jaekyun Seokef498052018-03-23 13:09:44 +09002002sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) {
2003 sp<MediaPlayer> mp = new MediaPlayer();
2004 status_t error;
2005 if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002006 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Jaekyun Seokef498052018-03-23 13:09:44 +09002007 error = mp->prepare();
2008 }
2009 if (error != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00002010 ALOGE("Failed to load CameraService sounds: %s", file);
Jaekyun Seokef498052018-03-23 13:09:44 +09002011 mp->disconnect();
2012 mp.clear();
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002013 return nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002014 }
2015 return mp;
2016}
2017
2018void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002019 ATRACE_CALL();
2020
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021 Mutex::Autolock lock(mSoundLock);
2022 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2023 if (mSoundRef++) return;
2024
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002025 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg");
2026 if (mSoundPlayer[SOUND_SHUTTER] == nullptr) {
2027 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
2028 }
2029 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg");
2030 if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) {
2031 mSoundPlayer[SOUND_RECORDING_START] =
2032 newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2033 }
2034 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg");
2035 if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) {
2036 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
2037 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038}
2039
2040void CameraService::releaseSound() {
2041 Mutex::Autolock lock(mSoundLock);
2042 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2043 if (--mSoundRef) return;
2044
2045 for (int i = 0; i < NUM_SOUNDS; i++) {
2046 if (mSoundPlayer[i] != 0) {
2047 mSoundPlayer[i]->disconnect();
2048 mSoundPlayer[i].clear();
2049 }
2050 }
2051}
2052
2053void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002054 ATRACE_CALL();
2055
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056 LOG1("playSound(%d)", kind);
2057 Mutex::Autolock lock(mSoundLock);
2058 sp<MediaPlayer> player = mSoundPlayer[kind];
2059 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002060 player->seekTo(0);
2061 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062 }
2063}
2064
2065// ----------------------------------------------------------------------------
2066
2067CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002068 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002069 const String16& clientPackageName,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002070 const String8& cameraIdStr,
2071 int api1CameraId, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002072 int clientPid, uid_t clientUid,
2073 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002074 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002075 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002076 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002077 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002078 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002079 servicePid),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002080 mCameraId(api1CameraId)
Igor Murashkin634a5152013-02-20 17:15:11 -08002081{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002083 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002084
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002085 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002086
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002088
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002089 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090}
2091
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092// tear down the client
2093CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002094 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002095 mDestructionStarted = true;
2096
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002097 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002098 // unconditionally disconnect. function is idempotent
2099 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002100}
2101
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002102sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2103
Igor Murashkin634a5152013-02-20 17:15:11 -08002104CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002105 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002106 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002107 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002108 int clientPid, uid_t clientUid,
2109 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002110 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2111 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2112 mServicePid(servicePid),
2113 mDisconnected(false),
2114 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002115{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002116 if (sCameraService == nullptr) {
2117 sCameraService = cameraService;
2118 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002119 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002120 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002121
2122 // In some cases the calling code has no access to the package it runs under.
2123 // For example, NDK camera API.
2124 // In this case we will get the packages for the calling UID and pick the first one
2125 // for attributing the app op. This will work correctly for runtime permissions
2126 // as for legacy apps we will toggle the app op for all packages in the UID.
2127 // The caveat is that the operation may be attributed to the wrong package and
2128 // stats based on app ops may be slightly off.
2129 if (mClientPackageName.size() <= 0) {
2130 sp<IServiceManager> sm = defaultServiceManager();
2131 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2132 if (binder == 0) {
2133 ALOGE("Cannot get permission service");
2134 // Leave mClientPackageName unchanged (empty) and the further interaction
2135 // with camera will fail in BasicClient::startCameraOps
2136 return;
2137 }
2138
2139 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2140 Vector<String16> packages;
2141
2142 permCtrl->getPackagesForUid(mClientUid, packages);
2143
2144 if (packages.isEmpty()) {
2145 ALOGE("No packages for calling UID");
2146 // Leave mClientPackageName unchanged (empty) and the further interaction
2147 // with camera will fail in BasicClient::startCameraOps
2148 return;
2149 }
2150 mClientPackageName = packages[0];
2151 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002152}
2153
2154CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002155 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002156 mDestructionStarted = true;
2157}
2158
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002159binder::Status CameraService::BasicClient::disconnect() {
2160 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002161 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002162 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002163 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002164 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002165
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002166 sCameraService->removeByClient(this);
2167 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002168 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002169
2170 sp<IBinder> remote = getRemote();
2171 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002172 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002173 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002174
2175 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002176 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002177 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2178 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2179 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002180
Igor Murashkincba2c162013-03-20 15:56:31 -07002181 // client shouldn't be able to call into us anymore
2182 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002183
2184 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002185}
2186
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002187status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2188 // No dumping of clients directly over Binder,
2189 // must go through CameraService::dump
2190 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2191 IPCThreadState::self()->getCallingUid(), NULL, 0);
2192 return OK;
2193}
2194
Ruben Brunkcc776712015-02-17 20:18:47 -08002195String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002196 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002197}
2198
2199
2200int CameraService::BasicClient::getClientPid() const {
2201 return mClientPid;
2202}
2203
Ruben Brunk6267b532015-04-30 17:44:07 -07002204uid_t CameraService::BasicClient::getClientUid() const {
2205 return mClientUid;
2206}
2207
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002208bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2209 // Defaults to API2.
2210 return level == API_2;
2211}
2212
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002213status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002214 ATRACE_CALL();
2215
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002216 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002217 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002218 mOpsCallback = new OpsCallback(this);
2219
Igor Murashkine6800ce2013-03-04 17:25:57 -08002220 {
2221 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002222 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002223 }
2224
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002225 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002226 mClientPackageName, mOpsCallback);
Svet Ganov1d519102018-02-26 10:48:14 -08002227 res = mAppOpsManager.startOpNoThrow(AppOpsManager::OP_CAMERA,
2228 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002229
Svetoslav28e8ef72015-05-11 19:21:31 -07002230 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002231 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2232 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002233 return PERMISSION_DENIED;
2234 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002235
Svetoslav28e8ef72015-05-11 19:21:31 -07002236 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002237 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2238 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002239 // Return the same error as for device policy manager rejection
2240 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002241 }
2242
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002243 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002244
2245 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002246 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002247
Emilian Peev573291c2018-02-10 02:10:56 +00002248 int apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1;
2249 if (canCastToApiClient(API_2)) {
2250 apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2;
2251 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002252 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002253 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
Emilian Peev573291c2018-02-10 02:10:56 +00002254 mCameraIdStr, mCameraFacing, mClientPackageName, apiLevel);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002255
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002256 return OK;
2257}
2258
2259status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002260 ATRACE_CALL();
2261
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002262 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002263 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002264 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002265 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002266 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002267 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002268
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002269 // This function is called when a client disconnects. This should
2270 // release the camera, but actually only if it was in a proper
2271 // functional state, i.e. with status NOT_AVAILABLE
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002272 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002273 StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002274
Ruben Brunkcc776712015-02-17 20:18:47 -08002275 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002276 sCameraService->updateStatus(StatusInternal::PRESENT,
2277 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002278
Emilian Peev573291c2018-02-10 02:10:56 +00002279 int apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1;
2280 if (canCastToApiClient(API_2)) {
2281 apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2;
2282 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002283 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002284 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
Emilian Peev573291c2018-02-10 02:10:56 +00002285 mCameraIdStr, mCameraFacing, mClientPackageName, apiLevel);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002286 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002287 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002288 if (mOpsCallback != NULL) {
2289 mAppOpsManager.stopWatchingMode(mOpsCallback);
2290 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002291 mOpsCallback.clear();
2292
2293 return OK;
2294}
2295
2296void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002297 ATRACE_CALL();
2298
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002299 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002300 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002301
2302 if (op != AppOpsManager::OP_CAMERA) {
2303 ALOGW("Unexpected app ops notification received: %d", op);
2304 return;
2305 }
2306
2307 int32_t res;
2308 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002309 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002310 ALOGV("checkOp returns: %d, %s ", res,
2311 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2312 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2313 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2314 "UNKNOWN");
2315
2316 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002317 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002318 myName.string());
Svet Ganova453d0d2018-01-11 15:37:58 -08002319 block();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002320 }
2321}
2322
Svet Ganova453d0d2018-01-11 15:37:58 -08002323void CameraService::BasicClient::block() {
2324 ATRACE_CALL();
2325
2326 // Reset the client PID to allow server-initiated disconnect,
2327 // and to prevent further calls by client.
2328 mClientPid = getCallingPid();
2329 CaptureResultExtras resultExtras; // a dummy result (invalid)
2330 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras);
2331 disconnect();
2332}
2333
Mathias Agopian65ab4712010-07-14 17:59:35 -07002334// ----------------------------------------------------------------------------
2335
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002336void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002337 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002338 (void) errorCode;
2339 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002340 if (mRemoteCallback != NULL) {
2341 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2342 } else {
2343 ALOGE("mRemoteCallback is NULL!!");
2344 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002345}
2346
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002347// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002348binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002349 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002350 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002351}
2352
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002353bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2354 return level == API_1;
2355}
2356
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002357CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2358 mClient(client) {
2359}
2360
2361void CameraService::Client::OpsCallback::opChanged(int32_t op,
2362 const String16& packageName) {
2363 sp<BasicClient> client = mClient.promote();
2364 if (client != NULL) {
2365 client->opChanged(op, packageName);
2366 }
2367}
2368
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369// ----------------------------------------------------------------------------
Svet Ganova453d0d2018-01-11 15:37:58 -08002370// UidPolicy
2371// ----------------------------------------------------------------------------
2372
2373void CameraService::UidPolicy::registerSelf() {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002374 Mutex::Autolock _l(mUidLock);
2375
Svet Ganova453d0d2018-01-11 15:37:58 -08002376 ActivityManager am;
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002377 if (mRegistered) return;
Svet Ganova453d0d2018-01-11 15:37:58 -08002378 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
2379 | ActivityManager::UID_OBSERVER_IDLE
2380 | ActivityManager::UID_OBSERVER_ACTIVE,
2381 ActivityManager::PROCESS_STATE_UNKNOWN,
2382 String16("cameraserver"));
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002383 status_t res = am.linkToDeath(this);
2384 if (res == OK) {
2385 mRegistered = true;
2386 ALOGV("UidPolicy: Registered with ActivityManager");
2387 }
Svet Ganova453d0d2018-01-11 15:37:58 -08002388}
2389
2390void CameraService::UidPolicy::unregisterSelf() {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002391 Mutex::Autolock _l(mUidLock);
2392
Svet Ganova453d0d2018-01-11 15:37:58 -08002393 ActivityManager am;
2394 am.unregisterUidObserver(this);
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002395 am.unlinkToDeath(this);
2396 mRegistered = false;
2397 mActiveUids.clear();
2398 ALOGV("UidPolicy: Unregistered with ActivityManager");
Svet Ganova453d0d2018-01-11 15:37:58 -08002399}
2400
2401void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) {
2402 onUidIdle(uid, disabled);
2403}
2404
2405void CameraService::UidPolicy::onUidActive(uid_t uid) {
2406 Mutex::Autolock _l(mUidLock);
2407 mActiveUids.insert(uid);
2408}
2409
2410void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) {
2411 bool deleted = false;
2412 {
2413 Mutex::Autolock _l(mUidLock);
2414 if (mActiveUids.erase(uid) > 0) {
2415 deleted = true;
2416 }
2417 }
2418 if (deleted) {
2419 sp<CameraService> service = mService.promote();
2420 if (service != nullptr) {
2421 service->blockClientsForUid(uid);
2422 }
2423 }
2424}
2425
2426bool CameraService::UidPolicy::isUidActive(uid_t uid) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002427 Mutex::Autolock _l(mUidLock);
2428 return isUidActiveLocked(uid);
2429}
2430
2431bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid) {
2432 // Non-app UIDs are considered always active
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002433 // If activity manager is unreachable, assume everything is active
2434 if (uid < FIRST_APPLICATION_UID || !mRegistered) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002435 return true;
2436 }
2437 auto it = mOverrideUids.find(uid);
2438 if (it != mOverrideUids.end()) {
2439 return it->second;
2440 }
2441 return mActiveUids.find(uid) != mActiveUids.end();
2442}
2443
2444void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid, bool active) {
2445 updateOverrideUid(uid, active, true);
2446}
2447
2448void CameraService::UidPolicy::removeOverrideUid(uid_t uid) {
2449 updateOverrideUid(uid, false, false);
2450}
2451
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002452void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) {
2453 Mutex::Autolock _l(mUidLock);
2454 ALOGV("UidPolicy: ActivityManager has died");
2455 mRegistered = false;
2456 mActiveUids.clear();
2457}
2458
Svet Ganova453d0d2018-01-11 15:37:58 -08002459void CameraService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
2460 bool wasActive = false;
2461 bool isActive = false;
2462 {
2463 Mutex::Autolock _l(mUidLock);
2464 wasActive = isUidActiveLocked(uid);
2465 mOverrideUids.erase(uid);
2466 if (insert) {
2467 mOverrideUids.insert(std::pair<uid_t, bool>(uid, active));
2468 }
2469 isActive = isUidActiveLocked(uid);
2470 }
2471 if (wasActive != isActive && !isActive) {
2472 sp<CameraService> service = mService.promote();
2473 if (service != nullptr) {
2474 service->blockClientsForUid(uid);
2475 }
2476 }
2477}
2478
2479// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002480// CameraState
2481// ----------------------------------------------------------------------------
2482
2483CameraService::CameraState::CameraState(const String8& id, int cost,
2484 const std::set<String8>& conflicting) : mId(id),
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002485 mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002486
2487CameraService::CameraState::~CameraState() {}
2488
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002489CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002490 Mutex::Autolock lock(mStatusLock);
2491 return mStatus;
2492}
2493
2494CameraParameters CameraService::CameraState::getShimParams() const {
2495 return mShimParams;
2496}
2497
2498void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2499 mShimParams = params;
2500}
2501
2502int CameraService::CameraState::getCost() const {
2503 return mCost;
2504}
2505
2506std::set<String8> CameraService::CameraState::getConflicting() const {
2507 return mConflicting;
2508}
2509
2510String8 CameraService::CameraState::getId() const {
2511 return mId;
2512}
2513
2514// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002515// ClientEventListener
2516// ----------------------------------------------------------------------------
2517
2518void CameraService::ClientEventListener::onClientAdded(
2519 const resource_policy::ClientDescriptor<String8,
2520 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002521 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002522 if (basicClient.get() != nullptr) {
2523 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2524 notifier.noteStartCamera(descriptor.getKey(),
2525 static_cast<int>(basicClient->getClientUid()));
2526 }
2527}
2528
2529void CameraService::ClientEventListener::onClientRemoved(
2530 const resource_policy::ClientDescriptor<String8,
2531 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002532 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002533 if (basicClient.get() != nullptr) {
2534 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2535 notifier.noteStopCamera(descriptor.getKey(),
2536 static_cast<int>(basicClient->getClientUid()));
2537 }
2538}
2539
2540
2541// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002542// CameraClientManager
2543// ----------------------------------------------------------------------------
2544
Ruben Brunk99e69712015-05-26 17:25:07 -07002545CameraService::CameraClientManager::CameraClientManager() {
2546 setListener(std::make_shared<ClientEventListener>());
2547}
2548
Ruben Brunkcc776712015-02-17 20:18:47 -08002549CameraService::CameraClientManager::~CameraClientManager() {}
2550
2551sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2552 const String8& id) const {
2553 auto descriptor = get(id);
2554 if (descriptor == nullptr) {
2555 return sp<BasicClient>{nullptr};
2556 }
2557 return descriptor->getValue();
2558}
2559
2560String8 CameraService::CameraClientManager::toString() const {
2561 auto all = getAll();
2562 String8 ret("[");
2563 bool hasAny = false;
2564 for (auto& i : all) {
2565 hasAny = true;
2566 String8 key = i->getKey();
2567 int32_t cost = i->getCost();
2568 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00002569 int32_t score = i->getPriority().getScore();
2570 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08002571 auto conflicting = i->getConflicting();
2572 auto clientSp = i->getValue();
2573 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002574 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002575 if (clientSp.get() != nullptr) {
2576 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002577 uid_t clientUid = clientSp->getClientUid();
2578 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002579 }
Emilian Peev8131a262017-02-01 12:33:43 +00002580 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
2581 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002582
Ruben Brunk6267b532015-04-30 17:44:07 -07002583 if (clientSp.get() != nullptr) {
2584 ret.appendFormat("User Id: %d, ", clientUserId);
2585 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002586 if (packageName.size() != 0) {
2587 ret.appendFormat("Client Package Name: %s", packageName.string());
2588 }
2589
2590 ret.append(", Conflicting Client Devices: {");
2591 for (auto& j : conflicting) {
2592 ret.appendFormat("%s, ", j.string());
2593 }
2594 ret.append("})");
2595 }
2596 if (hasAny) ret.append("\n");
2597 ret.append("]\n");
2598 return ret;
2599}
2600
2601CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2602 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00002603 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
2604 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002605
2606 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Emilian Peev8131a262017-02-01 12:33:43 +00002607 key, value, cost, conflictingKeys, score, ownerId, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002608}
2609
2610CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2611 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2612 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00002613 partial->getConflicting(), partial->getPriority().getScore(),
2614 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08002615}
2616
2617// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002618
2619static const int kDumpLockRetries = 50;
2620static const int kDumpLockSleep = 60000;
2621
2622static bool tryLock(Mutex& mutex)
2623{
2624 bool locked = false;
2625 for (int i = 0; i < kDumpLockRetries; ++i) {
2626 if (mutex.tryLock() == NO_ERROR) {
2627 locked = true;
2628 break;
2629 }
2630 usleep(kDumpLockSleep);
2631 }
2632 return locked;
2633}
2634
2635status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002636 ATRACE_CALL();
2637
Mathias Agopian65ab4712010-07-14 17:59:35 -07002638 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002639 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002640 getCallingPid(),
2641 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002642 return NO_ERROR;
2643 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002644 bool locked = tryLock(mServiceLock);
2645 // failed to lock - CameraService is probably deadlocked
2646 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002647 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002648 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002649
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002650 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002651 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002652
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002653 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002654 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002655
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002656 if (locked) mServiceLock.unlock();
2657 return NO_ERROR;
2658 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002659 dprintf(fd, "\n== Service global info: ==\n\n");
2660 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002661 dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size());
2662 for (size_t i = 0; i < mNormalDeviceIds.size(); i++) {
2663 dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str());
2664 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002665 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002666 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2667 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002668
2669 dumpEventLog(fd);
2670
2671 bool stateLocked = tryLock(mCameraStatesLock);
2672 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002673 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002674 }
2675
Emilian Peevbd8c5032018-02-14 23:05:40 +00002676 int argSize = args.size();
2677 for (int i = 0; i < argSize; i++) {
2678 if (args[i] == TagMonitor::kMonitorOption) {
2679 if (i + 1 < argSize) {
2680 mMonitorTags = String8(args[i + 1]);
2681 }
2682 break;
2683 }
2684 }
2685
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002686 for (auto& state : mCameraStates) {
2687 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002688
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002689 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002690
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002691 CameraParameters p = state.second->getShimParams();
2692 if (!p.isEmpty()) {
2693 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2694 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002695 }
2696
2697 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08002698 if (clientDescriptor != nullptr) {
2699 dprintf(fd, " Device %s is open. Client instance dump:\n",
2700 cameraId.string());
2701 dprintf(fd, " Client priority score: %d state: %d\n",
2702 clientDescriptor->getPriority().getScore(),
2703 clientDescriptor->getPriority().getState());
2704 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
2705
2706 auto client = clientDescriptor->getValue();
2707 dprintf(fd, " Client package: %s\n",
2708 String8(client->getPackageName()).string());
2709
2710 client->dumpClient(fd, args);
2711 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002712 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002713 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002714 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002715
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002716 }
2717
2718 if (stateLocked) mCameraStatesLock.unlock();
2719
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002720 if (locked) mServiceLock.unlock();
2721
Emilian Peevf53f66e2017-04-11 14:29:43 +01002722 mCameraProviderManager->dump(fd, args);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002723
2724 dprintf(fd, "\n== Vendor tags: ==\n\n");
2725
2726 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2727 if (desc == NULL) {
Emilian Peev71c73a22017-03-21 16:35:51 +00002728 sp<VendorTagDescriptorCache> cache =
2729 VendorTagDescriptorCache::getGlobalVendorTagCache();
2730 if (cache == NULL) {
2731 dprintf(fd, "No vendor tags.\n");
2732 } else {
2733 cache->dump(fd, /*verbosity*/2, /*indentation*/2);
2734 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002735 } else {
2736 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2737 }
2738
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002739 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002740 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002741 camera3::CameraTraces::dump(fd, args);
2742
2743 // Process dump arguments, if any
2744 int n = args.size();
2745 String16 verboseOption("-v");
2746 String16 unreachableOption("--unreachable");
2747 for (int i = 0; i < n; i++) {
2748 if (args[i] == verboseOption) {
2749 // change logging level
2750 if (i + 1 >= n) continue;
2751 String8 levelStr(args[i+1]);
2752 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002753 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002754 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002755 } else if (args[i] == unreachableOption) {
2756 // Dump memory analysis
2757 // TODO - should limit be an argument parameter?
2758 UnreachableMemoryInfo info;
2759 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2760 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002761 dprintf(fd, "\n== Unable to dump unreachable memory. "
2762 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002763 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002764 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002765 std::string s = info.ToString(/*log_contents*/ true);
2766 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002767 }
2768 }
2769 }
2770 return NO_ERROR;
2771}
2772
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002773void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002774 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002775
2776 Mutex::Autolock l(mLogLock);
2777 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002778 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002779 }
2780
2781 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002782 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002783 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002784 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002785 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002786 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002787}
2788
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002789void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002790 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002791 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2792 if (mTorchClientMap[i] == who) {
2793 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002794 String8 cameraId = mTorchClientMap.keyAt(i);
2795 status_t res = mFlashlight->setTorchMode(cameraId, false);
2796 if (res) {
2797 ALOGE("%s: torch client died but couldn't turn off torch: "
2798 "%s (%d)", __FUNCTION__, strerror(-res), res);
2799 return;
2800 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002801 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002802 break;
2803 }
2804 }
2805}
2806
Ruben Brunkcc776712015-02-17 20:18:47 -08002807/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002808
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002809 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002810 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2811 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002812 */
Yin-Chia Yehdbfcb382018-02-16 11:17:36 -08002813 // PID here is approximate and can be wrong.
Ruben Brunka8ca9152015-04-07 14:23:40 -07002814 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2815
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002816 // check torch client
2817 handleTorchClientBinderDied(who);
2818
2819 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002820 if(!evictClientIdByRemote(who)) {
2821 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002822 return;
2823 }
2824
Ruben Brunkcc776712015-02-17 20:18:47 -08002825 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2826 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002827}
2828
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002829void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002830 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002831}
2832
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002833void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2834 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002835 // Do not lock mServiceLock here or can get into a deadlock from
2836 // connect() -> disconnect -> updateStatus
2837
2838 auto state = getCameraState(cameraId);
2839
2840 if (state == nullptr) {
2841 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2842 cameraId.string());
2843 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002844 }
2845
Ruben Brunkcc776712015-02-17 20:18:47 -08002846 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2847 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2848 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002849 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002850
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002851 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002852 // Update torch status if it has a flash unit.
2853 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002854 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002855 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2856 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002857 TorchModeStatus newTorchStatus =
2858 status == StatusInternal::PRESENT ?
2859 TorchModeStatus::AVAILABLE_OFF :
2860 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002861 if (torchStatus != newTorchStatus) {
2862 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2863 }
2864 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002865 }
2866
2867 Mutex::Autolock lock(mStatusListenerLock);
2868
2869 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002870 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08002871 }
2872 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002873}
2874
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002875template<class Func>
2876void CameraService::CameraState::updateStatus(StatusInternal status,
2877 const String8& cameraId,
2878 std::initializer_list<StatusInternal> rejectSourceStates,
2879 Func onStatusUpdatedLocked) {
2880 Mutex::Autolock lock(mStatusLock);
2881 StatusInternal oldStatus = mStatus;
2882 mStatus = status;
2883
2884 if (oldStatus == status) {
2885 return;
2886 }
2887
2888 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
2889 cameraId.string(), oldStatus, status);
2890
2891 if (oldStatus == StatusInternal::NOT_PRESENT &&
2892 (status != StatusInternal::PRESENT &&
2893 status != StatusInternal::ENUMERATING)) {
2894
2895 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
2896 __FUNCTION__);
2897 mStatus = oldStatus;
2898 return;
2899 }
2900
2901 /**
2902 * Sometimes we want to conditionally do a transition.
2903 * For example if a client disconnects, we want to go to PRESENT
2904 * only if we weren't already in NOT_PRESENT or ENUMERATING.
2905 */
2906 for (auto& rejectStatus : rejectSourceStates) {
2907 if (oldStatus == rejectStatus) {
2908 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
2909 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
2910 mStatus = oldStatus;
2911 return;
2912 }
2913 }
2914
2915 onStatusUpdatedLocked(cameraId, status);
2916}
2917
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07002918void CameraService::updateProxyDeviceState(int newState,
Emilian Peev573291c2018-02-10 02:10:56 +00002919 const String8& cameraId, int facing, const String16& clientName, int apiLevel) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002920 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2921 if (proxyBinder == nullptr) return;
2922 String16 id(cameraId);
Emilian Peev573291c2018-02-10 02:10:56 +00002923 proxyBinder->notifyCameraState(id, newState, facing, clientName, apiLevel);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002924}
2925
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002926status_t CameraService::getTorchStatusLocked(
2927 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002928 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002929 if (!status) {
2930 return BAD_VALUE;
2931 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002932 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2933 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002934 // invalid camera ID or the camera doesn't have a flash unit
2935 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002936 }
2937
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002938 *status = mTorchStatusMap.valueAt(index);
2939 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002940}
2941
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002942status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002943 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002944 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2945 if (index == NAME_NOT_FOUND) {
2946 return BAD_VALUE;
2947 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002948 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002949
2950 return OK;
2951}
2952
Svet Ganova453d0d2018-01-11 15:37:58 -08002953void CameraService::blockClientsForUid(uid_t uid) {
2954 const auto clients = mActiveClientManager.getAll();
2955 for (auto& current : clients) {
2956 if (current != nullptr) {
2957 const auto basicClient = current->getValue();
2958 if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) {
2959 basicClient->block();
2960 }
2961 }
2962 }
2963}
2964
2965// NOTE: This is a remote API - make sure all args are validated
2966status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) {
2967 if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) {
2968 return PERMISSION_DENIED;
2969 }
2970 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
2971 return BAD_VALUE;
2972 }
2973 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
2974 return handleSetUidState(args, err);
2975 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
2976 return handleResetUidState(args, err);
2977 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
2978 return handleGetUidState(args, out, err);
2979 } else if (args.size() == 1 && args[0] == String16("help")) {
2980 printHelp(out);
2981 return NO_ERROR;
2982 }
2983 printHelp(err);
2984 return BAD_VALUE;
2985}
2986
2987status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) {
2988 PermissionController pc;
2989 int uid = pc.getPackageUid(args[1], 0);
2990 if (uid <= 0) {
2991 ALOGE("Unknown package: '%s'", String8(args[1]).string());
2992 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
2993 return BAD_VALUE;
2994 }
2995 bool active = false;
2996 if (args[2] == String16("active")) {
2997 active = true;
2998 } else if ((args[2] != String16("idle"))) {
2999 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
3000 return BAD_VALUE;
3001 }
3002 mUidPolicy->addOverrideUid(uid, active);
3003 return NO_ERROR;
3004}
3005
3006status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) {
3007 PermissionController pc;
3008 int uid = pc.getPackageUid(args[1], 0);
3009 if (uid < 0) {
3010 ALOGE("Unknown package: '%s'", String8(args[1]).string());
3011 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
3012 return BAD_VALUE;
3013 }
3014 mUidPolicy->removeOverrideUid(uid);
3015 return NO_ERROR;
3016}
3017
3018status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) {
3019 PermissionController pc;
3020 int uid = pc.getPackageUid(args[1], 0);
3021 if (uid <= 0) {
3022 ALOGE("Unknown package: '%s'", String8(args[1]).string());
3023 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
3024 return BAD_VALUE;
3025 }
3026 if (mUidPolicy->isUidActive(uid)) {
3027 return dprintf(out, "active\n");
3028 } else {
3029 return dprintf(out, "idle\n");
3030 }
3031}
3032
3033status_t CameraService::printHelp(int out) {
3034 return dprintf(out, "Camera service commands:\n"
3035 " get-uid-state <PACKAGE> gets the uid state\n"
3036 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
3037 " reset-uid-state <PACKAGE> clears the uid state override\n"
3038 " help print this message\n");
3039}
3040
Mathias Agopian65ab4712010-07-14 17:59:35 -07003041}; // namespace android