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