blob: d05d9285c7ccc3ea06cfdedb1b1e890857a5e095 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "transport.h"
21
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include "adb.h"
27
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028static int remote_read(apacket *p, atransport *t)
29{
30 if(usb_read(t->usb, &p->msg, sizeof(amessage))){
Yabin Cui7a3f8d62015-09-02 17:44:28 -070031 D("remote usb: read terminated (message)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032 return -1;
33 }
34
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010035 if(check_header(p, t)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070036 D("remote usb: check_header failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037 return -1;
38 }
39
40 if(p->msg.data_length) {
41 if(usb_read(t->usb, p->data, p->msg.data_length)){
Yabin Cui7a3f8d62015-09-02 17:44:28 -070042 D("remote usb: terminated (data)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043 return -1;
44 }
45 }
46
47 if(check_data(p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070048 D("remote usb: check_data failed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 return -1;
50 }
51
52 return 0;
53}
54
55static int remote_write(apacket *p, atransport *t)
56{
57 unsigned size = p->msg.data_length;
58
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070060 D("remote usb: 1 - write terminated");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 return -1;
62 }
63 if(p->msg.data_length == 0) return 0;
64 if(usb_write(t->usb, &p->data, size)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070065 D("remote usb: 2 - write terminated");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 return -1;
67 }
68
69 return 0;
70}
71
72static void remote_close(atransport *t)
73{
74 usb_close(t->usb);
75 t->usb = 0;
76}
77
78static void remote_kick(atransport *t)
79{
80 usb_kick(t->usb);
81}
82
Dan Albertdcd78a12015-05-18 16:43:57 -070083void init_usb_transport(atransport *t, usb_handle *h, ConnectionState state)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084{
Yabin Cui7a3f8d62015-09-02 17:44:28 -070085 D("transport: usb");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 t->close = remote_close;
Yabin Cui7f274902016-04-18 11:22:34 -070087 t->SetKickFunction(remote_kick);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088 t->read_from_remote = remote_read;
89 t->write_to_remote = remote_write;
90 t->sync_token = 1;
Mike Lockwood0927bf92009-08-08 12:37:44 -040091 t->connection_state = state;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 t->type = kTransportUsb;
93 t->usb = h;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094}
95
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070096#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
98{
Elliott Hughes55664902014-11-19 22:07:34 -080099 return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100}
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700101#endif