MtpDataPacket.cpp revision 64000788211f4c7e78c80a4a155390d1316e1176
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(16384),   // MAX_USBFS_BUFFER_SIZE
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    allocate(total);
355    int remaining = total - MTP_CONTAINER_HEADER_SIZE;
356    ret = ::read(fd, &mBuffer[0] + MTP_CONTAINER_HEADER_SIZE, remaining);
357    if (ret != remaining)
358        return -1;
359
360    mPacketSize = total;
361    mOffset = MTP_CONTAINER_HEADER_SIZE;
362    return total;
363}
364
365int MtpDataPacket::readDataHeader(int fd) {
366    int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
367    if (ret > 0)
368        mPacketSize = ret;
369    else
370        mPacketSize = 0;
371    return ret;
372}
373
374int MtpDataPacket::write(int fd) {
375    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
376    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
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
392int MtpDataPacket::writeData(int fd, void* data, uint32_t length) {
393    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length + MTP_CONTAINER_HEADER_SIZE);
394    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
395    int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE);
396    if (ret == MTP_CONTAINER_HEADER_SIZE)
397        ret = ::write(fd, data, length);
398    return (ret < 0 ? ret : 0);
399}
400
401#endif // MTP_DEVICE
402
403#ifdef MTP_HOST
404int MtpDataPacket::read(struct usb_request *request) {
405    // first read the header
406    request->buffer = mBuffer;
407    request->buffer_length = mBufferSize;
408    int length = transfer(request);
409    if (length >= MTP_CONTAINER_HEADER_SIZE) {
410        // look at the length field to see if the data spans multiple packets
411        uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
412        allocate(totalLength);
413        while (totalLength > length) {
414            request->buffer = mBuffer + length;
415            request->buffer_length = totalLength - length;
416            int ret = transfer(request);
417            if (ret >= 0)
418                length += ret;
419            else {
420                length = ret;
421                break;
422            }
423        }
424    }
425    if (length >= 0)
426        mPacketSize = length;
427    return length;
428}
429
430int MtpDataPacket::readData(struct usb_request *request, void* buffer, int length) {
431    int read = 0;
432    while (read < length) {
433        request->buffer = (char *)buffer + read;
434        request->buffer_length = length - read;
435        int ret = transfer(request);
436        if (ret < 0) {
437            return ret;
438        }
439        read += ret;
440    }
441    return read;
442}
443
444// Queue a read request.  Call readDataWait to wait for result
445int MtpDataPacket::readDataAsync(struct usb_request *req) {
446    if (usb_request_queue(req)) {
447        LOGE("usb_endpoint_queue failed, errno: %d", errno);
448        return -1;
449    }
450    return 0;
451}
452
453// Wait for result of readDataAsync
454int MtpDataPacket::readDataWait(struct usb_device *device) {
455    struct usb_request *req = usb_request_wait(device);
456    return (req ? req->actual_length : -1);
457}
458
459int MtpDataPacket::readDataHeader(struct usb_request *request) {
460    request->buffer = mBuffer;
461    request->buffer_length = request->max_packet_size;
462    int length = transfer(request);
463    if (length >= 0)
464        mPacketSize = length;
465    return length;
466}
467
468int MtpDataPacket::writeDataHeader(struct usb_request *request, uint32_t length) {
469    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
470    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
471    request->buffer = mBuffer;
472    request->buffer_length = MTP_CONTAINER_HEADER_SIZE;
473    int ret = transfer(request);
474    return (ret < 0 ? ret : 0);
475}
476
477int MtpDataPacket::write(struct usb_request *request) {
478    MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
479    MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
480
481    // send header separately from data
482    request->buffer = mBuffer;
483    request->buffer_length = MTP_CONTAINER_HEADER_SIZE;
484    int ret = transfer(request);
485    if (ret == MTP_CONTAINER_HEADER_SIZE) {
486        request->buffer = mBuffer + MTP_CONTAINER_HEADER_SIZE;
487        request->buffer_length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
488        ret = transfer(request);
489    }
490    return (ret < 0 ? ret : 0);
491}
492
493int MtpDataPacket::write(struct usb_request *request, void* buffer, uint32_t length) {
494    request->buffer = buffer;
495    request->buffer_length = length;
496    int ret = transfer(request);
497    return (ret < 0 ? ret : 0);
498}
499
500#endif // MTP_HOST
501
502void* MtpDataPacket::getData(int& outLength) const {
503    int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
504    if (length > 0) {
505        void* result = malloc(length);
506        if (result) {
507            memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length);
508            outLength = length;
509            return result;
510        }
511    }
512    outLength = 0;
513    return NULL;
514}
515
516}  // namespace android
517