• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "libpixelusb-common"
18 
19 #include "include/pixelusb/CommonUtils.h"
20 
21 #include <android-base/file.h>
22 #include <android-base/properties.h>
23 #include <sys/epoll.h>
24 #include <sys/eventfd.h>
25 #include <sys/inotify.h>
26 #include <utils/Log.h>
27 
28 #include <chrono>
29 #include <memory>
30 #include <mutex>
31 
32 namespace android {
33 namespace hardware {
34 namespace google {
35 namespace pixel {
36 namespace usb {
37 
38 using ::android::base::GetProperty;
39 using ::android::base::SetProperty;
40 using ::android::base::WriteStringToFile;
41 using ::std::chrono::microseconds;
42 using ::std::chrono::steady_clock;
43 using ::std::literals::chrono_literals::operator""ms;
44 
addEpollFd(const base::unique_fd & epfd,const base::unique_fd & fd)45 int addEpollFd(const base::unique_fd &epfd, const base::unique_fd &fd) {
46     struct epoll_event event;
47     int ret;
48 
49     event.data.fd = fd;
50     event.events = EPOLLIN;
51 
52     ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
53     if (ret)
54         ALOGE("epoll_ctl error %d", errno);
55 
56     return ret;
57 }
58 
getVendorFunctions()59 std::string getVendorFunctions() {
60     if (GetProperty(kBuildType, "") == "user")
61         return "user";
62 
63     std::string bootMode = GetProperty(PERSISTENT_BOOT_MODE, "");
64     std::string persistVendorFunctions = GetProperty(kPersistentVendorConfig, "");
65     std::string vendorFunctions = GetProperty(kVendorConfig, "");
66     std::string ret = "";
67 
68     if (vendorFunctions != "") {
69         ret = vendorFunctions;
70     } else if (bootMode == "usbradio" || bootMode == "factory" || bootMode == "ffbm-00" ||
71                bootMode == "ffbm-01" || bootMode == "usbuwb") {
72         if (persistVendorFunctions != "")
73             ret = persistVendorFunctions;
74         else
75             ret = "diag";
76         // vendor.usb.config will reflect the current configured functions
77         SetProperty(kVendorConfig, ret);
78     }
79 
80     return ret;
81 }
82 
unlinkFunctions(const char * path)83 int unlinkFunctions(const char *path) {
84     DIR *config = opendir(path);
85     struct dirent *function;
86     char filepath[kMaxFilePathLength];
87     int ret = 0;
88 
89     if (config == NULL)
90         return -1;
91 
92     // d_type does not seems to be supported in /config
93     // so filtering by name.
94     while (((function = readdir(config)) != NULL)) {
95         if ((strstr(function->d_name, FUNCTION_NAME) == NULL))
96             continue;
97         // build the path for each file in the folder.
98         snprintf(filepath, kMaxFilePathLength, "%s/%s", path, function->d_name);
99         ret = remove(filepath);
100         if (ret) {
101             ALOGE("Unable  remove file %s errno:%d", filepath, errno);
102             break;
103         }
104     }
105 
106     closedir(config);
107     return ret;
108 }
109 
linkFunction(const char * function,int index)110 int linkFunction(const char *function, int index) {
111     char functionPath[kMaxFilePathLength];
112     char link[kMaxFilePathLength];
113 
114     snprintf(functionPath, kMaxFilePathLength, "%s%s", FUNCTIONS_PATH, function);
115     snprintf(link, kMaxFilePathLength, "%s%d", FUNCTION_PATH, index);
116     if (symlink(functionPath, link)) {
117         ALOGE("Cannot create symlink %s -> %s errno:%d", link, functionPath, errno);
118         return -1;
119     }
120     return 0;
121 }
122 
setVidPidCommon(const char * vid,const char * pid)123 bool setVidPidCommon(const char *vid, const char *pid) {
124     if (!WriteStringToFile(vid, VENDOR_ID_PATH))
125         return false;
126 
127     if (!WriteStringToFile(pid, PRODUCT_ID_PATH))
128         return false;
129 
130     return true;
131 }
132 
resetGadgetCommon()133 bool resetGadgetCommon() {
134     ALOGI("setCurrentUsbFunctions None");
135 
136     if (!WriteStringToFile("none", PULLUP_PATH))
137         ALOGI("Gadget cannot be pulled down");
138 
139     if (!WriteStringToFile("0", DEVICE_CLASS_PATH))
140         return false;
141 
142     if (!WriteStringToFile("0", DEVICE_SUB_CLASS_PATH))
143         return false;
144 
145     if (!WriteStringToFile("0", DEVICE_PROTOCOL_PATH))
146         return false;
147 
148     if (!WriteStringToFile("0", DESC_USE_PATH))
149         return false;
150 
151     if (unlinkFunctions(CONFIG_PATH))
152         return false;
153 
154     return true;
155 }
156 
157 }  // namespace usb
158 }  // namespace pixel
159 }  // namespace google
160 }  // namespace hardware
161 }  // namespace android
162