Thread: Reset event not working as exepected!!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Reset event not working as exepected!!

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I see a big dumb global variable called g_ftmtest which seems to be used for everything, and even seems to be passed by reference to another thread. This is a sure recipe for disaster.

    It appears to contain pointers, but you don't show how those are initialised.

    There's too much missing to even make a wild guess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2011, 08:23 AM
  2. Color not working when using an mouse event.
    By h3ro in forum C# Programming
    Replies: 2
    Last Post: 01-18-2011, 08:54 PM
  3. Intercept mouse event and change to different event
    By ByThaBay in forum Windows Programming
    Replies: 1
    Last Post: 03-19-2008, 05:44 PM
  4. strtok reset
    By lavinpj1 in forum C Programming
    Replies: 3
    Last Post: 04-25-2006, 06:00 PM
  5. help why does cnt not reset to 1??
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-17-2002, 12:56 AM