www.pudn.com > Usb_Project.rar > rf_usb_diag.c


//////////////////////////////////////////////////////////////// 
// 
// This is a diagnostics application for accessing the USB device. 
// The code accesses the hardware via WinDriver functions. 
//  
// Copyright (c) 2003 - 2004 Jungo Ltd.  http://www.jungo.com 
//  
//////////////////////////////////////////////////////////////// 
 
#include  
#include  
 
#if defined(USB_DIAG_SAMPLE) 
 
#include "../../include/wdu_lib.h" 
#include "../../include/status_strings.h" 
#include "../../include/utils.h" 
#include "usb_diag_lib.h" 
 
// TODO: change the following definitions to match your device. 
#define DEFAULT_VENDOR_ID 0x1234 
#define DEFAULT_PRODUCT_ID 0x5678 
#define DEFAULT_LICENSE_STRING "12345abcde1234.license" 
 
#else 
 
// use in wizard's device-specific generated code 
#include "c:\windriver/include/wdu_lib.h" 
#include "c:\windriver/include/status_strings.h" 
#include "c:\windriver/include/utils.h" 
#include "usb_diag_lib.h" 
 
#define DEFAULT_VENDOR_ID         0x0471 
#define DEFAULT_PRODUCT_ID        0x0001 
#define DEFAULT_LICENSE_STRING    "6C3CC2FF76DF316EECA8011E8C7C6D27D725D9BA.fly" 
 
#endif 
 
#define USE_DEFAULT 0xffff 
#define ATTACH_EVENT_TIMEOUT 30 // in seconds 
#define TRANSFER_TIMEOUT 30000 // in msecs 
#if !defined(TRACE) 
#define TRACE printf 
#endif 
#if !defined(ERR) 
#define ERR printf 
#endif 
 
typedef struct DEVICE_CONTEXT 
{ 
    struct DEVICE_CONTEXT *pNext; 
    WDU_DEVICE_HANDLE hDevice; 
    DWORD dwVendorId; 
    DWORD dwProductId; 
    DWORD dwInterfaceNum; 
    DWORD dwAlternateSetting; 
} DEVICE_CONTEXT; 
 
typedef struct DRIVER_CONTEXT 
{ 
    HANDLE hEvent; 
    HANDLE hMutex; 
    DWORD dwDeviceCount; 
    DEVICE_CONTEXT *deviceContextList; 
    DEVICE_CONTEXT *pActiveDev; 
    HANDLE hDeviceUnusedEvent; 
} DRIVER_CONTEXT; 
 
char line[250]; 
 
BOOL DLLCALLCONV DeviceAttach(WDU_DEVICE_HANDLE hDevice,  
    WDU_DEVICE *pDeviceInfo, PVOID pUserData) 
{ 
    DRIVER_CONTEXT *pDrvCtx = (DRIVER_CONTEXT *)pUserData; 
    DEVICE_CONTEXT *pDevCtx, **ppDevCtx; 
    DWORD dwInterfaceNum = pDeviceInfo->pActiveInterface->pActiveAltSetting->Descriptor.bInterfaceNumber; 
    DWORD dwAlternateSetting = 0; 
     
    /* 
    // NOTE: For dwAlternateSetting != 0, call WDU_SetInterface() here 
    // (by default alternate setting 0 is set as the active alternate setting) 
    DWORD dwAttachError; 
 
    dwAttachError = WDU_SetInterface(hDevice, dwInterfaceNum, dwAlternateSetting); 
    if (dwAttachError) 
    { 
        ERR("DeviceAttach: WDU_SetInterface failed (num. %ld, alternate %ld) device 0x%p: error 0x%lx (\"%s\")\n", 
            dwInterfaceNum, dwAlternateSetting, hDevice,  
            dwAttachError, Stat2Str(dwAttachError)); 
 
        return FALSE; 
    } 
    */ 
 
    TRACE("DeviceAttach: received and accepted attach for vendor id 0x%x, " 
        "product id 0x%x, interface %ld, device handle 0x%p\n", 
        pDeviceInfo->Descriptor.idVendor, pDeviceInfo->Descriptor.idProduct, 
        dwInterfaceNum, hDevice); 
     
    // Add our device to the device list 
    pDevCtx = (DEVICE_CONTEXT *)malloc(sizeof(DEVICE_CONTEXT)); 
    if (!pDevCtx) 
    { 
        ERR("DeviceAttach: failed allocating memory\n"); 
        return FALSE; 
    } 
    BZERO(*pDevCtx); 
    pDevCtx->hDevice = hDevice; 
    pDevCtx->dwInterfaceNum = dwInterfaceNum; 
    pDevCtx->dwVendorId = pDeviceInfo->Descriptor.idVendor; 
    pDevCtx->dwProductId = pDeviceInfo->Descriptor.idProduct; 
    pDevCtx->dwAlternateSetting = dwAlternateSetting; 
     
    OsMutexLock(pDrvCtx->hMutex); 
    for (ppDevCtx = &pDrvCtx->deviceContextList; *ppDevCtx; ppDevCtx = &((*ppDevCtx)->pNext)); 
    *ppDevCtx = pDevCtx; 
    pDrvCtx->dwDeviceCount++; 
    OsMutexUnlock(pDrvCtx->hMutex); 
     
    OsEventSignal(pDrvCtx->hEvent); 
    // Accept control over this device 
    return TRUE; 
} 
 
