1.2.7.2 Using The Library

The RF PHY Sniffer library is an add-on that can be used along with the RF215 Driver. The RF215 driver is the one in charge of RF communications, while the Sniffer is used to encode the packets containing the RF frames and then send them through a serial interface.

Following examples illustrate how to use the Sniffer library (which in turn uses the USI service to send the frames to an external tool), to build a Sniffer application. Both G3 and PRIME based examples are provided.

Example implementation of a G3 Sniffer

APP_DATA appData;

static void _APP_RfRxIndCb(DRV_RF215_RX_INDICATION_OBJ* indObj, uintptr_t ctxt)
{
    DRV_RF215_PHY_CFG_OBJ rfPhyCfg;
    uint8_t* pRfSnifferData;
    size_t rfSnifferDataSize;
    uint16_t rfPayloadSymbols;
    
    /* Avoid warning */
    (void) ctxt;
    
    /* Get payload symbols in the received message */
    DRV_RF215_GetPib(appData.drvRf215Handle, RF215_PIB_PHY_RX_PAY_SYMBOLS, &rfPayloadSymbols);
    
    /* Get PHY configuration */
    DRV_RF215_GetPib(appData.drvRf215Handle, RF215_PIB_PHY_CONFIG, &rfPhyCfg);
    
    /* Serialize received RF message */
    pRfSnifferData = SRV_RSNIFFER_SerialRxMessage(indObj, &rfPhyCfg,
            rfPayloadSymbols, &rfSnifferDataSize);
    
    /* Send through USI */
    SRV_USI_Send_Message(appData.srvUSIHandle, SRV_USI_PROT_ID_SNIFF_G3,
            pRfSnifferData, rfSnifferDataSize);
}

void APP_Initialize(void)
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;

    /* Initialize driver handles */
    appData.drvRf215Handle = DRV_HANDLE_INVALID;
    appData.srvUSIHandle = DRV_HANDLE_INVALID;
}

void APP_Tasks(void)
{
    /* Check the application's current state. */
    switch(appData.state)
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            /* Check status of RF215 driver */
            SYS_STATUS rf215Status = DRV_RF215_Status(sysObj.drvRf215);
            if (rf215Status == SYS_STATUS_READY)
            {
                /* RF215 driver is ready to be opened */
                appData.drvRf215Handle = DRV_RF215_Open(DRV_RF215_INDEX_0, RF215_TRX_ID_RF09);
                if (appData.drvRf215Handle != DRV_HANDLE_INVALID)
                {
                    /* Register RF215 driver callback */
                    DRV_RF215_RxIndCallbackRegister(appData.drvRf215Handle, _APP_RfRxIndCb, 0);

                    /* Open USI Service */
                    appData.srvUSIHandle = SRV_USI_Open(SRV_USI_INDEX_0);

                    if (appData.srvUSIHandle != DRV_HANDLE_INVALID)
                    {
                        /* Set Application to next state */
                        appData.state = APP_STATE_CONFIG_USI;
                    }
                    else
                    {
                        /* Set Application to ERROR state */
                        appData.state = APP_STATE_ERROR;
                    }
                }
                else
                {
                    /* Set Application to ERROR state */
                    appData.state = APP_STATE_ERROR;
                }
            }
            else if (rf215Status == SYS_STATUS_ERROR)
            {
                /* Set Application to ERROR state */
                appData.state = APP_STATE_ERROR;
            }
            
            break;
        }

        case APP_STATE_CONFIG_USI:
        {
            if (SRV_USI_Status(appData.srvUSIHandle) == SRV_USI_STATUS_CONFIGURED)
            {
                /* Register USI callback */
                SRV_USI_CallbackRegister(appData.srvUSIHandle,
                        SRV_USI_PROT_ID_SNIFF_G3, APP_USIPhyProtocolEventHandler);
                    
                /* Set Application to next state */
                appData.state = APP_STATE_READY;
            }
            break;
        }

        case APP_STATE_READY:
        {
            /* Check USI status in case of USI device has been reset */
            if (SRV_USI_Status(appData.srvUSIHandle) == SRV_USI_STATUS_NOT_CONFIGURED)
            {
                /* Set Application to next state */
                appData.state = APP_STATE_CONFIG_USI; 
            }
            break;
        }

        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}

