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 | 2 | |
| 3 | 3 | #include <QCoreApplication> |
| 4 | 4 | #include <QList> |
| 5 | +#include <iostream> | |
| 5 | 6 | |
| 6 | 7 | #include "usbdevice.h" |
| 7 | 8 | |
| ... | ... | @@ -170,7 +171,7 @@ int USBDevice::bulkCommand(DataArray<unsigned char> *command, int attempts) { |
| 170 | 171 | /// \param length The length of data contained in the packets. |
| 171 | 172 | /// \param attempts The number of attempts, that are done on timeouts. |
| 172 | 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 | 175 | if (!this->handle) return LIBUSB_ERROR_NO_DEVICE; |
| 175 | 176 | |
| 176 | 177 | int errorCode = 0; |
| ... | ... | @@ -261,17 +262,16 @@ int USBDevice::getConnectionSpeed() { |
| 261 | 262 | /// \brief Gets the maximum size of one packet transmitted via bulk transfer. |
| 262 | 263 | /// \return The maximum packet size in bytes, -1 on error. |
| 263 | 264 | int USBDevice::getPacketSize() { |
| 264 | - switch (this->getConnectionSpeed()) { | |
| 265 | - case CONNECTION_FULLSPEED: | |
| 265 | + const int s = this->getConnectionSpeed(); | |
| 266 | + if (s == CONNECTION_FULLSPEED) | |
| 266 | 267 | return 64; |
| 267 | - break; | |
| 268 | - case CONNECTION_HIGHSPEED: | |
| 268 | + else if (s == CONNECTION_HIGHSPEED) | |
| 269 | 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 | 277 | /// \brief Get the oscilloscope model. |
| ... | ... | @@ -282,12 +282,6 @@ libusb_device *USBDevice::getRawDevice() const { return device; } |
| 282 | 282 | |
| 283 | 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 | 48 | int bulkRead(unsigned char *data, unsigned int length, int attempts = HANTEK_ATTEMPTS); |
| 49 | 49 | |
| 50 | 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 | 53 | int controlTransfer(unsigned char type, unsigned char request, unsigned char *data, unsigned int length, int value, |
| 54 | 54 | int index, int attempts = HANTEK_ATTEMPTS); | ... | ... |