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 17 #define LOG_TAG "MtpDataPacket" 18 19 #include "MtpDataPacket.h" 20 21 #include <algorithm> 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <stdio.h> 25 #include <sys/types.h> 26 #include <usbhost/usbhost.h> 27 #include "MtpStringBuffer.h" 28 #include "IMtpHandle.h" 29 30 namespace android { 31 32 namespace { 33 // Reads the exact |count| bytes from |fd| to |buf|. 34 // Returns |count| if it succeed to read the bytes. Otherwise returns -1. If it reaches EOF, the 35 // function regards it as an error. 36 ssize_t readExactBytes(int fd, void* buf, size_t count) { 37 if (count > SSIZE_MAX) { 38 return -1; 39 } 40 size_t read_count = 0; 41 while (read_count < count) { 42 int result = read(fd, static_cast<int8_t*>(buf) + read_count, count - read_count); 43 // Assume that EOF is error. 44 if (result <= 0) { 45 return -1; 46 } 47 read_count += result; 48 } 49 return read_count == count ? count : -1; 50 } 51 } // namespace 52 53 MtpDataPacket::MtpDataPacket() 54 : MtpPacket(MTP_BUFFER_SIZE), // MAX_USBFS_BUFFER_SIZE 55 mOffset(MTP_CONTAINER_HEADER_SIZE) 56 { 57 } 58 59 MtpDataPacket::~MtpDataPacket() { 60 } 61 62 void MtpDataPacket::reset() { 63 MtpPacket::reset(); 64 mOffset = MTP_CONTAINER_HEADER_SIZE; 65 } 66 67 void MtpDataPacket::setOperationCode(MtpOperationCode code) { 68 MtpPacket::putUInt16(MTP_CONTAINER_CODE_OFFSET, code); 69 } 70 71 void MtpDataPacket::setTransactionID(MtpTransactionID id) { 72 MtpPacket::putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id); 73 } 74 75 bool MtpDataPacket::getUInt8(uint8_t& value) { 76 if (mPacketSize - mOffset < sizeof(value)) 77 return false; 78 value = mBuffer[mOffset++]; 79 return true; 80 } 81 82 bool MtpDataPacket::getUInt16(uint16_t& value) { 83 if (mPacketSize - mOffset < sizeof(value)) 84 return false; 85 int offset = mOffset; 86 value = (uint16_t)mBuffer[offset] | ((uint16_t)mBuffer[offset + 1] << 8); 87 mOffset += sizeof(value); 88 return true; 89 } 90 91 bool MtpDataPacket::getUInt32(uint32_t& value) { 92 if (mPacketSize - mOffset < sizeof(value)) 93 return false; 94 int offset = mOffset; 95 value = (uint32_t)mBuffer[offset] | ((uint32_t)mBuffer[offset + 1] << 8) | 96 ((uint32_t)mBuffer[offset + 2] << 16) | ((uint32_t)mBuffer[offset + 3] << 24); 97 mOffset += sizeof(value); 98 return true; 99 } 100 101 bool MtpDataPacket::getUInt64(uint64_t& value) { 102 if (mPacketSize - mOffset < sizeof(value)) 103 return false; 104 int offset = mOffset; 105 value = (uint64_t)mBuffer[offset] | ((uint64_t)mBuffer[offset + 1] << 8) | 106 ((uint64_t)mBuffer[offset + 2] << 16) | ((uint64_t)mBuffer[offset + 3] << 24) | 107 ((uint64_t)mBuffer[offset + 4] << 32) | ((uint64_t)mBuffer[offset + 5] << 40) | 108 ((uint64_t)mBuffer[offset + 6] << 48) | ((uint64_t)mBuffer[offset + 7] << 56); 109 mOffset += sizeof(value); 110 return true; 111 } 112 113 bool MtpDataPacket::getUInt128(uint128_t& value) { 114 return getUInt32(value[0]) && getUInt32(value[1]) && getUInt32(value[2]) && getUInt32(value[3]); 115 } 116 117 bool MtpDataPacket::getString(MtpStringBuffer& string) 118 { 119 return string.readFromPacket(this); 120 } 121 122 Int8List* MtpDataPacket::getAInt8() { 123 uint32_t count; 124 if (!getUInt32(count)) 125 return NULL; 126 Int8List* result = new Int8List; 127 for (uint32_t i = 0; i < count; i++) { 128 int8_t value; 129 if (!getInt8(value)) { 130 delete result; 131 return NULL; 132 } 133 result->push_back(value); 134 } 135 return result; 136 } 137 138 UInt8List* MtpDataPacket::getAUInt8() { 139 uint32_t count; 140 if (!getUInt32(count)) 141 return NULL; 142 UInt8List* result = new UInt8List; 143 for (uint32_t i = 0; i < count; i++) { 144 uint8_t value; 145 if (!getUInt8(value)) { 146 delete result; 147 return NULL; 148 } 149 result->push_back(value); 150 } 151 return result; 152 } 153 154 Int16List* MtpDataPacket::getAInt16() { 155 uint32_t count; 156 if (!getUInt32(count)) 157 return NULL; 158 Int16List* result = new Int16List; 159 for (uint32_t i = 0; i < count; i++) { 160 int16_t value; 161 if (!getInt16(value)) { 162 delete result; 163 return NULL; 164 } 165 result->push_back(value); 166 } 167 return result; 168 } 169 170 UInt16List* MtpDataPacket::getAUInt16() { 171 uint32_t count; 172 if (!getUInt32(count)) 173 return NULL; 174 UInt16List* result = new UInt16List; 175 for (uint32_t i = 0; i < count; i++) { 176 uint16_t value; 177 if (!getUInt16(value)) { 178 delete result; 179 return NULL; 180 } 181 result->push_back(value); 182 } 183 return result; 184 } 185 186 Int32List* MtpDataPacket::getAInt32() { 187 uint32_t count; 188 if (!getUInt32(count)) 189 return NULL; 190 Int32List* result = new Int32List; 191 for (uint32_t i = 0; i < count; i++) { 192 int32_t value; 193 if (!getInt32(value)) { 194 delete result; 195 return NULL; 196 } 197 result->push_back(value); 198 } 199 return result; 200 } 201 202 UInt32List* MtpDataPacket::getAUInt32() { 203 uint32_t count; 204 if (!getUInt32(count)) 205 return NULL; 206 UInt32List* result = new UInt32List; 207 for (uint32_t i = 0; i < count; i++) { 208 uint32_t value; 209 if (!getUInt32(value)) { 210 delete result; 211 return NULL; 212 } 213 result->push_back(value); 214 } 215 return result; 216 } 217 218 Int64List* MtpDataPacket::getAInt64() { 219 uint32_t count; 220 if (!getUInt32(count)) 221 return NULL; 222 Int64List* result = new Int64List; 223 for (uint32_t i = 0; i < count; i++) { 224 int64_t value; 225 if (!getInt64(value)) { 226 delete result; 227 return NULL; 228 } 229 result->push_back(value); 230 } 231 return result; 232 } 233 234 UInt64List* MtpDataPacket::getAUInt64() { 235 uint32_t count; 236 if (!getUInt32(count)) 237 return NULL; 238 UInt64List* result = new UInt64List; 239 for (uint32_t i = 0; i < count; i++) { 240 uint64_t value; 241 if (!getUInt64(value)) { 242 delete result; 243 return NULL; 244 } 245 result->push_back(value); 246 } 247 return result; 248 } 249 250 void MtpDataPacket::putInt8(int8_t value) { 251 allocate(mOffset + 1); 252 mBuffer[mOffset++] = (uint8_t)value; 253 if (mPacketSize < mOffset) 254 mPacketSize = mOffset; 255 } 256 257 void MtpDataPacket::putUInt8(uint8_t value) { 258 allocate(mOffset + 1); 259 mBuffer[mOffset++] = (uint8_t)value; 260 if (mPacketSize < mOffset) 261 mPacketSize = mOffset; 262 } 263 264 void MtpDataPacket::putInt16(int16_t value) { 265 allocate(mOffset + 2); 266 mBuffer[mOffset++] = (uint8_t)(value & 0xFF); 267 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); 268 if (mPacketSize < mOffset) 269 mPacketSize = mOffset; 270 } 271 272 void MtpDataPacket::putUInt16(uint16_t value) { 273 allocate(mOffset + 2); 274 mBuffer[mOffset++] = (uint8_t)(value & 0xFF); 275 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); 276 if (mPacketSize < mOffset) 277 mPacketSize = mOffset; 278 } 279 280 void MtpDataPacket::putInt32(int32_t value) { 281 allocate(mOffset + 4); 282 mBuffer[mOffset++] = (uint8_t)(value & 0xFF); 283 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); 284 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); 285 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); 286 if (mPacketSize < mOffset) 287 mPacketSize = mOffset; 288 } 289 290 void MtpDataPacket::putUInt32(uint32_t value) { 291 allocate(mOffset + 4); 292 mBuffer[mOffset++] = (uint8_t)(value & 0xFF); 293 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); 294 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); 295 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); 296 if (mPacketSize < mOffset) 297 mPacketSize = mOffset; 298 } 299 300 void MtpDataPacket::putInt64(int64_t value) { 301 allocate(mOffset + 8); 302 mBuffer[mOffset++] = (uint8_t)(value & 0xFF); 303 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); 304 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); 305 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); 306 mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF); 307 mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF); 308 mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF); 309 mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF); 310 if (mPacketSize < mOffset) 311 mPacketSize = mOffset; 312 } 313 314 void MtpDataPacket::putUInt64(uint64_t value) { 315 allocate(mOffset + 8); 316 mBuffer[mOffset++] = (uint8_t)(value & 0xFF); 317 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); 318 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); 319 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); 320 mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF); 321 mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF); 322 mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF); 323 mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF); 324 if (mPacketSize < mOffset) 325 mPacketSize = mOffset; 326 } 327 328 void MtpDataPacket::putInt128(const int128_t& value) { 329 putInt32(value[0]); 330 putInt32(value[1]); 331 putInt32(value[2]); 332 putInt32(value[3]); 333 } 334 335 void MtpDataPacket::putUInt128(const uint128_t& value) { 336 putUInt32(value[0]); 337 putUInt32(value[1]); 338 putUInt32(value[2]); 339 putUInt32(value[3]); 340 } 341 342 void MtpDataPacket::putInt128(int64_t value) { 343 putInt64(value); 344 putInt64(value < 0 ? -1 : 0); 345 } 346 347 void MtpDataPacket::putUInt128(uint64_t value) { 348 putUInt64(value); 349 putUInt64(0); 350 } 351 352 void MtpDataPacket::putAInt8(const int8_t* values, int count) { 353 putUInt32(count); 354 for (int i = 0; i < count; i++) 355 putInt8(*values++); 356 } 357 358 void MtpDataPacket::putAUInt8(const uint8_t* values, int count) { 359 putUInt32(count); 360 for (int i = 0; i < count; i++) 361 putUInt8(*values++); 362 } 363 364 void MtpDataPacket::putAInt16(const int16_t* values, int count) { 365 putUInt32(count); 366 for (int i = 0; i < count; i++) 367 putInt16(*values++); 368 } 369 370 void MtpDataPacket::putAUInt16(const uint16_t* values, int count) { 371 putUInt32(count); 372 for (int i = 0; i < count; i++) 373 putUInt16(*values++); 374 } 375 376 void MtpDataPacket::putAUInt16(const UInt16List* values) { 377 size_t count = (values ? values->size() : 0); 378 putUInt32(count); 379 for (size_t i = 0; i < count; i++) 380 putUInt16((*values)[i]); 381 } 382 383 void MtpDataPacket::putAInt32(const int32_t* values, int count) { 384 putUInt32(count); 385 for (int i = 0; i < count; i++) 386 putInt32(*values++); 387 } 388 389 void MtpDataPacket::putAUInt32(const uint32_t* values, int count) { 390 putUInt32(count); 391 for (int i = 0; i < count; i++) 392 putUInt32(*values++); 393 } 394 395 void MtpDataPacket::putAUInt32(const UInt32List* list) { 396 if (!list) { 397 putEmptyArray(); 398 } else { 399 size_t size = list->size(); 400 putUInt32(size); 401 for (size_t i = 0; i < size; i++) 402 putUInt32((*list)[i]); 403 } 404 } 405 406 void MtpDataPacket::putAInt64(const int64_t* values, int count) { 407 putUInt32(count); 408 for (int i = 0; i < count; i++) 409 putInt64(*values++); 410 } 411 412 void MtpDataPacket::putAUInt64(const uint64_t* values, int count) { 413 putUInt32(count); 414 for (int i = 0; i < count; i++) 415 putUInt64(*values++); 416 } 417 418 void MtpDataPacket::putString(const MtpStringBuffer& string) { 419 string.writeToPacket(this); 420 } 421 422 void MtpDataPacket::putString(const char* s) { 423 MtpStringBuffer string(s); 424 string.writeToPacket(this); 425 } 426 427 void MtpDataPacket::putString(const uint16_t* string) { 428 int count = 0; 429 for (int i = 0; i <= MTP_STRING_MAX_CHARACTER_NUMBER; i++) { 430 if (string[i]) 431 count++; 432 else 433 break; 434 } 435 putUInt8(count > 0 ? count + 1 : 0); 436 for (int i = 0; i < count; i++) 437 putUInt16(string[i]); 438 // only terminate with zero if string is not empty 439 if (count > 0) 440 putUInt16(0); 441 } 442 443 #ifdef MTP_DEVICE 444 int MtpDataPacket::read(IMtpHandle *h) { 445 int ret = h->read(mBuffer, MTP_BUFFER_SIZE); 446 if (ret < MTP_CONTAINER_HEADER_SIZE) 447 return -1; 448 mPacketSize = ret; 449 mOffset = MTP_CONTAINER_HEADER_SIZE; 450 return ret; 451 } 452 453 int MtpDataPacket::write(IMtpHandle *h) { 454 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); 455 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); 456 int ret = h->write(mBuffer, mPacketSize); 457 return (ret < 0 ? ret : 0); 458 } 459 460 int MtpDataPacket::writeData(IMtpHandle *h, void* data, uint32_t length) { 461 allocate(length + MTP_CONTAINER_HEADER_SIZE); 462 memcpy(mBuffer + MTP_CONTAINER_HEADER_SIZE, data, length); 463 length += MTP_CONTAINER_HEADER_SIZE; 464 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length); 465 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); 466 int ret = h->write(mBuffer, length); 467 return (ret < 0 ? ret : 0); 468 } 469 470 #endif // MTP_DEVICE 471 472 #ifdef MTP_HOST 473 int MtpDataPacket::read(struct usb_request *request) { 474 // first read the header 475 request->buffer = mBuffer; 476 request->buffer_length = mBufferSize; 477 int length = transfer(request); 478 if (length >= MTP_CONTAINER_HEADER_SIZE) { 479 // look at the length field to see if the data spans multiple packets 480 uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); 481 allocate(totalLength); 482 while (totalLength > static_cast<uint32_t>(length)) { 483 request->buffer = mBuffer + length; 484 request->buffer_length = totalLength - length; 485 int ret = transfer(request); 486 if (ret >= 0) 487 length += ret; 488 else { 489 length = ret; 490 break; 491 } 492 } 493 } 494 if (length >= 0) 495 mPacketSize = length; 496 return length; 497 } 498 499 int MtpDataPacket::readData(struct usb_request *request, void* buffer, int length) { 500 int read = 0; 501 while (read < length) { 502 request->buffer = (char *)buffer + read; 503 request->buffer_length = length - read; 504 int ret = transfer(request); 505 if (ret < 0) { 506 return ret; 507 } 508 read += ret; 509 } 510 return read; 511 } 512 513 // Queue a read request. Call readDataWait to wait for result 514 int MtpDataPacket::readDataAsync(struct usb_request *req) { 515 if (usb_request_queue(req)) { 516 ALOGE("usb_endpoint_queue failed, errno: %d", errno); 517 return -1; 518 } 519 return 0; 520 } 521 522 // Wait for result of readDataAsync 523 int MtpDataPacket::readDataWait(struct usb_device *device) { 524 struct usb_request *req = usb_request_wait(device, -1); 525 return (req ? req->actual_length : -1); 526 } 527 528 int MtpDataPacket::readDataHeader(struct usb_request *request) { 529 request->buffer = mBuffer; 530 request->buffer_length = request->max_packet_size; 531 int length = transfer(request); 532 if (length >= 0) 533 mPacketSize = length; 534 return length; 535 } 536 537 int MtpDataPacket::write(struct usb_request *request, UrbPacketDivisionMode divisionMode) { 538 if (mPacketSize < MTP_CONTAINER_HEADER_SIZE || mPacketSize > MTP_BUFFER_SIZE) { 539 ALOGE("Illegal packet size."); 540 return -1; 541 } 542 543 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); 544 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); 545 546 size_t processedBytes = 0; 547 while (processedBytes < mPacketSize) { 548 const size_t write_size = 549 processedBytes == 0 && divisionMode == FIRST_PACKET_ONLY_HEADER ? 550 MTP_CONTAINER_HEADER_SIZE : mPacketSize - processedBytes; 551 request->buffer = mBuffer + processedBytes; 552 request->buffer_length = write_size; 553 const int result = transfer(request); 554 if (result < 0) { 555 ALOGE("Failed to write bytes to the device."); 556 return -1; 557 } 558 processedBytes += result; 559 } 560 561 return processedBytes == mPacketSize ? processedBytes : -1; 562 } 563 564 int MtpDataPacket::write(struct usb_request *request, 565 UrbPacketDivisionMode divisionMode, 566 int fd, 567 size_t payloadSize) { 568 // Obtain the greatest multiple of minimum packet size that is not greater than 569 // MTP_BUFFER_SIZE. 570 if (request->max_packet_size <= 0) { 571 ALOGE("Cannot determine bulk transfer size due to illegal max packet size %d.", 572 request->max_packet_size); 573 return -1; 574 } 575 const size_t maxBulkTransferSize = 576 MTP_BUFFER_SIZE - (MTP_BUFFER_SIZE % request->max_packet_size); 577 const size_t containerLength = payloadSize + MTP_CONTAINER_HEADER_SIZE; 578 size_t processedBytes = 0; 579 bool readError = false; 580 581 // Bind the packet with given request. 582 request->buffer = mBuffer; 583 allocate(maxBulkTransferSize); 584 585 while (processedBytes < containerLength) { 586 size_t bulkTransferSize = 0; 587 588 // prepare header. 589 const bool headerSent = processedBytes != 0; 590 if (!headerSent) { 591 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, containerLength); 592 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); 593 bulkTransferSize += MTP_CONTAINER_HEADER_SIZE; 594 } 595 596 // Prepare payload. 597 if (headerSent || divisionMode == FIRST_PACKET_HAS_PAYLOAD) { 598 const size_t processedPayloadBytes = 599 headerSent ? processedBytes - MTP_CONTAINER_HEADER_SIZE : 0; 600 const size_t maxRead = payloadSize - processedPayloadBytes; 601 const size_t maxWrite = maxBulkTransferSize - bulkTransferSize; 602 const size_t bulkTransferPayloadSize = std::min(maxRead, maxWrite); 603 // prepare payload. 604 if (!readError) { 605 const ssize_t result = readExactBytes( 606 fd, 607 mBuffer + bulkTransferSize, 608 bulkTransferPayloadSize); 609 if (result < 0) { 610 ALOGE("Found an error while reading data from FD. Send 0 data instead."); 611 readError = true; 612 } 613 } 614 if (readError) { 615 memset(mBuffer + bulkTransferSize, 0, bulkTransferPayloadSize); 616 } 617 bulkTransferSize += bulkTransferPayloadSize; 618 } 619 620 // Bulk transfer. 621 mPacketSize = bulkTransferSize; 622 request->buffer_length = bulkTransferSize; 623 const int result = transfer(request); 624 if (result != static_cast<ssize_t>(bulkTransferSize)) { 625 // Cannot recover writing error. 626 ALOGE("Found an error while write data to MtpDevice."); 627 return -1; 628 } 629 630 // Update variables. 631 processedBytes += bulkTransferSize; 632 } 633 634 return readError ? -1 : processedBytes; 635 } 636 637 #endif // MTP_HOST 638 639 void* MtpDataPacket::getData(int* outLength) const { 640 int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE; 641 if (length > 0) { 642 void* result = malloc(length); 643 if (result) { 644 memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length); 645 *outLength = length; 646 return result; 647 } 648 } 649 *outLength = 0; 650 return NULL; 651 } 652 653 } // namespace android 654