VOID DLLCALLCONV DeviceDetach(WDU_DEVICE_HANDLE hDevice, PVOID pUserData) 
{ 
    DRIVER_CONTEXT *pDrvCtx = (DRIVER_CONTEXT *)pUserData; 
    DEVICE_CONTEXT **pCur; 
    DEVICE_CONTEXT *pTmpDev; 
    BOOL bDetachActiveDev = FALSE; 
 
    TRACE("DeviceDetach: received detach for device handle 0x%p\n", hDevice); 
 
    OsMutexLock(pDrvCtx->hMutex); 
    for (pCur = &pDrvCtx->deviceContextList;  
        *pCur && (*pCur)->hDevice != hDevice;  
        pCur = &((*pCur)->pNext)); 
     
    if (*pCur == pDrvCtx->pActiveDev) 
    { 
        bDetachActiveDev = TRUE; 
        pDrvCtx->pActiveDev = NULL; 
    } 
 
    pTmpDev = *pCur; 
    *pCur = pTmpDev->pNext; 
    free(pTmpDev); 
     
    pDrvCtx->dwDeviceCount--; 
    OsMutexUnlock(pDrvCtx->hMutex); 
 
    // Detach callback must not return as long as hDevice is being used. 
    if (bDetachActiveDev) 
        OsEventWait(pDrvCtx->hDeviceUnusedEvent, INFINITE); 
} 
 
void Start_ListenToPipe(DWORD PipeNum); 
void Stop_ListenToPipe(DWORD PipeNum); 
 
extern unsigned int selected_mode; 
 
void WfmSeleModeMenu(void) 
{ 
    int cmd; 
 
    Stop_ListenToPipe(0x81); 
     
    printf("1. Device Numbering [startNo]\n"); 
    printf("2. Factory finalizing\n"); 
    printf("3. Exception cleaning\n"); 
    printf("4. Continuity test \n"); 
    printf("5. Zeroing \n"); 
    printf("9. Monitoring\n"); 
     
    fgets(line, sizeof(line), stdin); 
    sscanf(line, "%ld", &cmd); 
 
    selected_mode = cmd; 
    printf("Selected Mode %d\n", selected_mode); 
     
    if(selected_mode != 0) 
    { 
        Start_ListenToPipe(0x81); 
    } 
     
} 
 
 
int RelaySeleModeMenu(void); 
int RelaySeleModeDone(void); 
 
void _RelaySeleModeMenu(void) 
{ 
    int cmd; 
 
    Stop_ListenToPipe(0x81); 
 
    cmd = RelaySeleModeMenu(); 
     
    if(cmd != 0) 
    { 
        Start_ListenToPipe(0x81); 
    } 
 
    RelaySeleModeDone(); 
} 
 
