1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 
22 #include <android-base/unique_fd.h>
23 
24 // Allow tests to build on host
25 #if !defined(__INTRODUCED_IN)
26 #define __INTRODUCED_IN(__api_level) /* nothing */
27 #endif
28 
29 extern "C" {
30 
31 struct AdbConnectionClientContext;
32 
33 enum AdbConnectionClientInfoType {
34   pid,
35   debuggable,
36   profileable,
37   architecture,
38 };
39 
40 struct AdbConnectionClientInfo {
41   AdbConnectionClientInfoType type;
42   union {
43     uint64_t pid;
44     bool debuggable;
45     bool profileable;
46     struct {
47       const char* name;
48       size_t size;
49     } architecture;
50   } data;
51 };
52 
53 // Construct a context and connect to adbd.
54 // Returns null if we fail to connect to adbd.
55 // Note this is an apex interface as it's loaded by ART.
56 AdbConnectionClientContext* adbconnection_client_new(
57     const AdbConnectionClientInfo* const* info_elems, size_t info_count);
58 
59 // Update the apex client with the new name of the process. Nothing is transferred to the server.
60 // You need to call adbconnection_client_send_update to transmit the latest state to adbd.
61 void adbconnection_client_set_current_process_name(const char* process_name) __INTRODUCED_IN(37);
62 
63 // Update the apex client when a package name is added to the current process. Nothing is
64 // transferred to the server. You need to call adbconnection_client_send_update to transmit the
65 // latest state to adbd.
66 void adbconnection_client_add_application(const char* package_name) __INTRODUCED_IN(37);
67 
68 // Update the apex client when a package name is removed from the current process. Nothing is
69 // transferred to the server. You need to call adbconnection_client_send_update to transmit the
70 // latest state to adbd.
71 void adbconnection_client_remove_application(const char* package_name) __INTRODUCED_IN(37);
72 
73 // Update the apex client when the app is waiting for debugger (or not). Nothing is
74 // transferred to the server. You need to call adbconnection_client_send_update to transmit the
75 // latest state to adbd.
76 void adbconnection_client_set_waiting_for_debugger(bool waiting) __INTRODUCED_IN(37);
77 
78 // Update the apex client when app process uid is known. This is not the value from getuid() but
79 // the UserID profile (e.g.: A device with a single user will have one UserID=0 but a device with
80 // an additional work profile will have a second UserID). The origin of this value is the unix UID
81 // massaged in Framework via PER_USER_RANGE and USER_SYSTEM. Nothing is transferred to the server.
82 // You need to call adbconnection_client_send_update to transmit the latest state to adbd.
83 void adbconnection_client_set_user_id(int uid) __INTRODUCED_IN(37);
84 
85 // Check if the client has something to send to the server (we don't want to be woken by a
86 // writable socket if we have nothing to write). If it does, adbconnection_client_send_update
87 // should be called.
88 bool adbconnection_client_has_pending_update() __INTRODUCED_IN(37);
89 
90 // Write the latest appinfo state so adbd receives it.
91 void adbconnection_client_send_update(const AdbConnectionClientContext* ctx) __INTRODUCED_IN(37);
92 
93 void adbconnection_client_destroy(AdbConnectionClientContext* ctx);
94 
95 // Get an fd which can be polled upon to detect when a jdwp socket is available.
96 // You do not own this fd. Do not close it.
97 int adbconnection_client_pollfd(AdbConnectionClientContext* ctx);
98 
99 // Receive a jdwp client fd.
100 // Ownership is transferred to the caller of this function.
101 int adbconnection_client_receive_jdwp_fd(AdbConnectionClientContext* ctx);
102 }
103