1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "EmulatedCamera_HotplugThread"
#include <cutils/log.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/inotify.h>
#include "EmulatedCameraHotplugThread.h"
#include "EmulatedCameraFactory.h"
#define FAKE_HOTPLUG_FILE "/data/misc/media/emulator.camera.hotplug"
#define EVENT_SIZE (sizeof(struct inotify_event))
#define EVENT_BUF_LEN (1024*(EVENT_SIZE+16))
#define SubscriberInfo EmulatedCameraHotplugThread::SubscriberInfo
namespace android {
EmulatedCameraHotplugThread::EmulatedCameraHotplugThread(
const int* cameraIdArray,
size_t size) :
Thread(/*canCallJava*/false) {
mRunning = true;
mInotifyFd = 0;
for (size_t i = 0; i < size; ++i) {
int id = cameraIdArray[i];
if (createFileIfNotExists(id)) {
mSubscribedCameraIds.push_back(id);
}
}
}
EmulatedCameraHotplugThread::~EmulatedCameraHotplugThread() {
}
status_t EmulatedCameraHotplugThread::requestExitAndWait() {
ALOGE("%s: Not implemented. Use requestExit + join instead",
__FUNCTION__);
return INVALID_OPERATION;
}
void EmulatedCameraHotplugThread::requestExit() {
Mutex::Autolock al(mMutex);
ALOGV("%s: Requesting thread exit", __FUNCTION__);
mRunning = false;
bool rmWatchFailed = false;
Vector<SubscriberInfo>::iterator it;
for (it = mSubscribers.begin(); it != mSubscribers.end(); ++it) {
if (inotify_rm_watch(mInotifyFd, it->WatchID) == -1) {
ALOGE("%s: Could not remove watch for camID '%d',"
" error: '%s' (%d)",
__FUNCTION__, it->CameraID, strerror(errno),
errno);
rmWatchFailed = true ;
} else {
ALOGV("%s: Removed watch for camID '%d'",
__FUNCTION__, it->CameraID);
}
}
if (rmWatchFailed) { // unlikely
// Give the thread a fighting chance to error out on the next
// read
if (close(mInotifyFd) == -1) {
ALOGE("%s: close failure error: '%s' (%d)",
__FUNCTION__, strerror(errno), errno);
}
}
ALOGV("%s: Request exit complete.", __FUNCTION__);
}
status_t EmulatedCameraHotplugThread::readyToRun() {
Mutex::Autolock al(mMutex);
mInotifyFd = -1;
do {
ALOGV("%s: Initializing inotify", __FUNCTION__);
mInotifyFd = inotify_init();
if (mInotifyFd == -1) {
ALOGE("%s: inotify_init failure error: '%s' (%d)",
__FUNCTION__, strerror(errno), errno);
mRunning = false;
break;
}
/**
* For each fake camera file, add a watch for when
* the file is closed (if it was written to)
*/
Vector<int>::const_iterator it, end;
it = mSubscribedCameraIds.begin();
end = mSubscribedCameraIds.end();
for (; it != end; ++it) {
int cameraId = *it;
if (!addWatch(cameraId)) {
mRunning = false;
break;
}
}
} while(false);
if (!mRunning) {
status_t err = -errno;
if (mInotifyFd != -1) {
close(mInotifyFd);
}
return err;
}
return OK;
}
bool EmulatedCameraHotplugThread::threadLoop() {
// If requestExit was already called, mRunning will be false
while (mRunning) {
char buffer[EVENT_BUF_LEN];
int length = TEMP_FAILURE_RETRY(
read(mInotifyFd, buffer, EVENT_BUF_LEN));
if (length < 0) {
ALOGE("%s: Error reading from inotify FD, error: '%s' (%d)",
__FUNCTION__, strerror(errno),
errno);
mRunning = false;
break;
}
ALOGV("%s: Read %d bytes from inotify FD", __FUNCTION__, length);
int i = 0;
while (i < length) {
inotify_event* event = (inotify_event*) &buffer[i];
if (event->mask & IN_IGNORED) {
Mutex::Autolock al(mMutex);
if (!mRunning) {
ALOGV("%s: Shutting down thread", __FUNCTION__);
break;
} else {
ALOGE("%s: File was deleted, aborting",
__FUNCTION__);
mRunning = false;
break;
}
} else if (event->mask & IN_CLOSE_WRITE) {
int cameraId = getCameraId(event->wd);
if (cameraId < 0) {
ALOGE("%s: Got bad camera ID from WD '%d",
__FUNCTION__, event->wd);
} else {
// Check the file for the new hotplug event
String8 filePath = getFilePath(cameraId);
/**
* NOTE: we carefully avoid getting an inotify
* for the same exact file because it's opened for
* read-only, but our inotify is for write-only
*/
int newStatus = readFile(filePath);
if (newStatus < 0) {
mRunning = false;
break;
}
int halStatus = newStatus ?
CAMERA_DEVICE_STATUS_PRESENT :
CAMERA_DEVICE_STATUS_NOT_PRESENT;
gEmulatedCameraFactory.onStatusChanged(cameraId,
halStatus);
}
} else {
ALOGW("%s: Unknown mask 0x%x",
__FUNCTION__, event->mask);
}
i += EVENT_SIZE + event->len;
}
}
if (!mRunning) {
close(mInotifyFd);
return false;
}
return true;
}
String8 EmulatedCameraHotplugThread::getFilePath(int cameraId) const {
return String8::format(FAKE_HOTPLUG_FILE ".%d", cameraId);
}
bool EmulatedCameraHotplugThread::createFileIfNotExists(int cameraId) const
{
String8 filePath = getFilePath(cameraId);
// make sure this file exists and we have access to it
int fd = TEMP_FAILURE_RETRY(
open(filePath.string(), O_WRONLY | O_CREAT | O_TRUNC,
/* mode = ug+rwx */ S_IRWXU | S_IRWXG ));
if (fd == -1) {
ALOGE("%s: Could not create file '%s', error: '%s' (%d)",
__FUNCTION__, filePath.string(), strerror(errno), errno);
return false;
}
// File has '1' by default since we are plugged in by default
if (TEMP_FAILURE_RETRY(write(fd, "1\n", /*count*/2)) == -1) {
ALOGE("%s: Could not write '1' to file '%s', error: '%s' (%d)",
__FUNCTION__, filePath.string(), strerror(errno), errno);
return false;
}
close(fd);
return true;
}
int EmulatedCameraHotplugThread::getCameraId(String8 filePath) const {
Vector<int>::const_iterator it, end;
it = mSubscribedCameraIds.begin();
end = mSubscribedCameraIds.end();
for (; it != end; ++it) {
String8 camPath = getFilePath(*it);
if (camPath == filePath) {
return *it;
}
}
return NAME_NOT_FOUND;
}
int EmulatedCameraHotplugThread::getCameraId(int wd) const {
for (size_t i = 0; i < mSubscribers.size(); ++i) {
if (mSubscribers[i].WatchID == wd) {
return mSubscribers[i].CameraID;
}
}
return NAME_NOT_FOUND;
}
SubscriberInfo* EmulatedCameraHotplugThread::getSubscriberInfo(int cameraId)
{
for (size_t i = 0; i < mSubscribers.size(); ++i) {
if (mSubscribers[i].CameraID == cameraId) {
return (SubscriberInfo*)&mSubscribers[i];
}
}
return NULL;
}
bool EmulatedCameraHotplugThread::addWatch(int cameraId) {
String8 camPath = getFilePath(cameraId);
int wd = inotify_add_watch(mInotifyFd,
camPath.string(),
IN_CLOSE_WRITE);
if (wd == -1) {
ALOGE("%s: Could not add watch for '%s', error: '%s' (%d)",
__FUNCTION__, camPath.string(), strerror(errno),
errno);
mRunning = false;
return false;
}
ALOGV("%s: Watch added for camID='%d', wd='%d'",
__FUNCTION__, cameraId, wd);
SubscriberInfo si = { cameraId, wd };
mSubscribers.push_back(si);
return true;
}
bool EmulatedCameraHotplugThread::removeWatch(int cameraId) {
SubscriberInfo* si = getSubscriberInfo(cameraId);
if (!si) return false;
if (inotify_rm_watch(mInotifyFd, si->WatchID) == -1) {
ALOGE("%s: Could not remove watch for camID '%d', error: '%s' (%d)",
__FUNCTION__, cameraId, strerror(errno),
errno);
return false;
}
Vector<SubscriberInfo>::iterator it;
for (it = mSubscribers.begin(); it != mSubscribers.end(); ++it) {
if (it->CameraID == cameraId) {
break;
}
}
if (it != mSubscribers.end()) {
mSubscribers.erase(it);
}
return true;
}
int EmulatedCameraHotplugThread::readFile(String8 filePath) const {
int fd = TEMP_FAILURE_RETRY(
open(filePath.string(), O_RDONLY, /*mode*/0));
if (fd == -1) {
ALOGE("%s: Could not open file '%s', error: '%s' (%d)",
__FUNCTION__, filePath.string(), strerror(errno), errno);
return -1;
}
char buffer[1];
int length;
length = TEMP_FAILURE_RETRY(
read(fd, buffer, sizeof(buffer)));
int retval;
ALOGV("%s: Read file '%s', length='%d', buffer='%c'",
__FUNCTION__, filePath.string(), length, buffer[0]);
if (length == 0) { // EOF
retval = 0; // empty file is the same thing as 0
} else if (buffer[0] == '0') {
retval = 0;
} else { // anything non-empty that's not beginning with '0'
retval = 1;
}
close(fd);
return retval;
}
} //namespace android
|