if (ret != EOF) {
struct timespec to;
- struct timeval tp;
+ struct timespec now;
wait_again:
- gettimeofday(&tp, NULL);
- to.tv_sec = tp.tv_sec + PORT_TYPE_TIMEOUT;
- to.tv_nsec = tp.tv_usec * 1000;;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ to.tv_sec = now.tv_sec + PORT_TYPE_TIMEOUT;
+ to.tv_nsec = now.tv_nsec;
int err = pthread_cond_timedwait(&usb->mPartnerCV, &usb->mPartnerLock, &to);
// There are no uevent signals which implies role swap timed out.
return roleSwitch;
}
+Usb::Usb()
+ : mLock(PTHREAD_MUTEX_INITIALIZER),
+ mRoleSwitchLock(PTHREAD_MUTEX_INITIALIZER),
+ mPartnerLock(PTHREAD_MUTEX_INITIALIZER),
+ mPartnerUp(false) {
+ pthread_condattr_t attr;
+ if (pthread_condattr_init(&attr)) {
+ ALOGE("pthread_condattr_init failed: %s", strerror(errno));
+ abort();
+ }
+ if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) {
+ ALOGE("pthread_condattr_setclock failed: %s", strerror(errno));
+ abort();
+ }
+ if (pthread_cond_init(&mPartnerCV, &attr)) {
+ ALOGE("pthread_cond_init failed: %s", strerror(errno));
+ abort();
+ }
+ if (pthread_condattr_destroy(&attr)) {
+ ALOGE("pthread_condattr_destroy failed: %s", strerror(errno));
+ abort();
+ }
+}
Return<void> Usb::switchRole(const hidl_string &portName,
using ::android::sp;
struct Usb : public IUsb {
+ Usb();
+
Return<void> switchRole(const hidl_string& portName, const PortRole& role) override;
Return<void> setCallback(const sp<V1_0::IUsbCallback>& callback) override;
Return<void> queryPortStatus() override;
sp<V1_0::IUsbCallback> mCallback_1_0;
// Protects mCallback variable
- pthread_mutex_t mLock = PTHREAD_MUTEX_INITIALIZER;
+ pthread_mutex_t mLock;
// Protects roleSwitch operation
- pthread_mutex_t mRoleSwitchLock = PTHREAD_MUTEX_INITIALIZER;
+ pthread_mutex_t mRoleSwitchLock;
// Threads waiting for the partner to come back wait here
- pthread_cond_t mPartnerCV = PTHREAD_COND_INITIALIZER;
+ pthread_cond_t mPartnerCV;
// lock protecting mPartnerCV
- pthread_mutex_t mPartnerLock = PTHREAD_MUTEX_INITIALIZER;
+ pthread_mutex_t mPartnerLock;
// Variable to signal partner coming back online after type switch
bool mPartnerUp;