MtpDataPacket.cpp revision 782aef17c9921a3bf401a0432878df5031f2328b
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
28namespace android {
29
30MtpDataPacket::MtpDataPacket()
31    :   MtpPacket(512),
32        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
77void MtpDataPacket::getUInt128(uint128_t& value) {
78    value[0] = getUInt32();
79    value[1] = getUInt32();
80    value[2] = getUInt32();
81    value[3] = getUInt32();
82}
83
84void MtpDataPacket::getString(MtpStringBuffer& string)
85{
86    string.readFromPacket(this);
87}
88
89Int8List* 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
153void 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
231void 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
245void MtpDataPacket::putAInt8(const int8_t* values, int count) {
246    putUInt32(count);
247    for (int i = 0; i < count; i++)
248        putInt8(*values++);
249}
250
251void MtpDataPacket::putAUInt8(const uint8_t* values, int count) {
252    putUInt32(count);
253    for (int i = 0; i < count; i++)
254        putUInt8(*values++);
255}
256
257void MtpDataPacket::putAInt16(const int16_t* values, int count) {
258    putUInt32(count);
259    for (int i = 0; i < count; i++)
260        putInt16(*values++);
261}
262
263void MtpDataPacket::putAUInt16(const uint16_t* values, int count) {
264    putUInt32(count);
265    for (int i = 0; i < count; i++)
266        putUInt16(*values++);
267}
268
269void MtpDataPacket::putAUInt16(const UInt16List* values) {
270    size_t count = (values ? values->size() : 0);
271    putUInt32(count);
272    for (size_t i = 0; i < count; i++)
273        putUInt16((*values)[i]);
274}
275
276void MtpDataPacket::putAInt32(const int32_t* values, int count) {
277    putUInt32(count);
278    for (int i = 0; i < count; i++)
279        putInt32(*values++);
280}
281
282void MtpDataPacket::putAUInt32(const uint32_t* values, int count) {
283    putUInt32(count);
284    for (int i = 0; i < count; i++)
285        putUInt32(*values++);
286}
287
288void MtpDataPacket::putAUInt32(const UInt32List* list) {
289    if (!list) {
290        putEmptyArray();
291    } else {
292        size_t size = list->size();
293        putUInt32(size);
294        for (size_t i = 0; i < size; i++)
295            putUInt32((*list)[i]);
296    }
297}
298
299void MtpDataPacket::putAInt64(const int64_t* values, int count) {
300    putUInt32(count);
301    for (int i = 0; i < count; i++)
302        putInt64(*values++);
303}
304
305void MtpDataPacket::putAUInt64(const uint64_t* values, int count) {
306    putUInt32(count);
307    for (int i = 0; i < count; i++)
308        putUInt64(*values++);
309}
310
311void MtpDataPacket::putString(const MtpStringBuffer& string) {
312    string.writeToPacket(this);
313}
314
315void MtpDataPacket::putString(const char* s) {
316    MtpStringBuffer string(s);
317    string.writeToPacket(this);
318}
319
320void MtpDataPacket::putString(const uint16_t* string) {
321    int count = 0;
322    for (int i = 0; i < 256; i++) {
323        if (string[i])
324            count++;
325        else
326            break;
327    }
328    putUInt8(count);
329    for (int i = 0; i < count; i++)
330        putUInt16(string[i]);
331}
332
333#ifdef MTP_DEVICE
334int MtpDataPacket::read(int fd) {
335    // first read the header
336    int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
337    if (ret != MTP_CONTAINER_HEADER_SIZE)
338        return -1;
339    // then the following data
340    int total = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
341    int remaining = total - MTP_CONTAINER_HEADER_SIZE;
342    ret = ::read(fd, &mBuffer[0] + MTP_CONTAINER_HEADER_SIZE, remaining);
343    if (ret != remaining)
344        return -1;
345
346    mPacketSize = total;
347    mOffset = MTP_CONTAINER_HEADER_SIZE;
348    return total;
349}
350
351int MtpDataPacket::readDataHeader(int fd) {
352    int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
353    if (ret > 0)
354        mPacketSize = ret;
355    else
356        mPacketSize = 0;
357    return ret;
358}
359
360int MtpDataPacket::write(int fd) {
361    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
362    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
363
364    // send header separately from data
365    int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
366    if (ret == MTP_CONTAINER_HEADER_SIZE)
367        ret = ::write(fd, mBuffer + MTP_CONTAINER_HEADER_SIZE,
368                        mPacketSize - MTP_CONTAINER_HEADER_SIZE);
369    return (ret < 0 ? ret : 0);
370}
371
372int MtpDataPacket::writeDataHeader(int fd, uint32_t length) {
373    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
374    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
375    int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
376    return (ret < 0 ? ret : 0);
377}
378#endif // MTP_DEVICE
379
380#ifdef MTP_HOST
381int MtpDataPacket::read(struct usb_endpoint *ep) {
382    // first read the header
383    int length = transfer(ep, mBuffer, mBufferSize);
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        while (totalLength > length) {
388            allocate(length + mAllocationIncrement);
389            int ret = transfer(ep, mBuffer + length, mAllocationIncrement);
390            if (ret >= 0)
391                length += ret;
392            else {
393                length = ret;
394                break;
395            }
396        }
397    }
398    if (length >= 0)
399        mPacketSize = length;
400    return length;
401}
402
403int MtpDataPacket::readData(struct usb_endpoint *ep, void* buffer, int length) {
404    int packetSize = usb_endpoint_max_packet(ep);
405    int read = 0;
406    while (read < length) {
407        int ret = transfer(ep, (char *)buffer + read, packetSize);
408        if (ret < 0) {
409printf("MtpDataPacket::readData returning %d\n", ret);
410            return ret;
411        }
412        read += ret;
413    }
414printf("MtpDataPacket::readData returning %d\n", read);
415    return read;
416}
417
418int MtpDataPacket::readDataHeader(struct usb_endpoint *ep) {
419    int length = transfer(ep, mBuffer, usb_endpoint_max_packet(ep));
420    if (length >= 0)
421        mPacketSize = length;
422    return length;
423}
424
425int MtpDataPacket::writeDataHeader(struct usb_endpoint *ep, uint32_t length) {
426    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
427    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
428    int ret = transfer(ep, mBuffer, MTP_CONTAINER_HEADER_SIZE);
429    return (ret < 0 ? ret : 0);
430}
431
432int MtpDataPacket::write(struct usb_endpoint *ep) {
433    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
434    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
435
436    // send header separately from data
437    int ret = transfer(ep, mBuffer, MTP_CONTAINER_HEADER_SIZE);
438    if (ret == MTP_CONTAINER_HEADER_SIZE)
439        ret = transfer(ep, mBuffer + MTP_CONTAINER_HEADER_SIZE,
440                        mPacketSize - MTP_CONTAINER_HEADER_SIZE);
441    return (ret < 0 ? ret : 0);
442}
443
444int MtpDataPacket::write(struct usb_endpoint *ep, void* buffer, uint32_t length) {
445    int ret = 0;
446    int packetSize = usb_endpoint_max_packet(ep);
447    while (length > 0) {
448        int write = (length > packetSize ? packetSize : length);
449        int ret = transfer(ep, buffer, write);
450        if (ret < 0)
451            break;
452        length -= ret;
453    }
454    return (ret < 0 ? ret : 0);
455}
456
457#endif // MTP_HOST
458
459void* MtpDataPacket::getData(int& outLength) const {
460    int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
461    if (length > 0) {
462        void* result = malloc(length);
463        if (result) {
464            memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length);
465            outLength = length;
466            return result;
467        }
468    }
469    outLength = 0;
470    return NULL;
471}
472
473}  // namespace android
474