xref: /aosp_15_r20/external/perfetto/ui/src/plugins/dev.perfetto.RecordTrace/adb_interfaces.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15export interface Adb {
16  connect(device: USBDevice): Promise<void>;
17  disconnect(): Promise<void>;
18  // Executes a shell command non-interactively.
19  shell(cmd: string): Promise<AdbStream>;
20  // Waits until the shell get closed, and returns all the output.
21  shellOutputAsString(cmd: string): Promise<string>;
22  // Opens a connection to a UNIX socket.
23  socket(path: string): Promise<AdbStream>;
24}
25
26export interface AdbStream {
27  write(msg: string | Uint8Array): Promise<void>;
28  onMessage(message: AdbMsg): void;
29  close(): void;
30  setClosed(): void;
31
32  onConnect: VoidCallback;
33  onClose: VoidCallback;
34  onData: (raw: Uint8Array) => void;
35}
36
37export class MockAdb implements Adb {
38  connect(_: USBDevice): Promise<void> {
39    return Promise.resolve();
40  }
41
42  disconnect(): Promise<void> {
43    return Promise.resolve();
44  }
45
46  shell(_: string): Promise<AdbStream> {
47    return Promise.resolve(new MockAdbStream());
48  }
49
50  shellOutputAsString(_: string): Promise<string> {
51    return Promise.resolve('');
52  }
53
54  socket(_: string): Promise<AdbStream> {
55    return Promise.resolve(new MockAdbStream());
56  }
57}
58
59export class MockAdbStream implements AdbStream {
60  write(_: string | Uint8Array): Promise<void> {
61    return Promise.resolve();
62  }
63  onMessage = (_: AdbMsg) => {};
64  close() {}
65  setClosed() {}
66
67  onConnect = () => {};
68  onClose = () => {};
69  onData = (_: Uint8Array) => {};
70}
71
72export declare type CmdType =
73  | 'CNXN'
74  | 'AUTH'
75  | 'CLSE'
76  | 'OKAY'
77  | 'WRTE'
78  | 'OPEN';
79
80export interface AdbMsg {
81  cmd: CmdType;
82  arg0: number;
83  arg1: number;
84  data: Uint8Array;
85  dataLen: number;
86  dataChecksum: number;
87}
88