⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atlasusbdev.cpp

📁 cell phone source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PalmDev.cpp: implementation of the CAtlasUsbDev class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AtlasUsbDev.h"

#include <initguid.h>
DEFINE_GUID(GUID_CLASS_CENTRALITY_ATLAS_2X0A, 
0xfacad18d, 0x4b19, 0x4ae5, 0xa7, 0xe0, 0xb3, 0x9d, 0xaa, 0x2f, 0x7a, 0xc);


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAtlasUsbDev::CAtlasUsbDev()
{
	m_bInit = FALSE;

	m_hDev = INVALID_HANDLE_VALUE;

	for(int i = 0; i < MAX_PIPES; i++)
	{
		if(m_hPipe[i] != INVALID_HANDLE_VALUE)
		{
			m_hPipe[i] = INVALID_HANDLE_VALUE;
		}
	}

	m_strDevName.Empty();
	m_strDevName = "";
}

CAtlasUsbDev::~CAtlasUsbDev()
{

}

BOOL CAtlasUsbDev::Init()
{
	m_pGuid = (_GUID *)&GUID_CLASS_CENTRALITY_ATLAS_2X0A;

	//Open the device to init m_strDevName
	if(OpenDevice())
	{
		CloseDevice();
		return m_bInit = TRUE;
	}
	else
	{
		m_strDevName.Empty();
		m_strDevName = "";
		return m_bInit = FALSE;
	}
}

BOOL CAtlasUsbDev::OpenDevice()
{
	ULONG					NumberDevices;
	HDEVINFO					hardwareDeviceInfo;
	SP_INTERFACE_DEVICE_DATA	deviceInfoData;
	ULONG					i;
	BOOL						done;
	PUSB_DEVICE_DESCRIPTOR   usbDeviceInst;
	PUSB_DEVICE_DESCRIPTOR	*UsbDevices = &usbDeviceInst;

	CSingleLock sLock(&m_critical);
	sLock.Lock();

	if (m_hDev != INVALID_HANDLE_VALUE)
	{
		CloseDevice();
	}

	*UsbDevices = NULL;
	NumberDevices = 0;

	//
	// Open a handle to the plug and play dev node.
	// SetupDiGetClassDevs() returns a device information set that contains info on all 
	// installed devices of a specified class.
	//

	hardwareDeviceInfo = SetupDiGetClassDevs (
                           m_pGuid,
                           NULL, // Define no enumerator (global)
                           NULL, // Define no
                           (DIGCF_PRESENT | // Only Devices present
                            DIGCF_INTERFACEDEVICE)); // Function class devices.
	//
	// Take a wild guess at the number of devices we have;
	// Be prepared to realloc and retry if there are more than we guessed
	//
	NumberDevices = 4;
	done = FALSE;
	deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);

	i=0;
	while (!done) 
	{
		NumberDevices *= 2;

		if (*UsbDevices) 
		{
			*UsbDevices = (_USB_DEVICE_DESCRIPTOR *)realloc (*UsbDevices, (NumberDevices * sizeof (USB_DEVICE_DESCRIPTOR)));
		} 
		else 
		{
			*UsbDevices = (_USB_DEVICE_DESCRIPTOR *)calloc (NumberDevices, sizeof (USB_DEVICE_DESCRIPTOR));
		}

		if (NULL == *UsbDevices) 
		{

			// SetupDiDestroyDeviceInfoList destroys a device information set 
			// and frees all associated memory.

			SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
			sLock.Unlock();
			return FALSE;
		}

		usbDeviceInst = *UsbDevices + i;

		for (; i < NumberDevices; i++) 
		{

			// SetupDiEnumDeviceInterfaces() returns information about device interfaces 
			// exposed by one or more devices. Each call returns information about one interface;
			// the routine can be called repeatedly to get information about several interfaces
			// exposed by one or more devices.

			if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,
												0, // We don't care about specific PDOs
												m_pGuid,
												i,
												&deviceInfoData)) 
			{

				if(OpenOneDevice (hardwareDeviceInfo, &deviceInfoData))
				{
					done = TRUE;
					break;
				}
			}
			else 
			{
				if (ERROR_NO_MORE_ITEMS == GetLastError()) 
				{
					done = TRUE;
					break;
				}
			}
		}
	}

	NumberDevices = i;

	// SetupDiDestroyDeviceInfoList() destroys a device information set 
	// and frees all associated memory.

	SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
	free ( *UsbDevices );
	sLock.Unlock();
	return (m_hDev != INVALID_HANDLE_VALUE);
}

void CAtlasUsbDev::CloseDevice()
{
	if(m_hDev != INVALID_HANDLE_VALUE)
		CloseHandle(m_hDev);
	m_hDev = INVALID_HANDLE_VALUE;
}

