blob: 2f9e438bab62def0c6c0fedeb808d3a706b1ccef [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -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 * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++
17 */
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
30MtpPacket::MtpPacket(int bufferSize)
31 : mBuffer(NULL),
32 mBufferSize(bufferSize),
33 mAllocationIncrement(bufferSize),
34 mPacketSize(0)
35{
36 mBuffer = (uint8_t *)malloc(bufferSize);
37 if (!mBuffer) {
38 MTPE("out of memory!");
39 abort();
40 }
41}
42
43MtpPacket::~MtpPacket() {
44 if (mBuffer)
45 free(mBuffer);
46}
47
48void MtpPacket::reset() {
49 allocate(MTP_CONTAINER_HEADER_SIZE);
50 mPacketSize = MTP_CONTAINER_HEADER_SIZE;
51 memset(mBuffer, 0, mBufferSize);
52}
53
54void MtpPacket::allocate(int length) {
55 if (length > mBufferSize) {
56 int newLength = length + mAllocationIncrement;
57 mBuffer = (uint8_t *)realloc(mBuffer, newLength);
58 if (!mBuffer) {
59 MTPE("out of memory!");
60 abort();
61 }
62 mBufferSize = newLength;
63 }
64}
65
66void MtpPacket::dump() {
67#define DUMP_BYTES_PER_ROW 16
68 char buffer[500];
69 char* bufptr = buffer;
70
71 for (size_t i = 0; i < mPacketSize; i++) {
72 sprintf(bufptr, "%02X ", mBuffer[i]);
73 bufptr += strlen(bufptr);
74 if (i % DUMP_BYTES_PER_ROW == (DUMP_BYTES_PER_ROW - 1)) {
75 MTPD("%s", buffer);
76 bufptr = buffer;
77 }
78 }
79 if (bufptr != buffer) {
80 // print last line
81 MTPD("%s", buffer);
82 }
83 MTPD("\n");
84}
85
86void MtpPacket::copyFrom(const MtpPacket& src) {
87 int length = src.mPacketSize;
88 allocate(length);
89 mPacketSize = length;
90 memcpy(mBuffer, src.mBuffer, length);
91}
92
93uint16_t MtpPacket::getUInt16(int offset) const {
94 return ((uint16_t)mBuffer[offset + 1] << 8) | (uint16_t)mBuffer[offset];
95}
96
97uint32_t MtpPacket::getUInt32(int offset) const {
98 return ((uint32_t)mBuffer[offset + 3] << 24) | ((uint32_t)mBuffer[offset + 2] << 16) |
99 ((uint32_t)mBuffer[offset + 1] << 8) | (uint32_t)mBuffer[offset];
100}
101
102void MtpPacket::putUInt16(int offset, uint16_t value) {
103 mBuffer[offset++] = (uint8_t)(value & 0xFF);
104 mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
105}
106
107void MtpPacket::putUInt32(int offset, uint32_t value) {
108 mBuffer[offset++] = (uint8_t)(value & 0xFF);
109 mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
110 mBuffer[offset++] = (uint8_t)((value >> 16) & 0xFF);
111 mBuffer[offset++] = (uint8_t)((value >> 24) & 0xFF);
112}
113
114uint16_t MtpPacket::getContainerCode() const {
115 return getUInt16(MTP_CONTAINER_CODE_OFFSET);
116}
117
118void MtpPacket::setContainerCode(uint16_t code) {
119 putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
120}
121
122uint16_t MtpPacket::getContainerType() const {
123 return getUInt16(MTP_CONTAINER_TYPE_OFFSET);
124}
125
126MtpTransactionID MtpPacket::getTransactionID() const {
127 return getUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET);
128}
129
130void MtpPacket::setTransactionID(MtpTransactionID id) {
131 putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
132}
133
134uint32_t MtpPacket::getParameter(int index) const {
135 if (index < 1 || index > 5) {
136 MTPE("index %d out of range in MtpPacket::getParameter", index);
137 return 0;
138 }
139 return getUInt32(MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t));
140}
141
142void MtpPacket::setParameter(int index, uint32_t value) {
143 if (index < 1 || index > 5) {
144 MTPE("index %d out of range in MtpPacket::setParameter", index);
145 return;
146 }
147 int offset = MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t);
148 if (mPacketSize < offset + sizeof(uint32_t))
149 mPacketSize = offset + sizeof(uint32_t);
150 putUInt32(offset, value);
151}
152
153#ifdef MTP_HOST
154int MtpPacket::transfer(struct usb_request* request) {
155 int result = usb_device_bulk_transfer(request->dev,
156 request->endpoint,
157 request->buffer,
158 request->buffer_length,
159 0);
160 request->actual_length = result;
161 return result;
162}
163#endif
164