• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15
16export interface Adb {
17  connect(device: USBDevice): Promise<void>;
18  disconnect(): Promise<void>;
19  // Executes a shell command non-interactively.
20  shell(cmd: string): Promise<AdbStream>;
21  // Waits until the shell get closed, and returns all the output.
22  shellOutputAsString(cmd: string): Promise<string>;
23  // Opens a connection to a UNIX socket.
24  socket(path: string): Promise<AdbStream>;
25}
26
27export interface AdbStream {
28  write(msg: string|Uint8Array): Promise<void>;
29  onMessage(message: AdbMsg): void;
30  close(): void;
31  setClosed(): void;
32
33  onConnect: VoidCallback;
34  onClose: VoidCallback;
35  onData: (raw: Uint8Array) => void;
36}
37
38export class MockAdb implements Adb {
39  connect(_: USBDevice): Promise<void> {
40    return Promise.resolve();
41  }
42
43  disconnect(): Promise<void> {
44    return Promise.resolve();
45  }
46
47  shell(_: string): Promise<AdbStream> {
48    return Promise.resolve(new MockAdbStream());
49  }
50
51  shellOutputAsString(_: string): Promise<string> {
52    return Promise.resolve('');
53  }
54
55  socket(_: string): Promise<AdbStream> {
56    return Promise.resolve(new MockAdbStream());
57  }
58}
59
60export class MockAdbStream implements AdbStream {
61  write(_: string|Uint8Array): Promise<void> {
62    return Promise.resolve();
63  }
64  onMessage = (_: AdbMsg) => {};
65  close() {}
66  setClosed() {}
67
68  onConnect = () => {};
69  onClose = () => {};
70  onData = (_: Uint8Array) => {};
71}
72
73export declare type CmdType =
74    'CNXN' | 'AUTH' | 'CLSE' | 'OKAY' | 'WRTE' | 'OPEN';
75
76export interface AdbMsg {
77  cmd: CmdType;
78  arg0: number;
79  arg1: number;
80  data: Uint8Array;
81  dataLen: number;
82  dataChecksum: number;
83}