aasdk/src/Transport/USBTransport.cpp

95 lines
3.0 KiB
C++
Raw Normal View History

2018-02-11 20:36:55 +01:00
/*
* This file is part of aasdk library project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* 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/>.
*/
2020-05-13 04:55:03 +02:00
#include <aasdk/Transport/USBTransport.hpp>
2018-02-11 20:36:55 +01:00
namespace aasdk
{
namespace transport
{
USBTransport::USBTransport(boost::asio::io_service& ioService, usb::IAOAPDevice::Pointer aoapDevice)
2018-03-15 17:59:18 +01:00
: Transport(ioService)
, aoapDevice_(std::move(aoapDevice))
2018-02-11 20:36:55 +01:00
{}
2018-03-15 17:59:18 +01:00
void USBTransport::enqueueReceive(common::DataBuffer buffer)
2018-02-11 20:36:55 +01:00
{
2018-03-15 17:59:18 +01:00
auto usbEndpointPromise = usb::IUSBEndpoint::Promise::defer(receiveStrand_);
usbEndpointPromise->then([this, self = this->shared_from_this()](auto bytesTransferred) {
this->receiveHandler(bytesTransferred);
},
[this, self = this->shared_from_this()](auto e) {
this->rejectReceivePromises(e);
});
2018-02-11 20:36:55 +01:00
2018-03-15 17:59:18 +01:00
aoapDevice_->getInEndpoint().bulkTransfer(buffer, cReceiveTimeoutMs, std::move(usbEndpointPromise));
}
void USBTransport::enqueueSend(SendQueue::iterator queueElement)
{
this->doSend(queueElement, 0);
2018-02-11 20:36:55 +01:00
}
2018-03-15 17:59:18 +01:00
void USBTransport::doSend(SendQueue::iterator queueElement, common::Data::size_type offset)
2018-02-11 20:36:55 +01:00
{
auto usbEndpointPromise = usb::IUSBEndpoint::Promise::defer(sendStrand_);
2018-03-15 17:59:18 +01:00
usbEndpointPromise->then([this, self = this->shared_from_this(), queueElement, offset](size_t bytesTransferred) mutable {
this->sendHandler(queueElement, offset, bytesTransferred);
2018-02-11 20:36:55 +01:00
},
2018-03-15 17:59:18 +01:00
[this, self = this->shared_from_this(), queueElement](const error::Error& e) mutable {
queueElement->second->reject(e);
sendQueue_.erase(queueElement);
2018-02-11 20:36:55 +01:00
if(!sendQueue_.empty())
{
this->doSend(sendQueue_.begin(), 0);
}
});
2018-03-15 17:59:18 +01:00
aoapDevice_->getOutEndpoint().bulkTransfer(common::DataBuffer(queueElement->first, offset), cSendTimeoutMs, std::move(usbEndpointPromise));
2018-02-11 20:36:55 +01:00
}
2018-03-15 17:59:18 +01:00
void USBTransport::sendHandler(SendQueue::iterator queueElement, common::Data::size_type offset, size_t bytesTransferred)
2018-02-11 20:36:55 +01:00
{
2018-03-15 17:59:18 +01:00
if(offset + bytesTransferred < queueElement->first.size())
2018-02-11 20:36:55 +01:00
{
2018-03-15 17:59:18 +01:00
this->doSend(queueElement, offset + bytesTransferred);
2018-02-11 20:36:55 +01:00
}
else
{
2018-03-15 17:59:18 +01:00
queueElement->second->resolve();
sendQueue_.erase(queueElement);
2018-02-11 20:36:55 +01:00
if(!sendQueue_.empty())
{
this->doSend(sendQueue_.begin(), 0);
}
}
}
void USBTransport::stop()
{
aoapDevice_->getInEndpoint().cancelTransfers();
aoapDevice_->getOutEndpoint().cancelTransfers();
}
}
}