1 /* 2 * Copyright (C) 2025 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 #include "detach.h" 18 19 #include "usb.h" 20 21 AttachedDevices attached_devices [[clang::no_destroy]]; 22 RegisterAttach(const std::string & serial)23void AttachedDevices::RegisterAttach(const std::string& serial) { 24 std::lock_guard<std::mutex> lock(attached_devices_mutex_); 25 attached_devices_.insert(serial); 26 } 27 RegisterDetach(const std::string & serial)28void AttachedDevices::RegisterDetach(const std::string& serial) { 29 std::lock_guard<std::mutex> lock(attached_devices_mutex_); 30 attached_devices_.erase(serial); 31 } 32 IsAttached(const std::string & serial)33bool AttachedDevices::IsAttached(const std::string& serial) { 34 std::lock_guard<std::mutex> lock(attached_devices_mutex_); 35 return attached_devices_.contains(serial); 36 } 37 ShouldStartDetached(const Connection & c)38bool AttachedDevices::ShouldStartDetached(const Connection& c) { 39 if (!c.SupportsDetach()) { 40 return false; 41 } 42 static const char* env = getenv("ADB_LIBUSB_START_DETACHED"); 43 static bool should_start_detached = env && strcmp("1", env) == 0; 44 return should_start_detached && !IsAttached(c.Serial()); 45 }