📄 gps_interface.cpp
字号:
/*************************************************************************
cpp file for IoInterface class;
Autor :
Yu Lu
luyu1974@gmail.com
*************************************************************************/
#include "Gps_interface.h"
//---------------------------------------------------------------------------
/*++ Routine Description:
Constructor of class IoInterface;
Duty:
Open the handle, if success, set handleOpened true;
-- */
IoInterface::IoInterface()
{
count = 0;
ping_pong_flag = 0;
memset( (void *)gp2021regs, 0, 2*sizeof(gp2021_regs));
infofile.open(".\\log\\gps_interface.log",ios::out);
gpsHandle = OpenHandle();
if( gpsHandle != INVALID_HANDLE_VALUE )
{
handleOpened = true;
}
else
{
s_thread = NULL;
handleOpened = false;
}
}
//---------------------------------------------------------------------------
/*++
Routine Description:
Destructor of class interface;
if the gpsHandle is opened, close it;
--*/
IoInterface::~IoInterface()
{
if(handleOpened)
CloseHandle(gpsHandle);
if(s_thread)
{
delete s_thread;
}
}
//---------------------------------------------------------------------------
/*++
Routine Description:
Init proc for class;
Return non-zero if something wrong, otherwise return 0
Error code is defined in gbl_var.h
--*/
int IoInterface::InitProc()
{
if(!infofile)
return(IOINTERFACE_FILEOPENERR);
return (IOINTERFACE_SUCCESS);
}
//---------------------------------------------------------------------------
/*++
Routine Description:
print debug info for class;
--*/
void IoInterface::PrintDbgInfo(char* info)
{
if(infofile)
infofile << info << endl;
}
//---------------------------------------------------------------------------
/*++
Routine Description:
return the sataus of handleOpened;
--*/
bool IoInterface::IsHandleOpened()
{
return handleOpened;
}
//---------------------------------------------------------------------------
HANDLE IoInterface::OpenOneDevice(HDEVINFO HardwareDeviceInfo,
PSP_INTERFACE_DEVICE_DATA DeviceInfoData,
char *devName)
/*++
Routine Description:
Given the HardwareDeviceInfo, representing a handle to the plug and
play information, and deviceInfoData, representing a specific usb device,
open that device and fill in all the relevant information in the given
USB_DEVICE_DESCRIPTOR structure.
Arguments:
HardwareDeviceInfo: handle to info obtained from Pnp mgr via SetupDiGetClassDevs()
DeviceInfoData: ptr to info obtained via SetupDiEnumInterfaceDevice()
Return Value:
return HANDLE if the open and initialization was successfull,
else INVLAID_HANDLE_VALUE.
--*/
/***************************************************************************/
{
char buf[512];
PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
HANDLE hOut = INVALID_HANDLE_VALUE;
//
// 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 = (PSP_INTERFACE_DEVICE_DETAIL_DATA)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 INVALID_HANDLE_VALUE;
}
strcpy( devName,functionClassDeviceData->DevicePath) ;
sprintf(buf, "Attempting to open: %s", devName );
PrintDbgInfo(buf);
hOut = 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
if (INVALID_HANDLE_VALUE == hOut) {
sprintf(buf, "FAILED to CreateFile %s", devName);
PrintDbgInfo(buf);
}
free( functionClassDeviceData );
return hOut;
}
//---------------------------------------------------------------------------
HANDLE IoInterface::OpenUsbDevice( LPGUID pGuid, char *outNameBuf)
/*++
Routine Description:
Do the required PnP things in order to find
the next available proper device in the system at this time.
Arguments:
pGuid: ptr to GUID registered by the driver itself
outNameBuf: the generated name for this device
Return Value:
return HANDLE if the open and initialization was successful,
else INVLAID_HANDLE_VALUE.
--*/
/***************************************************************************/
{
ULONG NumberDevices;
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG i;
BOOLEAN done;
PUSB_DEVICE_DESCRIPTOR usbDeviceInst;
PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst;
char buf[512];
*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(
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 =
(PUSB_DEVICE_DESCRIPTOR)realloc(*UsbDevices, (NumberDevices * sizeof(USB_DEVICE_DESCRIPTOR)));
} else {
*UsbDevices = (PUSB_DEVICE_DESCRIPTOR)calloc(NumberDevices, sizeof(USB_DEVICE_DESCRIPTOR));
}
if (NULL == *UsbDevices) {
// SetupDiDestroyDeviceInfoList destroys a device information set
// and frees all associated memory.
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
return INVALID_HANDLE_VALUE;
}
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,
pGuid,
i,
&deviceInfoData))
{
hOut = OpenOneDevice(hardwareDeviceInfo, &deviceInfoData, outNameBuf);
if ( hOut != INVALID_HANDLE_VALUE ) {
done = TRUE;
break;
}
} else { // 'else' of "if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,"
sprintf(buf, "Error of SetupDiEnumDeviceInterfaces");
PrintDbgInfo(buf);
if(ERROR_NO_MORE_ITEMS == GetLastError())
{
done = TRUE;
break;
}
} // end of "if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,"
} // end of "for (; i < NumberDevices; i++) {"
} // end of "while (!done) {"
NumberDevices = i;
// SetupDiDestroyDeviceInfoList() destroys a device information set
// and frees all associated memory.
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
free ( *UsbDevices );
return hOut;
}
//---------------------------------------------------------------------------
/***************************************************************************/
HANDLE IoInterface::OpenHandle( void )
{
char buf[512];
HANDLE hDEV = OpenUsbDevice( (LPGUID)&GUID_INTERFACE_USBGPS, completeDeviceName);
if (hDEV == INVALID_HANDLE_VALUE) {
sprintf(buf,"Failed to open %s %s", completeDeviceName, GetLastError());
PrintDbgInfo(buf);
/*
ifofile << "\n Failed to open " << completeDeviceName << GetLastError();
*/
} else {
sprintf(buf,"Succeeding to open: %s %s", completeDeviceName, GetLastError());
PrintDbgInfo(buf);
}
return hDEV;
}
//---------------------------------------------------------------------------
/*++
Routine Description:
clear gp2021regs to all zeros;
--*/
void IoInterface::clear_all_reg(unsigned char bufferflag)
{
assert( bufferflag ==0 || bufferflag == 1);
memset( (void *)(gp2021regs+ bufferflag), 0, sizeof(gp2021_regs));
}
//---------------------------------------------------------------------------
/*++
Routine Description:
To read the register of USBN9603 , just for debugging
Argument:
addr: the address of register to be read;
out : the pointer that store the value of the register
return:
if successful , rutren true; otherwise return false
--*/
bool IoInterface::read_usbn9603_reg(const unsigned char addr, unsigned char *out)
{
char infobuf[256];
unsigned char buf[6]={0xaa, 'g', 0,0,0,0};
int nBytes;
if( handleOpened )
{
buf[4] = addr;
buf[5] = (buf[0]+buf[1]+buf[4]) % 256; // this is the checksum
if(!DeviceIoControl(gpsHandle,
IOCTL_USBGPS_BULK_WRITE,
buf,
6,
buf,
6,
(unsigned long *)&nBytes,
NULL) )
{
sprintf(infobuf, "An error occured writing data to usb handle");
PrintDbgInfo(infobuf);
return false;
}
if(!DeviceIoControl(gpsHandle,
IOCTL_USBGPS_BULK_READ,
NULL,
0,
buf,
6,
(unsigned long *)&nBytes,
NULL) )
{
sprintf(infobuf, "An error occured reading data from usb handle ");
PrintDbgInfo(infobuf);
return false;
}
*out = buf[0];
return true;
} // end of if( fileOpened )
else
return false;
}
//---------------------------------------------------------------------------
/*++
Routine Description:
To read all the registers of USBN9603 , just for debugging
Argument:
out : the pointer that store the values of the registers
return:
if successful , rutren true; otherwise return false
--*/
bool IoInterface::read_usbn9603_allreg( unsigned char *out)
{
char infobuf[256];
unsigned char buf[6]={0xaa, 'b', 0,0,0,0}, reg_buf[64];
int nBytes;
if( handleOpened )
{
buf[5] = (buf[0]+buf[1]) % 256; // this is the checksum
if(!DeviceIoControl(gpsHandle,
IOCTL_USBGPS_BULK_WRITE,
buf,
6,
buf,
6,
(unsigned long *)&nBytes,
NULL) )
{
sprintf(infobuf, "An error occured writing data to usb handle ");
PrintDbgInfo(infobuf);
return false;
}
if(!DeviceIoControl(gpsHandle,
IOCTL_USBGPS_BULK_READ,
NULL,
0,
reg_buf,
63,
(unsigned long *)&nBytes,
NULL) )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -