blob: 20225ba2cce5dca181ad7cc957b60b34b841b6d0 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
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 Lockwoodb14e5882010-06-29 18:11:52 -040017#define LOG_TAG "MtpDataPacket"
18
Mike Lockwood16864ba2010-05-11 17:16:59 -040019#include <stdio.h>
20#include <sys/types.h>
21#include <fcntl.h>
22
Mike Lockwood0cf89f22010-07-26 20:40:45 -040023#include <usbhost/usbhost.h>
24
Mike Lockwood16864ba2010-05-11 17:16:59 -040025#include "MtpDataPacket.h"
26#include "MtpStringBuffer.h"
27
Mike Lockwood7850ef92010-05-14 10:10:36 -040028namespace android {
29
Mike Lockwood16864ba2010-05-11 17:16:59 -040030MtpDataPacket::MtpDataPacket()
Mike Lockwood33bde8d2011-03-12 14:03:23 -050031 : MtpPacket(16384), // MAX_USBFS_BUFFER_SIZE
Mike Lockwood16864ba2010-05-11 17:16:59 -040032 mOffset(MTP_CONTAINER_HEADER_SIZE)
33{
34}
35
36MtpDataPacket::~MtpDataPacket() {
37}
38
39void MtpDataPacket::reset() {
40 MtpPacket::reset();
41 mOffset = MTP_CONTAINER_HEADER_SIZE;
42}
43
44void MtpDataPacket::setOperationCode(MtpOperationCode code) {
45 MtpPacket::putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
46}
47
48void MtpDataPacket::setTransactionID(MtpTransactionID id) {
49 MtpPacket::putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
50}
51
52uint16_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
59uint32_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
67uint64_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
Mike Lockwooda6c490b2010-06-05 22:45:01 -040077void MtpDataPacket::getUInt128(uint128_t& value) {
78 value[0] = getUInt32();
79 value[1] = getUInt32();
80 value[2] = getUInt32();
81 value[3] = getUInt32();
82}
83
Mike Lockwood16864ba2010-05-11 17:16:59 -040084void MtpDataPacket::getString(MtpStringBuffer& string)
85{
86 string.readFromPacket(this);
87}
88
Mike Lockwood335dd2b2010-05-19 10:33:39 -040089Int8List* 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
97UInt8List* 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
105Int16List* 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
113UInt16List* 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
121Int32List* 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
129UInt32List* 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
137Int64List* 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
145UInt64List* 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
Mike Lockwood16864ba2010-05-11 17:16:59 -0400153void MtpDataPacket::putInt8(int8_t value) {
154 allocate(mOffset + 1);
155 mBuffer[mOffset++] = (uint8_t)value;
156 if (mPacketSize < mOffset)
157 mPacketSize = mOffset;
158}
159
160void MtpDataPacket::putUInt8(uint8_t value) {
161 allocate(mOffset + 1);
162 mBuffer[mOffset++] = (uint8_t)value;
163 if (mPacketSize < mOffset)
164 mPacketSize = mOffset;
165}
166
167void 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
175void 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
183void 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
193void 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
203void 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
217void 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
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400231void MtpDataPacket::putInt128(const int128_t& value) {
232 putInt32(value[0]);
233 putInt32(value[1]);
234 putInt32(value[2]);
235 putInt32(value[3]);
236}
237
238void MtpDataPacket::putUInt128(const uint128_t& value) {
239 putUInt32(value[0]);
240 putUInt32(value[1]);
241 putUInt32(value[2]);
242 putUInt32(value[3]);
243}
244
Mike Lockwood8277cec2010-08-10 15:20:35 -0400245void MtpDataPacket::putInt128(int64_t value) {
246 putInt64(value);
Mike Lockwood59599882010-08-25 19:10:24 -0400247 putInt64(value < 0 ? -1 : 0);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400248}
249
250void MtpDataPacket::putUInt128(uint64_t value) {
251 putUInt64(value);
252 putUInt64(0);
253}
254
Mike Lockwood16864ba2010-05-11 17:16:59 -0400255void MtpDataPacket::putAInt8(const int8_t* values, int count) {
256 putUInt32(count);
257 for (int i = 0; i < count; i++)
258 putInt8(*values++);
259}
260
261void MtpDataPacket::putAUInt8(const uint8_t* values, int count) {
262 putUInt32(count);
263 for (int i = 0; i < count; i++)
264 putUInt8(*values++);
265}
266
267void MtpDataPacket::putAInt16(const int16_t* values, int count) {
268 putUInt32(count);
269 for (int i = 0; i < count; i++)
270 putInt16(*values++);
271}
272
273void MtpDataPacket::putAUInt16(const uint16_t* values, int count) {
274 putUInt32(count);
275 for (int i = 0; i < count; i++)
276 putUInt16(*values++);
277}
278
Mike Lockwood782aef12010-08-10 07:37:50 -0400279void 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
Mike Lockwood16864ba2010-05-11 17:16:59 -0400286void MtpDataPacket::putAInt32(const int32_t* values, int count) {
287 putUInt32(count);
288 for (int i = 0; i < count; i++)
289 putInt32(*values++);
290}
291
292void MtpDataPacket::putAUInt32(const uint32_t* values, int count) {
293 putUInt32(count);
294 for (int i = 0; i < count; i++)
295 putUInt32(*values++);
296}
297
298void 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
309void MtpDataPacket::putAInt64(const int64_t* values, int count) {
310 putUInt32(count);
311 for (int i = 0; i < count; i++)
312 putInt64(*values++);
313}
314
315void MtpDataPacket::putAUInt64(const uint64_t* values, int count) {
316 putUInt32(count);
317 for (int i = 0; i < count; i++)
318 putUInt64(*values++);
319}
320
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400321void MtpDataPacket::putString(const MtpStringBuffer& string) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400322 string.writeToPacket(this);
323}
324
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400325void MtpDataPacket::putString(const char* s) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400326 MtpStringBuffer string(s);
327 string.writeToPacket(this);
328}
329
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400330void 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 }
Mike Lockwoodde1e37a2010-08-18 12:31:09 -0400338 putUInt8(count > 0 ? count + 1 : 0);
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400339 for (int i = 0; i < count; i++)
340 putUInt16(string[i]);
Mike Lockwoodde1e37a2010-08-18 12:31:09 -0400341 // only terminate with zero if string is not empty
342 if (count > 0)
343 putUInt16(0);
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400344}
345
Mike Lockwood16864ba2010-05-11 17:16:59 -0400346#ifdef MTP_DEVICE
347int MtpDataPacket::read(int fd) {
Mike Lockwoodef441d92011-07-14 21:00:02 -0400348 int ret = ::read(fd, mBuffer, mBufferSize);
349 if (ret < MTP_CONTAINER_HEADER_SIZE)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400350 return -1;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400351 mPacketSize = ret;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400352 mOffset = MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400353 return ret;
354}
355
356int MtpDataPacket::write(int fd) {
357 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
358 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
Mike Lockwoodef441d92011-07-14 21:00:02 -0400359 int ret = ::write(fd, mBuffer, mPacketSize);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400360 return (ret < 0 ? ret : 0);
361}
Mike Lockwood64000782011-04-24 18:40:17 -0700362
363int MtpDataPacket::writeData(int fd, void* data, uint32_t length) {
Mike Lockwoodef441d92011-07-14 21:00:02 -0400364 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);
Mike Lockwood64000782011-04-24 18:40:17 -0700368 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
Mike Lockwoodef441d92011-07-14 21:00:02 -0400369 int ret = ::write(fd, mBuffer, length);
Mike Lockwood64000782011-04-24 18:40:17 -0700370 return (ret < 0 ? ret : 0);
371}
372
Mike Lockwood16864ba2010-05-11 17:16:59 -0400373#endif // MTP_DEVICE
374
375#ifdef MTP_HOST
Mike Lockwood42d0b792011-01-04 14:48:57 -0500376int MtpDataPacket::read(struct usb_request *request) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400377 // first read the header
Mike Lockwood42d0b792011-01-04 14:48:57 -0500378 request->buffer = mBuffer;
379 request->buffer_length = mBufferSize;
380 int length = transfer(request);
Mike Lockwood437e9452010-07-20 12:01:36 -0400381 if (length >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwood3e072b32010-06-10 16:34:20 -0400382 // look at the length field to see if the data spans multiple packets
383 uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
Mike Lockwood33bde8d2011-03-12 14:03:23 -0500384 allocate(totalLength);
Mike Lockwood3e072b32010-06-10 16:34:20 -0400385 while (totalLength > length) {
Mike Lockwood42d0b792011-01-04 14:48:57 -0500386 request->buffer = mBuffer + length;
Mike Lockwood33bde8d2011-03-12 14:03:23 -0500387 request->buffer_length = totalLength - length;
Mike Lockwood42d0b792011-01-04 14:48:57 -0500388 int ret = transfer(request);
Mike Lockwood3e072b32010-06-10 16:34:20 -0400389 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;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400400}
401
Mike Lockwood42d0b792011-01-04 14:48:57 -0500402int MtpDataPacket::readData(struct usb_request *request, void* buffer, int length) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400403 int read = 0;
404 while (read < length) {
Mike Lockwood42d0b792011-01-04 14:48:57 -0500405 request->buffer = (char *)buffer + read;
406 request->buffer_length = length - read;
407 int ret = transfer(request);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400408 if (ret < 0) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400409 return ret;
410 }
411 read += ret;
412 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400413 return read;
414}
415
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500416// Queue a read request. Call readDataWait to wait for result
Mike Lockwood42d0b792011-01-04 14:48:57 -0500417int MtpDataPacket::readDataAsync(struct usb_request *req) {
418 if (usb_request_queue(req)) {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500419 LOGE("usb_endpoint_queue failed, errno: %d", errno);
420 return -1;
421 }
422 return 0;
423}
424
425// Wait for result of readDataAsync
Mike Lockwood42d0b792011-01-04 14:48:57 -0500426int MtpDataPacket::readDataWait(struct usb_device *device) {
427 struct usb_request *req = usb_request_wait(device);
428 return (req ? req->actual_length : -1);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500429}
430
Mike Lockwood42d0b792011-01-04 14:48:57 -0500431int MtpDataPacket::readDataHeader(struct usb_request *request) {
432 request->buffer = mBuffer;
433 request->buffer_length = request->max_packet_size;
434 int length = transfer(request);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400435 if (length >= 0)
436 mPacketSize = length;
437 return length;
438}
439
Mike Lockwood42d0b792011-01-04 14:48:57 -0500440int MtpDataPacket::writeDataHeader(struct usb_request *request, uint32_t length) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400441 MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
442 MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
Mike Lockwood42d0b792011-01-04 14:48:57 -0500443 request->buffer = mBuffer;
444 request->buffer_length = MTP_CONTAINER_HEADER_SIZE;
445 int ret = transfer(request);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400446 return (ret < 0 ? ret : 0);
447}
448
Mike Lockwood42d0b792011-01-04 14:48:57 -0500449int MtpDataPacket::write(struct usb_request *request) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400450 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
Mike Lockwood42d0b792011-01-04 14:48:57 -0500454 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 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400462 return (ret < 0 ? ret : 0);
463}
464
Mike Lockwood42d0b792011-01-04 14:48:57 -0500465int 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);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400469 return (ret < 0 ? ret : 0);
470}
471
Mike Lockwood16864ba2010-05-11 17:16:59 -0400472#endif // MTP_HOST
Mike Lockwood7850ef92010-05-14 10:10:36 -0400473
Mike Lockwood3e072b32010-06-10 16:34:20 -0400474void* 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
Mike Lockwood7850ef92010-05-14 10:10:36 -0400488} // namespace android