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 "MtpPacket"
18 
19 #include "MtpDebug.h"
20 #include "MtpPacket.h"
21 #include "mtp.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 
27 #include <usbhost/usbhost.h>
28 
29 namespace android {
30 
MtpPacket(int bufferSize)31 MtpPacket::MtpPacket(int bufferSize)
32     :   mBuffer(NULL),
33         mBufferSize(bufferSize),
34         mAllocationIncrement(bufferSize),
35         mPacketSize(0)
36 {
37     mBuffer = (uint8_t *)malloc(bufferSize);
38     if (!mBuffer) {
39         ALOGE("out of memory!");
40         abort();
41     }
42 }
43 
~MtpPacket()44 MtpPacket::~MtpPacket() {
45     if (mBuffer)
46         free(mBuffer);
47 }
48 
reset()49 void MtpPacket::reset() {
50     allocate(MTP_CONTAINER_HEADER_SIZE);
51     mPacketSize = MTP_CONTAINER_HEADER_SIZE;
52     memset(mBuffer, 0, mBufferSize);
53 }
54 
allocate(size_t length)55 void MtpPacket::allocate(size_t length) {
56     if (length > mBufferSize) {
57         int newLength = length + mAllocationIncrement;
58         mBuffer = (uint8_t *)realloc(mBuffer, newLength);
59         if (!mBuffer) {
60             ALOGE("out of memory!");
61             abort();
62         }
63         mBufferSize = newLength;
64     }
65 }
66 
dump()67 void MtpPacket::dump() {
68 #define DUMP_BYTES_PER_ROW  16
69     char buffer[500];
70     char* bufptr = buffer;
71 
72     for (size_t i = 0; i < mPacketSize; i++) {
73         bufptr += snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%02X ",
74                            mBuffer[i]);
75         if (i % DUMP_BYTES_PER_ROW == (DUMP_BYTES_PER_ROW - 1)) {
76             ALOGV("%s", buffer);
77             bufptr = buffer;
78         }
79     }
80     if (bufptr != buffer) {
81         // print last line
82         ALOGV("%s", buffer);
83     }
84     ALOGV("\n");
85 }
86 
copyFrom(const MtpPacket & src)87 void MtpPacket::copyFrom(const MtpPacket& src) {
88     int length = src.mPacketSize;
89     allocate(length);
90     mPacketSize = length;
91     memcpy(mBuffer, src.mBuffer, length);
92 }
93 
getUInt16(int offset) const94 uint16_t MtpPacket::getUInt16(int offset) const {
95     if ((unsigned long)(offset+2) <= mBufferSize) {
96         return ((uint16_t)mBuffer[offset + 1] << 8) | (uint16_t)mBuffer[offset];
97     }
98     else {
99         ALOGE("offset for buffer read is greater than buffer size!");
100         return 0;
101     }
102 }
103 
getUInt32(int offset) const104 uint32_t MtpPacket::getUInt32(int offset) const {
105     if ((unsigned long)(offset+4) <= mBufferSize) {
106         return ((uint32_t)mBuffer[offset + 3] << 24) | ((uint32_t)mBuffer[offset + 2] << 16) |
107                ((uint32_t)mBuffer[offset + 1] << 8)  | (uint32_t)mBuffer[offset];
108     }
109     else {
110         ALOGE("offset for buffer read is greater than buffer size!");
111         return 0;
112     }
113 }
114 
putUInt16(int offset,uint16_t value)115 void MtpPacket::putUInt16(int offset, uint16_t value) {
116     if ((unsigned long)(offset+2) <= mBufferSize) {
117         mBuffer[offset++] = (uint8_t)(value & 0xFF);
118         mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
119     }
120     else {
121         ALOGE("offset for buffer write is greater than buffer size!");
122     }
123 }
124 
putUInt32(int offset,uint32_t value)125 void MtpPacket::putUInt32(int offset, uint32_t value) {
126     if ((unsigned long)(offset+4) <= mBufferSize) {
127         mBuffer[offset++] = (uint8_t)(value & 0xFF);
128         mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
129         mBuffer[offset++] = (uint8_t)((value >> 16) & 0xFF);
130         mBuffer[offset++] = (uint8_t)((value >> 24) & 0xFF);
131     }
132     else {
133         ALOGE("offset for buffer write is greater than buffer size!");
134     }
135 }
136 
getContainerCode() const137 uint16_t MtpPacket::getContainerCode() const {
138     return getUInt16(MTP_CONTAINER_CODE_OFFSET);
139 }
140 
setContainerCode(uint16_t code)141 void MtpPacket::setContainerCode(uint16_t code) {
142     putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
143 }
144 
getContainerType() const145 uint16_t MtpPacket::getContainerType() const {
146     return getUInt16(MTP_CONTAINER_TYPE_OFFSET);
147 }
148 
getTransactionID() const149 MtpTransactionID MtpPacket::getTransactionID() const {
150     return getUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET);
151 }
152 
setTransactionID(MtpTransactionID id)153 void MtpPacket::setTransactionID(MtpTransactionID id) {
154     putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
155 }
156 
getParameter(int index) const157 uint32_t MtpPacket::getParameter(int index) const {
158     if (index < 1 || index > 5) {
159         ALOGE("index %d out of range in MtpPacket::getParameter", index);
160         return 0;
161     }
162     return getUInt32(MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t));
163 }
164 
setParameter(int index,uint32_t value)165 void MtpPacket::setParameter(int index, uint32_t value) {
166     if (index < 1 || index > 5) {
167         ALOGE("index %d out of range in MtpPacket::setParameter", index);
168         return;
169     }
170     int offset = MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t);
171     if (mPacketSize < offset + sizeof(uint32_t)) {
172         mPacketSize = offset + sizeof(uint32_t);
173         allocate(mPacketSize);
174     }
175     putUInt32(offset, value);
176 }
177 
178 #ifdef MTP_HOST
transfer(struct usb_request * request)179 int MtpPacket::transfer(struct usb_request* request) {
180     if (request->dev == NULL) {
181         return -1;
182     }
183     int result = usb_device_bulk_transfer(request->dev,
184                             request->endpoint,
185                             request->buffer,
186                             request->buffer_length,
187                             5000);
188     request->actual_length = result;
189     return result;
190 }
191 #endif
192 
193 }  // namespace android
194