void DeviceDiagMenu(DRIVER_CONTEXT *pDrvCtx) 
{ 
    DWORD cmd; 
    DWORD dwPipeNum; 
    DWORD dwInterfaceNumber, dwAlternateSetting; 
    DWORD dwError; 
    WDU_DEVICE_HANDLE hDevice; 
 
    do { 
        if (!pDrvCtx->dwDeviceCount) 
        { 
            printf("\n"); 
            printf("No Devices are currently connected.\n"); 
            printf("Press Enter to re check or enter EXIT to exit\n"); 
            fgets(line, sizeof(line), stdin); 
            // Removing the '\n' character from the end 
            line[strlen(line)-1] = '\0'; 
 
            if (!stricmp(line, "EXIT")) 
                break; 
 
            continue; 
        } 
 
        OsMutexLock(pDrvCtx->hMutex); 
        if (!pDrvCtx->dwDeviceCount) 
        { 
            OsMutexUnlock(pDrvCtx->hMutex); 
            continue; 
        } 
         
        if (!pDrvCtx->pActiveDev) 
            pDrvCtx->pActiveDev = pDrvCtx->deviceContextList; 
         
        printf("\n"); 
        printf("Main Menu (active Dev/Prod/Interface/Alt. Setting: 0x%lx/0x%lx/%ld/%ld)\n", 
            pDrvCtx->pActiveDev->dwVendorId, pDrvCtx->pActiveDev->dwProductId,  
            pDrvCtx->pActiveDev->dwInterfaceNum, pDrvCtx->pActiveDev->dwAlternateSetting); 
        printf("----------\n"); 
        printf("1. Display device configurations\n"); 
        printf("2. Change interface alternate setting\n"); 
        printf("3. Reset Pipe\n"); 
        printf("4. Read/Write from pipes\n"); 
        if (pDrvCtx->dwDeviceCount > 1) 
            printf("5. Select device\n"); 
        printf("6. Water meter select Mode\n"); 
        printf("7. RF relay select Mode\n"); 
 
//        printf("11. Recording on/off\n"); 
 
        printf("99. Exit\n"); 
        printf("Enter option: "); 
        cmd = 0; 
        OsMutexUnlock(pDrvCtx->hMutex); 
 
        fgets(line, sizeof(line), stdin); 
        sscanf(line, "%ld", &cmd); 
         
        if (!pDrvCtx->pActiveDev) 
            continue; 
         
        OsEventReset(pDrvCtx->hDeviceUnusedEvent); 
         
        OsMutexLock(pDrvCtx->hMutex); 
        hDevice = pDrvCtx->pActiveDev->hDevice; 
        OsMutexUnlock(pDrvCtx->hMutex); 
 
        USB_Diag_LibInit("rf_usb.log", hDevice); 
 
        switch (cmd) 
        { 
        case 1: 
            PrintDeviceConfigurations(hDevice); 
            break; 
        case 2: 
            printf("Please enter the interface number (dec): "); 
            fgets(line, sizeof(line), stdin); 
            sscanf(line, "%ld", &dwInterfaceNumber); 
            printf("Please enter the alternate setting index (dec): "); 
            fgets(line, sizeof(line), stdin); 
            sscanf(line, "%ld", &dwAlternateSetting); 
            dwError = WDU_SetInterface(hDevice, dwInterfaceNumber, dwAlternateSetting); 
            if (dwError) 
            { 
                ERR("DeviceDiagMenu: WDU_SetInterface() failed: error 0x%lx (\"%s\")\n", 
                    dwError, Stat2Str(dwError)); 
            } 
            else 
            { 
                TRACE("DeviceDiagMenu: WDU_SetInterface() completed successfully\n"); 
                pDrvCtx->pActiveDev->dwInterfaceNum = dwInterfaceNumber; 
                pDrvCtx->pActiveDev->dwAlternateSetting = dwAlternateSetting; 
            } 
            break; 
        case 3: 
            printf("Please enter the pipe number (hex): 0x"); 
            fgets(line, sizeof(line), stdin); 
            sscanf(line, "%lx", &dwPipeNum); 
            printf("\n"); 
 
            dwError = WDU_ResetPipe(hDevice, dwPipeNum); 
            if (dwError) 
            { 
                ERR("DeviceDiagMenu: WDU_ResetPipe() failed: error 0x%lx (\"%s\")\n", 
                    dwError, Stat2Str(dwError)); 
            } 
            else 
                TRACE("DeviceDiagMenu: WDU_ResetPipe() completed successfully\n"); 
            break; 
        case 4: 
            ReadWritePipesMenu(hDevice); 
            break; 
        case 5: 
            if (pDrvCtx->dwDeviceCount > 1) 
            { 
                DWORD dwDeviceNum, i; 
                DEVICE_CONTEXT *pCur; 
 
                OsMutexLock(pDrvCtx->hMutex); 
                for (i=1, pCur=pDrvCtx->deviceContextList; pCur; pCur=pCur->pNext, i++) 
                { 
                    printf("  %ld. Vendor id: 0x%lx, Product id: 0x%lx, Interface " 
                        "number: %ld, Alt. Setting: %ld\n", i,  
                        pCur->dwVendorId, pCur->dwProductId, pCur->dwInterfaceNum, 
                        pCur->dwAlternateSetting); 
                } 
                OsMutexUnlock(pDrvCtx->hMutex); 
 
                printf("Please enter the device number (1 - %ld, dec): ", i-1); 
                fgets(line, sizeof(line), stdin); 
                sscanf(line, "%ld", &dwDeviceNum); 
 
                OsMutexLock(pDrvCtx->hMutex); 
                for (i=1, pCur=pDrvCtx->deviceContextList; pCur && ipNext, i++); 
                if (!pCur) 
                { 
                    printf("Device is not found\n"); 
                    OsMutexUnlock(pDrvCtx->hMutex); 
                    OsEventSignal(pDrvCtx->hDeviceUnusedEvent); 
                    continue; 
                } 
                pDrvCtx->pActiveDev = pCur; 
                OsMutexUnlock(pDrvCtx->hMutex); 
                break; 
            } 
        case 6: 
            WfmSeleModeMenu(); 
            break; 
        case 7: 
            _RelaySeleModeMenu(); 
            break; 
        case 11: 
            RF_USB_MAC_Init(); 
            break; 
        } 
        hDevice = NULL; 
        OsEventSignal(pDrvCtx->hDeviceUnusedEvent); 
    } while (cmd!=99); 
 
    USB_Diag_LibUninit(); 
         
} 
 
