| 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" | 
| Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame^] | 29 | #include "MtpDeviceInfo.h" | 
|  | 30 | #include "MtpStorageInfo.h" | 
| Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 31 | #include "MtpStringBuffer.h" | 
|  | 32 |  | 
| Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 33 | namespace android { | 
|  | 34 |  | 
| Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 35 | MtpClient::MtpClient(struct usb_endpoint *ep_in, struct usb_endpoint *ep_out, | 
|  | 36 | struct usb_endpoint *ep_intr) | 
|  | 37 | :   mEndpointIn(ep_in), | 
|  | 38 | mEndpointOut(ep_out), | 
|  | 39 | mEndpointIntr(ep_intr), | 
|  | 40 | mSessionID(0), | 
|  | 41 | mTransactionID(0) | 
|  | 42 | { | 
|  | 43 |  | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | MtpClient::~MtpClient() { | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 |  | 
|  | 50 | bool MtpClient::openSession() { | 
|  | 51 | printf("openSession\n"); | 
|  | 52 | mSessionID = 0; | 
|  | 53 | mTransactionID = 0; | 
|  | 54 | MtpSessionID newSession = 1; | 
|  | 55 | mRequest.reset(); | 
|  | 56 | mRequest.setParameter(1, newSession); | 
|  | 57 | if (!sendRequest(MTP_OPERATION_OPEN_SESSION)) | 
|  | 58 | return false; | 
|  | 59 | MtpResponseCode ret = readResponse(); | 
|  | 60 | if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN) | 
|  | 61 | newSession = mResponse.getParameter(1); | 
|  | 62 | else if (ret != MTP_RESPONSE_OK) | 
|  | 63 | return false; | 
|  | 64 |  | 
|  | 65 | mSessionID = newSession; | 
|  | 66 | mTransactionID = 1; | 
|  | 67 | return true; | 
|  | 68 | } | 
|  | 69 |  | 
| Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame^] | 70 | bool MtpClient::closeSession() { | 
|  | 71 | // FIXME | 
|  | 72 | return true; | 
| Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame^] | 75 | MtpDeviceInfo* MtpClient::getDeviceInfo() { | 
|  | 76 | mRequest.reset(); | 
|  | 77 | if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO)) | 
|  | 78 | return NULL; | 
|  | 79 | if (!readData()) | 
|  | 80 | return NULL; | 
|  | 81 | MtpResponseCode ret = readResponse(); | 
|  | 82 | printf("getDeviceInfo returned %04X\n", ret); | 
|  | 83 | if (ret == MTP_RESPONSE_OK) { | 
|  | 84 | MtpDeviceInfo* info = new MtpDeviceInfo; | 
|  | 85 | info->read(mData); | 
|  | 86 | return info; | 
|  | 87 | } | 
|  | 88 | return NULL; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | MtpStorageIDList* MtpClient::getStorageIDs() { | 
|  | 92 | mRequest.reset(); | 
|  | 93 | if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS)) | 
|  | 94 | return NULL; | 
|  | 95 | if (!readData()) | 
|  | 96 | return NULL; | 
|  | 97 | MtpResponseCode ret = readResponse(); | 
|  | 98 | if (ret == MTP_RESPONSE_OK) { | 
|  | 99 | return mData.getAUInt32(); | 
|  | 100 | } | 
|  | 101 | return NULL; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | MtpStorageInfo* MtpClient::getStorageInfo(MtpStorageID storageID) { | 
|  | 105 | mRequest.reset(); | 
|  | 106 | mRequest.setParameter(1, storageID); | 
|  | 107 | if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO)) | 
|  | 108 | return NULL; | 
|  | 109 | if (!readData()) | 
|  | 110 | return NULL; | 
|  | 111 | MtpResponseCode ret = readResponse(); | 
|  | 112 | printf("getStorageInfo returned %04X\n", ret); | 
|  | 113 | if (ret == MTP_RESPONSE_OK) { | 
|  | 114 | MtpStorageInfo* info = new MtpStorageInfo(storageID); | 
|  | 115 | info->read(mData); | 
|  | 116 | return info; | 
|  | 117 | } | 
|  | 118 | return NULL; | 
| Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
|  | 121 | bool MtpClient::sendRequest(MtpOperationCode operation) { | 
|  | 122 | printf("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation)); | 
|  | 123 | mRequest.setOperationCode(operation); | 
|  | 124 | if (mTransactionID > 0) | 
|  | 125 | mRequest.setTransactionID(mTransactionID++); | 
|  | 126 | int ret = mRequest.write(mEndpointOut); | 
|  | 127 | mRequest.dump(); | 
|  | 128 | return (ret > 0); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | bool MtpClient::sendData(MtpOperationCode operation) { | 
|  | 132 | printf("sendData\n"); | 
|  | 133 | mData.setOperationCode(mRequest.getOperationCode()); | 
|  | 134 | mData.setTransactionID(mRequest.getTransactionID()); | 
|  | 135 | int ret = mData.write(mEndpointOut); | 
|  | 136 | mData.dump(); | 
|  | 137 | return (ret > 0); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | bool MtpClient::readData() { | 
| Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame^] | 141 | mData.reset(); | 
|  | 142 | int ret = mData.read(mEndpointIn); | 
| Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 143 | printf("readData returned %d\n", ret); | 
|  | 144 | if (ret >= MTP_CONTAINER_HEADER_SIZE) { | 
|  | 145 | mData.dump(); | 
|  | 146 | return true; | 
|  | 147 | } | 
|  | 148 | else { | 
|  | 149 | printf("readResponse failed\n"); | 
|  | 150 | return false; | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | MtpResponseCode MtpClient::readResponse() { | 
|  | 155 | printf("readResponse\n"); | 
|  | 156 | int ret = mResponse.read(mEndpointIn); | 
|  | 157 | if (ret >= MTP_CONTAINER_HEADER_SIZE) { | 
|  | 158 | mResponse.dump(); | 
|  | 159 | return mResponse.getResponseCode(); | 
|  | 160 | } | 
|  | 161 | else { | 
|  | 162 | printf("readResponse failed\n"); | 
|  | 163 | return -1; | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 |  | 
| Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 167 | }  // namespace android |