blob: af67fc0722b121f0b22330f7e495236a8ed0464e [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -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
Mike Lockwoodb14e5882010-06-29 18:11:52 -040017#define LOG_TAG "MtpPacket"
18
19#include "MtpDebug.h"
20#include "MtpPacket.h"
21#include "mtp.h"
22
Mike Lockwood16864ba2010-05-11 17:16:59 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <stdio.h>
26
27#include <usbhost/usbhost.h>
28
Mike Lockwood7850ef92010-05-14 10:10:36 -040029namespace android {
30
Mike Lockwood16864ba2010-05-11 17:16:59 -040031MtpPacket::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) {
Steve Block29357bc2012-01-06 19:20:56 +000039 ALOGE("out of memory!");
Mike Lockwood16864ba2010-05-11 17:16:59 -040040 abort();
41 }
42}
43
44MtpPacket::~MtpPacket() {
45 if (mBuffer)
46 free(mBuffer);
47}
48
49void MtpPacket::reset() {
50 allocate(MTP_CONTAINER_HEADER_SIZE);
51 mPacketSize = MTP_CONTAINER_HEADER_SIZE;
52 memset(mBuffer, 0, mBufferSize);
53}
54
Mike Lockwoodab063842014-11-12 14:20:06 -080055void MtpPacket::allocate(size_t length) {
Mike Lockwood16864ba2010-05-11 17:16:59 -040056 if (length > mBufferSize) {
57 int newLength = length + mAllocationIncrement;
58 mBuffer = (uint8_t *)realloc(mBuffer, newLength);
59 if (!mBuffer) {
Steve Block29357bc2012-01-06 19:20:56 +000060 ALOGE("out of memory!");
Mike Lockwood16864ba2010-05-11 17:16:59 -040061 abort();
62 }
63 mBufferSize = newLength;
64 }
65}
66
67void MtpPacket::dump() {
Mike Lockwoodb14e5882010-06-29 18:11:52 -040068#define DUMP_BYTES_PER_ROW 16
69 char buffer[500];
70 char* bufptr = buffer;
71
Daichi Hironob3be0062016-02-25 12:42:58 +090072 for (size_t i = 0; i < mPacketSize; i++) {
George Burgess IV2c9cb622016-02-29 13:39:17 -080073 bufptr += snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%02X ",
74 mBuffer[i]);
Mike Lockwoodb14e5882010-06-29 18:11:52 -040075 if (i % DUMP_BYTES_PER_ROW == (DUMP_BYTES_PER_ROW - 1)) {
Steve Block3856b092011-10-20 11:56:00 +010076 ALOGV("%s", buffer);
Mike Lockwoodb14e5882010-06-29 18:11:52 -040077 bufptr = buffer;
78 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040079 }
Mike Lockwoodb14e5882010-06-29 18:11:52 -040080 if (bufptr != buffer) {
81 // print last line
Steve Block3856b092011-10-20 11:56:00 +010082 ALOGV("%s", buffer);
Mike Lockwoodb14e5882010-06-29 18:11:52 -040083 }
Steve Block3856b092011-10-20 11:56:00 +010084 ALOGV("\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -040085}
86
Mike Lockwoodf7454622010-12-09 18:34:18 -080087void MtpPacket::copyFrom(const MtpPacket& src) {
88 int length = src.mPacketSize;
89 allocate(length);
90 mPacketSize = length;
91 memcpy(mBuffer, src.mBuffer, length);
92}
93
Mike Lockwood16864ba2010-05-11 17:16:59 -040094uint16_t MtpPacket::getUInt16(int offset) const {
Shruti Bihania669e342023-07-10 08:53:42 +000095 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!");
Sarup Dalwanid4931082023-10-19 06:59:30 +0000100 return 0;
Shruti Bihania669e342023-07-10 08:53:42 +0000101 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400102}
103
104uint32_t MtpPacket::getUInt32(int offset) const {
Shruti Bihania669e342023-07-10 08:53:42 +0000105 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!");
Sarup Dalwanid4931082023-10-19 06:59:30 +0000111 return 0;
Shruti Bihania669e342023-07-10 08:53:42 +0000112 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400113}
114
115void MtpPacket::putUInt16(int offset, uint16_t value) {
Shruti Bihania669e342023-07-10 08:53:42 +0000116 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 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400123}
124
125void MtpPacket::putUInt32(int offset, uint32_t value) {
Shruti Bihania669e342023-07-10 08:53:42 +0000126 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 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400135}
136
137uint16_t MtpPacket::getContainerCode() const {
138 return getUInt16(MTP_CONTAINER_CODE_OFFSET);
139}
140
141void MtpPacket::setContainerCode(uint16_t code) {
142 putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
143}
144
Mike Lockwoodf7454622010-12-09 18:34:18 -0800145uint16_t MtpPacket::getContainerType() const {
146 return getUInt16(MTP_CONTAINER_TYPE_OFFSET);
147}
148
Mike Lockwood16864ba2010-05-11 17:16:59 -0400149MtpTransactionID MtpPacket::getTransactionID() const {
150 return getUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET);
151}
152
153void MtpPacket::setTransactionID(MtpTransactionID id) {
154 putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
155}
156
157uint32_t MtpPacket::getParameter(int index) const {
158 if (index < 1 || index > 5) {
Steve Block29357bc2012-01-06 19:20:56 +0000159 ALOGE("index %d out of range in MtpPacket::getParameter", index);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400160 return 0;
161 }
162 return getUInt32(MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t));
163}
164
165void MtpPacket::setParameter(int index, uint32_t value) {
166 if (index < 1 || index > 5) {
Steve Block29357bc2012-01-06 19:20:56 +0000167 ALOGE("index %d out of range in MtpPacket::setParameter", index);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400168 return;
169 }
170 int offset = MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t);
Ashish Kumar Guptae1494a22023-11-21 08:48:43 +0530171 if (mPacketSize < offset + sizeof(uint32_t)) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400172 mPacketSize = offset + sizeof(uint32_t);
Ashish Kumar Guptae1494a22023-11-21 08:48:43 +0530173 allocate(mPacketSize);
174 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400175 putUInt32(offset, value);
176}
177
178#ifdef MTP_HOST
Mike Lockwood42d0b792011-01-04 14:48:57 -0500179int MtpPacket::transfer(struct usb_request* request) {
Ashish Kumar Guptadcd86212023-10-17 15:54:31 +0530180 if (request->dev == NULL) {
181 return -1;
182 }
Mike Lockwood126ea732011-02-14 08:07:50 -0500183 int result = usb_device_bulk_transfer(request->dev,
184 request->endpoint,
185 request->buffer,
186 request->buffer_length,
James Wei8d96aa82021-06-19 20:48:10 +0800187 5000);
Mike Lockwood126ea732011-02-14 08:07:50 -0500188 request->actual_length = result;
189 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400190}
191#endif
Mike Lockwood7850ef92010-05-14 10:10:36 -0400192
193} // namespace android