BOOL CAtlasUsbDev::OpenOneDevice(IN HDEVINFO HardwareDeviceInfo,
							 IN PSP_INTERFACE_DEVICE_DATA DeviceInfoData)
{
    PSP_INTERFACE_DEVICE_DETAIL_DATA     functionClassDeviceData = NULL;
    ULONG                                predictedLength = 0;
    ULONG                                requiredLength = 0;

    //
    // allocate a function class device data structure to receive the
    // goods about this particular device.
    //
    SetupDiGetInterfaceDeviceDetail (
            HardwareDeviceInfo,
            DeviceInfoData,
            NULL, // probing so no output buffer yet
            0, // probing so output buffer length of zero
            &requiredLength,
            NULL); // not interested in the specific dev-node


    predictedLength = requiredLength;
    // sizeof (SP_FNCLASS_DEVICE_DATA) + 512;

    functionClassDeviceData = (_SP_DEVICE_INTERFACE_DETAIL_DATA_A *)malloc (predictedLength);
    functionClassDeviceData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);

    //
    // Retrieve the information from Plug and Play.
    //
    if (! SetupDiGetInterfaceDeviceDetail (
               HardwareDeviceInfo,
               DeviceInfoData,
               functionClassDeviceData,
               predictedLength,
               &requiredLength,
               NULL)) 
	{
		free( functionClassDeviceData );
        return FALSE;
    }

	m_strDevName.Format("%s",functionClassDeviceData->DevicePath) ;
//	TRACE( "Attempting to open %s Name Length: %d\n", m_strDevName, m_strDevName.GetLength() );

    m_hDev = CreateFile (
                  functionClassDeviceData->DevicePath,
                  GENERIC_READ | GENERIC_WRITE,
                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                  NULL, // no SECURITY_ATTRIBUTES structure
                  OPEN_EXISTING, // No special create flags
                  0, // No special attributes
                  NULL); // No template file

	free( functionClassDeviceData );
    if (INVALID_HANDLE_VALUE == m_hDev)
	{
		TRACE( "FAILED to open %s\n", m_strDevName);
		return FALSE;
    }
	return TRUE;
}

void CAtlasUsbDev::CloseAllPipes()
{
	for(int i = 0; i < MAX_PIPES; i++)
	{
		if(m_hPipe[i] != INVALID_HANDLE_VALUE)
		{
			CloseHandle(m_hPipe[i]);
			m_hPipe[i] = INVALID_HANDLE_VALUE;
		}
	}
}

BOOL CAtlasUsbDev::WritePipe(DWORD iPipe, BYTE *pBuf, UINT iLength, UINT &nBytesWritten)
{
    nBytesWritten = 0;
    
	if(m_hPipe[iPipe] == INVALID_HANDLE_VALUE)
	{
		TRACE("Open the pipe first!\n");
		return FALSE;
	}

	CSingleLock sLock(&m_critical);
	sLock.Lock();
	if(iLength > 0)
	{
        if (!WriteFile(m_hPipe[iPipe], 
                pBuf,
                iLength,
                (ULONG *)&nBytesWritten,
                NULL)) 
        { 
            TRACE("Read USB data error! Error code:%08x\r\n",GetLastError());
            sLock.Unlock();
            return FALSE;
        }

		if(nBytesWritten < iLength)
		{
			TRACE("Write %d bytes of %d bytes!\n", nBytesWritten, iLength);
			iLength -= nBytesWritten;
			if (!WriteFile(m_hPipe[iPipe], 
					  pBuf + nBytesWritten,
					  iLength,
					  (ULONG *)&nBytesWritten,
					  NULL))
            { 
                TRACE("Read USB data error! Error code:%08x\r\n",GetLastError());
                sLock.Unlock();
                return FALSE;
            }
			TRACE("Write %d bytes of %d bytes!\n", nBytesWritten, iLength);
		}
	}

	sLock.Unlock();
	
	return TRUE;
}

BOOL CAtlasUsbDev::ReadPipe(DWORD iPipe, BYTE *pBuf, UINT iLength, UINT &nBytesRead)
{
    BOOL    bResult;
	nBytesRead = 0;

    if(m_hPipe[iPipe] == INVALID_HANDLE_VALUE)
	{
		TRACE("Open the pipe first!\n");
		return FALSE;
	}

	CSingleLock sLock(&m_critical);
	sLock.Lock();
	bResult = ReadFile(m_hPipe[iPipe], 
                    pBuf,
                    iLength,
                    (ULONG *)&nBytesRead,
                    NULL);
    if (!bResult) 
    { 
        DWORD dwError;
        switch (dwError = GetLastError()) 
        {
        case ERROR_DEVICE_NOT_CONNECTED:
            TRACE("Read USB data error! USB disconnected!\r\n");
            break;
        default:
            TRACE("Read USB data error! Error code:%08x\r\n",dwError);
            break;
        }
        sLock.Unlock();
        return FALSE;
    }
    
    sLock.Unlock();

	if(nBytesRead == 0x7fc0 && iLength == 0x8000)
	{
		CFile tst;
		tst.Open("dumper.bin", CFile::modeCreate|CFile::modeWrite);
		tst.Write(pBuf, 0x7fc0);
		tst.Close();
	}
	return TRUE;
}

BOOL CAtlasUsbDev::SendVendorCommand(PATLAS_USB_MESSAGE vendorMsg)
{
	DWORD nReturnBytes;

	CSingleLock sLock(&m_critical);
	sLock.Lock();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -