Search in sources :

Example 1 with RobotUsbStuckUsbWriteException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException in project robotcode by OutoftheBoxFTC.

the class MonitoredUsbDeviceConnection method initializeMonitoring.

protected void initializeMonitoring() {
    // We allocate ahead of time rather than on each call in order to eash
    // pressure on the garbage collector.
    bulkTransferAction = new Callable<RobotUsbException>() {

        @Override
        public RobotUsbException call() {
            callResult = delegate.bulkTransfer(endpoint, buffer, offset, length, timeout);
            return null;
        }
    };
    controlTransferAction = new Callable<RobotUsbException>() {

        @Override
        public RobotUsbException call() {
            callResult = delegate.controlTransfer(requestType, request, value, index, buffer, offset, length, timeout);
            ;
            return null;
        }
    };
    failureAction = new Callable<RobotUsbException>() {

        @Override
        public RobotUsbException call() {
            // We close the device in the hopes of waking up the call. We return a
            // distinguished exception so that folks can in fact KNOW we closed the device. We
            // also set up so that a read that detects the closed device will return the same sort
            // of 'please try again' error rather than its usual 'i give up'.
            // 
            // Note: closing in this way *does* seem to unstick the call. However, we've rarely/never
            // been able to successfully re-open after that, even after waiting huge amounts of
            // time (like a second). Rather, we get stuck in trying to set the baud rate, inside of
            // a different native call (UsbDeviceConnection.native_control_request).
            // 
            Thread monitoredThread = monitor.getMonitoredThread();
            String threadMessage = monitoredThread == null ? "" : String.format(" threadId=%d TID=%d:", monitoredThread.getId(), ThreadPool.getTID(monitoredThread));
            String failureMessage;
            switch(failureType) {
                default:
                    failureMessage = "unknown failure";
                    break;
                case WRITE:
                    failureMessage = String.format("write(%d bytes)", length);
                    break;
                case CONTROL_TRANSFER:
                    failureMessage = String.format("control(%d bytes)", length);
                    break;
            }
            RobotLog.ee(TAG, "watchdog: stuck USB %s%s: serial=%s closing device", failureMessage, threadMessage, serialNumber, threadMessage);
            RobotUsbException deviceClosedReason = new RobotUsbStuckUsbWriteException(delegate, "watchdog: stuck USB %s: closed %s", failureMessage, serialNumber);
            ftDevice.setDeviceClosedReason(deviceClosedReason);
            // XYZZY
            ftDevice.close();
            return deviceClosedReason;
        }
    };
}
Also used : RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) RobotUsbStuckUsbWriteException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException)

Aggregations

RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)1 RobotUsbStuckUsbWriteException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException)1