# xmpl_linbuf_rx.c - Using Linear Buffers for Data Reception

Application Description

Code Example

    /* $Id: xmpl_linbuf_rx.c,v 1.3 2010/04/26 19:33:39 awachtler Exp $ */
    /* Example use of the buffer functions for receive operation
     * This example expects frames to receive from xmpl_linbuf_tx.c
     */
    #include <stdio.h>
    #include "board.h"
    #include "hif.h"
    #include "radio.h"
    #include "xmpl.h"

    #define XMPL_FRAME_SIZE (40)

    uint8_t rxbuf[XMPL_FRAME_SIZE + 2];
    uint8_t buf[sizeof(buffer_t) + XMPL_FRAME_SIZE];
    buffer_t *pbuf;

    int main(void)
    {
    uint8_t frame_header[] = {0x01, 0x80, 0, 0x11,0x22,0x33,0x44};

        /* Init ressources */
        LED_INIT();
    #if HIF_TYPE != HIF_NONE
        hif_init(9600);
    #endif

        radio_init(rxbuf,sizeof(rxbuf));
        sei();
        radio_set_param(RP_CHANNEL(CHANNEL));
        radio_set_state(STATE_RX);

        /* Initialize buffer structure */
        pbuf = buffer_init(buf, sizeof(buf), 0);

        while(1)
        {
            if (BUFFER_IS_LOCKED(pbuf) == true)
            {
                uint8_t c, i, seq;
                /* do a header compare to ensure that it "our" frame */
                for(i=0; i<sizeof(frame_header);i++)
                {
                    c = buffer_get_char(pbuf);
                    if ( (i != 2) && (c != frame_header[i]))
                    {
                        break;
                    }
                    if(i==2)
                    {
                        seq = c;
                    }
                }

                if (i==7)
                {
                    /* if this frame belongs to us,
                       blink the LED and if available
                       send the payload to the UART */
                    LED_TOGGLE(0);
                    /* output at hif */
                #if HIF_TYPE != HIF_NONE
                    //PRINTF("\n\r %03d: ", seq);
                    hif_put_blk (BUFFER_PDATA(pbuf), BUFFER_SIZE(pbuf));
                #endif
                }
                else
                {
                    LED_TOGGLE(1);
                }
                BUFFER_RESET(pbuf,0);
                BUFFER_SET_UNLOCK(pbuf);
            }
            /* wait after this run */
            WAIT_MS(500);
        }
    }

    uint8_t * usr_radio_receive_frame(uint8_t len, uint8_t *frm, uint8_t lqi, int8_t ed, uint8_t crc)
    {
        if ( BUFFER_IS_LOCKED(pbuf) == false && crc == 0)
        {
            /* copy the payload, reduced by the CRC bytes */
            buffer_append_block(pbuf, frm, len-2);
            BUFFER_SET_LOCK(pbuf);
        }
        return frm;
    }

    /* XEOF */