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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
| 24 | |
| 25 | #include <usbhost/usbhost.h> |
| 26 | |
| 27 | #include "MtpClient.h" |
| 28 | #include "MtpDebug.h" |
| 29 | #include "MtpStringBuffer.h" |
| 30 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame^] | 31 | namespace android { |
| 32 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 33 | MtpClient::MtpClient(struct usb_endpoint *ep_in, struct usb_endpoint *ep_out, |
| 34 | struct usb_endpoint *ep_intr) |
| 35 | : mEndpointIn(ep_in), |
| 36 | mEndpointOut(ep_out), |
| 37 | mEndpointIntr(ep_intr), |
| 38 | mSessionID(0), |
| 39 | mTransactionID(0) |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | MtpClient::~MtpClient() { |
| 45 | } |
| 46 | |
| 47 | |
| 48 | bool MtpClient::openSession() { |
| 49 | printf("openSession\n"); |
| 50 | mSessionID = 0; |
| 51 | mTransactionID = 0; |
| 52 | MtpSessionID newSession = 1; |
| 53 | mRequest.reset(); |
| 54 | mRequest.setParameter(1, newSession); |
| 55 | if (!sendRequest(MTP_OPERATION_OPEN_SESSION)) |
| 56 | return false; |
| 57 | MtpResponseCode ret = readResponse(); |
| 58 | if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN) |
| 59 | newSession = mResponse.getParameter(1); |
| 60 | else if (ret != MTP_RESPONSE_OK) |
| 61 | return false; |
| 62 | |
| 63 | mSessionID = newSession; |
| 64 | mTransactionID = 1; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool MtpClient::getDeviceInfo() { |
| 69 | mRequest.reset(); |
| 70 | if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO)) |
| 71 | return false; |
| 72 | if (!readData()) |
| 73 | return false; |
| 74 | MtpResponseCode ret = readResponse(); |
| 75 | if (ret == MTP_RESPONSE_OK) { |
| 76 | MtpStringBuffer string; |
| 77 | |
| 78 | // fill in device info |
| 79 | printf("MTP standard version: %d\n", mData.getUInt16()); |
| 80 | printf("MTP Vendor Extension ID: %d\n", mData.getUInt32()); |
| 81 | printf("MTP vendor extension version: %d\n", mData.getUInt16()); |
| 82 | mData.getString(string); |
| 83 | printf("vendor extension desc %s\n", (const char *)string); |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | bool MtpClient::closeSession() { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool MtpClient::sendRequest(MtpOperationCode operation) { |
| 95 | printf("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation)); |
| 96 | mRequest.setOperationCode(operation); |
| 97 | if (mTransactionID > 0) |
| 98 | mRequest.setTransactionID(mTransactionID++); |
| 99 | int ret = mRequest.write(mEndpointOut); |
| 100 | mRequest.dump(); |
| 101 | return (ret > 0); |
| 102 | } |
| 103 | |
| 104 | bool MtpClient::sendData(MtpOperationCode operation) { |
| 105 | printf("sendData\n"); |
| 106 | mData.setOperationCode(mRequest.getOperationCode()); |
| 107 | mData.setTransactionID(mRequest.getTransactionID()); |
| 108 | int ret = mData.write(mEndpointOut); |
| 109 | mData.dump(); |
| 110 | return (ret > 0); |
| 111 | } |
| 112 | |
| 113 | bool MtpClient::readData() { |
| 114 | int ret = mData.read(mEndpointIn); |
| 115 | printf("readData returned %d\n", ret); |
| 116 | if (ret >= MTP_CONTAINER_HEADER_SIZE) { |
| 117 | mData.dump(); |
| 118 | return true; |
| 119 | } |
| 120 | else { |
| 121 | printf("readResponse failed\n"); |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | MtpResponseCode MtpClient::readResponse() { |
| 127 | printf("readResponse\n"); |
| 128 | int ret = mResponse.read(mEndpointIn); |
| 129 | if (ret >= MTP_CONTAINER_HEADER_SIZE) { |
| 130 | mResponse.dump(); |
| 131 | return mResponse.getResponseCode(); |
| 132 | } |
| 133 | else { |
| 134 | printf("readResponse failed\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | } |
| 138 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame^] | 139 | } // namespace android |