Adding vendor/product/serial number strings support to our HidDevice.
authorsl
Thu, 22 May 2014 08:45:14 +0200
changeset 1621033ded8f86
parent 15 e5b84f315be7
child 17 63972dc16350
Adding vendor/product/serial number strings support to our HidDevice.
inc/HidDevice.h
src/HidDevice.cpp
     1.1 --- a/inc/HidDevice.h	Thu May 22 08:17:14 2014 +0200
     1.2 +++ b/inc/HidDevice.h	Thu May 22 08:45:14 2014 +0200
     1.3 @@ -8,6 +8,7 @@
     1.4  #include "HidReport.h"
     1.5  #include "hidapi.h"
     1.6  
     1.7 +const int KMaxHidStringChar=256;
     1.8  
     1.9  /**
    1.10  TODO: move to another header
    1.11 @@ -27,10 +28,21 @@
    1.12      int Write(const HidReport<S>& aOutputReport);
    1.13      //
    1.14      const wchar_t* Error();
    1.15 +    //
    1.16 +    wchar_t* Vendor();
    1.17 +    wchar_t* Product();
    1.18 +    wchar_t* SerialNumber();
    1.19 +
    1.20 +private:
    1.21 +    void FetchStrings();
    1.22  
    1.23  private:
    1.24      ///Our USB HID device
    1.25      hid_device* iHidDevice;
    1.26 +    //
    1.27 +    wchar_t iVendor[KMaxHidStringChar];
    1.28 +    wchar_t iProduct[KMaxHidStringChar];
    1.29 +    wchar_t iSerialNumber[KMaxHidStringChar];
    1.30      };
    1.31  
    1.32  
     2.1 --- a/src/HidDevice.cpp	Thu May 22 08:17:14 2014 +0200
     2.2 +++ b/src/HidDevice.cpp	Thu May 22 08:45:14 2014 +0200
     2.3 @@ -12,7 +12,7 @@
     2.4  
     2.5  HidDevice::HidDevice():iHidDevice(NULL)
     2.6      {
     2.7 -
     2.8 +    Close();
     2.9      }
    2.10  
    2.11  /**
    2.12 @@ -29,6 +29,8 @@
    2.13  		return 0;
    2.14  		}
    2.15  
    2.16 +    FetchStrings();
    2.17 +
    2.18  	return 1;
    2.19  	}
    2.20  
    2.21 @@ -37,6 +39,8 @@
    2.22  */
    2.23  int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    2.24  	{
    2.25 +    Close();
    2.26 +
    2.27  	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    2.28  
    2.29  	if (!iHidDevice)
    2.30 @@ -45,6 +49,8 @@
    2.31  		return 0;
    2.32  		}
    2.33  
    2.34 +    FetchStrings();
    2.35 +
    2.36  	return 1;
    2.37  	}
    2.38  
    2.39 @@ -54,6 +60,10 @@
    2.40  	{
    2.41  	hid_close(iHidDevice);
    2.42  	iHidDevice=NULL;
    2.43 +    //
    2.44 +    memset(iVendor,0,sizeof(iVendor));
    2.45 +    memset(iProduct,0,sizeof(iProduct));
    2.46 +    memset(iSerialNumber,0,sizeof(iSerialNumber));
    2.47  	}
    2.48  
    2.49  /**
    2.50 @@ -79,3 +89,34 @@
    2.51  	//Set read operation as non blocking
    2.52  	return hid_set_nonblocking(iHidDevice, aNonBlocking);
    2.53  	}
    2.54 +
    2.55 +/**
    2.56 +*/
    2.57 +wchar_t* HidDevice::Vendor()
    2.58 +    {
    2.59 +    return iVendor;
    2.60 +    }
    2.61 +
    2.62 +/**
    2.63 +*/
    2.64 +wchar_t* HidDevice::Product()
    2.65 +    {
    2.66 +    return iProduct;
    2.67 +    }
    2.68 +
    2.69 +/**
    2.70 +*/
    2.71 +wchar_t* HidDevice::SerialNumber()
    2.72 +    {
    2.73 +    return iSerialNumber;
    2.74 +    }
    2.75 +
    2.76 +/**
    2.77 +
    2.78 +*/
    2.79 +void HidDevice::FetchStrings()
    2.80 +    {
    2.81 +    hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor));
    2.82 +    hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct));
    2.83 +    hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber));
    2.84 +    }