117 lines
3.0 KiB
C
117 lines
3.0 KiB
C
/*! @file
|
|
@brief Network buffers
|
|
|
|
Network data is passed between application and IP stack via
|
|
a net_buf struct.
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2015 Intel Corporation
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* 2) Redistributions in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* 3) Neither the name of Intel Corporation nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software without
|
|
* specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <nanokernel.h>
|
|
#include <toolchain.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
#include <net/net_core.h>
|
|
#include <net/net_buf.h>
|
|
|
|
/* Available (free) buffers queue */
|
|
#define NUM_BUFS 2
|
|
static struct net_buf buffers[NUM_BUFS];
|
|
static struct nano_fifo free_bufs;
|
|
|
|
struct net_buf *net_buf_get_reserve(uint16_t reserve_head)
|
|
{
|
|
struct net_buf *buf;
|
|
|
|
buf = nano_fifo_get(&free_bufs);
|
|
if (!buf) {
|
|
NET_ERR("Failed to get free buffer\n");
|
|
return NULL;
|
|
}
|
|
|
|
buf->data = buf->buf + reserve_head;
|
|
buf->len = 0;
|
|
|
|
NET_DBG("buf %p reserve %u\n", buf, reserve_head);
|
|
|
|
return buf;
|
|
}
|
|
|
|
struct net_buf *net_buf_get(struct net_context *context)
|
|
{
|
|
struct net_buf *buf = net_buf_get_reserve(0);
|
|
|
|
if (!buf) {
|
|
return buf;
|
|
}
|
|
|
|
buf->context = context;
|
|
|
|
return buf;
|
|
}
|
|
|
|
void net_buf_put(struct net_buf *buf)
|
|
{
|
|
NET_DBG("buf %p\n", buf);
|
|
|
|
nano_fifo_put(&free_bufs, buf);
|
|
}
|
|
|
|
uint8_t *net_buf_add(struct net_buf *buf, uint16_t len)
|
|
{
|
|
uint8_t *tail = buf->data + buf->len;
|
|
buf->len += len;
|
|
return tail;
|
|
}
|
|
|
|
uint8_t *net_buf_push(struct net_buf *buf, uint16_t len)
|
|
{
|
|
buf->data -= len;
|
|
buf->len += len;
|
|
return buf->data;
|
|
}
|
|
|
|
uint8_t *net_buf_pull(struct net_buf *buf, uint16_t len)
|
|
{
|
|
buf->len -= len;
|
|
return buf->data += len;
|
|
}
|
|
|
|
void net_buf_init(void)
|
|
{
|
|
nano_fifo_init(&free_bufs);
|
|
|
|
for (int i = 0; i < NUM_BUFS; i++) {
|
|
nano_fifo_put(&free_bufs, &buffers[i]);
|
|
}
|
|
}
|