Contiki-NG
Loading...
Searching...
No Matches
uip-packetqueue.c
1#include "net/ipv6/uip-packetqueue.h"
2#include "lib/memb.h"
3#include <stdio.h>
4
5#define MAX_NUM_QUEUED_PACKETS 2
6MEMB(packets_memb, struct uip_packetqueue_packet, MAX_NUM_QUEUED_PACKETS);
7
8/*---------------------------------------------------------------------------*/
9#include "sys/log.h"
10#define LOG_MODULE "Packet-Q"
11#define LOG_LEVEL LOG_LEVEL_NONE
12/*---------------------------------------------------------------------------*/
13static void
14packet_timedout(void *ptr)
15{
16 struct uip_packetqueue_handle *h = ptr;
17
18 LOG_INFO("Timed out %p\n", h);
19 memb_free(&packets_memb, h->packet);
20 h->packet = NULL;
21}
22/*---------------------------------------------------------------------------*/
23void
24uip_packetqueue_new(struct uip_packetqueue_handle *handle)
25{
26 LOG_DBG("New %p\n", handle);
27 handle->packet = NULL;
28}
29/*---------------------------------------------------------------------------*/
30struct uip_packetqueue_packet *
31uip_packetqueue_alloc(struct uip_packetqueue_handle *handle,
32 clock_time_t lifetime)
33{
34 LOG_DBG("Alloc %p\n", handle);
35 if(handle->packet != NULL) {
36 LOG_DBG("Alloced\n");
37 return NULL;
38 }
39 handle->packet = memb_alloc(&packets_memb);
40 if(handle->packet != NULL) {
41 ctimer_set(&handle->packet->lifetimer, lifetime,
42 packet_timedout, handle);
43 } else {
44 LOG_ERR("Alloc failed\n");
45 }
46 return handle->packet;
47}
48/*---------------------------------------------------------------------------*/
49void
50uip_packetqueue_free(struct uip_packetqueue_handle *handle)
51{
52 LOG_DBG("Free %p\n", handle);
53 if(handle->packet != NULL) {
54 ctimer_stop(&handle->packet->lifetimer);
55 memb_free(&packets_memb, handle->packet);
56 handle->packet = NULL;
57 }
58}
59/*---------------------------------------------------------------------------*/
60uint8_t *
61uip_packetqueue_buf(const struct uip_packetqueue_handle *h)
62{
63 return h->packet != NULL ? h->packet->queue_buf: NULL;
64}
65/*---------------------------------------------------------------------------*/
66uint16_t
67uip_packetqueue_buflen(const struct uip_packetqueue_handle *h)
68{
69 return h->packet != NULL ? h->packet->queue_buf_len: 0;
70}
71/*---------------------------------------------------------------------------*/
72void
73uip_packetqueue_set_buflen(struct uip_packetqueue_handle *h, uint16_t len)
74{
75 if(h->packet != NULL) {
76 h->packet->queue_buf_len = len;
77 }
78}
79/*---------------------------------------------------------------------------*/
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition ctimer.c:150
static void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition ctimer.h:150
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition memb.c:78
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition memb.c:59
#define MEMB(name, structure, num)
Declare a memory block.
Definition memb.h:91
Header file for the logging system.
Memory block allocation routines.