1 /* netq.h -- Simple packet queue 2 * 3 * Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org> 4 * 5 * This file is part of the library tinyDTLS. Please see the file 6 * LICENSE for terms of use. 7 */ 8 9 #ifndef _DTLS_NETQ_H_ 10 #define _DTLS_NETQ_H_ 11 12 #include "tinydtls.h" 13 #include "global.h" 14 #include "dtls.h" 15 #include "dtls_time.h" 16 17 /** 18 * \defgroup netq Network Packet Queue 19 * The netq utility functions implement an ordered queue of data packets 20 * to send over the network and can also be used to queue received packets 21 * from the network. 22 * @{ 23 */ 24 25 #ifndef NETQ_MAXCNT 26 #ifdef DTLS_ECC 27 #define NETQ_MAXCNT 5 /**< maximum number of elements in netq structure */ 28 #elif defined(DTLS_PSK) 29 #define NETQ_MAXCNT 3 /**< maximum number of elements in netq structure */ 30 #endif 31 #endif 32 33 /** 34 * Datagrams in the netq_t structure have a fixed maximum size of 35 * DTLS_MAX_BUF to simplify memory management on constrained nodes. */ 36 typedef unsigned char netq_packet_t[DTLS_MAX_BUF]; 37 38 typedef struct netq_t { 39 struct netq_t *next; 40 41 clock_time_t t; /**< when to send PDU for the next time */ 42 unsigned int timeout; /**< randomized timeout value */ 43 44 dtls_peer_t *peer; /**< remote address */ 45 uint16_t epoch; 46 uint8_t type; 47 unsigned char retransmit_cnt; /**< retransmission counter, will be removed when zero */ 48 49 size_t length; /**< actual length of data */ 50 #ifndef WITH_CONTIKI 51 unsigned char data[]; /**< the datagram to send */ 52 #else 53 netq_packet_t data; /**< the datagram to send */ 54 #endif 55 } netq_t; 56 57 #ifndef WITH_CONTIKI netq_init()58static inline void netq_init() 59 { } 60 #else 61 void netq_init(); 62 #endif 63 64 /** 65 * Adds a node to the given queue, ordered by their time-stamp t. 66 * This function returns @c 0 on error, or non-zero if @p node has 67 * been added successfully. 68 * 69 * @param queue A pointer to the queue head where @p node will be added. 70 * @param node The new item to add. 71 * @return @c 0 on error, or non-zero if the new item was added. 72 */ 73 int netq_insert_node(list_t queue, netq_t *node); 74 75 /** Destroys specified node and releases any memory that was allocated 76 * for the associated datagram. */ 77 void netq_node_free(netq_t *node); 78 79 /** Removes all items from given queue and frees the allocated storage */ 80 void netq_delete_all(list_t queue); 81 82 /** Creates a new node suitable for adding to a netq_t queue. */ 83 netq_t *netq_node_new(size_t size); 84 85 /** 86 * Returns a pointer to the first item in given queue or NULL if 87 * empty. 88 */ 89 netq_t *netq_head(list_t queue); 90 91 netq_t *netq_next(netq_t *p); 92 void netq_remove(list_t queue, netq_t *p); 93 94 /** 95 * Removes the first item in given queue and returns a pointer to the 96 * removed element. If queue is empty when netq_pop_first() is called, 97 * this function returns NULL. 98 */ 99 netq_t *netq_pop_first(list_t queue); 100 101 /**@}*/ 102 103 #endif /* _DTLS_NETQ_H_ */ 104