Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e18ac38 authored by Josh Gao's avatar Josh Gao
Browse files

Reland "adb: turn CHECKs into an error + transport restart."

This reverts commit 2547f740.

Bug: http://b/134695864
Bug: http://b/133872605
Test: manually unplugged/replugged
Change-Id: Ic2af40b81354138a7842eb93aacc303885ac952e
(cherry picked from commit 7b304844)
parent d9684d59
Loading
Loading
Loading
Loading
+20 −7
Original line number Original line Diff line number Diff line
@@ -528,14 +528,16 @@ struct UsbFfsConnection : public Connection {
            }
            }


            if (id.direction == TransferDirection::READ) {
            if (id.direction == TransferDirection::READ) {
                HandleRead(id, event.res);
                if (!HandleRead(id, event.res)) {
                    return;
                }
            } else {
            } else {
                HandleWrite(id);
                HandleWrite(id);
            }
            }
        }
        }
    }
    }


    void HandleRead(TransferId id, int64_t size) {
    bool HandleRead(TransferId id, int64_t size) {
        uint64_t read_idx = id.id % kUsbReadQueueDepth;
        uint64_t read_idx = id.id % kUsbReadQueueDepth;
        IoReadBlock* block = &read_requests_[read_idx];
        IoReadBlock* block = &read_requests_[read_idx];
        block->pending = false;
        block->pending = false;
@@ -545,7 +547,7 @@ struct UsbFfsConnection : public Connection {
        if (block->id().id != needed_read_id_) {
        if (block->id().id != needed_read_id_) {
            LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for "
            LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for "
                         << needed_read_id_;
                         << needed_read_id_;
            return;
            return true;
        }
        }


        for (uint64_t id = needed_read_id_;; ++id) {
        for (uint64_t id = needed_read_id_;; ++id) {
@@ -554,15 +556,22 @@ struct UsbFfsConnection : public Connection {
            if (current_block->pending) {
            if (current_block->pending) {
                break;
                break;
            }
            }
            ProcessRead(current_block);
            if (!ProcessRead(current_block)) {
                return false;
            }
            ++needed_read_id_;
            ++needed_read_id_;
        }
        }

        return true;
    }
    }


    void ProcessRead(IoReadBlock* block) {
    bool ProcessRead(IoReadBlock* block) {
        if (!block->payload.empty()) {
        if (!block->payload.empty()) {
            if (!incoming_header_.has_value()) {
            if (!incoming_header_.has_value()) {
                CHECK_EQ(sizeof(amessage), block->payload.size());
                if (block->payload.size() != sizeof(amessage)) {
                    HandleError("received packet of unexpected length while reading header");
                    return false;
                }
                amessage& msg = incoming_header_.emplace();
                amessage& msg = incoming_header_.emplace();
                memcpy(&msg, block->payload.data(), sizeof(msg));
                memcpy(&msg, block->payload.data(), sizeof(msg));
                LOG(DEBUG) << "USB read:" << dump_header(&msg);
                LOG(DEBUG) << "USB read:" << dump_header(&msg);
@@ -570,7 +579,10 @@ struct UsbFfsConnection : public Connection {
            } else {
            } else {
                size_t bytes_left = incoming_header_->data_length - incoming_payload_.size();
                size_t bytes_left = incoming_header_->data_length - incoming_payload_.size();
                Block payload = std::move(block->payload);
                Block payload = std::move(block->payload);
                CHECK_LE(payload.size(), bytes_left);
                if (block->payload.size() > bytes_left) {
                    HandleError("received too many bytes while waiting for payload");
                    return false;
                }
                incoming_payload_.append(std::move(payload));
                incoming_payload_.append(std::move(payload));
            }
            }


@@ -593,6 +605,7 @@ struct UsbFfsConnection : public Connection {


        PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
        PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
        SubmitRead(block);
        SubmitRead(block);
        return true;
    }
    }


    bool SubmitRead(IoReadBlock* block) {
    bool SubmitRead(IoReadBlock* block) {