1 /*
2  * Copyright (C) 2023 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 #ifndef HARDWARE_GOOGLE_PIXEL_USB_UTILSCOMMON_H_
18 #define HARDWARE_GOOGLE_PIXEL_USB_UTILSCOMMON_H_
19 
20 #include <android-base/chrono_utils.h>
21 #include <android-base/unique_fd.h>
22 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
23 
24 #include <string>
25 
26 namespace android {
27 namespace hardware {
28 namespace google {
29 namespace pixel {
30 namespace usb {
31 
32 constexpr int kBufferSize = 512;
33 constexpr int kMaxFilePathLength = 256;
34 constexpr int kEpollEvents = 10;
35 constexpr bool kDebug = false;
36 constexpr int kDisconnectWaitUs = 100000;
37 constexpr int kPullUpDelay = 500000;
38 constexpr int kShutdownMonitor = 100;
39 
40 constexpr char kBuildType[] = "ro.build.type";
41 constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config";
42 constexpr char kVendorConfig[] = "vendor.usb.config";
43 constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config";
44 constexpr char kUvcEnabled[] = "ro.usb.uvc.enabled";
45 
46 #define GADGET_PATH "/config/usb_gadget/g1/"
47 #define PULLUP_PATH GADGET_PATH "UDC"
48 #define PERSISTENT_BOOT_MODE "ro.bootmode"
49 #define VENDOR_ID_PATH GADGET_PATH "idVendor"
50 #define PRODUCT_ID_PATH GADGET_PATH "idProduct"
51 #define DEVICE_CLASS_PATH GADGET_PATH "bDeviceClass"
52 #define DEVICE_SUB_CLASS_PATH GADGET_PATH "bDeviceSubClass"
53 #define DEVICE_PROTOCOL_PATH GADGET_PATH "bDeviceProtocol"
54 #define DESC_USE_PATH GADGET_PATH "os_desc/use"
55 #define OS_DESC_PATH GADGET_PATH "os_desc/b.1"
56 #define CONFIG_PATH GADGET_PATH "configs/b.1/"
57 #define FUNCTIONS_PATH GADGET_PATH "functions/"
58 #define FUNCTION_NAME "function"
59 #define FUNCTION_PATH CONFIG_PATH FUNCTION_NAME
60 #define RNDIS_PATH FUNCTIONS_PATH "gsi.rndis"
61 
62 using ::android::base::boot_clock;
63 using android::hardware::google::pixel::PixelAtoms::VendorUsbDataSessionEvent;
64 
65 // Adds the given fd to the epollfd(epfd).
66 int addEpollFd(const ::android::base::unique_fd &epfd, const ::android::base::unique_fd &fd);
67 // Extracts vendor functions from the vendor init properties.
68 std::string getVendorFunctions();
69 // Removes all the usb functions link in the specified path.
70 int unlinkFunctions(const char *path);
71 // Creates a configfs link for the function.
72 int linkFunction(const char *function, int index);
73 // Sets the USB VID and PID. Returns true on success, false on failure
74 bool setVidPidCommon(const char *vid, const char *pid);
75 // Pulls down USB gadget. Returns true on success, false on failure
76 bool resetGadgetCommon();
77 void BuildVendorUsbDataSessionEvent(bool is_host, boot_clock::time_point currentTime,
78                                     boot_clock::time_point startTime,
79                                     std::vector<std::string> *states,
80                                     std::vector<boot_clock::time_point> *timestamps,
81                                     VendorUsbDataSessionEvent *event);
82 
83 }  // namespace usb
84 }  // namespace pixel
85 }  // namespace google
86 }  // namespace hardware
87 }  // namespace android
88 
89 #endif  // HARDWARE_GOOGLE_PIXEL_USB_UTILSCOMMON_H_
90