Contiki-NG
uip-packetqueue.c
1 #include <stdio.h>
2 
3 #include "net/ipv6/uip.h"
4 
5 #include "lib/memb.h"
6 
7 #include "net/ipv6/uip-packetqueue.h"
8 
9 #define MAX_NUM_QUEUED_PACKETS 2
10 MEMB(packets_memb, struct uip_packetqueue_packet, MAX_NUM_QUEUED_PACKETS);
11 
12 #define DEBUG 0
13 #if DEBUG
14 #include <stdio.h>
15 #define PRINTF(...) printf(__VA_ARGS__)
16 #else
17 #define PRINTF(...)
18 #endif
19 
20 /*---------------------------------------------------------------------------*/
21 static void
22 packet_timedout(void *ptr)
23 {
24  struct uip_packetqueue_handle *h = ptr;
25 
26  PRINTF("uip_packetqueue_free timed out %p\n", h);
27  memb_free(&packets_memb, h->packet);
28  h->packet = NULL;
29 }
30 /*---------------------------------------------------------------------------*/
31 void
32 uip_packetqueue_new(struct uip_packetqueue_handle *handle)
33 {
34  PRINTF("uip_packetqueue_new %p\n", handle);
35  handle->packet = NULL;
36 }
37 /*---------------------------------------------------------------------------*/
38 struct uip_packetqueue_packet *
39 uip_packetqueue_alloc(struct uip_packetqueue_handle *handle, clock_time_t lifetime)
40 {
41  PRINTF("uip_packetqueue_alloc %p\n", handle);
42  if(handle->packet != NULL) {
43  PRINTF("alloced\n");
44  return NULL;
45  }
46  handle->packet = memb_alloc(&packets_memb);
47  if(handle->packet != NULL) {
48  ctimer_set(&handle->packet->lifetimer, lifetime,
49  packet_timedout, handle);
50  } else {
51  PRINTF("uip_packetqueue_alloc failed\n");
52  }
53  return handle->packet;
54 }
55 /*---------------------------------------------------------------------------*/
56 void
57 uip_packetqueue_free(struct uip_packetqueue_handle *handle)
58 {
59  PRINTF("uip_packetqueue_free %p\n", handle);
60  if(handle->packet != NULL) {
61  ctimer_stop(&handle->packet->lifetimer);
62  memb_free(&packets_memb, handle->packet);
63  handle->packet = NULL;
64  }
65 }
66 /*---------------------------------------------------------------------------*/
67 uint8_t *
68 uip_packetqueue_buf(struct uip_packetqueue_handle *h)
69 {
70  return h->packet != NULL? h->packet->queue_buf: NULL;
71 }
72 /*---------------------------------------------------------------------------*/
73 uint16_t
74 uip_packetqueue_buflen(struct uip_packetqueue_handle *h)
75 {
76  return h->packet != NULL? h->packet->queue_buf_len: 0;
77 }
78 /*---------------------------------------------------------------------------*/
79 void
80 uip_packetqueue_set_buflen(struct uip_packetqueue_handle *h, uint16_t len)
81 {
82  if(h->packet != NULL) {
83  h->packet->queue_buf_len = len;
84  }
85 }
86 /*---------------------------------------------------------------------------*/
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
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 ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
Memory block allocation routines.
Header file for the uIP TCP/IP stack.
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:90