Make ftdi_read_eeprom_location() endianess independent
authorYegor Yefremov <yegorslists@googlemail.com>
Fri, 10 Jun 2016 11:58:32 +0000 (13:58 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 15 Jun 2016 14:58:39 +0000 (16:58 +0200)
Read 16-bit integer as two byte array and combine these two bytes
to unsigned short.

src/ftdi.c

index f5d263c..fdf93fe 100644 (file)
@@ -4195,12 +4195,16 @@ int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int s
 */
 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
 {
+    unsigned char buf[2];
+
     if (ftdi == NULL || ftdi->usb_dev == NULL)
         ftdi_error_return(-2, "USB device unavailable");
 
-    if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (unsigned char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
+    if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, buf, 2, ftdi->usb_read_timeout) != 2)
         ftdi_error_return(-1, "reading eeprom failed");
 
+    *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
+
     return 0;
 }