Example implementation of a PRIME Sniffer

APP_DATA appData;

static void _APP_RfRxIndCb(DRV_RF215_RX_INDICATION_OBJ* indObj, uintptr_t ctxt)
{
    DRV_RF215_PHY_CFG_OBJ rfPhyConfig;
    uint8_t* pRfSnifferData;
    size_t rfSnifferDataSize;
    uint16_t rfPayloadSymbols;
    uint16_t rfChannel;

    /* Avoid warning */
    (void) ctxt;

    /* Get payload symbols in the received message */
    DRV_RF215_GetPib(appData.drvRf215Handle, RF215_PIB_PHY_RX_PAY_SYMBOLS, &rfPayloadSymbols);

    /* Get RF PHY configuration */
    DRV_RF215_GetPib(appData.drvRf215Handle, RF215_PIB_PHY_CONFIG, &rfPhyConfig);
    DRV_RF215_GetPib(appData.drvRf215Handle, RF215_PIB_PHY_CHANNEL_NUM, &rfChannel);

    /* Serialize received RF message */
    pRfSnifferData = SRV_RSNIFFER_SerialRxMessage(indObj, &rfPhyConfig,
            rfPayloadSymbols, rfChannel, &rfSnifferDataSize);

    /* Send through USI */
    SRV_USI_Send_Message(appData.srvUSIHandle, SRV_USI_PROT_ID_SNIF_PRIME,
            pRfSnifferData, rfSnifferDataSize);
}

void APP_Initialize(void)
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;

    /* Initialize driver handles */
    appData.drvRf215Handle = DRV_HANDLE_INVALID;
    appData.srvUSIHandle = DRV_HANDLE_INVALID;
}

void APP_Tasks(void)
{
    /* Check the application's current state. */
    switch(appData.state)
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            /* Check status of RF215 driver */
            SYS_STATUS rf215Status = DRV_RF215_Status(sysObj.drvRf215);
            if (rf215Status == SYS_STATUS_READY)
            {
                /* RF215 driver is ready to be opened */
                appData.drvRf215Handle = DRV_RF215_Open(DRV_RF215_INDEX_0, RF215_TRX_ID_RF09);
                if (appData.drvRf215Handle != DRV_HANDLE_INVALID)
                {
                    /* Register RF215 driver callback */
                    DRV_RF215_RxIndCallbackRegister(appData.drvRf215Handle, _APP_RfRxIndCb, 0);

                    /* Open USI Service */
                    appData.srvUSIHandle = SRV_USI_Open(SRV_USI_INDEX_0);

                    if (appData.srvUSIHandle != DRV_HANDLE_INVALID)
                    {
                        /* Set Application to next state */
                        appData.state = APP_STATE_CONFIG_USI;
                    }
                    else
                    {
                        /* Set Application to ERROR state */
                        appData.state = APP_STATE_ERROR;
                    }
                }
                else
                {
                    /* Set Application to ERROR state */
                    appData.state = APP_STATE_ERROR;
                }
            }
            else if (rf215Status == SYS_STATUS_ERROR)
            {
                /* Set Application to ERROR state */
                appData.state = APP_STATE_ERROR;
            }
            
            break;
        }

        case APP_STATE_CONFIG_USI:
        {
            if (SRV_USI_Status(appData.srvUSIHandle) == SRV_USI_STATUS_CONFIGURED)
            {
                /* Register USI callback */
                SRV_USI_CallbackRegister(appData.srvUSIHandle,
                        SRV_USI_PROT_ID_SNIF_PRIME, APP_USIPhyProtocolEventHandler);
                    
                /* Set Application to next state */
                appData.state = APP_STATE_READY;
            }
            break;
        }

        case APP_STATE_READY:
        {
            /* Check USI status in case of USI device has been reset */
            if (SRV_USI_Status(appData.srvUSIHandle) == SRV_USI_STATUS_NOT_CONFIGURED)
            {
                /* Set Application to next state */
                appData.state = APP_STATE_CONFIG_USI; 
            }
            break;
        }

        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}