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
17#define TRACE_TAG TRACE_TRANSPORT
18
19#include "sysdeps.h"
20#include "transport.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "adb.h"
27
28static int remote_read(apacket *p, atransport *t)
29{
30    if(usb_read(t->usb, &p->msg, sizeof(amessage))){
31        D("remote usb: read terminated (message)\n");
32        return -1;
33    }
34
35    if(check_header(p)) {
36        D("remote usb: check_header failed\n");
37        return -1;
38    }
39
40    if(p->msg.data_length) {
41        if(usb_read(t->usb, p->data, p->msg.data_length)){
42            D("remote usb: terminated (data)\n");
43            return -1;
44        }
45    }
46
47    if(check_data(p)) {
48        D("remote usb: check_data failed\n");
49        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
59    if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
60        D("remote usb: 1 - write terminated\n");
61        return -1;
62    }
63    if(p->msg.data_length == 0) return 0;
64    if(usb_write(t->usb, &p->data, size)) {
65        D("remote usb: 2 - write terminated\n");
66        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
83void init_usb_transport(atransport *t, usb_handle *h, int state)
84{
85    D("transport: usb\n");
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;
91    t->connection_state = state;
92    t->type = kTransportUsb;
93    t->usb = h;
94
95#if ADB_HOST
96    HOST = 1;
97#else
98    HOST = 0;
99#endif
100}
101
102#if ADB_HOST
103int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
104{
105    return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
106}
107#endif
108