Badhri Jagan Sridharan | b9f69ea | 2021-10-19 13:09:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "android.hardware.usb.aidl-service" |
| 18 | |
| 19 | #include <aidl/android/hardware/usb/PortRole.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/properties.h> |
| 22 | #include <android-base/strings.h> |
| 23 | #include <assert.h> |
| 24 | #include <dirent.h> |
| 25 | #include <pthread.h> |
| 26 | #include <stdio.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | #include <chrono> |
| 30 | #include <regex> |
| 31 | #include <thread> |
| 32 | #include <unordered_map> |
| 33 | |
| 34 | #include <cutils/uevent.h> |
| 35 | #include <sys/epoll.h> |
| 36 | #include <utils/Errors.h> |
| 37 | #include <utils/StrongPointer.h> |
| 38 | |
| 39 | #include "Usb.h" |
| 40 | |
| 41 | using android::base::GetProperty; |
| 42 | using android::base::Trim; |
| 43 | |
| 44 | namespace aidl { |
| 45 | namespace android { |
| 46 | namespace hardware { |
| 47 | namespace usb { |
| 48 | |
| 49 | constexpr char kTypecPath[] = "/sys/class/typec/"; |
| 50 | constexpr char kDataRoleNode[] = "/data_role"; |
| 51 | constexpr char kPowerRoleNode[] = "/power_role"; |
| 52 | |
| 53 | // Set by the signal handler to destroy the thread |
| 54 | volatile bool destroyThread; |
| 55 | |
| 56 | void queryVersionHelper(android::hardware::usb::Usb *usb, |
| 57 | std::vector<PortStatus> *currentPortStatus); |
| 58 | |
| 59 | ScopedAStatus Usb::enableUsbData(const string& in_portName, bool in_enable, int64_t in_transactionId) { |
| 60 | std::vector<PortStatus> currentPortStatus; |
| 61 | |
| 62 | pthread_mutex_lock(&mLock); |
| 63 | if (mCallback != NULL) { |
| 64 | ScopedAStatus ret = mCallback->notifyEnableUsbDataStatus( |
| 65 | in_portName, true, in_enable ? Status::SUCCESS : Status::ERROR, in_transactionId); |
| 66 | if (!ret.isOk()) |
| 67 | ALOGE("notifyEnableUsbDataStatus error %s", ret.getDescription().c_str()); |
| 68 | } else { |
| 69 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 70 | } |
| 71 | pthread_mutex_unlock(&mLock); |
| 72 | queryVersionHelper(this, ¤tPortStatus); |
| 73 | |
| 74 | return ScopedAStatus::ok(); |
| 75 | } |
| 76 | |
Badhri Jagan Sridharan | e745058 | 2021-12-27 03:42:19 -0800 | [diff] [blame] | 77 | ScopedAStatus Usb::enableUsbDataWhileDocked(const string& in_portName, int64_t in_transactionId) { |
| 78 | |
| 79 | pthread_mutex_lock(&mLock); |
| 80 | if (mCallback != NULL) { |
| 81 | ScopedAStatus ret = mCallback->notifyEnableUsbDataWhileDockedStatus( |
| 82 | in_portName, Status::NOT_SUPPORTED, in_transactionId); |
| 83 | if (!ret.isOk()) |
| 84 | ALOGE("notifyEnableUsbDataWhileDockedStatus error %s", ret.getDescription().c_str()); |
| 85 | } else { |
| 86 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 87 | } |
| 88 | pthread_mutex_unlock(&mLock); |
| 89 | |
| 90 | return ScopedAStatus::ok(); |
| 91 | } |
| 92 | |
Ricky Niu | 45131a7 | 2021-12-07 20:27:37 +0800 | [diff] [blame] | 93 | ScopedAStatus Usb::resetUsbPort(const string& in_portName, int64_t in_transactionId) { |
| 94 | |
| 95 | pthread_mutex_lock(&mLock); |
| 96 | if (mCallback != NULL) { |
| 97 | ScopedAStatus ret = mCallback->notifyResetUsbPortStatus( |
| 98 | in_portName, Status::NOT_SUPPORTED, in_transactionId); |
| 99 | if (!ret.isOk()) |
| 100 | ALOGE("notifyResetUsbPortStatus error %s", ret.getDescription().c_str()); |
| 101 | } else { |
| 102 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 103 | } |
| 104 | pthread_mutex_unlock(&mLock); |
| 105 | |
| 106 | return ScopedAStatus::ok(); |
| 107 | } |
| 108 | |
Badhri Jagan Sridharan | b9f69ea | 2021-10-19 13:09:44 -0700 | [diff] [blame] | 109 | Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus) { |
| 110 | string enabled, status, path, DetectedPath; |
| 111 | |
| 112 | for (int i = 0; i < currentPortStatus->size(); i++) { |
| 113 | (*currentPortStatus)[i].supportedContaminantProtectionModes |
| 114 | .push_back(ContaminantProtectionMode::NONE); |
| 115 | (*currentPortStatus)[i].contaminantProtectionStatus |
| 116 | = ContaminantProtectionStatus::NONE; |
| 117 | (*currentPortStatus)[i].contaminantDetectionStatus |
| 118 | = ContaminantDetectionStatus::NOT_SUPPORTED; |
| 119 | (*currentPortStatus)[i].supportsEnableContaminantPresenceDetection = false; |
| 120 | (*currentPortStatus)[i].supportsEnableContaminantPresenceProtection = false; |
| 121 | } |
| 122 | |
| 123 | return Status::SUCCESS; |
| 124 | } |
| 125 | |
| 126 | string appendRoleNodeHelper(const string &portName, PortRole::Tag tag) { |
| 127 | string node(kTypecPath + portName); |
| 128 | |
| 129 | switch (tag) { |
| 130 | case PortRole::dataRole: |
| 131 | return node + kDataRoleNode; |
| 132 | case PortRole::powerRole: |
| 133 | return node + kPowerRoleNode; |
| 134 | case PortRole::mode: |
| 135 | return node + "/port_type"; |
| 136 | default: |
| 137 | return ""; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | string convertRoletoString(PortRole role) { |
| 142 | if (role.getTag() == PortRole::powerRole) { |
| 143 | if (role.get<PortRole::powerRole>() == PortPowerRole::SOURCE) |
| 144 | return "source"; |
| 145 | else if (role.get<PortRole::powerRole>() == PortPowerRole::SINK) |
| 146 | return "sink"; |
| 147 | } else if (role.getTag() == PortRole::dataRole) { |
| 148 | if (role.get<PortRole::dataRole>() == PortDataRole::HOST) |
| 149 | return "host"; |
| 150 | if (role.get<PortRole::dataRole>() == PortDataRole::DEVICE) |
| 151 | return "device"; |
| 152 | } else if (role.getTag() == PortRole::mode) { |
| 153 | if (role.get<PortRole::mode>() == PortMode::UFP) |
| 154 | return "sink"; |
| 155 | if (role.get<PortRole::mode>() == PortMode::DFP) |
| 156 | return "source"; |
| 157 | } |
| 158 | return "none"; |
| 159 | } |
| 160 | |
| 161 | void extractRole(string *roleName) { |
| 162 | std::size_t first, last; |
| 163 | |
| 164 | first = roleName->find("["); |
| 165 | last = roleName->find("]"); |
| 166 | |
| 167 | if (first != string::npos && last != string::npos) { |
| 168 | *roleName = roleName->substr(first + 1, last - first - 1); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void switchToDrp(const string &portName) { |
| 173 | string filename = appendRoleNodeHelper(string(portName.c_str()), PortRole::mode); |
| 174 | FILE *fp; |
| 175 | |
| 176 | if (filename != "") { |
| 177 | fp = fopen(filename.c_str(), "w"); |
| 178 | if (fp != NULL) { |
| 179 | int ret = fputs("dual", fp); |
| 180 | fclose(fp); |
| 181 | if (ret == EOF) |
| 182 | ALOGE("Fatal: Error while switching back to drp"); |
| 183 | } else { |
| 184 | ALOGE("Fatal: Cannot open file to switch back to drp"); |
| 185 | } |
| 186 | } else { |
| 187 | ALOGE("Fatal: invalid node type"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | bool switchMode(const string &portName, const PortRole &in_role, struct Usb *usb) { |
| 192 | string filename = appendRoleNodeHelper(string(portName.c_str()), in_role.getTag()); |
| 193 | string written; |
| 194 | FILE *fp; |
| 195 | bool roleSwitch = false; |
| 196 | |
| 197 | if (filename == "") { |
| 198 | ALOGE("Fatal: invalid node type"); |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | fp = fopen(filename.c_str(), "w"); |
| 203 | if (fp != NULL) { |
| 204 | // Hold the lock here to prevent loosing connected signals |
| 205 | // as once the file is written the partner added signal |
| 206 | // can arrive anytime. |
| 207 | pthread_mutex_lock(&usb->mPartnerLock); |
| 208 | usb->mPartnerUp = false; |
| 209 | int ret = fputs(convertRoletoString(in_role).c_str(), fp); |
| 210 | fclose(fp); |
| 211 | |
| 212 | if (ret != EOF) { |
| 213 | struct timespec to; |
| 214 | struct timespec now; |
| 215 | |
| 216 | wait_again: |
| 217 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 218 | to.tv_sec = now.tv_sec + PORT_TYPE_TIMEOUT; |
| 219 | to.tv_nsec = now.tv_nsec; |
| 220 | |
| 221 | int err = pthread_cond_timedwait(&usb->mPartnerCV, &usb->mPartnerLock, &to); |
| 222 | // There are no uevent signals which implies role swap timed out. |
| 223 | if (err == ETIMEDOUT) { |
| 224 | ALOGI("uevents wait timedout"); |
| 225 | // Validity check. |
| 226 | } else if (!usb->mPartnerUp) { |
| 227 | goto wait_again; |
| 228 | // Role switch succeeded since usb->mPartnerUp is true. |
| 229 | } else { |
| 230 | roleSwitch = true; |
| 231 | } |
| 232 | } else { |
| 233 | ALOGI("Role switch failed while wrting to file"); |
| 234 | } |
| 235 | pthread_mutex_unlock(&usb->mPartnerLock); |
| 236 | } |
| 237 | |
| 238 | if (!roleSwitch) |
| 239 | switchToDrp(string(portName.c_str())); |
| 240 | |
| 241 | return roleSwitch; |
| 242 | } |
| 243 | |
| 244 | Usb::Usb() |
| 245 | : mLock(PTHREAD_MUTEX_INITIALIZER), |
| 246 | mRoleSwitchLock(PTHREAD_MUTEX_INITIALIZER), |
| 247 | mPartnerLock(PTHREAD_MUTEX_INITIALIZER), |
| 248 | mPartnerUp(false) |
| 249 | { |
| 250 | pthread_condattr_t attr; |
| 251 | if (pthread_condattr_init(&attr)) { |
| 252 | ALOGE("pthread_condattr_init failed: %s", strerror(errno)); |
| 253 | abort(); |
| 254 | } |
| 255 | if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) { |
| 256 | ALOGE("pthread_condattr_setclock failed: %s", strerror(errno)); |
| 257 | abort(); |
| 258 | } |
| 259 | if (pthread_cond_init(&mPartnerCV, &attr)) { |
| 260 | ALOGE("pthread_cond_init failed: %s", strerror(errno)); |
| 261 | abort(); |
| 262 | } |
| 263 | if (pthread_condattr_destroy(&attr)) { |
| 264 | ALOGE("pthread_condattr_destroy failed: %s", strerror(errno)); |
| 265 | abort(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | ScopedAStatus Usb::switchRole(const string& in_portName, |
| 270 | const PortRole& in_role, int64_t in_transactionId) { |
| 271 | string filename = appendRoleNodeHelper(string(in_portName.c_str()), in_role.getTag()); |
| 272 | string written; |
| 273 | FILE *fp; |
| 274 | bool roleSwitch = false; |
| 275 | |
| 276 | if (filename == "") { |
| 277 | ALOGE("Fatal: invalid node type"); |
| 278 | return ScopedAStatus::ok(); |
| 279 | } |
| 280 | |
| 281 | pthread_mutex_lock(&mRoleSwitchLock); |
| 282 | |
| 283 | ALOGI("filename write: %s role:%s", filename.c_str(), convertRoletoString(in_role).c_str()); |
| 284 | |
| 285 | if (in_role.getTag() == PortRole::mode) { |
| 286 | roleSwitch = switchMode(in_portName, in_role, this); |
| 287 | } else { |
| 288 | fp = fopen(filename.c_str(), "w"); |
| 289 | if (fp != NULL) { |
| 290 | int ret = fputs(convertRoletoString(in_role).c_str(), fp); |
| 291 | fclose(fp); |
| 292 | if ((ret != EOF) && ReadFileToString(filename, &written)) { |
| 293 | written = Trim(written); |
| 294 | extractRole(&written); |
| 295 | ALOGI("written: %s", written.c_str()); |
| 296 | if (written == convertRoletoString(in_role)) { |
| 297 | roleSwitch = true; |
| 298 | } else { |
| 299 | ALOGE("Role switch failed"); |
| 300 | } |
| 301 | } else { |
| 302 | ALOGE("failed to update the new role"); |
| 303 | } |
| 304 | } else { |
| 305 | ALOGE("fopen failed"); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | pthread_mutex_lock(&mLock); |
| 310 | if (mCallback != NULL) { |
| 311 | ScopedAStatus ret = mCallback->notifyRoleSwitchStatus( |
| 312 | in_portName, in_role, roleSwitch ? Status::SUCCESS : Status::ERROR, in_transactionId); |
| 313 | if (!ret.isOk()) |
| 314 | ALOGE("RoleSwitchStatus error %s", ret.getDescription().c_str()); |
| 315 | } else { |
| 316 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 317 | } |
| 318 | pthread_mutex_unlock(&mLock); |
| 319 | pthread_mutex_unlock(&mRoleSwitchLock); |
| 320 | |
| 321 | return ScopedAStatus::ok(); |
| 322 | } |
| 323 | |
Badhri Jagan Sridharan | 623f133 | 2021-11-25 09:35:21 -0800 | [diff] [blame] | 324 | ScopedAStatus Usb::limitPowerTransfer(const string& in_portName, bool /*in_limit*/, |
| 325 | int64_t in_transactionId) { |
| 326 | std::vector<PortStatus> currentPortStatus; |
| 327 | |
| 328 | pthread_mutex_lock(&mLock); |
| 329 | if (mCallback != NULL && in_transactionId >= 0) { |
| 330 | ScopedAStatus ret = mCallback->notifyLimitPowerTransferStatus( |
| 331 | in_portName, false, Status::NOT_SUPPORTED, in_transactionId); |
| 332 | if (!ret.isOk()) |
| 333 | ALOGE("limitPowerTransfer error %s", ret.getDescription().c_str()); |
| 334 | } else { |
| 335 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 336 | } |
| 337 | pthread_mutex_unlock(&mLock); |
| 338 | |
| 339 | return ScopedAStatus::ok(); |
| 340 | } |
| 341 | |
Badhri Jagan Sridharan | b9f69ea | 2021-10-19 13:09:44 -0700 | [diff] [blame] | 342 | Status getAccessoryConnected(const string &portName, string *accessory) { |
| 343 | string filename = kTypecPath + portName + "-partner/accessory_mode"; |
| 344 | |
| 345 | if (!ReadFileToString(filename, accessory)) { |
| 346 | ALOGE("getAccessoryConnected: Failed to open filesystem node: %s", filename.c_str()); |
| 347 | return Status::ERROR; |
| 348 | } |
| 349 | *accessory = Trim(*accessory); |
| 350 | |
| 351 | return Status::SUCCESS; |
| 352 | } |
| 353 | |
| 354 | Status getCurrentRoleHelper(const string &portName, bool connected, PortRole *currentRole) { |
| 355 | string filename; |
| 356 | string roleName; |
| 357 | string accessory; |
| 358 | |
| 359 | // Mode |
| 360 | |
| 361 | if (currentRole->getTag() == PortRole::powerRole) { |
| 362 | filename = kTypecPath + portName + kPowerRoleNode; |
| 363 | currentRole->set<PortRole::powerRole>(PortPowerRole::NONE); |
| 364 | } else if (currentRole->getTag() == PortRole::dataRole) { |
| 365 | filename = kTypecPath + portName + kDataRoleNode; |
| 366 | currentRole->set<PortRole::dataRole>(PortDataRole::NONE); |
| 367 | } else if (currentRole->getTag() == PortRole::mode) { |
| 368 | filename = kTypecPath + portName + kDataRoleNode; |
| 369 | currentRole->set<PortRole::mode>(PortMode::NONE); |
| 370 | } else { |
| 371 | return Status::ERROR; |
| 372 | } |
| 373 | |
| 374 | if (!connected) |
| 375 | return Status::SUCCESS; |
| 376 | |
| 377 | if (currentRole->getTag() == PortRole::mode) { |
| 378 | if (getAccessoryConnected(portName, &accessory) != Status::SUCCESS) { |
| 379 | return Status::ERROR; |
| 380 | } |
| 381 | if (accessory == "analog_audio") { |
| 382 | currentRole->set<PortRole::mode>(PortMode::AUDIO_ACCESSORY); |
| 383 | return Status::SUCCESS; |
| 384 | } else if (accessory == "debug") { |
| 385 | currentRole->set<PortRole::mode>(PortMode::DEBUG_ACCESSORY); |
| 386 | return Status::SUCCESS; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (!ReadFileToString(filename, &roleName)) { |
| 391 | ALOGE("getCurrentRole: Failed to open filesystem node: %s", filename.c_str()); |
| 392 | return Status::ERROR; |
| 393 | } |
| 394 | |
| 395 | roleName = Trim(roleName); |
| 396 | extractRole(&roleName); |
| 397 | |
| 398 | if (roleName == "source") { |
| 399 | currentRole->set<PortRole::powerRole>(PortPowerRole::SOURCE); |
| 400 | } else if (roleName == "sink") { |
| 401 | currentRole->set<PortRole::powerRole>(PortPowerRole::SINK); |
| 402 | } else if (roleName == "host") { |
| 403 | if (currentRole->getTag() == PortRole::dataRole) |
| 404 | currentRole->set<PortRole::dataRole>(PortDataRole::HOST); |
| 405 | else |
| 406 | currentRole->set<PortRole::mode>(PortMode::DFP); |
| 407 | } else if (roleName == "device") { |
| 408 | if (currentRole->getTag() == PortRole::dataRole) |
| 409 | currentRole->set<PortRole::dataRole>(PortDataRole::DEVICE); |
| 410 | else |
| 411 | currentRole->set<PortRole::mode>(PortMode::UFP); |
| 412 | } else if (roleName != "none") { |
| 413 | /* case for none has already been addressed. |
| 414 | * so we check if the role isn't none. |
| 415 | */ |
| 416 | return Status::UNRECOGNIZED_ROLE; |
| 417 | } |
| 418 | |
| 419 | return Status::SUCCESS; |
| 420 | } |
| 421 | |
| 422 | Status getTypeCPortNamesHelper(std::unordered_map<string, bool> *names) { |
| 423 | DIR *dp; |
| 424 | |
| 425 | dp = opendir(kTypecPath); |
| 426 | if (dp != NULL) { |
| 427 | struct dirent *ep; |
| 428 | |
| 429 | while ((ep = readdir(dp))) { |
| 430 | if (ep->d_type == DT_LNK) { |
| 431 | if (string::npos == string(ep->d_name).find("-partner")) { |
| 432 | std::unordered_map<string, bool>::const_iterator portName = |
| 433 | names->find(ep->d_name); |
| 434 | if (portName == names->end()) { |
| 435 | names->insert({ep->d_name, false}); |
| 436 | } |
| 437 | } else { |
| 438 | (*names)[std::strtok(ep->d_name, "-")] = true; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | closedir(dp); |
| 443 | return Status::SUCCESS; |
| 444 | } |
| 445 | |
| 446 | ALOGE("Failed to open /sys/class/typec"); |
| 447 | return Status::ERROR; |
| 448 | } |
| 449 | |
| 450 | bool canSwitchRoleHelper(const string &portName) { |
| 451 | string filename = kTypecPath + portName + "-partner/supports_usb_power_delivery"; |
| 452 | string supportsPD; |
| 453 | |
| 454 | if (ReadFileToString(filename, &supportsPD)) { |
| 455 | supportsPD = Trim(supportsPD); |
| 456 | if (supportsPD == "yes") { |
| 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | Status getPortStatusHelper(std::vector<PortStatus> *currentPortStatus) { |
| 465 | std::unordered_map<string, bool> names; |
| 466 | Status result = getTypeCPortNamesHelper(&names); |
| 467 | int i = -1; |
| 468 | |
| 469 | if (result == Status::SUCCESS) { |
| 470 | currentPortStatus->resize(names.size()); |
| 471 | for (std::pair<string, bool> port : names) { |
| 472 | i++; |
| 473 | ALOGI("%s", port.first.c_str()); |
| 474 | (*currentPortStatus)[i].portName = port.first; |
| 475 | |
| 476 | PortRole currentRole; |
| 477 | currentRole.set<PortRole::powerRole>(PortPowerRole::NONE); |
| 478 | if (getCurrentRoleHelper(port.first, port.second, ¤tRole) == Status::SUCCESS){ |
| 479 | (*currentPortStatus)[i].currentPowerRole = currentRole.get<PortRole::powerRole>(); |
| 480 | } else { |
| 481 | ALOGE("Error while retrieving portNames"); |
| 482 | goto done; |
| 483 | } |
| 484 | |
| 485 | currentRole.set<PortRole::dataRole>(PortDataRole::NONE); |
| 486 | if (getCurrentRoleHelper(port.first, port.second, ¤tRole) == Status::SUCCESS) { |
| 487 | (*currentPortStatus)[i].currentDataRole = currentRole.get<PortRole::dataRole>(); |
| 488 | } else { |
| 489 | ALOGE("Error while retrieving current port role"); |
| 490 | goto done; |
| 491 | } |
| 492 | |
| 493 | currentRole.set<PortRole::mode>(PortMode::NONE); |
| 494 | if (getCurrentRoleHelper(port.first, port.second, ¤tRole) == Status::SUCCESS) { |
| 495 | (*currentPortStatus)[i].currentMode = currentRole.get<PortRole::mode>(); |
| 496 | } else { |
| 497 | ALOGE("Error while retrieving current data role"); |
| 498 | goto done; |
| 499 | } |
| 500 | |
| 501 | (*currentPortStatus)[i].canChangeMode = true; |
| 502 | (*currentPortStatus)[i].canChangeDataRole = |
| 503 | port.second ? canSwitchRoleHelper(port.first) : false; |
| 504 | (*currentPortStatus)[i].canChangePowerRole = |
| 505 | port.second ? canSwitchRoleHelper(port.first) : false; |
| 506 | |
| 507 | (*currentPortStatus)[i].supportedModes.push_back(PortMode::DRP); |
Badhri Jagan Sridharan | e745058 | 2021-12-27 03:42:19 -0800 | [diff] [blame] | 508 | (*currentPortStatus)[i].usbDataStatus.push_back(UsbDataStatus::ENABLED); |
Badhri Jagan Sridharan | b9f69ea | 2021-10-19 13:09:44 -0700 | [diff] [blame] | 509 | |
| 510 | ALOGI("%d:%s connected:%d canChangeMode:%d canChagedata:%d canChangePower:%d " |
| 511 | "usbDataEnabled:%d", |
| 512 | i, port.first.c_str(), port.second, |
| 513 | (*currentPortStatus)[i].canChangeMode, |
| 514 | (*currentPortStatus)[i].canChangeDataRole, |
| 515 | (*currentPortStatus)[i].canChangePowerRole, 0); |
| 516 | } |
| 517 | |
| 518 | return Status::SUCCESS; |
| 519 | } |
| 520 | done: |
| 521 | return Status::ERROR; |
| 522 | } |
| 523 | |
| 524 | void queryVersionHelper(android::hardware::usb::Usb *usb, |
| 525 | std::vector<PortStatus> *currentPortStatus) { |
| 526 | Status status; |
| 527 | pthread_mutex_lock(&usb->mLock); |
| 528 | status = getPortStatusHelper(currentPortStatus); |
| 529 | queryMoistureDetectionStatus(currentPortStatus); |
| 530 | if (usb->mCallback != NULL) { |
| 531 | ScopedAStatus ret = usb->mCallback->notifyPortStatusChange(*currentPortStatus, |
| 532 | status); |
| 533 | if (!ret.isOk()) |
| 534 | ALOGE("queryPortStatus error %s", ret.getDescription().c_str()); |
| 535 | } else { |
| 536 | ALOGI("Notifying userspace skipped. Callback is NULL"); |
| 537 | } |
| 538 | pthread_mutex_unlock(&usb->mLock); |
| 539 | } |
| 540 | |
| 541 | ScopedAStatus Usb::queryPortStatus(int64_t in_transactionId) { |
| 542 | std::vector<PortStatus> currentPortStatus; |
| 543 | |
| 544 | queryVersionHelper(this, ¤tPortStatus); |
| 545 | pthread_mutex_lock(&mLock); |
| 546 | if (mCallback != NULL) { |
| 547 | ScopedAStatus ret = mCallback->notifyQueryPortStatus( |
| 548 | "all", Status::SUCCESS, in_transactionId); |
| 549 | if (!ret.isOk()) |
| 550 | ALOGE("notifyQueryPortStatus error %s", ret.getDescription().c_str()); |
| 551 | } else { |
| 552 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 553 | } |
| 554 | pthread_mutex_unlock(&mLock); |
| 555 | |
| 556 | return ScopedAStatus::ok(); |
| 557 | } |
| 558 | |
| 559 | ScopedAStatus Usb::enableContaminantPresenceDetection(const string& in_portName, |
| 560 | bool /*in_enable*/, int64_t in_transactionId) { |
| 561 | std::vector<PortStatus> currentPortStatus; |
| 562 | |
| 563 | pthread_mutex_lock(&mLock); |
| 564 | if (mCallback != NULL) { |
| 565 | ScopedAStatus ret = mCallback->notifyContaminantEnabledStatus( |
| 566 | in_portName, false, Status::ERROR, in_transactionId); |
| 567 | if (!ret.isOk()) |
| 568 | ALOGE("enableContaminantPresenceDetection error %s", ret.getDescription().c_str()); |
| 569 | } else { |
| 570 | ALOGE("Not notifying the userspace. Callback is not set"); |
| 571 | } |
| 572 | pthread_mutex_unlock(&mLock); |
| 573 | |
| 574 | queryVersionHelper(this, ¤tPortStatus); |
| 575 | return ScopedAStatus::ok(); |
| 576 | } |
| 577 | |
| 578 | |
| 579 | struct data { |
| 580 | int uevent_fd; |
| 581 | ::aidl::android::hardware::usb::Usb *usb; |
| 582 | }; |
| 583 | |
| 584 | static void uevent_event(uint32_t /*epevents*/, struct data *payload) { |
| 585 | char msg[UEVENT_MSG_LEN + 2]; |
| 586 | char *cp; |
| 587 | int n; |
| 588 | |
| 589 | n = uevent_kernel_multicast_recv(payload->uevent_fd, msg, UEVENT_MSG_LEN); |
| 590 | if (n <= 0) |
| 591 | return; |
| 592 | if (n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
| 593 | return; |
| 594 | |
| 595 | msg[n] = '\0'; |
| 596 | msg[n + 1] = '\0'; |
| 597 | cp = msg; |
| 598 | |
| 599 | while (*cp) { |
| 600 | if (std::regex_match(cp, std::regex("(add)(.*)(-partner)"))) { |
| 601 | ALOGI("partner added"); |
| 602 | pthread_mutex_lock(&payload->usb->mPartnerLock); |
| 603 | payload->usb->mPartnerUp = true; |
| 604 | pthread_cond_signal(&payload->usb->mPartnerCV); |
| 605 | pthread_mutex_unlock(&payload->usb->mPartnerLock); |
| 606 | } else if (!strncmp(cp, "DEVTYPE=typec_", strlen("DEVTYPE=typec_"))) { |
| 607 | std::vector<PortStatus> currentPortStatus; |
| 608 | queryVersionHelper(payload->usb, ¤tPortStatus); |
| 609 | |
| 610 | // Role switch is not in progress and port is in disconnected state |
| 611 | if (!pthread_mutex_trylock(&payload->usb->mRoleSwitchLock)) { |
| 612 | for (unsigned long i = 0; i < currentPortStatus.size(); i++) { |
| 613 | DIR *dp = |
| 614 | opendir(string(kTypecPath + |
| 615 | string(currentPortStatus[i].portName.c_str()) + |
| 616 | "-partner").c_str()); |
| 617 | if (dp == NULL) { |
| 618 | switchToDrp(currentPortStatus[i].portName); |
| 619 | } else { |
| 620 | closedir(dp); |
| 621 | } |
| 622 | } |
| 623 | pthread_mutex_unlock(&payload->usb->mRoleSwitchLock); |
| 624 | } |
| 625 | break; |
| 626 | } /* advance to after the next \0 */ |
| 627 | while (*cp++) { |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | void *work(void *param) { |
| 633 | int epoll_fd, uevent_fd; |
| 634 | struct epoll_event ev; |
| 635 | int nevents = 0; |
| 636 | struct data payload; |
| 637 | |
| 638 | uevent_fd = uevent_open_socket(UEVENT_MAX_EVENTS * UEVENT_MSG_LEN, true); |
| 639 | |
| 640 | if (uevent_fd < 0) { |
| 641 | ALOGE("uevent_init: uevent_open_socket failed\n"); |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | payload.uevent_fd = uevent_fd; |
| 646 | payload.usb = (::aidl::android::hardware::usb::Usb *)param; |
| 647 | |
| 648 | fcntl(uevent_fd, F_SETFL, O_NONBLOCK); |
| 649 | |
| 650 | ev.events = EPOLLIN; |
| 651 | ev.data.ptr = (void *)uevent_event; |
| 652 | |
| 653 | epoll_fd = epoll_create(UEVENT_MAX_EVENTS); |
| 654 | if (epoll_fd == -1) { |
| 655 | ALOGE("epoll_create failed; errno=%d", errno); |
| 656 | goto error; |
| 657 | } |
| 658 | |
| 659 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, uevent_fd, &ev) == -1) { |
| 660 | ALOGE("epoll_ctl failed; errno=%d", errno); |
| 661 | goto error; |
| 662 | } |
| 663 | |
| 664 | while (!destroyThread) { |
| 665 | struct epoll_event events[UEVENT_MAX_EVENTS]; |
| 666 | |
| 667 | nevents = epoll_wait(epoll_fd, events, UEVENT_MAX_EVENTS, -1); |
| 668 | if (nevents == -1) { |
| 669 | if (errno == EINTR) |
| 670 | continue; |
| 671 | ALOGE("usb epoll_wait failed; errno=%d", errno); |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | for (int n = 0; n < nevents; ++n) { |
| 676 | if (events[n].data.ptr) |
| 677 | (*(void (*)(int, struct data *payload))events[n].data.ptr)(events[n].events, |
| 678 | &payload); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | ALOGI("exiting worker thread"); |
| 683 | error: |
| 684 | close(uevent_fd); |
| 685 | |
| 686 | if (epoll_fd >= 0) |
| 687 | close(epoll_fd); |
| 688 | |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | void sighandler(int sig) { |
| 693 | if (sig == SIGUSR1) { |
| 694 | destroyThread = true; |
| 695 | ALOGI("destroy set"); |
| 696 | return; |
| 697 | } |
| 698 | signal(SIGUSR1, sighandler); |
| 699 | } |
| 700 | |
| 701 | ScopedAStatus Usb::setCallback( |
| 702 | const shared_ptr<IUsbCallback>& in_callback) { |
| 703 | |
| 704 | pthread_mutex_lock(&mLock); |
| 705 | if ((mCallback == NULL && in_callback == NULL) || |
| 706 | (mCallback != NULL && in_callback != NULL)) { |
| 707 | mCallback = in_callback; |
| 708 | pthread_mutex_unlock(&mLock); |
| 709 | return ScopedAStatus::ok(); |
| 710 | } |
| 711 | |
| 712 | mCallback = in_callback; |
| 713 | ALOGI("registering callback"); |
| 714 | |
| 715 | if (mCallback == NULL) { |
| 716 | if (!pthread_kill(mPoll, SIGUSR1)) { |
| 717 | pthread_join(mPoll, NULL); |
| 718 | ALOGI("pthread destroyed"); |
| 719 | } |
| 720 | pthread_mutex_unlock(&mLock); |
| 721 | return ScopedAStatus::ok(); |
| 722 | } |
| 723 | |
| 724 | destroyThread = false; |
| 725 | signal(SIGUSR1, sighandler); |
| 726 | |
| 727 | /* |
| 728 | * Create a background thread if the old callback value is NULL |
| 729 | * and being updated with a new value. |
| 730 | */ |
| 731 | if (pthread_create(&mPoll, NULL, work, this)) { |
| 732 | ALOGE("pthread creation failed %d", errno); |
| 733 | mCallback = NULL; |
| 734 | } |
| 735 | |
| 736 | pthread_mutex_unlock(&mLock); |
| 737 | return ScopedAStatus::ok(); |
| 738 | } |
| 739 | |
| 740 | } // namespace usb |
| 741 | } // namespace hardware |
| 742 | } // namespace android |
| 743 | } // aidl |