MtpDataPacket.cpp 19 KB

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