📄 1394main.c
字号:
hDevInfo = SetupDiGetClassDevs( guid,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_INTERFACEDEVICE)
);
if (!hDevInfo) {
dwError = GetLastError();
TRACE(TL_ERROR, (hWnd, "SetupDiGetClassDevs failed = 0x%x\r\n", dwError));
}
else {
// here we're going to loop and find all of the test
// 1394 interfaces available.
while (TRUE) {
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (SetupDiEnumDeviceInterfaces(hDevInfo, 0, guid, i, &deviceInterfaceData)) {
// figure out the size...
bReturn = SetupDiGetDeviceInterfaceDetail( hDevInfo,
&deviceInterfaceData,
NULL,
0,
&requiredSize,
NULL
);
if (!bReturn) {
dwError = GetLastError();
TRACE(TL_ERROR, (hWnd, "SetupDiGetDeviceInterfaceDetail failed (size) = 0x%x\r\n", dwError));
}
DeviceInterfaceDetailData = LocalAlloc(LPTR, requiredSize);
DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
TRACE(TL_TRACE, (hWnd, "DeviceInterfaceDetailData = 0x%x\r\n", DeviceInterfaceDetailData));
bReturn = SetupDiGetDeviceInterfaceDetail( hDevInfo,
&deviceInterfaceData,
DeviceInterfaceDetailData,
requiredSize,
NULL,
NULL
);
if (!bReturn) {
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_ITEMS) {
TRACE(TL_ERROR, (hWnd, "SetupDiGetDeviceInterfaceDetail failed (actual) = 0x%x\r\n", dwError));
}
LocalFree(DeviceInterfaceDetailData);
break;
}
else {
// we have a device, lets save it.
pDeviceData->numDevices++;
lstrcpy(pDeviceData->deviceList[i].DeviceName, DeviceInterfaceDetailData->DevicePath);
LocalFree(DeviceInterfaceDetailData);
}
}
else {
dwError = GetLastError();
TRACE(TL_ERROR, (hWnd, "SetupDiEnumDeviceInterfaces failed = 0x%x\r\n", dwError));
break;
}
i++;
} // while
SetupDiDestroyDeviceInfoList(hDevInfo);
}
return dwError;
}
void
InternalGetDeviceList(
HWND hWnd
)
{
PDEVICE_DATA DiagDeviceData;
PDEVICE_DATA VDevDeviceData;
GUID t1394DiagGuid = GUID_1394DIAG;
GUID t1394VDevGuid = GUID_1394VDEV;
TRACE(TL_TRACE, (hWnd, "Enter InternalGetDeviceList\r\n"));
//StopBusResetThread(hWnd);
// zero out number of devices and discover all the ones attached
DiagDeviceData = &SharedData->DiagDeviceData;
DiagDeviceData->numDevices = 0;
VDevDeviceData = &SharedData->VDevDeviceData;
VDevDeviceData->numDevices = 0;
// get 1394Diag and 1394Vdev lists
InternalFillDeviceList (hWnd, &t1394DiagGuid, DiagDeviceData);
InternalFillDeviceList (hWnd, &t1394VDevGuid, VDevDeviceData);
TRACE(TL_TRACE, (hWnd, "DiagDeviceData->numDevices = 0x%x\r\n", DiagDeviceData->numDevices));
TRACE(TL_TRACE, (hWnd, "VDevDeviceData->numDevices = 0x%x\r\n", VDevDeviceData->numDevices));
// if we have at least one device, try to start our bus
// reset thread. this will return if one's already been
// created.
if (DiagDeviceData->numDevices || VDevDeviceData->numDevices) {
//StartBusResetThread(hWnd);
}
TRACE(TL_TRACE, (hWnd, "Exit InternalGetDeviceList\r\n"));
} // InternalGetDeviceList
void
InternalRegisterClient(
HWND hWnd
)
{
PCLIENT_NODE ClientNode;
TRACE(TL_TRACE, (SharedData->g_hWnd, "Enter InternalRegisterClient\r\n"));
TRACE(TL_TRACE, (SharedData->g_hWnd, "hWnd = 0x%x\r\n", hWnd));
if (hWnd) {
ClientNode = LocalAlloc(LPTR, sizeof(CLIENT_NODE));
if (!ClientNode) {
TRACE(TL_ERROR, (hWnd, "Could not allocate ClientNode!\r\n"));
goto Exit_InternalRegisterClient;
}
ClientNode->hWnd = hWnd;
InsertTailList(&ClientList, (PLIST_NODE)ClientNode);
}
Exit_InternalRegisterClient:
TRACE(TL_TRACE, (SharedData->g_hWnd, "Exit InternalRegisterClient\r\n"));
} // InternalRegisterClient
void
InternalDeRegisterClient(
HWND hWnd
)
{
PCLIENT_NODE ClientNode;
TRACE(TL_TRACE, (SharedData->g_hWnd, "Enter InternalDeRegisterClient\r\n"));
TRACE(TL_TRACE, (SharedData->g_hWnd, "Removing hWnd = 0x%x\r\n", hWnd));
if (hWnd) {
ClientNode = (PCLIENT_NODE)ClientList.Next;
while (ClientNode) {
TRACE(TL_TRACE, (SharedData->g_hWnd, "Current hWnd = 0x%x\r\n", hWnd));
if (ClientNode->hWnd == hWnd) {
RemoveEntryList(&ClientList, (PLIST_NODE)ClientNode);
break;
}
ClientNode = ClientNode->Next;
}
}
TRACE(TL_TRACE, (SharedData->g_hWnd, "Exit InternalDeRegisterClient\r\n"));
} // InternalDeRegisterClient
void
NotifyClients(
HWND hWnd,
DWORD dwNotify
)
{
PCLIENT_NODE ClientNode;
TRACE(TL_TRACE, (hWnd, "Enter NotifyClients\r\n"));
switch (dwNotify) {
case NOTIFY_DEVICE_CHANGE:
TRACE(TL_TRACE, (hWnd, "NOTIFY_DEVICE_CHANGE\r\n"));
ClientNode = (PCLIENT_NODE)ClientList.Next;
while (ClientNode) {
SendMessage(ClientNode->hWnd, NOTIFY_DEVICE_CHANGE, 0, 0);
ClientNode = ClientNode->Next;
}
break; // NOTIFY_DEVICE_CHANGE
case NOTIFY_BUS_RESET:
TRACE(TL_TRACE, (hWnd, "NOTIFY_BUS_RESET\r\n"));
ClientNode = (PCLIENT_NODE)ClientList.Next;
while (ClientNode) {
SendMessage(ClientNode->hWnd, NOTIFY_BUS_RESET, 0, 0);
ClientNode = ClientNode->Next;
}
break; // NOTIFY_BUS_RESET
default:
break;
}
TRACE(TL_TRACE, (hWnd, "Exit NotifyClients\r\n"));
} // NotifyClients
void
StartBusResetThread(
HWND hWnd
)
{
//
// StartBusResetThread is only called if we find a 1394 test device
//
TRACE(TL_TRACE, (hWnd, "Enter StartBusResetThread\r\n"));
// make sure we don't already have a thread...
if (BusResetThreadParams) {
TRACE(TL_TRACE, (hWnd, "bus reset thread already exists\r\n"));
return;
}
// allocate our bus reset thread params...
BusResetThreadParams = LocalAlloc(LPTR, sizeof(BUS_RESET_THREAD_PARAMS));
if (!BusResetThreadParams) {
TRACE(TL_ERROR, (hWnd, "Could not allocate BusResetThreadParams!\r\n"));
goto Exit_StartBusResetThread;
}
ZeroMemory(BusResetThreadParams, sizeof(BUS_RESET_THREAD_PARAMS));
TRACE(TL_TRACE, (hWnd, "BusResetThreadParams = 0x%x\r\n", BusResetThreadParams));
TRACE(TL_TRACE, (hWnd, "Creating BusResetThread...\r\n"));
// create a bus reset thread...
// If we have a diag device use that, otherwise use vdev device
if (SharedData->DiagDeviceData.numDevices) {
BusResetThreadParams->szDeviceName = SharedData->DiagDeviceData.deviceList[0].DeviceName;
}
else if (SharedData->VDevDeviceData.numDevices) {
BusResetThreadParams->szDeviceName = SharedData->VDevDeviceData.deviceList[0].DeviceName;
}
else {
// no devices are present which can give us bus reset info, so free up memory and return
LocalFree(BusResetThreadParams);
return;
}
BusResetThreadParams->bKill = FALSE;
BusResetThreadParams->hThread = CreateThread( NULL,
0,
BusResetThread,
(LPVOID)BusResetThreadParams,
0,
&BusResetThreadParams->ThreadId
);
Exit_StartBusResetThread:
TRACE(TL_TRACE, (hWnd, "Exit StartBusResetThread\r\n"));
} // StartBusResetThread
void
StopBusResetThread(
HWND hWnd
)
{
TRACE(TL_TRACE, (hWnd, "Enter StopBusResetThread\r\n"));
// see if the thread's running
if (!BusResetThreadParams) {
TRACE(TL_TRACE, (hWnd, "no thread to stop...\r\n"));
return;
}
// kill the bus reset thread
BusResetThreadParams->bKill = TRUE;
if (BusResetThreadParams->hEvent)
{
SetEvent(BusResetThreadParams->hEvent);
}
// wait for the thread to complete
if (BusResetThreadParams->hThread)
{
WaitForSingleObject(BusResetThreadParams->hThread, INFINITE);
CloseHandle(BusResetThreadParams->hThread);
}
LocalFree(BusResetThreadParams);
BusResetThreadParams = NULL;
TRACE(TL_TRACE, (hWnd, "Exit StopBusResetThread\r\n"));
} // StopBusResetThread
DWORD
WINAPI
BusResetThread(
LPVOID lpParameter
)
{
PBUS_RESET_THREAD_PARAMS pThreadParams;
HANDLE hDevice;
DWORD dwRet, dwBytesRet;
OVERLAPPED overLapped;
pThreadParams = (PBUS_RESET_THREAD_PARAMS)lpParameter;
TRACE(TL_TRACE, (NULL, "Enter BusResetThread\r\n"));
// try to open the device
hDevice = OpenDevice(NULL, pThreadParams->szDeviceName, TRUE);
// device opened, so let's do loopback
if (hDevice != INVALID_HANDLE_VALUE) {
overLapped.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
pThreadParams->hEvent = overLapped.hEvent;
while (TRUE) {
DeviceIoControl( hDevice,
IOCTL_BUS_RESET_NOTIFY,
NULL,
0,
NULL,
0,
&dwBytesRet,
&overLapped
);
dwRet = GetLastError();
TRACE(TL_TRACE, (NULL, "BusThreadProc: DeviceIoControl: dwRet = %d\r\n", dwRet));
// we should always return pending, if not, something's wrong...
if (dwRet == ERROR_IO_PENDING) {
WaitForSingleObject(overLapped.hEvent, INFINITE);
ResetEvent(overLapped.hEvent);
if (!pThreadParams->bKill) {
if (dwRet) {
// bus reset!
TRACE(TL_TRACE, (NULL, "BusThreadProc: BUS RESET!!!\r\n"));
NotifyClients(NULL, NOTIFY_BUS_RESET);
}
else {
// we got an error, just break here and we'll exit
dwRet = GetLastError();
TRACE(TL_ERROR, (NULL, "BusThreadProc: dwRet = 0x%x\r\n"));
break;
}
}
else {
TRACE(TL_TRACE, (NULL, "BusThreadProc: Cancelling thread...\r\n"));
CancelIo(hDevice);
break;
}
}
else {
TRACE(TL_WARNING, (NULL, "BusThreadProc: IOCTL didn't return PENDING\r\n"));
break;
}
} // while
// free up all resources
CloseHandle(hDevice);
CloseHandle(overLapped.hEvent);
}
TRACE(TL_TRACE, (NULL, "Exit BusResetThread\r\n"));
// that's it, shut this thread down
ExitThread(0);
} // BusResetThread
//
// misc exported functions
//
void
WINAPI
DiagnosticMode(
HWND hWnd,
PSTR szBusName,
BOOL bMode,
BOOL bAll
)
{
HANDLE hDevice;
DWORD controlCode;
DWORD dwRet, dwBytesRet;
UCHAR BusName[32] = "\\\\.\\1394BUS ";
ULONG i = 0;
TRACE(TL_TRACE, (hWnd, "Enter DiagnosticMode\r\n"));
controlCode = (bMode) ? IOCTL_1394_TOGGLE_ENUM_TEST_ON : IOCTL_1394_TOGGLE_ENUM_TEST_OFF;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -