Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 3e6616d | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 17 | #define LOG_TAG "MtpDataPacket" |
| 18 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
| 22 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 23 | #include <usbhost/usbhost.h> |
| 24 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 25 | #include "MtpDataPacket.h" |
| 26 | #include "MtpStringBuffer.h" |
| 27 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 30 | MtpDataPacket::MtpDataPacket() |
| 31 | : MtpPacket(512), |
| 32 | mOffset(MTP_CONTAINER_HEADER_SIZE) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | MtpDataPacket::~MtpDataPacket() { |
| 37 | } |
| 38 | |
| 39 | void MtpDataPacket::reset() { |
| 40 | MtpPacket::reset(); |
| 41 | mOffset = MTP_CONTAINER_HEADER_SIZE; |
| 42 | } |
| 43 | |
| 44 | void MtpDataPacket::setOperationCode(MtpOperationCode code) { |
| 45 | MtpPacket::putUInt16(MTP_CONTAINER_CODE_OFFSET, code); |
| 46 | } |
| 47 | |
| 48 | void MtpDataPacket::setTransactionID(MtpTransactionID id) { |
| 49 | MtpPacket::putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id); |
| 50 | } |
| 51 | |
| 52 | uint16_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 | |
| 59 | uint32_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 | |
| 67 | uint64_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 | |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 77 | void MtpDataPacket::getUInt128(uint128_t& value) { |
| 78 | value[0] = getUInt32(); |
| 79 | value[1] = getUInt32(); |
| 80 | value[2] = getUInt32(); |
| 81 | value[3] = getUInt32(); |
| 82 | } |
| 83 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 84 | void MtpDataPacket::getString(MtpStringBuffer& string) |
| 85 | { |
| 86 | string.readFromPacket(this); |
| 87 | } |
| 88 | |
Mike Lockwood | 5bae7f6 | 2010-05-19 10:33:39 -0400 | [diff] [blame] | 89 | Int8List* 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 | |
| 97 | UInt8List* 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 | |
| 105 | Int16List* 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 | |
| 113 | UInt16List* 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 | |
| 121 | Int32List* 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 | |
| 129 | UInt32List* 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 | |
| 137 | Int64List* 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 | |
| 145 | UInt64List* 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 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 153 | void MtpDataPacket::putInt8(int8_t value) { |
| 154 | allocate(mOffset + 1); |
| 155 | mBuffer[mOffset++] = (uint8_t)value; |
| 156 | if (mPacketSize < mOffset) |
| 157 | mPacketSize = mOffset; |
| 158 | } |
| 159 | |
| 160 | void MtpDataPacket::putUInt8(uint8_t value) { |
| 161 | allocate(mOffset + 1); |
| 162 | mBuffer[mOffset++] = (uint8_t)value; |
| 163 | if (mPacketSize < mOffset) |
| 164 | mPacketSize = mOffset; |
| 165 | } |
| 166 | |
| 167 | void 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 | |
| 175 | void 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 | |
| 183 | void 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 | |
| 193 | void 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 | |
| 203 | void 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 | |
| 217 | void 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 | |
Mike Lockwood | 90f4873 | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 231 | void MtpDataPacket::putInt128(const int128_t& value) { |
| 232 | putInt32(value[0]); |
| 233 | putInt32(value[1]); |
| 234 | putInt32(value[2]); |
| 235 | putInt32(value[3]); |
| 236 | } |
| 237 | |
| 238 | void MtpDataPacket::putUInt128(const uint128_t& value) { |
| 239 | putUInt32(value[0]); |
| 240 | putUInt32(value[1]); |
| 241 | putUInt32(value[2]); |
| 242 | putUInt32(value[3]); |
| 243 | } |
| 244 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 245 | void MtpDataPacket::putInt128(int64_t value) { |
| 246 | putInt64(value); |
Mike Lockwood | 53b8f17 | 2010-08-25 19:10:24 -0400 | [diff] [blame] | 247 | putInt64(value < 0 ? -1 : 0); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void MtpDataPacket::putUInt128(uint64_t value) { |
| 251 | putUInt64(value); |
| 252 | putUInt64(0); |
| 253 | } |
| 254 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 255 | void MtpDataPacket::putAInt8(const int8_t* values, int count) { |
| 256 | putUInt32(count); |
| 257 | for (int i = 0; i < count; i++) |
| 258 | putInt8(*values++); |
| 259 | } |
| 260 | |
| 261 | void MtpDataPacket::putAUInt8(const uint8_t* values, int count) { |
| 262 | putUInt32(count); |
| 263 | for (int i = 0; i < count; i++) |
| 264 | putUInt8(*values++); |
| 265 | } |
| 266 | |
| 267 | void MtpDataPacket::putAInt16(const int16_t* values, int count) { |
| 268 | putUInt32(count); |
| 269 | for (int i = 0; i < count; i++) |
| 270 | putInt16(*values++); |
| 271 | } |
| 272 | |
| 273 | void MtpDataPacket::putAUInt16(const uint16_t* values, int count) { |
| 274 | putUInt32(count); |
| 275 | for (int i = 0; i < count; i++) |
| 276 | putUInt16(*values++); |
| 277 | } |
| 278 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 279 | void 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 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 286 | void MtpDataPacket::putAInt32(const int32_t* values, int count) { |
| 287 | putUInt32(count); |
| 288 | for (int i = 0; i < count; i++) |
| 289 | putInt32(*values++); |
| 290 | } |
| 291 | |
| 292 | void MtpDataPacket::putAUInt32(const uint32_t* values, int count) { |
| 293 | putUInt32(count); |
| 294 | for (int i = 0; i < count; i++) |
| 295 | putUInt32(*values++); |
| 296 | } |
| 297 | |
| 298 | void 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 | |
| 309 | void MtpDataPacket::putAInt64(const int64_t* values, int count) { |
| 310 | putUInt32(count); |
| 311 | for (int i = 0; i < count; i++) |
| 312 | putInt64(*values++); |
| 313 | } |
| 314 | |
| 315 | void MtpDataPacket::putAUInt64(const uint64_t* values, int count) { |
| 316 | putUInt32(count); |
| 317 | for (int i = 0; i < count; i++) |
| 318 | putUInt64(*values++); |
| 319 | } |
| 320 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 321 | void MtpDataPacket::putString(const MtpStringBuffer& string) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 322 | string.writeToPacket(this); |
| 323 | } |
| 324 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 325 | void MtpDataPacket::putString(const char* s) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 326 | MtpStringBuffer string(s); |
| 327 | string.writeToPacket(this); |
| 328 | } |
| 329 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 330 | void 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 | } |
Mike Lockwood | 9e83786 | 2010-08-18 12:31:09 -0400 | [diff] [blame] | 338 | putUInt8(count > 0 ? count + 1 : 0); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 339 | for (int i = 0; i < count; i++) |
| 340 | putUInt16(string[i]); |
Mike Lockwood | 9e83786 | 2010-08-18 12:31:09 -0400 | [diff] [blame] | 341 | // only terminate with zero if string is not empty |
| 342 | if (count > 0) |
| 343 | putUInt16(0); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 346 | #ifdef MTP_DEVICE |
| 347 | int MtpDataPacket::read(int fd) { |
| 348 | // first read the header |
| 349 | int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 350 | if (ret != MTP_CONTAINER_HEADER_SIZE) |
| 351 | return -1; |
| 352 | // then the following data |
| 353 | int total = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); |
Mike Lockwood | 9dfc7de | 2010-11-30 12:16:58 -0500 | [diff] [blame] | 354 | allocate(total); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 355 | int remaining = total - MTP_CONTAINER_HEADER_SIZE; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 356 | ret = ::read(fd, &mBuffer[0] + MTP_CONTAINER_HEADER_SIZE, remaining); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 357 | if (ret != remaining) |
| 358 | return -1; |
| 359 | |
| 360 | mPacketSize = total; |
| 361 | mOffset = MTP_CONTAINER_HEADER_SIZE; |
| 362 | return total; |
| 363 | } |
| 364 | |
| 365 | int 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 | |
| 374 | int MtpDataPacket::write(int fd) { |
| 375 | MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 376 | MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 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 | |
| 385 | int 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 |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 394 | int MtpDataPacket::read(struct usb_request *request) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 395 | // first read the header |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 396 | request->buffer = mBuffer; |
| 397 | request->buffer_length = mBufferSize; |
| 398 | int length = transfer(request); |
Mike Lockwood | 3c51d6a | 2010-07-20 12:01:36 -0400 | [diff] [blame] | 399 | if (length >= MTP_CONTAINER_HEADER_SIZE) { |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 400 | // look at the length field to see if the data spans multiple packets |
| 401 | uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); |
| 402 | while (totalLength > length) { |
| 403 | allocate(length + mAllocationIncrement); |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 404 | request->buffer = mBuffer + length; |
| 405 | request->buffer_length = mAllocationIncrement; |
| 406 | int ret = transfer(request); |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 407 | if (ret >= 0) |
| 408 | length += ret; |
| 409 | else { |
| 410 | length = ret; |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | if (length >= 0) |
| 416 | mPacketSize = length; |
| 417 | return length; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 420 | int MtpDataPacket::readData(struct usb_request *request, void* buffer, int length) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 421 | int read = 0; |
| 422 | while (read < length) { |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 423 | request->buffer = (char *)buffer + read; |
| 424 | request->buffer_length = length - read; |
| 425 | int ret = transfer(request); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 426 | if (ret < 0) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 427 | return ret; |
| 428 | } |
| 429 | read += ret; |
| 430 | } |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 431 | return read; |
| 432 | } |
| 433 | |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 434 | // Queue a read request. Call readDataWait to wait for result |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 435 | int MtpDataPacket::readDataAsync(struct usb_request *req) { |
| 436 | if (usb_request_queue(req)) { |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 437 | LOGE("usb_endpoint_queue failed, errno: %d", errno); |
| 438 | return -1; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | // Wait for result of readDataAsync |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 444 | int MtpDataPacket::readDataWait(struct usb_device *device) { |
| 445 | struct usb_request *req = usb_request_wait(device); |
| 446 | return (req ? req->actual_length : -1); |
Mike Lockwood | 954c267 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 449 | int MtpDataPacket::readDataHeader(struct usb_request *request) { |
| 450 | request->buffer = mBuffer; |
| 451 | request->buffer_length = request->max_packet_size; |
| 452 | int length = transfer(request); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 453 | if (length >= 0) |
| 454 | mPacketSize = length; |
| 455 | return length; |
| 456 | } |
| 457 | |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 458 | int MtpDataPacket::writeDataHeader(struct usb_request *request, uint32_t length) { |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 459 | MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length); |
| 460 | MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 461 | request->buffer = mBuffer; |
| 462 | request->buffer_length = MTP_CONTAINER_HEADER_SIZE; |
| 463 | int ret = transfer(request); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 464 | return (ret < 0 ? ret : 0); |
| 465 | } |
| 466 | |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 467 | int MtpDataPacket::write(struct usb_request *request) { |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 468 | MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 469 | MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); |
| 470 | |
| 471 | // send header separately from data |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 472 | request->buffer = mBuffer; |
| 473 | request->buffer_length = MTP_CONTAINER_HEADER_SIZE; |
| 474 | int ret = transfer(request); |
| 475 | if (ret == MTP_CONTAINER_HEADER_SIZE) { |
| 476 | request->buffer = mBuffer + MTP_CONTAINER_HEADER_SIZE; |
| 477 | request->buffer_length = mPacketSize - MTP_CONTAINER_HEADER_SIZE; |
| 478 | ret = transfer(request); |
| 479 | } |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 480 | return (ret < 0 ? ret : 0); |
| 481 | } |
| 482 | |
Mike Lockwood | 215b682 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 483 | int MtpDataPacket::write(struct usb_request *request, void* buffer, uint32_t length) { |
| 484 | request->buffer = buffer; |
| 485 | request->buffer_length = length; |
| 486 | int ret = transfer(request); |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 487 | return (ret < 0 ? ret : 0); |
| 488 | } |
| 489 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 490 | #endif // MTP_HOST |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 491 | |
Mike Lockwood | dda5686 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 492 | void* MtpDataPacket::getData(int& outLength) const { |
| 493 | int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE; |
| 494 | if (length > 0) { |
| 495 | void* result = malloc(length); |
| 496 | if (result) { |
| 497 | memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length); |
| 498 | outLength = length; |
| 499 | return result; |
| 500 | } |
| 501 | } |
| 502 | outLength = 0; |
| 503 | return NULL; |
| 504 | } |
| 505 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 506 | } // namespace android |