The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_TRANSPORT |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "transport.h" |
| 21 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | static int remote_read(apacket *p, atransport *t) |
| 29 | { |
| 30 | if(usb_read(t->usb, &p->msg, sizeof(amessage))){ |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 31 | D("remote usb: read terminated (message)"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | return -1; |
| 33 | } |
| 34 | |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 35 | if(check_header(p, t)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 36 | D("remote usb: check_header failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | if(p->msg.data_length) { |
| 41 | if(usb_read(t->usb, p->data, p->msg.data_length)){ |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 42 | D("remote usb: terminated (data)"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if(check_data(p)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 48 | D("remote usb: check_data failed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int remote_write(apacket *p, atransport *t) |
| 56 | { |
| 57 | unsigned size = p->msg.data_length; |
| 58 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | if(usb_write(t->usb, &p->msg, sizeof(amessage))) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 60 | D("remote usb: 1 - write terminated"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | if(p->msg.data_length == 0) return 0; |
| 64 | if(usb_write(t->usb, &p->data, size)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 65 | D("remote usb: 2 - write terminated"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static void remote_close(atransport *t) |
| 73 | { |
| 74 | usb_close(t->usb); |
| 75 | t->usb = 0; |
| 76 | } |
| 77 | |
| 78 | static void remote_kick(atransport *t) |
| 79 | { |
| 80 | usb_kick(t->usb); |
| 81 | } |
| 82 | |
Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 83 | void init_usb_transport(atransport *t, usb_handle *h, ConnectionState state) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 85 | D("transport: usb"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | t->close = remote_close; |
| 87 | t->kick = remote_kick; |
| 88 | t->read_from_remote = remote_read; |
| 89 | t->write_to_remote = remote_write; |
| 90 | t->sync_token = 1; |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 91 | t->connection_state = state; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | t->type = kTransportUsb; |
| 93 | t->usb = h; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Xavier Ducrohet | a09fbd1 | 2009-05-20 17:33:53 -0700 | [diff] [blame] | 96 | #if ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol) |
| 98 | { |
Elliott Hughes | 5566490 | 2014-11-19 22:07:34 -0800 | [diff] [blame] | 99 | return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | } |
Xavier Ducrohet | a09fbd1 | 2009-05-20 17:33:53 -0700 | [diff] [blame] | 101 | #endif |