Commit c8f3559b00922cfca63956a332e01293eb409ffd
Committed by
David Gräff
1 parent
6570088d
usbdevice: Use if/else instead of switch for getPacketSize and fail with a std::…
…runtime_error if result is unexpected
Showing
2 changed files
with
12 additions
and
18 deletions
openhantek/src/hantek/usb/usbdevice.cpp
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | #include <QCoreApplication> | 3 | #include <QCoreApplication> |
| 4 | #include <QList> | 4 | #include <QList> |
| 5 | +#include <iostream> | ||
| 5 | 6 | ||
| 6 | #include "usbdevice.h" | 7 | #include "usbdevice.h" |
| 7 | 8 | ||
| @@ -170,7 +171,7 @@ int USBDevice::bulkCommand(DataArray<unsigned char> *command, int attempts) { | @@ -170,7 +171,7 @@ int USBDevice::bulkCommand(DataArray<unsigned char> *command, int attempts) { | ||
| 170 | /// \param length The length of data contained in the packets. | 171 | /// \param length The length of data contained in the packets. |
| 171 | /// \param attempts The number of attempts, that are done on timeouts. | 172 | /// \param attempts The number of attempts, that are done on timeouts. |
| 172 | /// \return Number of received bytes on success, libusb error code on error. | 173 | /// \return Number of received bytes on success, libusb error code on error. |
| 173 | -int USBDevice::bulkReadMulti(unsigned char *data, unsigned int length, int attempts) { | 174 | +int USBDevice::bulkReadMulti(unsigned char *data, unsigned length, int attempts) { |
| 174 | if (!this->handle) return LIBUSB_ERROR_NO_DEVICE; | 175 | if (!this->handle) return LIBUSB_ERROR_NO_DEVICE; |
| 175 | 176 | ||
| 176 | int errorCode = 0; | 177 | int errorCode = 0; |
| @@ -261,17 +262,16 @@ int USBDevice::getConnectionSpeed() { | @@ -261,17 +262,16 @@ int USBDevice::getConnectionSpeed() { | ||
| 261 | /// \brief Gets the maximum size of one packet transmitted via bulk transfer. | 262 | /// \brief Gets the maximum size of one packet transmitted via bulk transfer. |
| 262 | /// \return The maximum packet size in bytes, -1 on error. | 263 | /// \return The maximum packet size in bytes, -1 on error. |
| 263 | int USBDevice::getPacketSize() { | 264 | int USBDevice::getPacketSize() { |
| 264 | - switch (this->getConnectionSpeed()) { | ||
| 265 | - case CONNECTION_FULLSPEED: | 265 | + const int s = this->getConnectionSpeed(); |
| 266 | + if (s == CONNECTION_FULLSPEED) | ||
| 266 | return 64; | 267 | return 64; |
| 267 | - break; | ||
| 268 | - case CONNECTION_HIGHSPEED: | 268 | + else if (s == CONNECTION_HIGHSPEED) |
| 269 | return 512; | 269 | return 512; |
| 270 | - break; | ||
| 271 | - default: | ||
| 272 | - return -1; | ||
| 273 | - break; | 270 | + else if (s > CONNECTION_HIGHSPEED) { |
| 271 | + std::cerr << "Unknown USB speed. Please correct source code in USBDevice::getPacketSize()" << std::endl; | ||
| 272 | + throw new std::runtime_error("Unknown USB speed"); | ||
| 274 | } | 273 | } |
| 274 | + return -1; | ||
| 275 | } | 275 | } |
| 276 | 276 | ||
| 277 | /// \brief Get the oscilloscope model. | 277 | /// \brief Get the oscilloscope model. |
| @@ -282,12 +282,6 @@ libusb_device *USBDevice::getRawDevice() const { return device; } | @@ -282,12 +282,6 @@ libusb_device *USBDevice::getRawDevice() const { return device; } | ||
| 282 | 282 | ||
| 283 | const DSOModel &USBDevice::getModel() const { return model; } | 283 | const DSOModel &USBDevice::getModel() const { return model; } |
| 284 | 284 | ||
| 285 | -void USBDevice::setEnableBulkTransfer(bool enable) | ||
| 286 | -{ | ||
| 287 | - allowBulkTransfer = enable; | ||
| 288 | -} | 285 | +void USBDevice::setEnableBulkTransfer(bool enable) { allowBulkTransfer = enable; } |
| 289 | 286 | ||
| 290 | -void USBDevice::overwriteInPacketLength(int len) | ||
| 291 | -{ | ||
| 292 | - inPacketLength = len; | ||
| 293 | -} | 287 | +void USBDevice::overwriteInPacketLength(int len) { inPacketLength = len; } |
openhantek/src/hantek/usb/usbdevice.h
| @@ -48,7 +48,7 @@ class USBDevice : public QObject { | @@ -48,7 +48,7 @@ class USBDevice : public QObject { | ||
| 48 | int bulkRead(unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS); | 48 | int bulkRead(unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS); |
| 49 | 49 | ||
| 50 | int bulkCommand(DataArray<unsigned char> *command, int attempts = HANTEK_ATTEMPTS); | 50 | int bulkCommand(DataArray<unsigned char> *command, int attempts = HANTEK_ATTEMPTS); |
| 51 | - int bulkReadMulti(unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS_MULTI); | 51 | + int bulkReadMulti(unsigned char *data, unsigned length, int attempts = HANTEK_ATTEMPTS_MULTI); |
| 52 | 52 | ||
| 53 | int controlTransfer(unsigned char type, unsigned char request, unsigned char *data, unsigned int length, int value, | 53 | int controlTransfer(unsigned char type, unsigned char request, unsigned char *data, unsigned int length, int value, |
| 54 | int index, int attempts = HANTEK_ATTEMPTS); | 54 | int index, int attempts = HANTEK_ATTEMPTS); |