| 1 |
/* |
| 2 |
* Device.cpp |
| 3 |
* Copyright (C) 2022 pulkomandy <pulkomandy@shredder> |
| 4 |
* |
| 5 |
* Distributed under terms of the MIT license. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "Device.h" |
| 9 |
|
| 10 |
|
| 11 |
namespace mtp { namespace usb |
| 12 |
{ |
| 13 |
|
| 14 |
Device::Device(ContextPtr ctx, BUSBDevice* handle) |
| 15 |
: _context(ctx) |
| 16 |
, _handle(handle) |
| 17 |
{ |
| 18 |
} |
| 19 |
|
| 20 |
Device::~Device() |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
void Device::Reset() |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
void Device::ReadControl(u8 type, u8 req, u16 value, u16 index, ByteArray &data, int timeout) |
| 29 |
{ |
| 30 |
fprintf(stderr, "Control read(type %x req %x value %x index %d size %lu) with timeout %d…", |
| 31 |
type, req, value, index, data.size(), timeout); |
| 32 |
ssize_t result = _handle->ControlTransfer(type, req, value, index, data.size(), const_cast<u8*>(data.data())); |
| 33 |
if (result >= 0) |
| 34 |
data.resize(result); |
| 35 |
else |
| 36 |
throw std::runtime_error("read fail"); |
| 37 |
fprintf(stderr, " complete (%ld).\n", result); |
| 38 |
} |
| 39 |
|
| 40 |
void Device::WriteControl(u8 type, u8 req, u16 value, u16 index, const ByteArray &data, int timeout) |
| 41 |
{ |
| 42 |
fprintf(stderr, "Control write with timeout %d\n", timeout); |
| 43 |
_handle->ControlTransfer(USB_REQTYPE_DEVICE_IN, req, value, index, data.size(), const_cast<u8*>(data.data())); |
| 44 |
} |
| 45 |
|
| 46 |
void Device::SetConfiguration(int idx) |
| 47 |
{ |
| 48 |
_handle->SetConfiguration(_handle->ConfigurationAt(idx)); |
| 49 |
} |
| 50 |
|
| 51 |
void Device::WriteBulk(const EndpointPtr & ep, const IObjectInputStreamPtr &inputStream, int timeout) |
| 52 |
{ |
| 53 |
ByteArray data(inputStream->GetSize()); |
| 54 |
inputStream->Read(data.data(), data.size()); |
| 55 |
ssize_t tr = ep->_endpoint.BulkTransfer(data.data(), data.size()); |
| 56 |
if (tr != (int)data.size()) |
| 57 |
throw std::runtime_error("short write"); |
| 58 |
} |
| 59 |
|
| 60 |
void Device::ReadBulk(const EndpointPtr & ep, const IObjectOutputStreamPtr &outputStream, int timeout) |
| 61 |
{ |
| 62 |
ByteArray data(ep->GetMaxPacketSize()); |
| 63 |
ssize_t tr; |
| 64 |
{ |
| 65 |
tr = ep->_endpoint.BulkTransfer(data.data(), data.size()); |
| 66 |
outputStream->Write(data.data(), tr); |
| 67 |
} |
| 68 |
while(tr == (int)data.size()); |
| 69 |
} |
| 70 |
|
| 71 |
void Device::ClearHalt(const EndpointPtr & ep) |
| 72 |
{ |
| 73 |
ep->_endpoint.ClearStall(); |
| 74 |
} |
| 75 |
|
| 76 |
InterfaceTokenPtr Device::ClaimInterface(const InterfacePtr & interface) |
| 77 |
{ |
| 78 |
return InterfaceTokenPtr(); |
| 79 |
} |
| 80 |
|
| 81 |
}} |