• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 /*
4  * Copyright (C) 2017 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <mutex>
22 #include <vector>
23 
24 #include <android-base/unique_fd.h>
25 #include <asyncio/AsyncIO.h>
26 
27 struct aio_block {
28     std::vector<struct iocb> iocb;
29     std::vector<struct iocb*> iocbs;
30     std::vector<struct io_event> events;
31     aio_context_t ctx;
32     int num_submitted;
33     int fd;
34 };
35 
36 struct usb_handle {
usb_handleusb_handle37     usb_handle() : kicked(false) {
38     }
39 
40     std::condition_variable notify;
41     std::mutex lock;
42     std::atomic<bool> kicked;
43     bool open_new_connection = true;
44 
45     int (*write)(usb_handle* h, const void* data, int len);
46     int (*read)(usb_handle* h, void* data, int len, bool allow_partial);
47     void (*kick)(usb_handle* h);
48     void (*close)(usb_handle* h);
49 
50     // FunctionFS
51     android::base::unique_fd control;
52     android::base::unique_fd bulk_out;  // "out" from the host's perspective => source for adbd
53     android::base::unique_fd bulk_in;   // "in" from the host's perspective => sink for adbd
54 
55     // Access to these blocks is very not thread safe. Have one block for each of the
56     // read and write threads.
57     struct aio_block read_aiob;
58     struct aio_block write_aiob;
59 
60     bool reads_zero_packets;
61     size_t io_size;
62 };
63 
64 usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size);
65 bool open_functionfs(android::base::unique_fd* control, android::base::unique_fd* bulk_out,
66                      android::base::unique_fd* bulk_in);
67