Contiki 2.6

uip-packetqueue.c

00001 #include <stdio.h>
00002 
00003 #include "net/uip.h"
00004 
00005 #include "lib/memb.h"
00006 
00007 #include "net/uip-packetqueue.h"
00008 
00009 #define MAX_NUM_QUEUED_PACKETS 2
00010 MEMB(packets_memb, struct uip_packetqueue_packet, MAX_NUM_QUEUED_PACKETS);
00011 
00012 #define DEBUG 0
00013 #if DEBUG
00014 #include <stdio.h>
00015 #define PRINTF(...) printf(__VA_ARGS__)
00016 #else
00017 #define PRINTF(...)
00018 #endif
00019 
00020 /*---------------------------------------------------------------------------*/
00021 static void
00022 packet_timedout(void *ptr)
00023 {
00024   struct uip_packetqueue_handle *h = ptr;
00025 
00026   PRINTF("uip_packetqueue_free timed out %p\n", h);
00027   memb_free(&packets_memb, h->packet);
00028   h->packet = NULL;
00029 }
00030 /*---------------------------------------------------------------------------*/
00031 void
00032 uip_packetqueue_new(struct uip_packetqueue_handle *handle)
00033 {
00034   PRINTF("uip_packetqueue_new %p\n", handle);
00035   handle->packet = NULL;
00036 }
00037 /*---------------------------------------------------------------------------*/
00038 struct uip_packetqueue_packet *
00039 uip_packetqueue_alloc(struct uip_packetqueue_handle *handle, clock_time_t lifetime)
00040 {
00041   PRINTF("uip_packetqueue_alloc %p\n", handle);
00042   if(handle->packet != NULL) {
00043     PRINTF("alloced\n");
00044     return NULL;
00045   }
00046   handle->packet = memb_alloc(&packets_memb);
00047   if(handle->packet != NULL) {
00048     ctimer_set(&handle->packet->lifetimer, lifetime,
00049                packet_timedout, handle);
00050   } else {
00051     PRINTF("uip_packetqueue_alloc failed\n");
00052   }
00053   return handle->packet;
00054 }
00055 /*---------------------------------------------------------------------------*/
00056 void
00057 uip_packetqueue_free(struct uip_packetqueue_handle *handle)
00058 {
00059   PRINTF("uip_packetqueue_free %p\n", handle);
00060   if(handle->packet != NULL) {
00061     ctimer_stop(&handle->packet->lifetimer);
00062     memb_free(&packets_memb, handle->packet);
00063     handle->packet = NULL;
00064   }
00065 }
00066 /*---------------------------------------------------------------------------*/
00067 uint8_t *
00068 uip_packetqueue_buf(struct uip_packetqueue_handle *h)
00069 {
00070   return h->packet != NULL? h->packet->queue_buf: NULL;
00071 }
00072 /*---------------------------------------------------------------------------*/
00073 uint16_t
00074 uip_packetqueue_buflen(struct uip_packetqueue_handle *h)
00075 {
00076   return h->packet != NULL? h->packet->queue_buf_len: 0;
00077 }
00078 /*---------------------------------------------------------------------------*/
00079 void
00080 uip_packetqueue_set_buflen(struct uip_packetqueue_handle *h, uint16_t len)
00081 {
00082   if(h->packet != NULL) {
00083     h->packet->queue_buf_len = len;
00084   }
00085 }
00086 /*---------------------------------------------------------------------------*/