MtpDataPacket.cpp revision 5959988b63ace3fba2fc78b135a4f5ef25dcf860
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::putInt128(int64_t value) {
246    putInt64(value);
247    putInt64(value < 0 ? -1 : 0);
248}
249
250void MtpDataPacket::putUInt128(uint64_t value) {
251    putUInt64(value);
252    putUInt64(0);
253}
254
255void 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
279void 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
286void 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
321void MtpDataPacket::putString(const MtpStringBuffer& string) {
322    string.writeToPacket(this);
323}
324
325void MtpDataPacket::putString(const char* s) {
326    MtpStringBuffer string(s);
327    string.writeToPacket(this);
328}
329
330void 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
347int MtpDataPacket::read(int fd) {
348    // first read the header
349    int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
350    if (ret != MTP_CONTAINER_HEADER_SIZE)
351        return -1;
352    // then the following data
353    int total = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
354    int remaining = total - MTP_CONTAINER_HEADER_SIZE;
355    ret = ::read(fd, &mBuffer[0] + MTP_CONTAINER_HEADER_SIZE, remaining);
356    if (ret != remaining)
357        return -1;
358
359    mPacketSize = total;
360    mOffset = MTP_CONTAINER_HEADER_SIZE;
361    return total;
362}
363
364int MtpDataPacket::readDataHeader(int fd) {
365    int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
366    if (ret > 0)
367        mPacketSize = ret;
368    else
369        mPacketSize = 0;
370    return ret;
371}
372
373int MtpDataPacket::write(int fd) {
374    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
375    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
376    dump();
377    // send header separately from data
378    int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
379    if (ret == MTP_CONTAINER_HEADER_SIZE)
380        ret = ::write(fd, mBuffer + MTP_CONTAINER_HEADER_SIZE,
381                        mPacketSize - MTP_CONTAINER_HEADER_SIZE);
382    return (ret < 0 ? ret : 0);
383}
384
385int MtpDataPacket::writeDataHeader(int fd, uint32_t length) {
386    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
387    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
388    int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
389    return (ret < 0 ? ret : 0);
390}
391#endif // MTP_DEVICE
392
393#ifdef MTP_HOST
394int MtpDataPacket::read(struct usb_endpoint *ep) {
395    // first read the header
396    int length = transfer(ep, mBuffer, mBufferSize);
397    if (length >= MTP_CONTAINER_HEADER_SIZE) {
398        // look at the length field to see if the data spans multiple packets
399        uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
400        while (totalLength > length) {
401            allocate(length + mAllocationIncrement);
402            int ret = transfer(ep, mBuffer + length, mAllocationIncrement);
403            if (ret >= 0)
404                length += ret;
405            else {
406                length = ret;
407                break;
408            }
409        }
410    }
411    if (length >= 0)
412        mPacketSize = length;
413    return length;
414}
415
416int MtpDataPacket::readData(struct usb_endpoint *ep, void* buffer, int length) {
417    int packetSize = usb_endpoint_max_packet(ep);
418    int read = 0;
419    while (read < length) {
420        int ret = transfer(ep, (char *)buffer + read, packetSize);
421        if (ret < 0) {
422printf("MtpDataPacket::readData returning %d\n", ret);
423            return ret;
424        }
425        read += ret;
426    }
427printf("MtpDataPacket::readData returning %d\n", read);
428    return read;
429}
430
431int MtpDataPacket::readDataHeader(struct usb_endpoint *ep) {
432    int length = transfer(ep, mBuffer, usb_endpoint_max_packet(ep));
433    if (length >= 0)
434        mPacketSize = length;
435    return length;
436}
437
438int MtpDataPacket::writeDataHeader(struct usb_endpoint *ep, uint32_t length) {
439    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
440    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
441    int ret = transfer(ep, mBuffer, MTP_CONTAINER_HEADER_SIZE);
442    return (ret < 0 ? ret : 0);
443}
444
445int MtpDataPacket::write(struct usb_endpoint *ep) {
446    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
447    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
448
449    // send header separately from data
450    int ret = transfer(ep, mBuffer, MTP_CONTAINER_HEADER_SIZE);
451    if (ret == MTP_CONTAINER_HEADER_SIZE)
452        ret = transfer(ep, mBuffer + MTP_CONTAINER_HEADER_SIZE,
453                        mPacketSize - MTP_CONTAINER_HEADER_SIZE);
454    return (ret < 0 ? ret : 0);
455}
456
457int MtpDataPacket::write(struct usb_endpoint *ep, void* buffer, uint32_t length) {
458    int ret = 0;
459    int packetSize = usb_endpoint_max_packet(ep);
460    while (length > 0) {
461        int write = (length > packetSize ? packetSize : length);
462        int ret = transfer(ep, buffer, write);
463        if (ret < 0)
464            break;
465        length -= ret;
466    }
467    return (ret < 0 ? ret : 0);
468}
469
470#endif // MTP_HOST
471
472void* MtpDataPacket::getData(int& outLength) const {
473    int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
474    if (length > 0) {
475        void* result = malloc(length);
476        if (result) {
477            memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length);
478            outLength = length;
479            return result;
480        }
481    }
482    outLength = 0;
483    return NULL;
484}
485
486}  // namespace android
487