Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 17 | #define LOG_TAG "MtpDevice" |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 18 | |
| 19 | #include "MtpDebug.h" |
| 20 | #include "MtpDevice.h" |
| 21 | #include "MtpDeviceInfo.h" |
| 22 | #include "MtpObjectInfo.h" |
| 23 | #include "MtpProperty.h" |
| 24 | #include "MtpStorageInfo.h" |
| 25 | #include "MtpStringBuffer.h" |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 26 | #include "MtpUtils.h" |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 27 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 35 | #include <endian.h> |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 36 | |
| 37 | #include <usbhost/usbhost.h> |
| 38 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 39 | namespace android { |
| 40 | |
| 41 | MtpDevice::MtpDevice(struct usb_device* device, int interface, |
| 42 | struct usb_endpoint *ep_in, struct usb_endpoint *ep_out, |
| 43 | struct usb_endpoint *ep_intr) |
| 44 | : mDevice(device), |
| 45 | mInterface(interface), |
| 46 | mEndpointIn(ep_in), |
| 47 | mEndpointOut(ep_out), |
| 48 | mEndpointIntr(ep_intr), |
| 49 | mDeviceInfo(NULL), |
| 50 | mID(usb_device_get_unique_id(device)), |
| 51 | mSessionID(0), |
| 52 | mTransactionID(0) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | MtpDevice::~MtpDevice() { |
| 57 | close(); |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 58 | for (int i = 0; i < mDeviceProperties.size(); i++) |
| 59 | delete mDeviceProperties[i]; |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void MtpDevice::initialize() { |
| 63 | openSession(); |
| 64 | mDeviceInfo = getDeviceInfo(); |
| 65 | if (mDeviceInfo) { |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 66 | if (mDeviceInfo->mDeviceProperties) { |
| 67 | int count = mDeviceInfo->mDeviceProperties->size(); |
| 68 | for (int i = 0; i < count; i++) { |
| 69 | MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i]; |
| 70 | MtpProperty* property = getDevicePropDesc(propCode); |
Mike Lockwood | e4880e4 | 2010-12-07 11:24:28 -0800 | [diff] [blame] | 71 | if (property) |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 72 | mDeviceProperties.push(property); |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 73 | } |
| 74 | } |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | void MtpDevice::close() { |
| 79 | if (mDevice) { |
| 80 | usb_device_release_interface(mDevice, mInterface); |
| 81 | usb_device_close(mDevice); |
| 82 | mDevice = NULL; |
| 83 | } |
| 84 | } |
| 85 | |
Mike Lockwood | e4880e4 | 2010-12-07 11:24:28 -0800 | [diff] [blame] | 86 | void MtpDevice::print() { |
| 87 | if (mDeviceInfo) { |
| 88 | mDeviceInfo->print(); |
| 89 | |
| 90 | if (mDeviceInfo->mDeviceProperties) { |
| 91 | LOGI("***** DEVICE PROPERTIES *****\n"); |
| 92 | int count = mDeviceInfo->mDeviceProperties->size(); |
| 93 | for (int i = 0; i < count; i++) { |
| 94 | MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i]; |
| 95 | MtpProperty* property = getDevicePropDesc(propCode); |
| 96 | if (property) { |
| 97 | property->print(); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (mDeviceInfo->mPlaybackFormats) { |
| 104 | LOGI("***** OBJECT PROPERTIES *****\n"); |
| 105 | int count = mDeviceInfo->mPlaybackFormats->size(); |
| 106 | for (int i = 0; i < count; i++) { |
| 107 | MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i]; |
| 108 | LOGI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format)); |
| 109 | MtpObjectPropertyList* props = getObjectPropsSupported(format); |
| 110 | if (props) { |
| 111 | for (int j = 0; j < props->size(); j++) { |
| 112 | MtpObjectProperty prop = (*props)[j]; |
| 113 | MtpProperty* property = getObjectPropDesc(prop); |
| 114 | if (property) |
| 115 | property->print(); |
| 116 | else |
| 117 | LOGE("could not fetch property: %s", |
| 118 | MtpDebug::getObjectPropCodeName(prop)); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 125 | const char* MtpDevice::getDeviceName() { |
| 126 | if (mDevice) |
| 127 | return usb_device_get_name(mDevice); |
| 128 | else |
| 129 | return "???"; |
| 130 | } |
| 131 | |
| 132 | bool MtpDevice::openSession() { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 133 | Mutex::Autolock autoLock(mMutex); |
| 134 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 135 | mSessionID = 0; |
| 136 | mTransactionID = 0; |
| 137 | MtpSessionID newSession = 1; |
| 138 | mRequest.reset(); |
| 139 | mRequest.setParameter(1, newSession); |
| 140 | if (!sendRequest(MTP_OPERATION_OPEN_SESSION)) |
| 141 | return false; |
| 142 | MtpResponseCode ret = readResponse(); |
| 143 | if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN) |
| 144 | newSession = mResponse.getParameter(1); |
| 145 | else if (ret != MTP_RESPONSE_OK) |
| 146 | return false; |
| 147 | |
| 148 | mSessionID = newSession; |
| 149 | mTransactionID = 1; |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | bool MtpDevice::closeSession() { |
| 154 | // FIXME |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | MtpDeviceInfo* MtpDevice::getDeviceInfo() { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 159 | Mutex::Autolock autoLock(mMutex); |
| 160 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 161 | mRequest.reset(); |
| 162 | if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO)) |
| 163 | return NULL; |
| 164 | if (!readData()) |
| 165 | return NULL; |
| 166 | MtpResponseCode ret = readResponse(); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 167 | if (ret == MTP_RESPONSE_OK) { |
| 168 | MtpDeviceInfo* info = new MtpDeviceInfo; |
| 169 | info->read(mData); |
| 170 | return info; |
| 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | MtpStorageIDList* MtpDevice::getStorageIDs() { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 176 | Mutex::Autolock autoLock(mMutex); |
| 177 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 178 | mRequest.reset(); |
| 179 | if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS)) |
| 180 | return NULL; |
| 181 | if (!readData()) |
| 182 | return NULL; |
| 183 | MtpResponseCode ret = readResponse(); |
| 184 | if (ret == MTP_RESPONSE_OK) { |
| 185 | return mData.getAUInt32(); |
| 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 191 | Mutex::Autolock autoLock(mMutex); |
| 192 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 193 | mRequest.reset(); |
| 194 | mRequest.setParameter(1, storageID); |
| 195 | if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO)) |
| 196 | return NULL; |
| 197 | if (!readData()) |
| 198 | return NULL; |
| 199 | MtpResponseCode ret = readResponse(); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 200 | if (ret == MTP_RESPONSE_OK) { |
| 201 | MtpStorageInfo* info = new MtpStorageInfo(storageID); |
| 202 | info->read(mData); |
| 203 | return info; |
| 204 | } |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID, |
| 209 | MtpObjectFormat format, MtpObjectHandle parent) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 210 | Mutex::Autolock autoLock(mMutex); |
| 211 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 212 | mRequest.reset(); |
| 213 | mRequest.setParameter(1, storageID); |
| 214 | mRequest.setParameter(2, format); |
| 215 | mRequest.setParameter(3, parent); |
| 216 | if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES)) |
| 217 | return NULL; |
| 218 | if (!readData()) |
| 219 | return NULL; |
| 220 | MtpResponseCode ret = readResponse(); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 221 | if (ret == MTP_RESPONSE_OK) { |
| 222 | return mData.getAUInt32(); |
| 223 | } |
| 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 228 | Mutex::Autolock autoLock(mMutex); |
| 229 | |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 230 | // FIXME - we might want to add some caching here |
| 231 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 232 | mRequest.reset(); |
| 233 | mRequest.setParameter(1, handle); |
| 234 | if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO)) |
| 235 | return NULL; |
| 236 | if (!readData()) |
| 237 | return NULL; |
| 238 | MtpResponseCode ret = readResponse(); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 239 | if (ret == MTP_RESPONSE_OK) { |
| 240 | MtpObjectInfo* info = new MtpObjectInfo(handle); |
| 241 | info->read(mData); |
| 242 | return info; |
| 243 | } |
| 244 | return NULL; |
| 245 | } |
| 246 | |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 247 | void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 248 | Mutex::Autolock autoLock(mMutex); |
| 249 | |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 250 | mRequest.reset(); |
| 251 | mRequest.setParameter(1, handle); |
| 252 | if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) { |
| 253 | MtpResponseCode ret = readResponse(); |
| 254 | if (ret == MTP_RESPONSE_OK) { |
| 255 | return mData.getData(outLength); |
| 256 | } |
| 257 | } |
| 258 | outLength = 0; |
| 259 | return NULL; |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 260 | } |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 261 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 262 | MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) { |
| 263 | Mutex::Autolock autoLock(mMutex); |
| 264 | |
| 265 | mRequest.reset(); |
| 266 | MtpObjectHandle parent = info->mParent; |
| 267 | if (parent == 0) |
| 268 | parent = MTP_PARENT_ROOT; |
| 269 | |
| 270 | mRequest.setParameter(1, info->mStorageID); |
| 271 | mRequest.setParameter(2, info->mParent); |
| 272 | |
| 273 | mData.putUInt32(info->mStorageID); |
| 274 | mData.putUInt16(info->mFormat); |
| 275 | mData.putUInt16(info->mProtectionStatus); |
| 276 | mData.putUInt32(info->mCompressedSize); |
| 277 | mData.putUInt16(info->mThumbFormat); |
| 278 | mData.putUInt32(info->mThumbCompressedSize); |
| 279 | mData.putUInt32(info->mThumbPixWidth); |
| 280 | mData.putUInt32(info->mThumbPixHeight); |
| 281 | mData.putUInt32(info->mImagePixWidth); |
| 282 | mData.putUInt32(info->mImagePixHeight); |
| 283 | mData.putUInt32(info->mImagePixDepth); |
| 284 | mData.putUInt32(info->mParent); |
| 285 | mData.putUInt16(info->mAssociationType); |
| 286 | mData.putUInt32(info->mAssociationDesc); |
| 287 | mData.putUInt32(info->mSequenceNumber); |
| 288 | mData.putString(info->mName); |
| 289 | |
| 290 | char created[100], modified[100]; |
| 291 | formatDateTime(info->mDateCreated, created, sizeof(created)); |
| 292 | formatDateTime(info->mDateModified, modified, sizeof(modified)); |
| 293 | |
| 294 | mData.putString(created); |
| 295 | mData.putString(modified); |
| 296 | if (info->mKeywords) |
| 297 | mData.putString(info->mKeywords); |
| 298 | else |
| 299 | mData.putEmptyString(); |
| 300 | |
| 301 | if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 302 | MtpResponseCode ret = readResponse(); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 303 | if (ret == MTP_RESPONSE_OK) { |
| 304 | info->mStorageID = mResponse.getParameter(1); |
| 305 | info->mParent = mResponse.getParameter(2); |
| 306 | info->mHandle = mResponse.getParameter(3); |
| 307 | return info->mHandle; |
| 308 | } |
| 309 | } |
| 310 | return (MtpObjectHandle)-1; |
| 311 | } |
| 312 | |
| 313 | bool MtpDevice::sendObject(MtpObjectInfo* info, int srcFD) { |
| 314 | Mutex::Autolock autoLock(mMutex); |
| 315 | |
| 316 | int remaining = info->mCompressedSize; |
| 317 | mRequest.reset(); |
| 318 | mRequest.setParameter(1, info->mHandle); |
| 319 | if (sendRequest(MTP_OPERATION_SEND_OBJECT)) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 320 | // send data header |
| 321 | writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining); |
| 322 | |
| 323 | char buffer[65536]; |
| 324 | while (remaining > 0) { |
| 325 | int count = read(srcFD, buffer, sizeof(buffer)); |
| 326 | if (count > 0) { |
| 327 | int written = mData.write(mEndpointOut, buffer, count); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 328 | // FIXME check error |
| 329 | remaining -= count; |
| 330 | } else { |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | MtpResponseCode ret = readResponse(); |
| 336 | return (remaining == 0 && ret == MTP_RESPONSE_OK); |
| 337 | } |
| 338 | |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 339 | bool MtpDevice::deleteObject(MtpObjectHandle handle) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 340 | Mutex::Autolock autoLock(mMutex); |
| 341 | |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 342 | mRequest.reset(); |
| 343 | mRequest.setParameter(1, handle); |
| 344 | if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) { |
| 345 | MtpResponseCode ret = readResponse(); |
| 346 | if (ret == MTP_RESPONSE_OK) |
| 347 | return true; |
| 348 | } |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) { |
| 353 | MtpObjectInfo* info = getObjectInfo(handle); |
| 354 | if (info) |
| 355 | return info->mParent; |
| 356 | else |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) { |
| 361 | MtpObjectInfo* info = getObjectInfo(handle); |
| 362 | if (info) |
| 363 | return info->mStorageID; |
| 364 | else |
| 365 | return -1; |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Mike Lockwood | 5768f10 | 2010-12-07 10:58:56 -0800 | [diff] [blame] | 368 | MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) { |
| 369 | Mutex::Autolock autoLock(mMutex); |
| 370 | |
| 371 | mRequest.reset(); |
| 372 | mRequest.setParameter(1, format); |
| 373 | if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED)) |
| 374 | return NULL; |
| 375 | if (!readData()) |
| 376 | return NULL; |
| 377 | MtpResponseCode ret = readResponse(); |
| 378 | if (ret == MTP_RESPONSE_OK) { |
| 379 | return mData.getAUInt16(); |
| 380 | } |
| 381 | return NULL; |
| 382 | |
| 383 | } |
| 384 | |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 385 | MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 386 | Mutex::Autolock autoLock(mMutex); |
| 387 | |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 388 | mRequest.reset(); |
| 389 | mRequest.setParameter(1, code); |
| 390 | if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC)) |
| 391 | return NULL; |
| 392 | if (!readData()) |
| 393 | return NULL; |
| 394 | MtpResponseCode ret = readResponse(); |
| 395 | if (ret == MTP_RESPONSE_OK) { |
| 396 | MtpProperty* property = new MtpProperty; |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame] | 397 | property->read(mData); |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 398 | return property; |
| 399 | } |
| 400 | return NULL; |
| 401 | } |
| 402 | |
Mike Lockwood | 5768f10 | 2010-12-07 10:58:56 -0800 | [diff] [blame] | 403 | MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code) { |
| 404 | Mutex::Autolock autoLock(mMutex); |
| 405 | |
| 406 | mRequest.reset(); |
| 407 | mRequest.setParameter(1, code); |
| 408 | if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC)) |
| 409 | return NULL; |
| 410 | if (!readData()) |
| 411 | return NULL; |
| 412 | MtpResponseCode ret = readResponse(); |
| 413 | if (ret == MTP_RESPONSE_OK) { |
| 414 | MtpProperty* property = new MtpProperty; |
| 415 | property->read(mData); |
| 416 | return property; |
| 417 | } |
| 418 | return NULL; |
| 419 | } |
| 420 | |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 421 | // reads the object's data and writes it to the specified file path |
Mike Lockwood | 929b3da | 2010-11-19 13:52:20 -0500 | [diff] [blame] | 422 | bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) { |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 423 | LOGD("readObject: %s", destPath); |
| 424 | int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC); |
| 425 | if (fd < 0) { |
| 426 | LOGE("open failed for %s", destPath); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 427 | return false; |
| 428 | } |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 429 | |
Mike Lockwood | 929b3da | 2010-11-19 13:52:20 -0500 | [diff] [blame] | 430 | fchown(fd, getuid(), group); |
| 431 | // set permissions |
| 432 | int mask = umask(0); |
| 433 | fchmod(fd, perm); |
| 434 | umask(mask); |
| 435 | |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 436 | Mutex::Autolock autoLock(mMutex); |
| 437 | bool result = false; |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 438 | |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 439 | mRequest.reset(); |
| 440 | mRequest.setParameter(1, handle); |
| 441 | if (sendRequest(MTP_OPERATION_GET_OBJECT) |
| 442 | && mData.readDataHeader(mEndpointIn)) { |
| 443 | uint32_t length = mData.getContainerLength(); |
| 444 | if (length < MTP_CONTAINER_HEADER_SIZE) |
| 445 | goto fail; |
| 446 | length -= MTP_CONTAINER_HEADER_SIZE; |
| 447 | uint32_t remaining = length; |
| 448 | |
| 449 | int initialDataLength = 0; |
| 450 | void* initialData = mData.getData(initialDataLength); |
| 451 | if (initialData) { |
| 452 | if (initialDataLength > 0) { |
| 453 | if (write(fd, initialData, initialDataLength) != initialDataLength) |
| 454 | goto fail; |
| 455 | remaining -= initialDataLength; |
| 456 | } |
| 457 | free(initialData); |
| 458 | } |
| 459 | |
| 460 | // USB reads greater than 16K don't work |
| 461 | char buffer1[16384], buffer2[16384]; |
| 462 | char* readBuffer = buffer1; |
| 463 | char* writeBuffer = NULL; |
| 464 | int writeLength = 0; |
| 465 | |
| 466 | while (remaining > 0 || writeBuffer) { |
| 467 | if (remaining > 0) { |
| 468 | // queue up a read request |
| 469 | int readSize = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining); |
| 470 | if (mData.readDataAsync(mEndpointIn, readBuffer, readSize)) { |
| 471 | LOGE("readDataAsync failed"); |
| 472 | goto fail; |
| 473 | } |
| 474 | } else { |
| 475 | readBuffer = NULL; |
| 476 | } |
| 477 | |
| 478 | if (writeBuffer) { |
| 479 | // write previous buffer |
| 480 | if (write(fd, writeBuffer, writeLength) != writeLength) { |
| 481 | LOGE("write failed"); |
| 482 | // wait for pending read before failing |
| 483 | if (readBuffer) |
| 484 | mData.readDataWait(mEndpointIn); |
| 485 | goto fail; |
| 486 | } |
| 487 | writeBuffer = NULL; |
| 488 | } |
| 489 | |
| 490 | // wait for read to complete |
| 491 | if (readBuffer) { |
| 492 | int read = mData.readDataWait(mEndpointIn); |
| 493 | if (read < 0) |
| 494 | goto fail; |
| 495 | |
| 496 | writeBuffer = readBuffer; |
| 497 | writeLength = read; |
| 498 | remaining -= read; |
| 499 | readBuffer = (readBuffer == buffer1 ? buffer2 : buffer1); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | MtpResponseCode response = readResponse(); |
| 504 | if (response == MTP_RESPONSE_OK) |
| 505 | result = true; |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 506 | } |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 507 | |
| 508 | fail: |
| 509 | ::close(fd); |
| 510 | return result; |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 511 | } |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 512 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 513 | bool MtpDevice::sendRequest(MtpOperationCode operation) { |
Mike Lockwood | 456d8e6 | 2010-07-27 11:50:34 -0400 | [diff] [blame] | 514 | LOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation)); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 515 | mRequest.setOperationCode(operation); |
| 516 | if (mTransactionID > 0) |
| 517 | mRequest.setTransactionID(mTransactionID++); |
| 518 | int ret = mRequest.write(mEndpointOut); |
| 519 | mRequest.dump(); |
| 520 | return (ret > 0); |
| 521 | } |
| 522 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 523 | bool MtpDevice::sendData() { |
Mike Lockwood | 456d8e6 | 2010-07-27 11:50:34 -0400 | [diff] [blame] | 524 | LOGV("sendData\n"); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 525 | mData.setOperationCode(mRequest.getOperationCode()); |
| 526 | mData.setTransactionID(mRequest.getTransactionID()); |
| 527 | int ret = mData.write(mEndpointOut); |
| 528 | mData.dump(); |
| 529 | return (ret > 0); |
| 530 | } |
| 531 | |
| 532 | bool MtpDevice::readData() { |
| 533 | mData.reset(); |
| 534 | int ret = mData.read(mEndpointIn); |
Mike Lockwood | 456d8e6 | 2010-07-27 11:50:34 -0400 | [diff] [blame] | 535 | LOGV("readData returned %d\n", ret); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 536 | if (ret >= MTP_CONTAINER_HEADER_SIZE) { |
| 537 | mData.dump(); |
| 538 | return true; |
| 539 | } |
| 540 | else { |
Mike Lockwood | 456d8e6 | 2010-07-27 11:50:34 -0400 | [diff] [blame] | 541 | LOGV("readResponse failed\n"); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 546 | bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) { |
| 547 | mData.setOperationCode(operation); |
| 548 | mData.setTransactionID(mRequest.getTransactionID()); |
| 549 | return (!mData.writeDataHeader(mEndpointOut, dataLength)); |
| 550 | } |
| 551 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 552 | MtpResponseCode MtpDevice::readResponse() { |
Mike Lockwood | 456d8e6 | 2010-07-27 11:50:34 -0400 | [diff] [blame] | 553 | LOGV("readResponse\n"); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 554 | int ret = mResponse.read(mEndpointIn); |
| 555 | if (ret >= MTP_CONTAINER_HEADER_SIZE) { |
| 556 | mResponse.dump(); |
| 557 | return mResponse.getResponseCode(); |
| 558 | } |
| 559 | else { |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 560 | LOGD("readResponse failed\n"); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 561 | return -1; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | } // namespace android |