forked from f1xpl/aasdk
-
Notifications
You must be signed in to change notification settings - Fork 47
USBTransport_8cpp
github-actions edited this page Mar 15, 2026
·
3 revisions
title: src/Transport/USBTransport.cpp
| Name |
|---|
| aasdk |
| aasdk::transport |
// This file is part of aasdk library project.
// Copyright (C) 2018 f1x.studio (Michal Szwaj)
// Copyright (C) 2024 CubeOne (Simon Dean - simon.dean@cubeone.co.uk)
// Copyright (C) 2026 OpenCarDev (Matthew Hilton - matthilton2005@gmail.com)
//
// aasdk is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// aasdk is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with aasdk. If not, see <http://www.gnu.org/licenses/>.
#include <aasdk/Transport/USBTransport.hpp>
#include <aasdk/Common/Log.hpp>
namespace aasdk {
namespace transport {
USBTransport::USBTransport(boost::asio::io_service &ioService, usb::IAOAPDevice::Pointer aoapDevice)
: Transport(ioService), aoapDevice_(std::move(aoapDevice)) {}
void USBTransport::enqueueReceive(common::DataBuffer buffer) {
const auto inEndpoint = aoapDevice_->getInEndpoint().getAddress();
AASDK_LOG(debug) << "[USBTransport] enqueueReceive endpoint=0x" << std::hex
<< static_cast<int>(inEndpoint) << std::dec
<< " requestedBytes=" << buffer.size;
auto usbEndpointPromise = usb::IUSBEndpoint::Promise::defer(receiveStrand_);
usbEndpointPromise->then([this, self = this->shared_from_this(), inEndpoint](auto bytesTransferred) {
AASDK_LOG(debug) << "[USBTransport] receiveComplete endpoint=0x"
<< std::hex << static_cast<int>(inEndpoint)
<< std::dec << " bytesTransferred=" << bytesTransferred;
this->receiveHandler(bytesTransferred);
},
[this, self = this->shared_from_this(), inEndpoint](auto e) {
AASDK_LOG(warning) << "[USBTransport] receiveError endpoint=0x"
<< std::hex << static_cast<int>(inEndpoint)
<< std::dec << " code=" << static_cast<int>(e.getCode())
<< " native=" << e.getNativeCode()
<< " what=" << e.what();
this->rejectReceivePromises(e);
});
aoapDevice_->getInEndpoint().bulkTransfer(buffer, cReceiveTimeoutMs, std::move(usbEndpointPromise));
}
void USBTransport::enqueueSend(SendQueue::iterator queueElement) {
this->doSend(queueElement, 0);
}
void USBTransport::doSend(SendQueue::iterator queueElement, common::Data::size_type offset) {
const auto outEndpoint = aoapDevice_->getOutEndpoint().getAddress();
const auto remainingBytes = queueElement->first.size() - offset;
AASDK_LOG(debug) << "[USBTransport] doSend endpoint=0x" << std::hex
<< static_cast<int>(outEndpoint) << std::dec
<< " offset=" << offset
<< " remainingBytes=" << remainingBytes
<< " totalMessageBytes=" << queueElement->first.size();
auto usbEndpointPromise = usb::IUSBEndpoint::Promise::defer(sendStrand_);
usbEndpointPromise->then(
[this, self = this->shared_from_this(), queueElement, offset, outEndpoint](size_t bytesTransferred) mutable {
AASDK_LOG(debug) << "[USBTransport] sendComplete endpoint=0x" << std::hex
<< static_cast<int>(outEndpoint) << std::dec
<< " offset=" << offset
<< " bytesTransferred=" << bytesTransferred
<< " totalMessageBytes=" << queueElement->first.size();
this->sendHandler(queueElement, offset, bytesTransferred);
},
[this, self = this->shared_from_this(), queueElement, offset, outEndpoint](const error::Error &e) mutable {
AASDK_LOG(warning) << "[USBTransport] sendError endpoint=0x" << std::hex
<< static_cast<int>(outEndpoint) << std::dec
<< " offset=" << offset
<< " totalMessageBytes=" << queueElement->first.size()
<< " code=" << static_cast<int>(e.getCode())
<< " native=" << e.getNativeCode()
<< " what=" << e.what();
queueElement->second->reject(e);
sendQueue_.erase(queueElement);
if (!sendQueue_.empty()) {
this->doSend(sendQueue_.begin(), 0);
}
});
aoapDevice_->getOutEndpoint().bulkTransfer(common::DataBuffer(queueElement->first, offset), cSendTimeoutMs,
std::move(usbEndpointPromise));
}
void USBTransport::sendHandler(SendQueue::iterator queueElement, common::Data::size_type offset,
size_t bytesTransferred) {
if (offset + bytesTransferred < queueElement->first.size()) {
this->doSend(queueElement, offset + bytesTransferred);
} else {
queueElement->second->resolve();
sendQueue_.erase(queueElement);
if (!sendQueue_.empty()) {
this->doSend(sendQueue_.begin(), 0);
}
}
}
void USBTransport::stop() {
aoapDevice_->getInEndpoint().cancelTransfers();
aoapDevice_->getOutEndpoint().cancelTransfers();
}
}
}Updated on 2026-03-15 at 09:02:41 +0000