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 // Android metrics requires number of elements in any repeated field cannot exceed 127 elements
39 constexpr int kWestworldRepeatedFieldSizeLimit = 127;
40 
41 using ::android::base::GetProperty;
42 using ::android::base::SetProperty;
43 using ::android::base::WriteStringToFile;
44 using ::std::chrono::microseconds;
45 using ::std::chrono::steady_clock;
46 using ::std::literals::chrono_literals::operator""ms;
47 using android::hardware::google::pixel::PixelAtoms::VendorUsbDataSessionEvent;
48 using android::hardware::google::pixel::PixelAtoms::
49         VendorUsbDataSessionEvent_UsbDataRole_USB_ROLE_DEVICE;
50 using android::hardware::google::pixel::PixelAtoms::
51         VendorUsbDataSessionEvent_UsbDataRole_USB_ROLE_HOST;
52 using android::hardware::google::pixel::PixelAtoms::VendorUsbDataSessionEvent_UsbDeviceState;
53 using android::hardware::google::pixel::PixelAtoms::
54         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_ADDRESSED;
55 using android::hardware::google::pixel::PixelAtoms::
56         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_ATTACHED;
57 using android::hardware::google::pixel::PixelAtoms::
58         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_CONFIGURED;
59 using android::hardware::google::pixel::PixelAtoms::
60         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_DEFAULT;
61 using android::hardware::google::pixel::PixelAtoms::
62         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_NOT_ATTACHED;
63 using android::hardware::google::pixel::PixelAtoms::
64         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_POWERED;
65 using android::hardware::google::pixel::PixelAtoms::
66         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_SUSPENDED;
67 using android::hardware::google::pixel::PixelAtoms::
68         VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_UNKNOWN;
69 
addEpollFd(const base::unique_fd & epfd,const base::unique_fd & fd)70 int addEpollFd(const base::unique_fd &epfd, const base::unique_fd &fd) {
71     struct epoll_event event;
72     int ret;
73 
74     event.data.fd = fd;
75     event.events = EPOLLIN;
76 
77     ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
78     if (ret)
79         ALOGE("epoll_ctl error %d", errno);
80 
81     return ret;
82 }
83 
getVendorFunctions()84 std::string getVendorFunctions() {
85     if (GetProperty(kBuildType, "") == "user")
86         return "user";
87 
88     std::string bootMode = GetProperty(PERSISTENT_BOOT_MODE, "");
89     std::string persistVendorFunctions = GetProperty(kPersistentVendorConfig, "");
90     std::string vendorFunctions = GetProperty(kVendorConfig, "");
91     std::string ret = "";
92 
93     if (vendorFunctions != "") {
94         ret = vendorFunctions;
95     } else if (bootMode == "usbradio" || bootMode == "factory" || bootMode == "ffbm-00" ||
96                bootMode == "ffbm-01" || bootMode == "usbuwb") {
97         if (persistVendorFunctions != "")
98             ret = persistVendorFunctions;
99         else
100             ret = "diag";
101         // vendor.usb.config will reflect the current configured functions
102         SetProperty(kVendorConfig, ret);
103     }
104 
105     return ret;
106 }
107 
unlinkFunctions(const char * path)108 int unlinkFunctions(const char *path) {
109     DIR *config = opendir(path);
110     struct dirent *function;
111     char filepath[kMaxFilePathLength];
112     int ret = 0;
113 
114     if (config == NULL)
115         return -1;
116 
117     // d_type does not seems to be supported in /config
118     // so filtering by name.
119     while (((function = readdir(config)) != NULL)) {
120         if ((strstr(function->d_name, FUNCTION_NAME) == NULL))
121             continue;
122         // build the path for each file in the folder.
123         snprintf(filepath, kMaxFilePathLength, "%s/%s", path, function->d_name);
124         ret = remove(filepath);
125         if (ret) {
126             ALOGE("Unable  remove file %s errno:%d", filepath, errno);
127             break;
128         }
129     }
130 
131     closedir(config);
132     return ret;
133 }
134 
linkFunction(const char * function,int index)135 int linkFunction(const char *function, int index) {
136     char functionPath[kMaxFilePathLength];
137     char link[kMaxFilePathLength];
138 
139     snprintf(functionPath, kMaxFilePathLength, "%s%s", FUNCTIONS_PATH, function);
140     snprintf(link, kMaxFilePathLength, "%s%d", FUNCTION_PATH, index);
141     if (symlink(functionPath, link)) {
142         ALOGE("Cannot create symlink %s -> %s errno:%d", link, functionPath, errno);
143         return -1;
144     }
145     return 0;
146 }
147 
setVidPidCommon(const char * vid,const char * pid)148 bool setVidPidCommon(const char *vid, const char *pid) {
149     if (!WriteStringToFile(vid, VENDOR_ID_PATH))
150         return false;
151 
152     if (!WriteStringToFile(pid, PRODUCT_ID_PATH))
153         return false;
154 
155     return true;
156 }
157 
resetGadgetCommon()158 bool resetGadgetCommon() {
159     ALOGI("setCurrentUsbFunctions None");
160 
161     if (!WriteStringToFile("none", PULLUP_PATH))
162         ALOGI("Gadget cannot be pulled down");
163 
164     if (!WriteStringToFile("0", DEVICE_CLASS_PATH))
165         return false;
166 
167     if (!WriteStringToFile("0", DEVICE_SUB_CLASS_PATH))
168         return false;
169 
170     if (!WriteStringToFile("0", DEVICE_PROTOCOL_PATH))
171         return false;
172 
173     if (!WriteStringToFile("0", DESC_USE_PATH))
174         return false;
175 
176     if (unlinkFunctions(CONFIG_PATH))
177         return false;
178 
179     return true;
180 }
181 
stringToUsbDeviceStateProto(const std::string & state)182 static VendorUsbDataSessionEvent_UsbDeviceState stringToUsbDeviceStateProto(
183         const std::string &state) {
184     if (state == "not attached\n") {
185         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_NOT_ATTACHED;
186     } else if (state == "attached\n") {
187         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_ATTACHED;
188     } else if (state == "powered\n") {
189         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_POWERED;
190     } else if (state == "default\n") {
191         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_DEFAULT;
192     } else if (state == "addressed\n") {
193         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_ADDRESSED;
194     } else if (state == "configured\n") {
195         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_CONFIGURED;
196     } else if (state == "suspended\n") {
197         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_SUSPENDED;
198     } else {
199         return VendorUsbDataSessionEvent_UsbDeviceState_USB_STATE_UNKNOWN;
200     }
201 }
202 
BuildVendorUsbDataSessionEvent(bool is_host,boot_clock::time_point currentTime,boot_clock::time_point startTime,std::vector<std::string> * states,std::vector<boot_clock::time_point> * timestamps,VendorUsbDataSessionEvent * event)203 void BuildVendorUsbDataSessionEvent(bool is_host, boot_clock::time_point currentTime,
204                                     boot_clock::time_point startTime,
205                                     std::vector<std::string> *states,
206                                     std::vector<boot_clock::time_point> *timestamps,
207                                     VendorUsbDataSessionEvent *event) {
208     if (is_host) {
209         event->set_usb_role(VendorUsbDataSessionEvent_UsbDataRole_USB_ROLE_HOST);
210     } else {
211         event->set_usb_role(VendorUsbDataSessionEvent_UsbDataRole_USB_ROLE_DEVICE);
212     }
213 
214     for (int i = 0; i < states->size() && i < kWestworldRepeatedFieldSizeLimit; i++) {
215         event->add_usb_states(stringToUsbDeviceStateProto(states->at(i)));
216     }
217 
218     for (int i = 0; i < timestamps->size() && i < kWestworldRepeatedFieldSizeLimit; i++) {
219         event->add_elapsed_time_ms(
220                 std::chrono::duration_cast<std::chrono::milliseconds>(timestamps->at(i) - startTime)
221                         .count());
222     }
223 
224     event->set_duration_ms(
225             std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count());
226 }
227 
228 }  // namespace usb
229 }  // namespace pixel
230 }  // namespace google
231 }  // namespace hardware
232 }  // namespace android
233