Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2011 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 #ifndef ANDROID_ADB_SERVER_H_
     18 #define ANDROID_ADB_SERVER_H_
     19 
     20 /*
     21  * Encapsulates a socket server that is bound to ADB port, and bridges ADB host
     22  * connections and data to ADB daemon running inside the guest.
     23  */
     24 
     25 /* Callback to be invoked wheh host ADB gets connected with the guest ADB.
     26  * Param:
     27  *  opaque - An opaque pointer associated with the guest. This pointer contains
     28  *      the 'opaque' parameter that was passed to the adb_server_register_guest
     29  *      routine.
     30  *  connection - An opaque pointer defining the connection between the host and
     31  *      the guest ADBs. This pointer must be used for further operations on the
     32  *      host <-> guest connection.
     33  */
     34 typedef void (*adbguest_connect)(void* opaque, void* connection);
     35 
     36 /* Callback to be invoked wheh the host ADB sends data to the guest ADB.
     37  * Param:
     38  *  opaque - An opaque pointer associated with the guest. This pointer contains
     39  *      the 'opaque' parameter that was passed to the adb_server_register_guest
     40  *      routine.
     41  *  connection - An opaque pointer defining the connection between the host and
     42  *      the guest ADB. This pointer must be used for further operations on the
     43  *      host <-> guest connection.
     44  *  buff, size - Buffer that has ben sent by the host.
     45  */
     46 typedef void (*adbguest_read)(void* opaque,
     47                               void* connection,
     48                               const void* buff,
     49                               int size);
     50 
     51 /* Callback to be invoked wheh the host ADB gets disconnected.
     52  * Param:
     53  *  opaque - An opaque pointer associated with the guest. This pointer contains
     54  *      the 'opaque' parameter that was passed to the adb_server_register_guest
     55  *      routine.
     56  *  connection - An opaque pointer defining the connection between the host and
     57  *      the guest ADB. This pointer must be used for further operations on the
     58  *      host <-> guest connection.
     59  */
     60 typedef void (*adbguest_disconnect)(void* opaque, void* connection);
     61 
     62 /* Defines a set of callbacks for a guest ADB. */
     63 typedef struct AdbGuestRoutines AdbGuestRoutines;
     64 struct AdbGuestRoutines {
     65     /* Callback to invoke when ADB host is connected. */
     66     adbguest_connect     on_connected;
     67     /* Callback to invoke when ADB host is disconnected. */
     68     adbguest_disconnect  on_disconnect;
     69     /* Callback to invoke when ADB host sends data. */
     70     adbguest_read        on_read;
     71 };
     72 
     73 /* Initializes ADB server.
     74  * Param:
     75  *  port - socket port that is assigned for communication with the ADB host. This
     76  *      is 'base port' + 1.
     77  * Return:
     78  *  0 on success, or != 0 on failure.
     79  */
     80 extern int adb_server_init(int port);
     81 
     82 /* Checks if ADB server has been initialized. */
     83 extern int adb_server_is_initialized(void);
     84 
     85 /* Registers ADB guest with the ADB server.
     86  * There can be two cases here, as far as connection with the host is concerned:
     87  *  - There is no host connection to immediately associate the guest with. In
     88  *    this case the guest will be registered as "pending connection", and routine
     89  *    will return.
     90  *  - There is a pending host connection to associate with the new guest. In this
     91  *    case the association will be made in this routine, and 'adbguest_connect'
     92  *    callback will be called before this routine returns.
     93  * Param:
     94  *  opaque Opaque pointer associated with the guest. This pointer will be passed
     95  *      back to thee guest API in callback routines.
     96  *  callbacks Contains callback routines for the registering guest.
     97  * Return:
     98  *  An opaque pointer associated with the ADB guest on success, or NULL on
     99  *  failure. The pointer returned from this routine must be passed into ADB
    100  *  server API called from the guest.
    101  */
    102 extern void* adb_server_register_guest(void* opaque, AdbGuestRoutines* callbacks);
    103 
    104 /* Completes connection with the guest.
    105  * This routine is called by the guest when it receives a 'start' request from
    106  * ADB guest. This request tells the system that ADB daemon running inside the
    107  * guest is ready to receive data.
    108  * Param:
    109  *  opaque - An opaque pointer returned from adb_server_register_guest.
    110  */
    111 extern void adb_server_complete_connection(void* opaque);
    112 
    113 /* Handles data received from the guest.
    114  * Param:
    115  *  opaque - An opaque pointer returned from adb_server_register_guest.
    116  * data, size - Data buffer received from the guest.
    117  */
    118 extern void adb_server_on_guest_message(void* opaque,
    119                                         const uint8_t* data,
    120                                         int size);
    121 
    122 /* Notifies the ADB server that the guest has closed its connection.
    123  * Param:
    124  *  opaque - An opaque pointer returned from adb_server_register_guest.
    125  */
    126 extern void adb_server_on_guest_closed(void* opaque);
    127 
    128 #endif  /* ANDROID_ADB_SERVER_H_ */
    129