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