1// Copyright (C) 2022 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// Begin Websocket //////////////////////////////////////////////////////// 16 17export const WEBSOCKET_UNABLE_TO_CONNECT = 18 'Unable to connect to device via websocket.'; 19 20// https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1 21export const WEBSOCKET_CLOSED_ABNORMALLY_CODE = 1006; 22 23// The messages read by the adb server have their length prepended in hex. 24// This method adds the length at the beginning of the message. 25// Example: 'host:track-devices' -> '0012host:track-devices' 26// go/codesearch/aosp-android11/system/core/adb/SERVICES.TXT 27export function buildAbdWebsocketCommand(cmd: string) { 28 const hdr = cmd.length.toString(16).padStart(4, '0'); 29 return hdr + cmd; 30} 31 32// Sample user agent for Chrome on Mac OS: 33// 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 34// (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36' 35export function isMacOs(userAgent: string) { 36 return userAgent.toLowerCase().includes(' mac os '); 37} 38 39// Sample user agent for Chrome on Linux: 40// Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) 41// Chrome/105.0.0.0 Safari/537.36 42export function isLinux(userAgent: string) { 43 return userAgent.toLowerCase().includes(' linux '); 44} 45 46// Sample user agent for Chrome on Chrome OS: 47// "Mozilla/5.0 (X11; CrOS x86_64 14816.99.0) AppleWebKit/537.36 48// (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36" 49// This condition is wider, in the unlikely possibility of different casing, 50export function isCrOS(userAgent: string) { 51 return userAgent.toLowerCase().includes(' cros '); 52} 53 54// End Websocket ////////////////////////////////////////////////////////// 55 56// Begin Adb ////////////////////////////////////////////////////////////// 57 58export const BINARY_PUSH_FAILURE = 'BinaryPushFailure'; 59export const BINARY_PUSH_UNKNOWN_RESPONSE = 'BinaryPushUnknownResponse'; 60 61// In case the device doesn't have the tracebox, we upload the latest version 62// to this path. 63export const TRACEBOX_DEVICE_PATH = '/data/local/tmp/tracebox'; 64 65// Experimentally, this takes 900ms on the first fetch and 20-30ms after 66// because of caching. 67export const TRACEBOX_FETCH_TIMEOUT = 30000; 68 69// Message shown to the user when they need to allow authentication on the 70// device in order to connect. 71export const ALLOW_USB_DEBUGGING = 72 'Please allow USB debugging on device and try again.'; 73 74// If the Android device has the tracing service on it (from API version 29), 75// then we can connect to this consumer socket. 76export const DEFAULT_TRACED_CONSUMER_SOCKET_PATH = 77 'localfilesystem:/dev/socket/traced_consumer'; 78 79// If the Android device does not have the tracing service on it (before API 80// version 29), we will have to push the tracebox on the device. Then, we 81// can connect to this consumer socket (using it does not require system admin 82// privileges). 83export const CUSTOM_TRACED_CONSUMER_SOCKET_PATH = 84 'localabstract:traced_consumer'; 85 86// End Adb ///////////////////////////////////////////////////////////////// 87 88 89// Begin Webusb /////////////////////////////////////////////////////////// 90 91export const NO_DEVICE_SELECTED = 'No device selected.'; 92 93export interface UsbInterfaceAndEndpoint { 94 readonly configurationValue: number; 95 readonly usbInterfaceNumber: number; 96 readonly endpoints: USBEndpoint[]; 97} 98 99export const ADB_DEVICE_FILTER = { 100 classCode: 255, // USB vendor specific code 101 subclassCode: 66, // Android vendor specific subclass 102 protocolCode: 1, // Adb protocol 103}; 104 105export function findInterfaceAndEndpoint(device: USBDevice): 106 UsbInterfaceAndEndpoint|undefined { 107 const adbDeviceFilter = ADB_DEVICE_FILTER; 108 for (const config of device.configurations) { 109 for (const interface_ of config.interfaces) { 110 for (const alt of interface_.alternates) { 111 if (alt.interfaceClass === adbDeviceFilter.classCode && 112 alt.interfaceSubclass === adbDeviceFilter.subclassCode && 113 alt.interfaceProtocol === adbDeviceFilter.protocolCode) { 114 return { 115 configurationValue: config.configurationValue, 116 usbInterfaceNumber: interface_.interfaceNumber, 117 endpoints: alt.endpoints, 118 }; 119 } // if (alternate) 120 } // for (interface.alternates) 121 } // for (configuration.interfaces) 122 } // for (configurations) 123 124 return undefined; 125} 126 127// End Webusb ////////////////////////////////////////////////////////////// 128 129 130// Begin Chrome /////////////////////////////////////////////////////////// 131 132export const EXTENSION_ID = 'lfmkphfpdbjijhpomgecfikhfohaoine'; 133export const EXTENSION_URL = 134 `https://chrome.google.com/webstore/detail/perfetto-ui/${EXTENSION_ID}`; 135export const EXTENSION_NAME = 'Chrome extension'; 136export const EXTENSION_NOT_INSTALLED = 137 `To trace Chrome from the Perfetto UI, you need to install our 138 ${EXTENSION_URL} and then reload this page.`; 139 140export const MALFORMED_EXTENSION_MESSAGE = 'Malformed extension message.'; 141export const BUFFER_USAGE_NOT_ACCESSIBLE = 'Buffer usage not accessible'; 142export const BUFFER_USAGE_INCORRECT_FORMAT = 143 'The buffer usage data has am incorrect format'; 144 145// End Chrome ///////////////////////////////////////////////////////////// 146 147 148// Begin Traced ////////////////////////////////////////////////////////// 149 150export const RECORDING_IN_PROGRESS = 'Recording in progress'; 151export const PARSING_UNKNWON_REQUEST_ID = 'Unknown request id'; 152export const PARSING_UNABLE_TO_DECODE_METHOD = 'Unable to decode method'; 153export const PARSING_UNRECOGNIZED_PORT = 'Unrecognized consumer port response'; 154export const PARSING_UNRECOGNIZED_MESSAGE = 'Unrecognized frame message'; 155 156// End Traced /////////////////////////////////////////////////////////// 157