xmpl_linbuf_tx.c - Using Linear Buffers for Data Transmission

Application Description

The application fills a piece of text as chunks in a linear buffer and sends it. The example illustrates

- the initialization of a linear buffer (see Buffer Managment),
- the appending of characters as payload,
- the prepending of a frame header prior the payload,
- the extraction of data pointer and data size from the buffer.
- reset of the buffer.

Code Example

    /* $Id$ */
    /* Example use of the buffer functions */
    #include <stdio.h>
    #include "board.h"
    #include "hif.h"
    #include "radio.h"
    #include "xmpl.h"

    int main(void)
    {

    /* buffer is smaller than a limmerick line */
    #define XMPL_FRAME_SIZE (40)
    /*two byte more for the CRC */
    uint8_t txbuf[sizeof(buffer_t) + XMPL_FRAME_SIZE + 2];

    uint8_t frame_header[] = {0x01, 0x80, 0, 0x11,0x22,0x33,0x44};
    buffer_t *pbuf;
    //                           1         2         3         4
    //                  1234567890123456789012345678901234567890123456789
    char limmerick[] = "A wonderful bird is the pelican,\n\r"
                       "His bill will hold more than his belican,\n\r"
                       "He can take in his beak\n\r"
                       "Enough food for a week\n\r"
                       "But I'm damned if I see how the helican!\n\r\n\r";

    char *plim;

        /* Prerequisite: Init radio */
        LED_INIT();
        radio_init(NULL, 0);
        sei();
        radio_set_param(RP_CHANNEL(CHANNEL));
        radio_set_state(STATE_TX);

        /* Initialize the buffer structure */
        pbuf = buffer_init(txbuf, sizeof(txbuf)-2, sizeof(frame_header));
        plim = limmerick;
        while(1)
        {
            /* fill buffer until '\n' or '\0' or EOF is reached */
            do
            {
                if (buffer_append_char(pbuf, *plim) == EOF)
                {
                    break;
                }
                plim++;
            }
            while (*plim != 0 && *plim != '\n');

            /* finalize the buffer and transmit it */
            buffer_prepend_block(pbuf, frame_header, sizeof(frame_header));
            radio_set_state(STATE_TX);
            radio_send_frame(BUFFER_SIZE(pbuf)+ 2, BUFFER_PDATA(pbuf), 0);

            /* wait after this run */
            LED_TOGGLE(0);
            WAIT_MS(500);

            /* prepare next run */
            BUFFER_RESET(pbuf,sizeof(frame_header));
            frame_header[2]++;
            if (*plim == 0) plim = limmerick;
        }
    }
    /* XEOF */