HidDevice.cpp
author sl
Tue, 08 Jul 2014 18:59:41 +0200
changeset 4 36894fc4dad6
child 7 8bac7aac665c
permissions -rw-r--r--
Adding APIs for string and requests.
     1 //
     2 //
     3 //
     4 
     5 #include "HidDevice.h"
     6 
     7 
     8 
     9 //
    10 // class HidDevice
    11 //
    12 
    13 HidDevice::HidDevice():iHidDevice(NULL)
    14     {
    15     Close();
    16     }
    17 
    18 /**
    19 */
    20 int HidDevice::Open(const char* aPath)
    21 	{
    22 	Close();
    23 
    24 	iHidDevice =  hid_open_path(aPath);
    25 
    26 	if (!iHidDevice)
    27 		{
    28 		//Fail to connect our device
    29 		return 0;
    30 		}
    31 
    32     FetchStrings();
    33 
    34 	return 1;
    35 	}
    36 
    37 /**
    38 See hidapi documentation.
    39 */
    40 int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
    41 	{
    42     Close();
    43 
    44 	iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
    45 
    46 	if (!iHidDevice)
    47 		{
    48 		//Fail to connect our device
    49 		return 0;
    50 		}
    51 
    52     FetchStrings();
    53 
    54 	return 1;
    55 	}
    56 
    57 /**
    58 */
    59 void HidDevice::Close()
    60 	{
    61 	hid_close(iHidDevice);
    62 	iHidDevice=NULL;
    63     //
    64     memset(iVendor,0,sizeof(iVendor));
    65     memset(iProduct,0,sizeof(iProduct));
    66     memset(iSerialNumber,0,sizeof(iSerialNumber));
    67 	}
    68 
    69 /**
    70 */
    71 bool HidDevice::IsOpen()
    72     {
    73     return iHidDevice!=NULL;
    74     }
    75 
    76 
    77 /**
    78 */
    79 const wchar_t* HidDevice::Error()
    80 	{
    81 	return hid_error(iHidDevice);
    82 	}
    83 
    84 /**
    85 */
    86 int HidDevice::SetNonBlocking(int aNonBlocking)
    87 	{
    88 	//Success we are now connected to our HID device
    89 	//Set read operation as non blocking
    90 	return hid_set_nonblocking(iHidDevice, aNonBlocking);
    91 	}
    92 
    93 /**
    94 */
    95 wchar_t* HidDevice::Vendor()
    96     {
    97     return iVendor;
    98     }
    99 
   100 /**
   101 */
   102 wchar_t* HidDevice::Product()
   103     {
   104     return iProduct;
   105     }
   106 
   107 /**
   108 */
   109 wchar_t* HidDevice::SerialNumber()
   110     {
   111     return iSerialNumber;
   112     }
   113 
   114 /**
   115 
   116 */
   117 void HidDevice::FetchStrings()
   118     {
   119     hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor));
   120     hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct));
   121     hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber));
   122     }