Mike Lockwood | 16864ba | 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 | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame^] | 17 | #define LOG_TAG "MtpDataPacket" |
| 18 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
| 22 | |
| 23 | #include "MtpDataPacket.h" |
| 24 | #include "MtpStringBuffer.h" |
| 25 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 28 | MtpDataPacket::MtpDataPacket() |
| 29 | : MtpPacket(512), |
| 30 | mOffset(MTP_CONTAINER_HEADER_SIZE) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | MtpDataPacket::~MtpDataPacket() { |
| 35 | } |
| 36 | |
| 37 | void MtpDataPacket::reset() { |
| 38 | MtpPacket::reset(); |
| 39 | mOffset = MTP_CONTAINER_HEADER_SIZE; |
| 40 | } |
| 41 | |
| 42 | void MtpDataPacket::setOperationCode(MtpOperationCode code) { |
| 43 | MtpPacket::putUInt16(MTP_CONTAINER_CODE_OFFSET, code); |
| 44 | } |
| 45 | |
| 46 | void MtpDataPacket::setTransactionID(MtpTransactionID id) { |
| 47 | MtpPacket::putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id); |
| 48 | } |
| 49 | |
| 50 | uint16_t MtpDataPacket::getUInt16() { |
| 51 | int offset = mOffset; |
| 52 | uint16_t result = (uint16_t)mBuffer[offset] | ((uint16_t)mBuffer[offset + 1] << 8); |
| 53 | mOffset += 2; |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | uint32_t MtpDataPacket::getUInt32() { |
| 58 | int offset = mOffset; |
| 59 | uint32_t result = (uint32_t)mBuffer[offset] | ((uint32_t)mBuffer[offset + 1] << 8) | |
| 60 | ((uint32_t)mBuffer[offset + 2] << 16) | ((uint32_t)mBuffer[offset + 3] << 24); |
| 61 | mOffset += 4; |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | uint64_t MtpDataPacket::getUInt64() { |
| 66 | int offset = mOffset; |
| 67 | uint64_t result = (uint64_t)mBuffer[offset] | ((uint64_t)mBuffer[offset + 1] << 8) | |
| 68 | ((uint64_t)mBuffer[offset + 2] << 16) | ((uint64_t)mBuffer[offset + 3] << 24) | |
| 69 | ((uint64_t)mBuffer[offset + 4] << 32) | ((uint64_t)mBuffer[offset + 5] << 40) | |
| 70 | ((uint64_t)mBuffer[offset + 6] << 48) | ((uint64_t)mBuffer[offset + 7] << 56); |
| 71 | mOffset += 8; |
| 72 | return result; |
| 73 | } |
| 74 | |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 75 | void MtpDataPacket::getUInt128(uint128_t& value) { |
| 76 | value[0] = getUInt32(); |
| 77 | value[1] = getUInt32(); |
| 78 | value[2] = getUInt32(); |
| 79 | value[3] = getUInt32(); |
| 80 | } |
| 81 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 82 | void MtpDataPacket::getString(MtpStringBuffer& string) |
| 83 | { |
| 84 | string.readFromPacket(this); |
| 85 | } |
| 86 | |
Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame] | 87 | Int8List* MtpDataPacket::getAInt8() { |
| 88 | Int8List* result = new Int8List; |
| 89 | int count = getUInt32(); |
| 90 | for (int i = 0; i < count; i++) |
| 91 | result->push(getInt8()); |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | UInt8List* MtpDataPacket::getAUInt8() { |
| 96 | UInt8List* result = new UInt8List; |
| 97 | int count = getUInt32(); |
| 98 | for (int i = 0; i < count; i++) |
| 99 | result->push(getUInt8()); |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | Int16List* MtpDataPacket::getAInt16() { |
| 104 | Int16List* result = new Int16List; |
| 105 | int count = getUInt32(); |
| 106 | for (int i = 0; i < count; i++) |
| 107 | result->push(getInt16()); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | UInt16List* MtpDataPacket::getAUInt16() { |
| 112 | UInt16List* result = new UInt16List; |
| 113 | int count = getUInt32(); |
| 114 | for (int i = 0; i < count; i++) |
| 115 | result->push(getUInt16()); |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | Int32List* MtpDataPacket::getAInt32() { |
| 120 | Int32List* result = new Int32List; |
| 121 | int count = getUInt32(); |
| 122 | for (int i = 0; i < count; i++) |
| 123 | result->push(getInt32()); |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | UInt32List* MtpDataPacket::getAUInt32() { |
| 128 | UInt32List* result = new UInt32List; |
| 129 | int count = getUInt32(); |
| 130 | for (int i = 0; i < count; i++) |
| 131 | result->push(getUInt32()); |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | Int64List* MtpDataPacket::getAInt64() { |
| 136 | Int64List* result = new Int64List; |
| 137 | int count = getUInt32(); |
| 138 | for (int i = 0; i < count; i++) |
| 139 | result->push(getInt64()); |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | UInt64List* MtpDataPacket::getAUInt64() { |
| 144 | UInt64List* result = new UInt64List; |
| 145 | int count = getUInt32(); |
| 146 | for (int i = 0; i < count; i++) |
| 147 | result->push(getUInt64()); |
| 148 | return result; |
| 149 | } |
| 150 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 151 | void MtpDataPacket::putInt8(int8_t value) { |
| 152 | allocate(mOffset + 1); |
| 153 | mBuffer[mOffset++] = (uint8_t)value; |
| 154 | if (mPacketSize < mOffset) |
| 155 | mPacketSize = mOffset; |
| 156 | } |
| 157 | |
| 158 | void MtpDataPacket::putUInt8(uint8_t value) { |
| 159 | allocate(mOffset + 1); |
| 160 | mBuffer[mOffset++] = (uint8_t)value; |
| 161 | if (mPacketSize < mOffset) |
| 162 | mPacketSize = mOffset; |
| 163 | } |
| 164 | |
| 165 | void MtpDataPacket::putInt16(int16_t value) { |
| 166 | allocate(mOffset + 2); |
| 167 | mBuffer[mOffset++] = (uint8_t)(value & 0xFF); |
| 168 | mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); |
| 169 | if (mPacketSize < mOffset) |
| 170 | mPacketSize = mOffset; |
| 171 | } |
| 172 | |
| 173 | void MtpDataPacket::putUInt16(uint16_t value) { |
| 174 | allocate(mOffset + 2); |
| 175 | mBuffer[mOffset++] = (uint8_t)(value & 0xFF); |
| 176 | mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); |
| 177 | if (mPacketSize < mOffset) |
| 178 | mPacketSize = mOffset; |
| 179 | } |
| 180 | |
| 181 | void MtpDataPacket::putInt32(int32_t value) { |
| 182 | allocate(mOffset + 4); |
| 183 | mBuffer[mOffset++] = (uint8_t)(value & 0xFF); |
| 184 | mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); |
| 185 | mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); |
| 186 | mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); |
| 187 | if (mPacketSize < mOffset) |
| 188 | mPacketSize = mOffset; |
| 189 | } |
| 190 | |
| 191 | void MtpDataPacket::putUInt32(uint32_t value) { |
| 192 | allocate(mOffset + 4); |
| 193 | mBuffer[mOffset++] = (uint8_t)(value & 0xFF); |
| 194 | mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); |
| 195 | mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); |
| 196 | mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); |
| 197 | if (mPacketSize < mOffset) |
| 198 | mPacketSize = mOffset; |
| 199 | } |
| 200 | |
| 201 | void MtpDataPacket::putInt64(int64_t value) { |
| 202 | allocate(mOffset + 8); |
| 203 | mBuffer[mOffset++] = (uint8_t)(value & 0xFF); |
| 204 | mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); |
| 205 | mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); |
| 206 | mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); |
| 207 | mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF); |
| 208 | mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF); |
| 209 | mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF); |
| 210 | mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF); |
| 211 | if (mPacketSize < mOffset) |
| 212 | mPacketSize = mOffset; |
| 213 | } |
| 214 | |
| 215 | void MtpDataPacket::putUInt64(uint64_t value) { |
| 216 | allocate(mOffset + 8); |
| 217 | mBuffer[mOffset++] = (uint8_t)(value & 0xFF); |
| 218 | mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF); |
| 219 | mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF); |
| 220 | mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF); |
| 221 | mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF); |
| 222 | mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF); |
| 223 | mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF); |
| 224 | mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF); |
| 225 | if (mPacketSize < mOffset) |
| 226 | mPacketSize = mOffset; |
| 227 | } |
| 228 | |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 229 | void MtpDataPacket::putInt128(const int128_t& value) { |
| 230 | putInt32(value[0]); |
| 231 | putInt32(value[1]); |
| 232 | putInt32(value[2]); |
| 233 | putInt32(value[3]); |
| 234 | } |
| 235 | |
| 236 | void MtpDataPacket::putUInt128(const uint128_t& value) { |
| 237 | putUInt32(value[0]); |
| 238 | putUInt32(value[1]); |
| 239 | putUInt32(value[2]); |
| 240 | putUInt32(value[3]); |
| 241 | } |
| 242 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 243 | void MtpDataPacket::putAInt8(const int8_t* values, int count) { |
| 244 | putUInt32(count); |
| 245 | for (int i = 0; i < count; i++) |
| 246 | putInt8(*values++); |
| 247 | } |
| 248 | |
| 249 | void MtpDataPacket::putAUInt8(const uint8_t* values, int count) { |
| 250 | putUInt32(count); |
| 251 | for (int i = 0; i < count; i++) |
| 252 | putUInt8(*values++); |
| 253 | } |
| 254 | |
| 255 | void MtpDataPacket::putAInt16(const int16_t* values, int count) { |
| 256 | putUInt32(count); |
| 257 | for (int i = 0; i < count; i++) |
| 258 | putInt16(*values++); |
| 259 | } |
| 260 | |
| 261 | void MtpDataPacket::putAUInt16(const uint16_t* values, int count) { |
| 262 | putUInt32(count); |
| 263 | for (int i = 0; i < count; i++) |
| 264 | putUInt16(*values++); |
| 265 | } |
| 266 | |
| 267 | void MtpDataPacket::putAInt32(const int32_t* values, int count) { |
| 268 | putUInt32(count); |
| 269 | for (int i = 0; i < count; i++) |
| 270 | putInt32(*values++); |
| 271 | } |
| 272 | |
| 273 | void MtpDataPacket::putAUInt32(const uint32_t* values, int count) { |
| 274 | putUInt32(count); |
| 275 | for (int i = 0; i < count; i++) |
| 276 | putUInt32(*values++); |
| 277 | } |
| 278 | |
| 279 | void MtpDataPacket::putAUInt32(const UInt32List* list) { |
| 280 | if (!list) { |
| 281 | putEmptyArray(); |
| 282 | } else { |
| 283 | size_t size = list->size(); |
| 284 | putUInt32(size); |
| 285 | for (size_t i = 0; i < size; i++) |
| 286 | putUInt32((*list)[i]); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void MtpDataPacket::putAInt64(const int64_t* values, int count) { |
| 291 | putUInt32(count); |
| 292 | for (int i = 0; i < count; i++) |
| 293 | putInt64(*values++); |
| 294 | } |
| 295 | |
| 296 | void MtpDataPacket::putAUInt64(const uint64_t* values, int count) { |
| 297 | putUInt32(count); |
| 298 | for (int i = 0; i < count; i++) |
| 299 | putUInt64(*values++); |
| 300 | } |
| 301 | |
| 302 | void MtpDataPacket::putString(const MtpStringBuffer& string) |
| 303 | { |
| 304 | string.writeToPacket(this); |
| 305 | } |
| 306 | |
| 307 | void MtpDataPacket::putString(const char* s) |
| 308 | { |
| 309 | MtpStringBuffer string(s); |
| 310 | string.writeToPacket(this); |
| 311 | } |
| 312 | |
| 313 | #ifdef MTP_DEVICE |
| 314 | int MtpDataPacket::read(int fd) { |
| 315 | // first read the header |
| 316 | int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 317 | if (ret != MTP_CONTAINER_HEADER_SIZE) |
| 318 | return -1; |
| 319 | // then the following data |
| 320 | int total = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); |
| 321 | int remaining = total - MTP_CONTAINER_HEADER_SIZE; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 322 | ret = ::read(fd, &mBuffer[0] + MTP_CONTAINER_HEADER_SIZE, remaining); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 323 | if (ret != remaining) |
| 324 | return -1; |
| 325 | |
| 326 | mPacketSize = total; |
| 327 | mOffset = MTP_CONTAINER_HEADER_SIZE; |
| 328 | return total; |
| 329 | } |
| 330 | |
| 331 | int MtpDataPacket::readDataHeader(int fd) { |
| 332 | int ret = ::read(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE); |
| 333 | if (ret > 0) |
| 334 | mPacketSize = ret; |
| 335 | else |
| 336 | mPacketSize = 0; |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | int MtpDataPacket::write(int fd) { |
| 341 | MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 342 | MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); |
| 343 | |
| 344 | // send header separately from data |
| 345 | int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE); |
| 346 | if (ret == MTP_CONTAINER_HEADER_SIZE) |
| 347 | ret = ::write(fd, mBuffer + MTP_CONTAINER_HEADER_SIZE, |
| 348 | mPacketSize - MTP_CONTAINER_HEADER_SIZE); |
| 349 | return (ret < 0 ? ret : 0); |
| 350 | } |
| 351 | |
| 352 | int MtpDataPacket::writeDataHeader(int fd, uint32_t length) { |
| 353 | MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length); |
| 354 | MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); |
| 355 | int ret = ::write(fd, mBuffer, MTP_CONTAINER_HEADER_SIZE); |
| 356 | return (ret < 0 ? ret : 0); |
| 357 | } |
| 358 | #endif // MTP_DEVICE |
| 359 | |
| 360 | #ifdef MTP_HOST |
| 361 | int MtpDataPacket::read(struct usb_endpoint *ep) { |
| 362 | // first read the header |
Mike Lockwood | 3e072b3 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 363 | int length = transfer(ep, mBuffer, mBufferSize); |
| 364 | if (length > MTP_CONTAINER_HEADER_SIZE) { |
| 365 | // look at the length field to see if the data spans multiple packets |
| 366 | uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); |
| 367 | while (totalLength > length) { |
| 368 | allocate(length + mAllocationIncrement); |
| 369 | int ret = transfer(ep, mBuffer + length, mAllocationIncrement); |
| 370 | if (ret >= 0) |
| 371 | length += ret; |
| 372 | else { |
| 373 | length = ret; |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | if (length >= 0) |
| 379 | mPacketSize = length; |
| 380 | return length; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | int MtpDataPacket::write(struct usb_endpoint *ep) { |
| 384 | MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 385 | MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA); |
| 386 | |
| 387 | // send header separately from data |
| 388 | int ret = transfer(ep, mBuffer, MTP_CONTAINER_HEADER_SIZE); |
| 389 | if (ret == MTP_CONTAINER_HEADER_SIZE) |
| 390 | ret = transfer(ep, mBuffer + MTP_CONTAINER_HEADER_SIZE, |
| 391 | mPacketSize - MTP_CONTAINER_HEADER_SIZE); |
| 392 | return (ret < 0 ? ret : 0); |
| 393 | } |
| 394 | |
| 395 | #endif // MTP_HOST |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 396 | |
Mike Lockwood | 3e072b3 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 397 | void* MtpDataPacket::getData(int& outLength) const { |
| 398 | int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE; |
| 399 | if (length > 0) { |
| 400 | void* result = malloc(length); |
| 401 | if (result) { |
| 402 | memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length); |
| 403 | outLength = length; |
| 404 | return result; |
| 405 | } |
| 406 | } |
| 407 | outLength = 0; |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 411 | } // namespace android |