1d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/*
2d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Copyright (C) 2011 The Android Open Source Project
3d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *
4d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Licensed under the Apache License, Version 2.0 (the "License");
5d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * you may not use this file except in compliance with the License.
6d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * You may obtain a copy of the License at
7d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *
8d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      http://www.apache.org/licenses/LICENSE-2.0
9d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *
10d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Unless required by applicable law or agreed to in writing, software
11d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * distributed under the License is distributed on an "AS IS" BASIS,
12d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * See the License for the specific language governing permissions and
14d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * limitations under the License.
15d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
16d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
17d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine#ifndef ANDROID_ADB_SERVER_H_
18d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine#define ANDROID_ADB_SERVER_H_
19d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
20d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/*
21d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Encapsulates a socket server that is bound to ADB port, and bridges ADB host
22d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * connections and data to ADB daemon running inside the guest.
23d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
24d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
25d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Callback to be invoked wheh host ADB gets connected with the guest ADB.
26d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
27d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque - An opaque pointer associated with the guest. This pointer contains
28d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      the 'opaque' parameter that was passed to the adb_server_register_guest
29d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      routine.
30d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  connection - An opaque pointer defining the connection between the host and
31d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      the guest ADBs. This pointer must be used for further operations on the
32d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      host <-> guest connection.
33d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
34d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkinetypedef void (*adbguest_connect)(void* opaque, void* connection);
35d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
36d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Callback to be invoked wheh the host ADB sends data to the guest ADB.
37d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
38d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque - An opaque pointer associated with the guest. This pointer contains
39d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      the 'opaque' parameter that was passed to the adb_server_register_guest
40d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      routine.
41d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  connection - An opaque pointer defining the connection between the host and
42d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      the guest ADB. This pointer must be used for further operations on the
43d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      host <-> guest connection.
44d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  buff, size - Buffer that has ben sent by the host.
45d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
46d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkinetypedef void (*adbguest_read)(void* opaque,
47d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine                              void* connection,
48d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine                              const void* buff,
49d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine                              int size);
50d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
51d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Callback to be invoked wheh the host ADB gets disconnected.
52d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
53d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque - An opaque pointer associated with the guest. This pointer contains
54d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      the 'opaque' parameter that was passed to the adb_server_register_guest
55d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      routine.
56d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  connection - An opaque pointer defining the connection between the host and
57d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      the guest ADB. This pointer must be used for further operations on the
58d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      host <-> guest connection.
59d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
60d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkinetypedef void (*adbguest_disconnect)(void* opaque, void* connection);
61d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
62d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Defines a set of callbacks for a guest ADB. */
63d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkinetypedef struct AdbGuestRoutines AdbGuestRoutines;
64d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkinestruct AdbGuestRoutines {
65d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine    /* Callback to invoke when ADB host is connected. */
66d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine    adbguest_connect     on_connected;
67d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine    /* Callback to invoke when ADB host is disconnected. */
68d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine    adbguest_disconnect  on_disconnect;
69d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine    /* Callback to invoke when ADB host sends data. */
70d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine    adbguest_read        on_read;
71d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine};
72d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
73d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Initializes ADB server.
74d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
75d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  port - socket port that is assigned for communication with the ADB host. This
76d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      is 'base port' + 1.
77d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Return:
78d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  0 on success, or != 0 on failure.
79d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
80d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkineextern int adb_server_init(int port);
81d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
82d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Checks if ADB server has been initialized. */
83d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkineextern int adb_server_is_initialized(void);
84d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
85d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Registers ADB guest with the ADB server.
86d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * There can be two cases here, as far as connection with the host is concerned:
87d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  - There is no host connection to immediately associate the guest with. In
88d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *    this case the guest will be registered as "pending connection", and routine
89d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *    will return.
90d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  - There is a pending host connection to associate with the new guest. In this
91d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *    case the association will be made in this routine, and 'adbguest_connect'
92d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *    callback will be called before this routine returns.
93d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
94d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque Opaque pointer associated with the guest. This pointer will be passed
95d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *      back to thee guest API in callback routines.
96d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  callbacks Contains callback routines for the registering guest.
97d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Return:
98d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  An opaque pointer associated with the ADB guest on success, or NULL on
99d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  failure. The pointer returned from this routine must be passed into ADB
100d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  server API called from the guest.
101d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
102d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkineextern void* adb_server_register_guest(void* opaque, AdbGuestRoutines* callbacks);
103d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
104d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Completes connection with the guest.
105d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * This routine is called by the guest when it receives a 'start' request from
106d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * ADB guest. This request tells the system that ADB daemon running inside the
107d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * guest is ready to receive data.
108d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
109d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque - An opaque pointer returned from adb_server_register_guest.
110d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
111d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkineextern void adb_server_complete_connection(void* opaque);
112d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
113d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Handles data received from the guest.
114d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
115d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque - An opaque pointer returned from adb_server_register_guest.
116d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * data, size - Data buffer received from the guest.
117d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
118d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkineextern void adb_server_on_guest_message(void* opaque,
119d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine                                        const uint8_t* data,
120d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine                                        int size);
121d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
122d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine/* Notifies the ADB server that the guest has closed its connection.
123d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine * Param:
124d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine *  opaque - An opaque pointer returned from adb_server_register_guest.
125d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine */
126d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkineextern void adb_server_on_guest_closed(void* opaque);
127d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine
128d86c724b74e6c04a89219d87559d0b580e100445Vladimir Chtchetkine#endif  /* ANDROID_ADB_SERVER_H_ */
129