blob: de3c1997f29c3f52918396b2b2663e119f26ab20 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -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
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 Lockwood335dd2b2010-05-19 10:33:39 -040029#include "MtpDeviceInfo.h"
Mike Lockwoode13401b2010-05-19 15:12:14 -040030#include "MtpObjectInfo.h"
Mike Lockwood335dd2b2010-05-19 10:33:39 -040031#include "MtpStorageInfo.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040032#include "MtpStringBuffer.h"
33
Mike Lockwood7850ef92010-05-14 10:10:36 -040034namespace android {
35
Mike Lockwood16864ba2010-05-11 17:16:59 -040036MtpClient::MtpClient(struct usb_endpoint *ep_in, struct usb_endpoint *ep_out,
37 struct usb_endpoint *ep_intr)
38 : mEndpointIn(ep_in),
39 mEndpointOut(ep_out),
40 mEndpointIntr(ep_intr),
41 mSessionID(0),
42 mTransactionID(0)
43{
44
45}
46
47MtpClient::~MtpClient() {
48}
49
Mike Lockwood16864ba2010-05-11 17:16:59 -040050bool MtpClient::openSession() {
51printf("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 Lockwood335dd2b2010-05-19 10:33:39 -040070bool MtpClient::closeSession() {
71 // FIXME
72 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -040073}
74
Mike Lockwood335dd2b2010-05-19 10:33:39 -040075MtpDeviceInfo* 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();
82printf("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
91MtpStorageIDList* 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
104MtpStorageInfo* 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();
112printf("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 Lockwood16864ba2010-05-11 17:16:59 -0400119}
120
Mike Lockwoode13401b2010-05-19 15:12:14 -0400121MtpObjectHandleList* MtpClient::getObjectHandles(MtpStorageID storageID,
122 MtpObjectFormat format, MtpObjectHandle parent) {
123 mRequest.reset();
124 mRequest.setParameter(1, storageID);
125 mRequest.setParameter(2, format);
126 mRequest.setParameter(3, parent);
127 if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
128 return NULL;
129 if (!readData())
130 return NULL;
131 MtpResponseCode ret = readResponse();
132printf("getObjectHandles returned %04X\n", ret);
133 if (ret == MTP_RESPONSE_OK) {
134 return mData.getAUInt32();
135 }
136 return NULL;
137}
138
139MtpObjectInfo* MtpClient::getObjectInfo(MtpObjectHandle handle) {
140 mRequest.reset();
141 mRequest.setParameter(1, handle);
142 if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
143 return NULL;
144 if (!readData())
145 return NULL;
146 MtpResponseCode ret = readResponse();
147printf("getObjectInfo returned %04X\n", ret);
148 if (ret == MTP_RESPONSE_OK) {
149 MtpObjectInfo* info = new MtpObjectInfo(handle);
150 info->read(mData);
151 return info;
152 }
153 return NULL;
154}
155
Mike Lockwood16864ba2010-05-11 17:16:59 -0400156bool MtpClient::sendRequest(MtpOperationCode operation) {
157 printf("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
158 mRequest.setOperationCode(operation);
159 if (mTransactionID > 0)
160 mRequest.setTransactionID(mTransactionID++);
161 int ret = mRequest.write(mEndpointOut);
162 mRequest.dump();
163 return (ret > 0);
164}
165
166bool MtpClient::sendData(MtpOperationCode operation) {
167 printf("sendData\n");
168 mData.setOperationCode(mRequest.getOperationCode());
169 mData.setTransactionID(mRequest.getTransactionID());
170 int ret = mData.write(mEndpointOut);
171 mData.dump();
172 return (ret > 0);
173}
174
175bool MtpClient::readData() {
Mike Lockwood335dd2b2010-05-19 10:33:39 -0400176 mData.reset();
177 int ret = mData.read(mEndpointIn);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400178 printf("readData returned %d\n", ret);
179 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
180 mData.dump();
181 return true;
182 }
183 else {
184 printf("readResponse failed\n");
185 return false;
186 }
187}
188
189MtpResponseCode MtpClient::readResponse() {
190 printf("readResponse\n");
191 int ret = mResponse.read(mEndpointIn);
192 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
193 mResponse.dump();
194 return mResponse.getResponseCode();
195 }
196 else {
197 printf("readResponse failed\n");
198 return -1;
199 }
200}
201
Mike Lockwood7850ef92010-05-14 10:10:36 -0400202} // namespace android