Esempio n. 1
0
//-----------------------------------------------------------------------------
// <HidController::Read>
// Read data from the HID port
//-----------------------------------------------------------------------------
void HidController::Read
(
)
{
	uint8 buffer[FEATURE_REPORT_LENGTH];
	int bytesRead = 0;
	uint8 inputReport[INPUT_REPORT_LENGTH];
	TimeStamp readTimer;

 	while( true )
	{
		// Rx feature report buffer should contain
		// [0]      - 0x05 (rx feature report ID)
		// [1]      - length of rx data (or 0x00 and no further bytes if no rx data waiting)
		// [2]...   - rx data
	  	// We poll this waiting for data.
		bytesRead = GetFeatureReport(FEATURE_REPORT_LENGTH, 0x5, buffer);
		CHECK_HIDAPI_RESULT(bytesRead, HidPortError);
		if( bytesRead >= 2 )
		{

			if( buffer[1] > 0 )
			{
				string tmp = "";
				for (int i = 0; i < buffer[1]; i++)
				{
					char bstr[16];
					snprintf( bstr, sizeof(bstr), "0x%.2x ", buffer[2+i] );
					tmp += bstr;
				}
				Log::Write( LogLevel_Detail, "hid report read=%d ID=%d len=%d %s", bytesRead, buffer[0], buffer[1], tmp.c_str() );
			}

			if( buffer[1] > 0 )
			{
				Put( &buffer[2], buffer[1] );
			}
		}
		if( readTimer.TimeRemaining() <= 0 )
		{
			// Hang a hid_read to acknowledge receipt. Seems the response is conveying
			// transaction status.
			// Wayne-Dalton input report data is structured as follows (best guess):
			// [0] 0x03      - input report ID
			// [1] 0x01      - ??? never changes
			// [2] 0xNN      - if 0x01, no feature reports waiting
			//                 if 0x02, feature report ID 0x05 is waiting to be retrieved
			// [3,4] 0xNNNN  - Number of ZWave messages?

			int hidApiResult = hid_read( m_hHidController, inputReport, INPUT_REPORT_LENGTH );
			//if( hidApiResult != 0 )
			//{
			//	string tmp = "";
			//	for( int i = 0; i < INPUT_REPORT_LENGTH; i++ )
			//	{
			//		char bstr[16];
			//		snprintf(bstr, sizeof(bstr), "%02x ", inputReport[i] );
			//		tmp += bstr;
			//	}
			//	Log::Write( LogLevel_Detail, "hid read %d %s", hidApiResult, tmp.c_str() );
			//}

			if( hidApiResult == -1 )
			{
				const wchar_t* errString = hid_error(m_hHidController);
				Log::Write( LogLevel_Warning, "Error: HID port returned error reading input bytes: 0x%08hx, HIDAPI error string: %ls", hidApiResult, errString );
			}
			readTimer.SetTime( 100 );
		}
		m_thread->Sleep(10);
	}

HidPortError:
	Log::Write( LogLevel_Warning, "Error: HID port returned error reading rest of packet: 0x%08hx, HIDAPI error string:", bytesRead );
	Log::Write( LogLevel_Warning, "%ls", hid_error(m_hHidController));
}