Better cleanup and error handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
diff --git a/libztex.c b/libztex.c
index aca07ac..acc5025 100644
--- a/libztex.c
+++ b/libztex.c
@@ -53,7 +53,7 @@ static bool libztex_checkDevice (struct libusb_device *dev) {
applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
return false;
}
- if (!(desc.idVendor == 0x221A && desc.idProduct == 0x0100)) {
+ if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
return false;
}
return true;
@@ -264,14 +264,18 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
return 1;
}
- libusb_open(dev, &newdev->hndl);
+ err = libusb_open(dev, &newdev->hndl);
+ if (unlikely(err != 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
+ return err;
+ }
+
cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
LIBZTEX_SNSTRING_LEN+1);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
return cnt;
}
- applog(LOG_WARNING, "-- %s", newdev->snString);
cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
if (unlikely(cnt < 0)) {
@@ -356,6 +360,7 @@ void libztex_destroy_device (struct libztex_device* ztex) {
}
if (ztex->bitFileName != NULL) {
free(ztex->bitFileName);
+ ztex->bitFileName = NULL;
}
free(ztex);
}
@@ -415,7 +420,9 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
int cnt;
-
+ if (ztex->hndl == NULL) {
+ return 0;
+ }
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
@@ -428,6 +435,10 @@ int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data
// length of buf must be 8 * (numNonces + 1)
unsigned char rbuf[12*8];
int cnt, i;
+
+ if (ztex->hndl == NULL) {
+ return 0;
+ }
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12*ztex->numNonces, 1000);
if (unlikely(cnt < 0)) {
diff --git a/libztex.h b/libztex.h
index dbcbe88..ec6d56f 100644
--- a/libztex.h
+++ b/libztex.h
@@ -84,6 +84,7 @@ struct libztex_hash_data {
extern int libztex_scanDevices (struct libztex_dev_list ***devs);
extern void libztex_freeDevList (struct libztex_dev_list **devs);
+extern int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex);
extern void libztex_destroy_device (struct libztex_device* ztex);
extern int libztex_configureFpga (struct libztex_device *dev);
extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq);