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 <stdio.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22
23 #include <usbhost/usbhost.h>
24
25 #include "MtpDataPacket.h"
26 #include "MtpStringBuffer.h"
27
28 namespace android {
29
MtpDataPacket()30 MtpDataPacket::MtpDataPacket()
31 : MtpPacket(16384), // MAX_USBFS_BUFFER_SIZE
32 mOffset(MTP_CONTAINER_HEADER_SIZE)
33 {
34 }
35
~MtpDataPacket()36 MtpDataPacket::~MtpDataPacket() {
37 }
38
reset()39 void MtpDataPacket::reset() {
40 MtpPacket::reset();
41 mOffset = MTP_CONTAINER_HEADER_SIZE;
42 }
43
setOperationCode(MtpOperationCode code)44 void MtpDataPacket::setOperationCode(MtpOperationCode code) {
45 MtpPacket::putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
46 }
47
setTransactionID(MtpTransactionID id)48 void MtpDataPacket::setTransactionID(MtpTransactionID id) {
49 MtpPacket::putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
50 }
51
getUInt16()52 uint16_t MtpDataPacket::getUInt16() {
53 int offset = mOffset;
54 uint16_t result = (uint16_t)mBuffer[offset] | ((uint16_t)mBuffer[offset + 1] << 8);
55 mOffset += 2;
56 return result;
57 }
58
getUInt32()59 uint32_t MtpDataPacket::getUInt32() {
60 int offset = mOffset;
61 uint32_t result = (uint32_t)mBuffer[offset] | ((uint32_t)mBuffer[offset + 1] << 8) |
62 ((uint32_t)mBuffer[offset + 2] << 16) | ((uint32_t)mBuffer[offset + 3] << 24);
63 mOffset += 4;
64 return result;
65 }
66
getUInt64()67 uint64_t MtpDataPacket::getUInt64() {
68 int offset = mOffset;
69 uint64_t result = (uint64_t)mBuffer[offset] | ((uint64_t)mBuffer[offset + 1] << 8) |
70 ((uint64_t)mBuffer[offset + 2] << 16) | ((uint64_t)mBuffer[offset + 3] << 24) |
71 ((uint64_t)mBuffer[offset + 4] << 32) | ((uint64_t)mBuffer[offset + 5] << 40) |
72 ((uint64_t)mBuffer[offset + 6] << 48) | ((uint64_t)mBuffer[offset + 7] << 56);
73 mOffset += 8;
74 return result;
75 }
76
getUInt128(uint128_t & value)77 void MtpDataPacket::getUInt128(uint128_t& value) {
78 value[0] = getUInt32();
79 value[1] = getUInt32();
80 value[2] = getUInt32();
81 value[3] = getUInt32();
82 }
83
getString(MtpStringBuffer & string)84 void MtpDataPacket::getString(MtpStringBuffer& string)
85 {
86 string.readFromPacket(this);
87 }
88
getAInt8()89 Int8List* MtpDataPacket::getAInt8() {
90 Int8List* result = new Int8List;
91 int count = getUInt32();
92 for (int i = 0; i < count; i++)
93 result->push(getInt8());
94 return result;
95 }
96
getAUInt8()97 UInt8List* MtpDataPacket::getAUInt8() {
98 UInt8List* result = new UInt8List;
99 int count = getUInt32();
100 for (int i = 0; i < count; i++)
101 result->push(getUInt8());
102 return result;
103 }
104
getAInt16()105 Int16List* MtpDataPacket::getAInt16() {
106 Int16List* result = new Int16List;
107 int count = getUInt32();
108 for (int i = 0; i < count; i++)
109 result->push(getInt16());
110 return result;
111 }
112
getAUInt16()113 UInt16List* MtpDataPacket::getAUInt16() {
114 UInt16List* result = new UInt16List;
115 int count = getUInt32();
116 for (int i = 0; i < count; i++)
117 result->push(getUInt16());
118 return result;
119 }
120
getAInt32()121 Int32List* MtpDataPacket::getAInt32() {
122 Int32List* result = new Int32List;
123 int count = getUInt32();
124 for (int i = 0; i < count; i++)
125 result->push(getInt32());
126 return result;
127 }
128
getAUInt32()129 UInt32List* MtpDataPacket::getAUInt32() {
130 UInt32List* result = new UInt32List;
131 int count = getUInt32();
132 for (int i = 0; i < count; i++)
133 result->push(getUInt32());
134 return result;
135 }
136
getAInt64()137 Int64List* MtpDataPacket::getAInt64() {
138 Int64List* result = new Int64List;
139 int count = getUInt32();
140 for (int i = 0; i < count; i++)
141 result->push(getInt64());
142 return result;
143 }
144
getAUInt64()145 UInt64List* MtpDataPacket::getAUInt64() {
146 UInt64List* result = new UInt64List;
147 int count = getUInt32();
148 for (int i = 0; i < count; i++)
149 result->push(getUInt64());
150 return result;
151 }
152
putInt8(int8_t value)153 void MtpDataPacket::putInt8(int8_t value) {
154 allocate(mOffset + 1);
155 mBuffer[mOffset++] = (uint8_t)value;
156 if (mPacketSize < mOffset)
157 mPacketSize = mOffset;
158 }
159
putUInt8(uint8_t value)160 void MtpDataPacket::putUInt8(uint8_t value) {
161 allocate(mOffset + 1);
162 mBuffer[mOffset++] = (uint8_t)value;
163 if (mPacketSize < mOffset)
164 mPacketSize = mOffset;
165 }
166
putInt16(int16_t value)167 void MtpDataPacket::putInt16(int16_t value) {
168 allocate(mOffset + 2);
169 mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
170 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
171 if (mPacketSize < mOffset)
172 mPacketSize = mOffset;
173 }
174
putUInt16(uint16_t value)175 void MtpDataPacket::putUInt16(uint16_t value) {
176 allocate(mOffset + 2);
177 mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
178 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
179 if (mPacketSize < mOffset)
180 mPacketSize = mOffset;
181 }
182
putInt32(int32_t value)183 void MtpDataPacket::putInt32(int32_t value) {
184 allocate(mOffset + 4);
185 mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
186 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
187 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
188 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
189 if (mPacketSize < mOffset)
190 mPacketSize = mOffset;
191 }
192
putUInt32(uint32_t value)193 void MtpDataPacket::putUInt32(uint32_t value) {
194 allocate(mOffset + 4);
195 mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
196 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
197 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
198 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
199 if (mPacketSize < mOffset)
200 mPacketSize = mOffset;
201 }
202
putInt64(int64_t value)203 void MtpDataPacket::putInt64(int64_t value) {
204 allocate(mOffset + 8);
205 mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
206 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
207 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
208 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
209 mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF);
210 mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF);
211 mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF);
212 mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF);
213 if (mPacketSize < mOffset)
214 mPacketSize = mOffset;
215 }
216
putUInt64(uint64_t value)217 void MtpDataPacket::putUInt64(uint64_t value) {
218 allocate(mOffset + 8);
219 mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
220 mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
221 mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
222 mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
223 mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF);
224 mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF);
225 mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF);
226 mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF);
227 if (mPacketSize < mOffset)
228 mPacketSize = mOffset;
229 }
230
putInt128(const int128_t & value)231 void MtpDataPacket::putInt128(const int128_t& value) {
232 putInt32(value[0]);
233 putInt32(value[1]);
234 putInt32(value[2]);
235 putInt32(value[3]);
236 }
237
putUInt128(const uint128_t & value)238 void MtpDataPacket::putUInt128(const uint128_t& value) {
239 putUInt32(value[0]);
240 putUInt32(value[1]);
241 putUInt32(value[2]);
242 putUInt32(value[3]);
243 }
244
putInt128(int64_t value)245 void MtpDataPacket::putInt128(int64_t value) {
246 putInt64(value);
247 putInt64(value < 0 ? -1 : 0);
248 }
249
putUInt128(uint64_t value)250 void MtpDataPacket::putUInt128(uint64_t value) {
251 putUInt64(value);
252 putUInt64(0);
253 }
254
putAInt8(const int8_t * values,int count)255 void MtpDataPacket::putAInt8(const int8_t* values, int count) {
256 putUInt32(count);
257 for (int i = 0; i < count; i++)
258 putInt8(*values++);
259 }
260
putAUInt8(const uint8_t * values,int count)261 void MtpDataPacket::putAUInt8(const uint8_t* values, int count) {
262 putUInt32(count);
263 for (int i = 0; i < count; i++)
264 putUInt8(*values++);
265 }
266
putAInt16(const int16_t * values,int count)267 void MtpDataPacket::putAInt16(const int16_t* values, int count) {
268 putUInt32(count);
269 for (int i = 0; i < count; i++)
270 putInt16(*values++);
271 }
272
putAUInt16(const uint16_t * values,int count)273 void MtpDataPacket::putAUInt16(const uint16_t* values, int count) {
274 putUInt32(count);
275 for (int i = 0; i < count; i++)
276 putUInt16(*values++);
277 }
278
putAUInt16(const UInt16List * values)279 void MtpDataPacket::putAUInt16(const UInt16List* values) {
280 size_t count = (values ? values->size() : 0);
281 putUInt32(count);
282 for (size_t i = 0; i < count; i++)
283 putUInt16((*values)[i]);
284 }
285
putAInt32(const int32_t * values,int count)286 void MtpDataPacket::putAInt32(const int32_t* values, int count) {
287 putUInt32(count);
288 for (int i = 0; i < count; i++)
289 putInt32(*values++);
290 }
291
putAUInt32(const uint32_t * values,int count)292 void MtpDataPacket::putAUInt32(const uint32_t* values, int count) {
293 putUInt32(count);
294 for (int i = 0; i < count; i++)
295 putUInt32(*values++);
296 }
297
putAUInt32(const UInt32List * list)298 void MtpDataPacket::putAUInt32(const UInt32List* list) {
299 if (!list) {
300 putEmptyArray();
301 } else {
302 size_t size = list->size();
303 putUInt32(size);
304 for (size_t i = 0; i < size; i++)
305 putUInt32((*list)[i]);
306 }
307 }
308
putAInt64(const int64_t * values,int count)309 void MtpDataPacket::putAInt64(const int64_t* values, int count) {
310 putUInt32(count);
311 for (int i = 0; i < count; i++)
312 putInt64(*values++);
313 }
314
putAUInt64(const uint64_t * values,int count)315 void MtpDataPacket::putAUInt64(const uint64_t* values, int count) {
316 putUInt32(count);
317 for (int i = 0; i < count; i++)
318 putUInt64(*values++);
319 }
320
putString(const MtpStringBuffer & string)321 void MtpDataPacket::putString(const MtpStringBuffer& string) {
322 string.writeToPacket(this);
323 }
324
putString(const char * s)325 void MtpDataPacket::putString(const char* s) {
326 MtpStringBuffer string(s);
327 string.writeToPacket(this);
328 }
329
putString(const uint16_t * string)330 void MtpDataPacket::putString(const uint16_t* string) {
331 int count = 0;
332 for (int i = 0; i < 256; i++) {
333 if (string[i])
334 count++;
335 else
336 break;
337 }
338 putUInt8(count > 0 ? count + 1 : 0);
339 for (int i = 0; i < count; i++)
340 putUInt16(string[i]);
341 // only terminate with zero if string is not empty
342 if (count > 0)
343 putUInt16(0);
344 }
345
346 #ifdef MTP_DEVICE
read(int fd)347 int MtpDataPacket::read(int fd) {
348 int ret = ::read(fd, mBuffer, mBufferSize);
349 if (ret < MTP_CONTAINER_HEADER_SIZE)
350 return -1;
351 mPacketSize = ret;
352 mOffset = MTP_CONTAINER_HEADER_SIZE;
353 return ret;
354 }
355
write(int fd)356 int MtpDataPacket::write(int fd) {
357 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
358 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
359 int ret = ::write(fd, mBuffer, mPacketSize);
360 return (ret < 0 ? ret : 0);
361 }
362
writeData(int fd,void * data,uint32_t length)363 int MtpDataPacket::writeData(int fd, void* data, uint32_t length) {
364 allocate(length);
365 memcpy(mBuffer + MTP_CONTAINER_HEADER_SIZE, data, length);
366 length += MTP_CONTAINER_HEADER_SIZE;
367 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
368 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
369 int ret = ::write(fd, mBuffer, length);
370 return (ret < 0 ? ret : 0);
371 }
372
373 #endif // MTP_DEVICE
374
375 #ifdef MTP_HOST
read(struct usb_request * request)376 int MtpDataPacket::read(struct usb_request *request) {
377 // first read the header
378 request->buffer = mBuffer;
379 request->buffer_length = mBufferSize;
380 int length = transfer(request);
381 if (length >= MTP_CONTAINER_HEADER_SIZE) {
382 // look at the length field to see if the data spans multiple packets
383 uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
384 allocate(totalLength);
385 while (totalLength > length) {
386 request->buffer = mBuffer + length;
387 request->buffer_length = totalLength - length;
388 int ret = transfer(request);
389 if (ret >= 0)
390 length += ret;
391 else {
392 length = ret;
393 break;
394 }
395 }
396 }
397 if (length >= 0)
398 mPacketSize = length;
399 return length;
400 }
401
readData(struct usb_request * request,void * buffer,int length)402 int MtpDataPacket::readData(struct usb_request *request, void* buffer, int length) {
403 int read = 0;
404 while (read < length) {
405 request->buffer = (char *)buffer + read;
406 request->buffer_length = length - read;
407 int ret = transfer(request);
408 if (ret < 0) {
409 return ret;
410 }
411 read += ret;
412 }
413 return read;
414 }
415
416 // Queue a read request. Call readDataWait to wait for result
readDataAsync(struct usb_request * req)417 int MtpDataPacket::readDataAsync(struct usb_request *req) {
418 if (usb_request_queue(req)) {
419 LOGE("usb_endpoint_queue failed, errno: %d", errno);
420 return -1;
421 }
422 return 0;
423 }
424
425 // Wait for result of readDataAsync
readDataWait(struct usb_device * device)426 int MtpDataPacket::readDataWait(struct usb_device *device) {
427 struct usb_request *req = usb_request_wait(device);
428 return (req ? req->actual_length : -1);
429 }
430
readDataHeader(struct usb_request * request)431 int MtpDataPacket::readDataHeader(struct usb_request *request) {
432 request->buffer = mBuffer;
433 request->buffer_length = request->max_packet_size;
434 int length = transfer(request);
435 if (length >= 0)
436 mPacketSize = length;
437 return length;
438 }
439
writeDataHeader(struct usb_request * request,uint32_t length)440 int MtpDataPacket::writeDataHeader(struct usb_request *request, uint32_t length) {
441 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
442 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
443 request->buffer = mBuffer;
444 request->buffer_length = MTP_CONTAINER_HEADER_SIZE;
445 int ret = transfer(request);
446 return (ret < 0 ? ret : 0);
447 }
448
write(struct usb_request * request)449 int MtpDataPacket::write(struct usb_request *request) {
450 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
451 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
452
453 // send header separately from data
454 request->buffer = mBuffer;
455 request->buffer_length = MTP_CONTAINER_HEADER_SIZE;
456 int ret = transfer(request);
457 if (ret == MTP_CONTAINER_HEADER_SIZE) {
458 request->buffer = mBuffer + MTP_CONTAINER_HEADER_SIZE;
459 request->buffer_length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
460 ret = transfer(request);
461 }
462 return (ret < 0 ? ret : 0);
463 }
464
write(struct usb_request * request,void * buffer,uint32_t length)465 int MtpDataPacket::write(struct usb_request *request, void* buffer, uint32_t length) {
466 request->buffer = buffer;
467 request->buffer_length = length;
468 int ret = transfer(request);
469 return (ret < 0 ? ret : 0);
470 }
471
472 #endif // MTP_HOST
473
getData(int & outLength) const474 void* MtpDataPacket::getData(int& outLength) const {
475 int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
476 if (length > 0) {
477 void* result = malloc(length);
478 if (result) {
479 memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length);
480 outLength = length;
481 return result;
482 }
483 }
484 outLength = 0;
485 return NULL;
486 }
487
488 } // namespace android
489