int main() 
{ 
    DWORD dwError; 
    WORD wVendorId = 0; 
    WORD wProductId = 0; 
    WDU_DRIVER_HANDLE hDriver = 0; 
    DRIVER_CONTEXT DrvCtx; 
 
    WDU_MATCH_TABLE matchTable; 
    WDU_EVENT_TABLE eventTable; 
 
    BZERO(DrvCtx); 
     
    wVendorId = USE_DEFAULT; 
    wProductId = USE_DEFAULT; 
 
#if defined(USB_DIAG_SAMPLE) 
    printf("Enter device vendor id (hex) (=0x%x):\n", DEFAULT_VENDOR_ID); 
    fgets(line, sizeof(line), stdin); 
    sscanf(line, "%hx", &wVendorId); 
     
    printf("Enter device product id (hex) (=0x%x):\n", DEFAULT_PRODUCT_ID); 
    fgets(line, sizeof(line), stdin); 
    sscanf(line, "%hx", &wProductId); 
#endif 
 
    // use defaults 
    if (wVendorId == USE_DEFAULT) 
        wVendorId = DEFAULT_VENDOR_ID; 
    if (wProductId == USE_DEFAULT) 
        wProductId = DEFAULT_PRODUCT_ID; 
 
    dwError = OsEventCreate(&DrvCtx.hEvent); 
    if (dwError) 
    { 
        ERR("main: OsEventCreate() failed on event 0x%p: error 0x%lx (\"%s\")\n", 
            DrvCtx.hEvent, dwError, Stat2Str(dwError)); 
        goto Exit; 
    } 
 
    dwError = OsMutexCreate(&DrvCtx.hMutex); 
    if (dwError) 
    { 
        ERR("main: OsMutexCreate() failed on mutex 0x%p: error 0x%lx (\"%s\")\n", 
            DrvCtx.hMutex, dwError, Stat2Str(dwError)); 
        goto Exit; 
    } 
     
    // When hDeviceUnusedEvent is not signaled, hDevice is in use, and therefore the  
    // detach callback will wait for it before returning. 
    // When it is signaled - hDevice is not being used. 
    dwError = OsEventCreate(&DrvCtx.hDeviceUnusedEvent); 
    if (dwError) 
    { 
        ERR("main: OsMutexCreate() failed on mutex 0x%p: error 0x%lx (\"%s\")\n", 
            DrvCtx.hDeviceUnusedEvent, dwError, Stat2Str(dwError)); 
        goto Exit; 
    } 
    OsEventSignal(DrvCtx.hDeviceUnusedEvent); 
     
    BZERO(matchTable); 
    matchTable.wVendorId = wVendorId; 
    matchTable.wProductId = wProductId; 
 
    BZERO(eventTable); 
    eventTable.pfDeviceAttach = DeviceAttach; 
    eventTable.pfDeviceDetach = DeviceDetach; 
    eventTable.pUserData = &DrvCtx; 
 
    dwError = WDU_Init(&hDriver, &matchTable, 1, &eventTable, DEFAULT_LICENSE_STRING, WD_ACKNOWLEDGE); 
    if (dwError) 
    { 
        ERR("main: failed to initialize USB driver: error 0x%lx (\"%s\")\n",  
                dwError, Stat2Str(dwError)); 
        goto Exit; 
    } 
    
    printf("Please make sure the device is attached:\n"); 
 
    // Wait for the device to be attached 
    dwError = OsEventWait(DrvCtx.hEvent, ATTACH_EVENT_TIMEOUT); 
    if (dwError) 
    { 
        if (dwError==WD_TIME_OUT_EXPIRED) 
            ERR("Timeout expired for connection with the device.\n"  
                "Check that the device is connected and try again.\n"); 
        else 
            ERR("main: OsEventWait() failed on event 0x%p: error 0x%lx (\"%s\")\n", 
                DrvCtx.hEvent, dwError, Stat2Str(dwError)); 
        goto Exit; 
    } 
 
    DeviceDiagMenu(&DrvCtx); 
Exit: 
    if (DrvCtx.hEvent) 
        OsEventClose(DrvCtx.hEvent); 
    if (DrvCtx.hMutex) 
        OsMutexClose(DrvCtx.hMutex); 
    if (DrvCtx.hDeviceUnusedEvent) 
        OsEventClose(DrvCtx.hDeviceUnusedEvent); 
    if (hDriver) 
        WDU_Uninit(hDriver); 
    return 0; 
}