Massive refactor to accommodate RISC-V targets
- Refactored entire codebase (excluding the Insight component) to accommodate multiple target architectures (no longer specific to AVR) - Deleted 'generate SVD' GDB monitor command - I will eventually move this functionality to the Bloom website - Added unit size property to address spaces - Many other changes which I couldn't be bothered to describe here
This commit is contained in:
@@ -29,7 +29,7 @@ namespace Usb
|
||||
Logger::debug("HID device path: " + hidInterfacePath);
|
||||
|
||||
if ((hidDevice = ::hid_open_path(hidInterfacePath.c_str())) == nullptr) {
|
||||
throw DeviceInitializationFailure("Failed to open HID device via hidapi.");
|
||||
throw DeviceInitializationFailure{"Failed to open HID device via hidapi."};
|
||||
}
|
||||
|
||||
this->hidDevice.reset(hidDevice);
|
||||
@@ -41,11 +41,11 @@ namespace Usb
|
||||
}
|
||||
|
||||
std::vector<unsigned char> HidInterface::read(std::optional<std::chrono::milliseconds> timeout) {
|
||||
auto output = std::vector<unsigned char>();
|
||||
auto output = std::vector<unsigned char>{};
|
||||
|
||||
const auto readSize = this->inputReportSize;
|
||||
auto transferredByteCount = int(0);
|
||||
auto totalByteCount = std::size_t(0);
|
||||
auto totalByteCount = std::size_t{0};
|
||||
|
||||
do {
|
||||
output.resize(totalByteCount + readSize, 0x00);
|
||||
@@ -58,12 +58,12 @@ namespace Usb
|
||||
);
|
||||
|
||||
if (transferredByteCount == -1) {
|
||||
throw DeviceCommunicationFailure("Failed to read from HID device.");
|
||||
throw DeviceCommunicationFailure{"Failed to read from HID device."};
|
||||
}
|
||||
|
||||
if (totalByteCount == 0) {
|
||||
// After the first read, set the timeout to 1 millisecond, as we don't want to wait for subsequent data
|
||||
timeout = std::chrono::milliseconds(1);
|
||||
timeout = std::chrono::milliseconds{1};
|
||||
}
|
||||
|
||||
totalByteCount += static_cast<std::size_t>(transferredByteCount);
|
||||
@@ -76,9 +76,7 @@ namespace Usb
|
||||
|
||||
void HidInterface::write(std::vector<unsigned char>&& buffer) {
|
||||
if (buffer.size() > this->inputReportSize) {
|
||||
throw DeviceCommunicationFailure(
|
||||
"Cannot send data via HID interface - data exceeds maximum packet size."
|
||||
);
|
||||
throw DeviceCommunicationFailure{"Cannot send data via HID interface - data exceeds maximum packet size."};
|
||||
}
|
||||
|
||||
if (buffer.size() < this->inputReportSize) {
|
||||
@@ -93,9 +91,11 @@ namespace Usb
|
||||
const auto length = buffer.size();
|
||||
|
||||
if ((transferred = ::hid_write(this->hidDevice.get(), buffer.data(), length)) != length) {
|
||||
Logger::debug("Attempted to write " + std::to_string(length)
|
||||
+ " bytes to HID interface. Bytes written: " + std::to_string(transferred));
|
||||
throw DeviceCommunicationFailure("Failed to write data to HID interface.");
|
||||
Logger::debug(
|
||||
"Attempted to write " + std::to_string(length)
|
||||
+ " bytes to HID interface. Bytes written: " + std::to_string(transferred)
|
||||
);
|
||||
throw DeviceCommunicationFailure{"Failed to write data to HID interface."};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ namespace Usb
|
||||
::hid_free_enumeration
|
||||
);
|
||||
|
||||
auto matchedDevice = std::optional<::hid_device_info*>();
|
||||
auto matchedDevice = std::optional<::hid_device_info*>{};
|
||||
|
||||
auto* hidDeviceInfo = hidDeviceInfoList.get();
|
||||
while (hidDeviceInfo != nullptr) {
|
||||
@@ -118,9 +118,9 @@ namespace Usb
|
||||
}
|
||||
|
||||
if (!matchedDevice.has_value()) {
|
||||
throw DeviceInitializationFailure("Failed to match interface number with HID interface.");
|
||||
throw DeviceInitializationFailure{"Failed to match interface number with HID interface."};
|
||||
}
|
||||
|
||||
return std::string(matchedDevice.value()->path);
|
||||
return {matchedDevice.value()->path};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace Usb
|
||||
private:
|
||||
using HidDevice = std::unique_ptr<::hid_device, decltype(&::hid_close)>;
|
||||
|
||||
HidDevice hidDevice = HidDevice(nullptr, ::hid_close);
|
||||
HidDevice hidDevice = {nullptr, ::hid_close};
|
||||
|
||||
std::uint16_t vendorId = 0;
|
||||
std::uint16_t productId = 0;
|
||||
|
||||
@@ -30,18 +30,18 @@ namespace Usb
|
||||
auto devices = this->findMatchingDevices(this->vendorId, this->productId);
|
||||
|
||||
if (devices.empty()) {
|
||||
throw DeviceNotFound(
|
||||
throw DeviceNotFound{
|
||||
"Failed to find USB device with matching vendor and product ID. Please examine the debug tool's USB "
|
||||
"connection, as well as the selected environment's debug tool configuration, in bloom.yaml"
|
||||
);
|
||||
"connection, as well as the selected environment's debug tool configuration, in bloom.yaml"
|
||||
};
|
||||
}
|
||||
|
||||
if (devices.size() > 1) {
|
||||
// TODO: implement support for multiple devices via serial number matching?
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Numerous devices of matching vendor and product ID found.\n"
|
||||
"Please ensure that only one debug tool is connected and then try again."
|
||||
);
|
||||
"Please ensure that only one debug tool is connected and then try again."
|
||||
};
|
||||
}
|
||||
|
||||
// For now, just use the first device found.
|
||||
@@ -51,9 +51,9 @@ namespace Usb
|
||||
const int libusbStatusCode = ::libusb_open(this->libusbDevice.get(), &deviceHandle);
|
||||
|
||||
if (libusbStatusCode < 0) {
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Failed to open USB device - error code " + std::to_string(libusbStatusCode) + " returned."
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
this->libusbDeviceHandle.reset(deviceHandle);
|
||||
@@ -65,12 +65,12 @@ namespace Usb
|
||||
|
||||
auto statusCode = ::libusb_get_device_descriptor(this->libusbDevice.get(), &desc);
|
||||
if (statusCode != 0) {
|
||||
throw DeviceCommunicationFailure(
|
||||
throw DeviceCommunicationFailure{
|
||||
"Failed to retrieve USB device descriptor - status code: " + std::to_string(statusCode)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
auto data = std::array<unsigned char, 256>();
|
||||
auto data = std::array<unsigned char, 256>{};
|
||||
const auto transferredBytes = ::libusb_get_string_descriptor_ascii(
|
||||
this->libusbDeviceHandle.get(),
|
||||
desc.iSerialNumber,
|
||||
@@ -79,12 +79,12 @@ namespace Usb
|
||||
);
|
||||
|
||||
if (transferredBytes <= 0) {
|
||||
throw DeviceCommunicationFailure(
|
||||
throw DeviceCommunicationFailure{
|
||||
"Failed to retrieve serial number from USB device - status code: " + std::to_string(transferredBytes)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
return std::string(data.begin(), data.begin() + transferredBytes);
|
||||
return {data.begin(), data.begin() + transferredBytes};
|
||||
}
|
||||
|
||||
void UsbDevice::setConfiguration(std::uint8_t configurationIndex) {
|
||||
@@ -96,9 +96,9 @@ namespace Usb
|
||||
);
|
||||
|
||||
if (libusbStatusCode < 0) {
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Failed to set USB configuration - error code " + std::to_string(libusbStatusCode) + " returned."
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ namespace Usb
|
||||
}
|
||||
}
|
||||
|
||||
throw DeviceInitializationFailure("Failed to obtain address of USB endpoint");
|
||||
throw DeviceInitializationFailure{"Failed to obtain address of USB endpoint"};
|
||||
}
|
||||
|
||||
std::uint16_t UsbDevice::getEndpointMaxPacketSize(std::uint8_t endpointAddress) {
|
||||
@@ -142,11 +142,11 @@ namespace Usb
|
||||
}
|
||||
}
|
||||
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Failed to obtain maximum packet size of USB endpoint (address: 0x"
|
||||
+ Services::StringService::toHex(endpointAddress) + "). Endpoint not found. Selected configuration "
|
||||
"value (" + std::to_string(activeConfigDescriptor->bConfigurationValue) + ")"
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<LibusbDevice> UsbDevice::findMatchingDevices(std::uint16_t vendorId, std::uint16_t productId) {
|
||||
@@ -156,9 +156,9 @@ namespace Usb
|
||||
|
||||
auto libusbStatusCode = ::libusb_get_device_list(UsbDevice::libusbContext.get(), &devices);
|
||||
if (libusbStatusCode < 0) {
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Failed to retrieve USB devices - return code: '" + std::to_string(libusbStatusCode) + "'"
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
ssize_t i = 0;
|
||||
@@ -167,8 +167,10 @@ namespace Usb
|
||||
struct ::libusb_device_descriptor desc = {};
|
||||
|
||||
if ((libusbStatusCode = ::libusb_get_device_descriptor(device, &desc)) < 0) {
|
||||
Logger::warning("Failed to retrieve USB device descriptor - return code: '"
|
||||
+ std::to_string(libusbStatusCode) + "'");
|
||||
Logger::warning(
|
||||
"Failed to retrieve USB device descriptor - return code: '"
|
||||
+ std::to_string(libusbStatusCode) + "'"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -191,16 +193,13 @@ namespace Usb
|
||||
: ::libusb_get_active_config_descriptor(this->libusbDevice.get(), &configDescriptor);
|
||||
|
||||
if (libusbStatusCode < 0) {
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Failed to obtain USB configuration descriptor - error code " + std::to_string(libusbStatusCode)
|
||||
+ " returned."
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
return LibusbConfigDescriptor(
|
||||
configDescriptor,
|
||||
::libusb_free_config_descriptor
|
||||
);
|
||||
return {configDescriptor, ::libusb_free_config_descriptor};
|
||||
}
|
||||
|
||||
void UsbDevice::detachKernelDriverFromInterface(std::uint8_t interfaceNumber) {
|
||||
@@ -214,7 +213,7 @@ namespace Usb
|
||||
}
|
||||
|
||||
} else if (libusbStatusCode != 0) {
|
||||
throw DeviceInitializationFailure("Failed to check for active kernel driver on USB interface.");
|
||||
throw DeviceInitializationFailure{"Failed to check for active kernel driver on USB interface."};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,10 +64,10 @@ namespace Usb
|
||||
virtual ~UsbDevice();
|
||||
|
||||
protected:
|
||||
static inline LibusbContext libusbContext = LibusbContext(nullptr, ::libusb_exit);
|
||||
static inline LibusbContext libusbContext = {nullptr, ::libusb_exit};
|
||||
|
||||
LibusbDevice libusbDevice = LibusbDevice(nullptr, ::libusb_unref_device);
|
||||
LibusbDeviceHandle libusbDeviceHandle = LibusbDeviceHandle(nullptr, ::libusb_close);
|
||||
LibusbDevice libusbDevice = {nullptr, ::libusb_unref_device};
|
||||
LibusbDeviceHandle libusbDeviceHandle = {nullptr, ::libusb_close};
|
||||
|
||||
std::vector<LibusbDevice> findMatchingDevices(std::uint16_t vendorId, std::uint16_t productId);
|
||||
|
||||
|
||||
@@ -24,10 +24,10 @@ namespace Usb
|
||||
|
||||
const auto statusCode = ::libusb_claim_interface(this->deviceHandle, this->interfaceNumber);
|
||||
if (statusCode != 0) {
|
||||
throw DeviceInitializationFailure(
|
||||
throw DeviceInitializationFailure{
|
||||
"Failed to claim USB interface (number: " + std::to_string(this->interfaceNumber) + ") - error code: "
|
||||
+ std::to_string(statusCode)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
this->claimed = true;
|
||||
@@ -49,11 +49,11 @@ namespace Usb
|
||||
std::uint8_t endpointAddress,
|
||||
std::optional<std::chrono::milliseconds> timeout
|
||||
) {
|
||||
auto output = std::vector<unsigned char>();
|
||||
auto output = std::vector<unsigned char>{};
|
||||
|
||||
constexpr auto transferSize = 512;
|
||||
auto bytesTransferred = int(0);
|
||||
auto totalByteCount = std::size_t(0);
|
||||
auto bytesTransferred = int{0};
|
||||
auto totalByteCount = std::size_t{0};
|
||||
|
||||
do {
|
||||
output.resize(totalByteCount + transferSize, 0x00);
|
||||
@@ -68,12 +68,12 @@ namespace Usb
|
||||
);
|
||||
|
||||
if (statusCode != 0) {
|
||||
throw DeviceCommunicationFailure("Failed to read from bulk endpoint");
|
||||
throw DeviceCommunicationFailure{"Failed to read from bulk endpoint"};
|
||||
}
|
||||
|
||||
if (totalByteCount == 0) {
|
||||
// After the first read, set the timeout to 1 millisecond, as we don't want to wait for subsequent data
|
||||
timeout = std::chrono::milliseconds(1);
|
||||
timeout = std::chrono::milliseconds{1};
|
||||
}
|
||||
|
||||
totalByteCount += static_cast<std::size_t>(bytesTransferred);
|
||||
@@ -86,11 +86,11 @@ namespace Usb
|
||||
|
||||
void UsbInterface::writeBulk(std::uint8_t endpointAddress, std::vector<unsigned char>&& buffer) {
|
||||
if (buffer.size() > std::numeric_limits<int>::max()) {
|
||||
throw DeviceCommunicationFailure("Attempted to send too much data to bulk endpoint");
|
||||
throw DeviceCommunicationFailure{"Attempted to send too much data to bulk endpoint"};
|
||||
}
|
||||
|
||||
const auto length = static_cast<int>(buffer.size());
|
||||
auto bytesTransferred = int(0);
|
||||
auto bytesTransferred = int{0};
|
||||
|
||||
const auto statusCode = ::libusb_bulk_transfer(
|
||||
this->deviceHandle,
|
||||
@@ -106,7 +106,7 @@ namespace Usb
|
||||
"Attempted to write " + std::to_string(length) + " bytes to USB bulk endpoint. Bytes written: "
|
||||
+ std::to_string(bytesTransferred) + ". Status code: " + std::to_string(statusCode)
|
||||
);
|
||||
throw DeviceCommunicationFailure("Failed to write data to bulk endpoint");
|
||||
throw DeviceCommunicationFailure{"Failed to write data to bulk endpoint"};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user