Hi all, I am using the following code to Read/Write to one stream driver. This works fine if Read is proper, but in case of error,it is giving problem. following is the code:
Code:
int main(int argc, _TCHAR* argv[])
{
HANDLE hFTMdrv = INVALID_HANDLE_VALUE;
HANDLE hDiagdrv = INVALID_HANDLE_VALUE;
HANDLE EvtHdl = NULL;
BYTE OutWritebuff[MAX_LEN_OUT_WRITE_TRANSPORT]= {0x1};
BYTE INwriteBuff[FTM_HCI_CMD_MAX_SIZE] = {0};
DWORD NumBytesRead = 0;
BOOL bError = TRUE;
int nError = 0;
DWORD j = 0;
//Initiate FTM main struct
//InitiateStruct(&g_ftmtest);
//Open FTM driver Handle
CHECK_FALSE(OpenFTMDrv(&g_ftmtest));
//Open DIAG driver Handle
CHECK_FALSE(OpenDiagDrv(&hDiagdrv));
//Intiiate DIAG driver
CHECK_FALSE(InitiateDiagChannel(&g_ftmtest));
//Now register for CMD with DIAG
CHECK_FALSE(RegisterforDiagCmd(&g_ftmtest));
CHECK_FALSE(AllocateDiagBuffer(&g_ftmtest));
CHECK_FALSE(AllocateTransportBuffer(&g_ftmtest));
callextraRead(&g_ftmtest); //There is one extra event coming at the boot time, removing it by reading.
ResetEvent(g_ftmtest.hEvt);
#ifdef SIMULATE_DIAG_READ
//Now create Read thread that wait infinitely for Read on transport driver
wprintf(L"FTMApp:Creating thread\n",nError);
CHECK_FALSE(CreateReadThread1(&g_ftmtest));
#endif
while(g_ftmtest.hEvt != NULL){
wprintf(L"FTMApp:Waiting for the Event to be set from DIAG\n");
// Diag Event occured
if ((WaitForSingleObject(g_ftmtest.hEvt, 30000)) == WAIT_OBJECT_0)
{
wprintf(L"*****FTMApp:in WaitForSingleObject\n");
//set current FTM state
//g_ftmtest.FTMstate = BT_FTM_BUSY;
#ifdef SIMULATE_DIAG_READ
//Read and pasrse the cmd from the DIAG and fill struct
CHECK_FALSE(ReadDiagCmd_replicate(&g_ftmtest)); //AJ : to be removed after testing
#else //Real Time
//Read and pasrse the cmd from the DIAG and fill struct
CHECK_FALSE(ReadDiagCmd(&g_ftmtest));
//Now create Read thread that wait infinitely for Read on transport driver
CHECK_FALSE(CreateReadThread(&g_ftmtest));
#endif
memset(g_ftmtest.buffTransport.pWrite, 0, FTM_HCI_CMD_MAX_SIZE);
switch(g_ftmtest.diagpktinfo.ss_cc)
{
case FTM_BT_SUSSUBSYS_ID :
{
g_ftmtest.diagpktinfo.payload_len = (g_ftmtest.BytesReadfromDiag - QRCT_HEADER_LENGTH); //10 byte is QRCT header
//make sure lengths are correct and as per driver's expectations
if(g_ftmtest.diagpktinfo.payload_len >= MIN_LEN_WRITE_TRANSPORT )
{
assert(pg_ftmtest->buffTransport.pWrite != NULL);
CHECK_FALSE(CheckPayloadifCommand(&g_ftmtest));
//send write IOCTL with parsed command
memcpy((void*)(g_ftmtest.buffTransport.pWrite), (void*)(g_ftmtest.buffdiag.pRead +QRCT_HEADER_LENGTH+1), (g_ftmtest.diagpktinfo.payload_len-1));
if(!(DeviceIoControl(g_ftmtest.hFTMdrv, (ULONG)IOCTL_BTFTM_HCI_WRITE, (g_ftmtest.buffTransport.pWrite), (g_ftmtest.diagpktinfo.payload_len-1), OutWritebuff,
MAX_LEN_OUT_WRITE_TRANSPORT, &g_ftmtest.bytesReturnedfromtransportWrite, NULL)))
{
nError = GetLastError();
wprintf(L"FTMApp:Error :IOCTL_BTFTM_HCI_WRITE failed Error =%x\n",nError);
CloseHandle(g_ftmtest.hReadThread);
g_ftmtest.hReadThread = NULL;
}
//wprintf(L"FTMApp:biggrin:IAG_SUBSYS_FTM, Write Successful,Bytes returned=%d" ,g_ftmtest.bytesReturnedfromtransportWrite);
g_ftmtest.bytesReturnedfromtransportWrite = 0;
//Sleep(100);
}
else{
wprintf(L"FTMApp:Error: Command does not seem right\n");
CloseHandle(g_ftmtest.hReadThread);
g_ftmtest.hReadThread = NULL;
}
}
break;
default:
wprintf(L"IN FTMApp:main: Received un-supported command \n");
CloseHandle(g_ftmtest.hReadThread);
g_ftmtest.hReadThread = NULL;
break;
}
}
ResetEvent(g_ftmtest.hEvt);
wprintf(L"FTMApp:Event is reset\n");
} //while()
Exit:
FTMApp_Cleanup(&g_ftmtest);
}
Here if the command parsed is not right it goes into block which has:
wprintf(L"FTMApp:Error: Command does not seem right\n");
and then prints:
FTMApp:Event is reset
FTMApp:Waiting for the Event to be set from DIAG
*****FTMApp:in WaitForSingleObject
"FTMApp:Error: Command does not seem right
FTMApp:Event is reset
FTMApp:Waiting for the Event to be set from DIAG
*****FTMApp:in WaitForSingleObject
"FTMApp:Error: Command does not seem right
FTMApp:Event is reset
FTMApp:Waiting for the Event to be set from DIAG
while it should be non signaled just after it comes out of error block and executes ResetEvent() first time itself. But it is setting and resetting twice and then stops responding. Means program hangs after it. Any clue will be highly appreciated.
thanks,
AJ