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

📄 ezmain.cpp

📁 FX2 68013的控制面板源程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:


/**************************************************
 * ParseDeviceDescriptor proc                     *
 *                                                *
 * Purpose:                                       *  
 *      Parses the device descriptor data and     *
 *      displays the interpretation to the        *
 *      windows specified                         *
 *                                                * 
 * Input:                                         *
 *      pvBuffer:                                 * 
 *          Pointer to a buffer that contains the *
 *          raw device descriptor data.           *
 *      hOutputBox:                               *
 *          Handle to the window where the        *
 *          resultant interpreted data is to go.  *
 *                                                *
 * Return Value:                                  *
 *          None                                  *
 **************************************************/
void
ParseDeviceDescriptor(PVOID pvBuffer, CString& hOutputBox)
{
    pUsb_Device_Descriptor pDevDescr = (pUsb_Device_Descriptor) pvBuffer;
    int                    nItems    = 0;
    char                   temp[64]  = "";

    MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
    
    wsprintf (temp, "Device Descriptor:  ");
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bLength:  %d", pDevDescr->bLength);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bDescriptorType:  %d", pDevDescr->bDescriptorType);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bcdUSB:  %d", pDevDescr->bcdUSB);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bDeviceClass:  %#x", pDevDescr->bDeviceClass);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bDeviceSubClass:  %#x", pDevDescr->bDeviceSubClass);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bDeviceProtocol:  %#x", pDevDescr->bDeviceProtocol);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bMaxPacketSize0:  %#x", pDevDescr->bMaxPacketSize0);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "idVendor:  %#x", pDevDescr->idVendor);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "idProduct:  %#x", pDevDescr->idProduct);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bcdDevice:  %#x", pDevDescr->bcdDevice);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "iManufacturer:  %#x", pDevDescr->iManufacturer);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "iProduct:  %#x", pDevDescr->iProduct);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "iSerialNumber:  %#x", pDevDescr->iSerialNumber);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bNumConfigurations:  %#x", pDevDescr->bNumConfigurations);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

}/* ParseDeviceDescriptor */ 


typedef struct _USB_ENDPOINT_DESCRIPTOR 
{
    UCHAR	bLength;
    UCHAR	bDescriptorType;
    UCHAR	bEndpointAddress;
    UCHAR	bmAttributes;
    USHORT	wMaxPacketSize;
    UCHAR	bInterval;
} USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;

typedef struct _USB_INTERFACE_DESCRIPTOR 
{
    UCHAR	bLength;
    UCHAR	bDescriptorType;
    UCHAR	bInterfaceNumber;
    UCHAR	bAlternateSetting;
    UCHAR	bNumEndpoints;
    UCHAR	bInterfaceClass;
    UCHAR	bInterfaceSubClass;
    UCHAR	bInterfaceProtocol;
    UCHAR	iInterface;
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;

/**************************************************
 * ParseConfigurationDescriptor proc              *
 *                                                *
 * Purpose:                                       *  
 *      Parses the config descriptor data and     *
 *      displays the interpretation to the        *
 *      windows specified                         *
 *                                                * 
 * Input:                                         *
 *      pvBuffer:                                 * 
 *          Pointer to a buffer that contains the *
 *          raw device descriptor data.           *
 *      hOutputBox:                               *
 *          Handle to the window where the        *
 *          resultant interpreted data is to go.  *
 *                                                *
 * Return Value:                                  *
 *          None                                  *
 **************************************************/
 
void
ParseConfigurationDescriptor(PVOID pvBuffer, CString& hOutputBox)
{
    pUsb_Configuration_Descriptor pCD = (pUsb_Configuration_Descriptor) pvBuffer;
    PUSB_INTERFACE_DESCRIPTOR     pID = NULL; 
    PUSB_ENDPOINT_DESCRIPTOR      pED = NULL;
    
    int                    nItems    = 0;
    char                   temp[64]  = "";
    int                    i         = 0;

    MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
    
    wsprintf (temp, "Config Descriptor:  ");
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bLength: 0x%x", pCD->bLength);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bDescriptorType:  %d", pCD->bDescriptorType);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "wTotalLength:  %d (0x%x)", pCD->wTotalLength, pCD->wTotalLength);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bNumInterfaces:  %d", pCD->bNumInterfaces);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bConfigurationValue:  %d", pCD->bConfigurationValue);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "iConfiguration:  %d", pCD->iConfiguration);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bmAttributes: 0x%x", pCD->bmAttributes);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "MaxPower:  %d", pCD->MaxPower);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "*********************************");
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

  for( pID = (PUSB_INTERFACE_DESCRIPTOR) (((char*)pCD) + pCD->bLength); 
       pID < (PUSB_INTERFACE_DESCRIPTOR) (((char*)pCD) + pCD->wTotalLength);
	   pID->bLength) // Note: Quits if pointer is not growing - i.e. pID->bLength == 0
  { // Itterate over Interface Descriptors

    // Interface Descriptor
    //pID = (PUSB_INTERFACE_DESCRIPTOR) (((char*)pCD) + pCD->bLength);

    MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);

    wsprintf (temp, "Interface Descriptor:  ");
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "--------------------------------");
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
    
    wsprintf (temp, "bLength:  0x%x", pID->bLength);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bDescriptorType:  %d", pID->bDescriptorType);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bInterfaceNumber:  %d", pID->bInterfaceNumber);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bAlternateSetting:  %d", pID->bAlternateSetting);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bNumEndpoints:  %d", pID->bNumEndpoints);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bInterfaceClass:  %d (0x%x)", pID->bInterfaceClass, pID->bInterfaceClass);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bInterfaceSubClass:  %d (0x%x)", pID->bInterfaceSubClass, pID->bInterfaceSubClass);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "bInterfaceProtocol:  %d (0x%x)", pID->bInterfaceProtocol, pID->bInterfaceProtocol);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "iInterface:  %d", pID->iInterface);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    wsprintf (temp, "*********************************");
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

    // Endpoint Descriptor
    pED = (PUSB_ENDPOINT_DESCRIPTOR) (((char*)pID) + pID->bLength);

    for (i=0;i<pID->bNumEndpoints;i++) {

        MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
		
        wsprintf (temp, "Endpoint Descriptor %d  ", i);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

        wsprintf (temp, "--------------------------------");
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

        wsprintf (temp, "bLength:  0x%x", pED->bLength);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

        wsprintf (temp, "bDescriptorType:  %d", pED->bDescriptorType);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

        wsprintf (temp, "bEndpointAddress:  0x%x", pED->bEndpointAddress);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
    
        wsprintf (temp, "bmAttributes:  0x%x", pED->bmAttributes);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

        wsprintf (temp, "wMaxPacketSize:  %d", pED->wMaxPacketSize);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

        wsprintf (temp, "bInterval:  %d", pED->bInterval);
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
    
        wsprintf (temp, "*********************************");
        EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
   
		pED = (PUSB_ENDPOINT_DESCRIPTOR) (((char*)pED) + (pED->bLength));

    }/* for */
	pID = (PUSB_INTERFACE_DESCRIPTOR) (char*)pED; // pointer should grow
  } // Itterate over Interface Descriptors

}/* ParseConfigurationDescriptor */

void
ParseStringDescriptor(PVOID pvBuffer, CString& hOutputBox)
{
    pUsb_String_Descriptor pCD = (pUsb_String_Descriptor) pvBuffer;
    PUSB_INTERFACE_DESCRIPTOR     pID = NULL; 
    PUSB_ENDPOINT_DESCRIPTOR      pED = NULL;
    
    int                    nItems    = 0;
    char                   temp[256]  = "";
    int                    i         = 0;

    MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);

    
	wsprintf (temp, "StringDescriptor=");
	for(i=0; i<((pCD->bLength/2)-1) && i<(sizeof(temp)-17); i++)
		wsprintf (temp+17+i, "%c", pCD->bString[i]);
    EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);

}/* ParseStringDescriptor */


#define ONE_K       0x400
#define ONE_MEG     (ONE_K * ONE_K)

// Assign data only if successfully scanned data.
// Return 0 if no data scanned
int my_sscanf(LPCSTR cBuffer, DWORD *data)
{
	char *ptr;
   char buffer[0x100];
	int value;

   strcpy(buffer, cBuffer);

	if (ptr = strchr(_strlwr(buffer), 'k'))
	{
		*ptr = 0;
		if (sscanf(buffer, "%d", &value))
		{
			*data = value * ONE_K;
			return 1;
		}
		*ptr = 'K';		// failed, but let's put it back
	}

	if (ptr = strchr(_strlwr(buffer), 'm'))
	{
		*ptr = 0;
		if (sscanf(buffer, "%d", &value))
		{
			*data = value * ONE_MEG;
			return 1;
		}
		*ptr = 'M';		// failed, but let's put it back
	}

	if (ptr = strpbrk(buffer, "xX"))
	{
		if (sscanf(ptr+1, "%x", &value))
			{
				*data = value;
				return 1;
			}
	}
	else if (sscanf(buffer, "%d", &value))
	{
		*data = value;
		return 1;
	}
	return 0;
}


#ifdef USING_MFC
void EzSendMessage(CString& hDlg, int OpType, int* OtherOp, LPARAM pStr)
{
	TRACE("Send=%s\n", pStr);
	if(hDlg)
	{
		hDlg += (char*)pStr;
		hDlg += "\n";
	}
/*TPMTPMTPM
		int curCursorPos = hDlg->GetTextLength();
		if(curCursorPos)
			hDlg->SetSel(curCursorPos, curCursorPos);
		CString strOut;
		strOut.Format("%s\n", (char*)pStr);
		hDlg->ReplaceSel(strOut);
*/
}
#endif

#define STR_BOGUS_CRC_ERROR "Data error (cyclic redundancy check)"
void ShowSystemError(CString& hOutputBox)
{
   DWORD LastErr;
   int test_int = 0;
  if(LastErr = GetLastError())
  {
	LPVOID lpMsgBuf;
	FormatMessage(     
	FORMAT_MESSAGE_ALLOCATE_BUFFER | 
	FORMAT_MESSAGE_FROM_SYSTEM |     
	FORMAT_MESSAGE_IGNORE_INSERTS,
	NULL,
	LastErr,
	MAKELANGID(LANG_NEUTRAL, 
	SUBLANG_DEFAULT), // Default language
	(LPTSTR) &lpMsgBuf,    
	0,    
	NULL 
	);// Process any inserts in lpMsgBuf.
	if( !memcmp((char *)lpMsgBuf, STR_BOGUS_CRC_ERROR, strlen(STR_BOGUS_CRC_ERROR)) )
	{// Supress bogus CRC error.
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Endpoint Error"); 
	}
	else
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)lpMsgBuf);
	LocalFree( lpMsgBuf );
  }
}

BOOL DoIoctl(
  HANDLE hDevice,              // handle to device of interest
  DWORD dwIoControlCode,       // control code of operation to perform
  LPVOID lpInBuffer,           // pointer to buffer to supply input data
  DWORD nInBufferSize,         // size, in bytes, of input buffer
  LPVOID lpOutBuffer,          // pointer to buffer to receive output data
  DWORD nOutBufferSize,        // size, in bytes, of output buffer
  LPDWORD lpBytesReturned,     // pointer to variable to receive byte count
  LPOVERLAPPED lpOverlapped,    // pointer to structure for asynchronous operation
  CString& hOutputBox
)
{
    char                   temp[512]  = "";
    char                   temp2[64]  = "";
	ULONG	i;
	PUCHAR	ptr;
    BOOLEAN bResult                         = FALSE;
					if (hDevice != NULL) 
					{
						bResult = DeviceIoControl (hDevice,
						dwIoControlCode,
						lpInBuffer,
						nInBufferSize,
						lpOutBuffer,
						nOutBufferSize,
						lpBytesReturned,
						lpOverlapped);
					}/* if valid driver handle */
                
					if (bResult==TRUE)
					{
#ifdef TNG_TEST
						if(dwIoControlCode == IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST)
							*lpBytesReturned = sizeof(TYPE_DMA_TEST_RESULT);
#endif
//						if(theApp.m_nVerbose)
//							DumpBuffer(lpOutBuffer, *lpBytesReturned, hOutputBox);
///*
						if(theApp.m_nVerbose)
////							DumpBuffer(lpOutBuffer, *lpBytesReturned, hOutputBox);
						{
							ptr = (PUCHAR) lpOutBuffer;
							wsprintf(temp,"%04X ",0x0);
							for (i = 0; i < (*lpBytesReturned - 16); i++)
							{
								wsprintf(temp2,"%02X ",*ptr++);
								strcat(temp,temp2);
							}
//							ptr = (PUCHAR) lpOutBuffer;
							for (; i < (*lpBytesReturned - 8); i++)
							{
									wsprintf(temp2,"%c",*ptr++);
								strcat(temp,temp2);
							}
							wsprintf(temp2," ");
							strcat(temp,temp2);
							for (; i < *lpBytesReturned; i++)
							{
									wsprintf(temp2,"%c",*ptr++);
								strcat(temp,temp2);
							}
							EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
						}
//*/
						/*
						char                   temp[64]  = "";
						wsprintf (temp, "NOTE: nBytes=%d", *lpBytesReturned);
						if(theApp.m_nVerbose)
							EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
						*/
					}
					else
					{
						EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Vendor Request failed");
						ShowSystemError(hOutputBox);
					}
					return(bResult);
}

⌨️ 快捷键说明

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