1 // Copyright (C) 2020 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 #include <err.h>
16 #include <getopt.h>
17 #include <stdarg.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 
21 #include <string>
22 #include <string_view>
23 #include <variant>
24 #include <vector>
25 
26 #include <libusb/libusb.h>
27 
28 struct AllDevices {};
29 struct SingleDevice {};
30 struct Serial {
31     std::string_view serial;
32 };
33 
34 using DeviceSelection = std::variant<std::monostate, AllDevices, SingleDevice, Serial>;
35 
Usage(int rc)36 [[noreturn]] static void Usage(int rc) {
37     fprintf(stderr, "usage: [ANDROID_SERIAL=SERIAL] usbreset [-d] [-s SERIAL]\n");
38     fprintf(stderr, "\t-a --all\t\tReset all connected devices\n");
39     fprintf(stderr, "\t-d --device\t\tReset the single connected device\n");
40     fprintf(stderr, "\t-s --serial\t\tReset device with specified serial\n");
41     exit(rc);
42 }
43 
SetOption(DeviceSelection * out,DeviceSelection in)44 static void SetOption(DeviceSelection* out, DeviceSelection in) {
45     if (!std::get_if<std::monostate>(out)) {
46         printf("error: multiple device selection options provided\n");
47         Usage(1);
48     }
49 
50     *out = in;
51 }
52 
PrintLibusbError(int err,const char * fmt,...)53 static __attribute__((format(printf, 2, 3))) void PrintLibusbError(int err, const char* fmt, ...) {
54     va_list args;
55     va_start(args, fmt);
56     vprintf(fmt, args);
57     vprintf(fmt, args);
58     va_end(args);
59 
60     printf(": %s", libusb_strerror(static_cast<libusb_error>(err)));
61 }
62 
IsAdbInterface(const libusb_interface_descriptor * desc)63 static bool IsAdbInterface(const libusb_interface_descriptor* desc) {
64     return desc->bInterfaceClass == 0xFF && desc->bInterfaceSubClass == 0x42 &&
65            desc->bInterfaceProtocol == 0x1;
66 }
67 
main(int argc,char ** argv)68 int main(int argc, char** argv) {
69     std::variant<std::monostate, AllDevices, SingleDevice, Serial> selection;
70 
71     static constexpr struct option long_opts[] = {
72             {"all", 0, 0, 'a'},    {"help", 0, 0, 'h'}, {"serial", required_argument, 0, 's'},
73             {"device", 0, 0, 'd'}, {0, 0, 0, 0},
74     };
75 
76     int opt;
77     while ((opt = getopt_long(argc, argv, "adhs:", long_opts, nullptr)) != -1) {
78         if (opt == 'h') {
79             Usage(0);
80         } else if (opt == 'a') {
81             SetOption(&selection, AllDevices{});
82         } else if (opt == 's') {
83             SetOption(&selection, Serial{optarg});
84         } else if (opt == 'd') {
85             SetOption(&selection, Serial{optarg});
86         } else {
87             errx(1, "unknown option: '%c'", opt);
88         }
89     }
90 
91     if (std::get_if<std::monostate>(&selection)) {
92         const char* env = getenv("ANDROID_SERIAL");
93         if (env) {
94             SetOption(&selection, Serial{env});
95         } else {
96             fprintf(stderr, "adb_usbreset: no device specified\n");
97             Usage(1);
98         }
99     }
100 
101     libusb_context* ctx;
102     int rc = libusb_init(&ctx);
103     if (rc != LIBUSB_SUCCESS) {
104         PrintLibusbError(rc, "error: failed to initialize libusb");
105         exit(1);
106     }
107 
108     libusb_device** device_list;
109     ssize_t device_count = libusb_get_device_list(ctx, &device_list);
110     if (device_count < 0) {
111         PrintLibusbError(device_count, "error: failed to list devices");
112         exit(1);
113     }
114 
115     std::vector<std::pair<std::string, libusb_device_handle*>> selected_devices;
116     for (int i = 0; i < device_count; ++i) {
117         libusb_device* device = device_list[i];
118         libusb_device_descriptor device_desc;
119 
120         // Always succeeds for LIBUSB_API_VERSION >= 0x01000102.
121         libusb_get_device_descriptor(device, &device_desc);
122         static_assert(LIBUSB_API_VERSION >= 0x01000102);
123 
124         libusb_config_descriptor* config_desc;
125         rc = libusb_get_active_config_descriptor(device, &config_desc);
126         if (rc != 0) {
127             PrintLibusbError(rc, "warning: failed to get config descriptor");
128             continue;
129         }
130 
131         bool found_adb_interface = false;
132         for (int i = 0; i < config_desc->bNumInterfaces; ++i) {
133             if (IsAdbInterface(&config_desc->interface[i].altsetting[0])) {
134                 found_adb_interface = true;
135                 break;
136             }
137         }
138 
139         if (found_adb_interface) {
140             libusb_device_handle* device_handle;
141             rc = libusb_open(device, &device_handle);
142             if (rc != 0) {
143                 PrintLibusbError(rc, "warning: failed to open device");
144                 continue;
145             }
146 
147             char buf[128];
148             rc = libusb_get_string_descriptor_ascii(device_handle, device_desc.iSerialNumber,
149                                                     reinterpret_cast<unsigned char*>(buf),
150                                                     sizeof(buf));
151 
152             if (rc < 0) {
153                 PrintLibusbError(rc, "warning: failed to get device serial");
154                 continue;
155             }
156 
157             std::string serial(buf, buf + rc);
158             if (auto s = std::get_if<Serial>(&selection)) {
159                 if (s->serial == serial) {
160                     selected_devices.push_back(std::make_pair(std::move(serial), device_handle));
161                 }
162             } else {
163                 selected_devices.push_back(std::make_pair(std::move(serial), device_handle));
164             }
165         }
166     }
167 
168     if (selected_devices.empty()) {
169         errx(1, "no devices match criteria");
170     } else if (std::get_if<SingleDevice>(&selection) && selected_devices.size() != 1) {
171         errx(1, "more than 1 device connected");
172     }
173 
174     bool success = true;
175     for (auto& [serial, device_handle] : selected_devices) {
176         rc = libusb_reset_device(device_handle);
177         // libusb_reset_device will try to restore the previous state, and will return
178         // LIBUSB_ERROR_NOT_FOUND if it can't.
179         if (rc == 0 || rc == LIBUSB_ERROR_NOT_FOUND) {
180             printf("%s: successfully reset\n", serial.c_str());
181         } else {
182             PrintLibusbError(rc, "%s: failed to reset", serial.c_str());
183             success = false;
184         }
185     }
186 
187     return !success;
188 }
189