[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup packet
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Packet map and queue.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #ifndef __NET_PACKET_H__
|
---|
| 38 | #define __NET_PACKET_H__
|
---|
| 39 |
|
---|
| 40 | /** Packet identifier type.
|
---|
| 41 | * Value zero (0) is used as an invalid identifier.
|
---|
| 42 | */
|
---|
| 43 | typedef int packet_id_t;
|
---|
| 44 |
|
---|
| 45 | /** Type definition of the packet.
|
---|
| 46 | * @see packet
|
---|
| 47 | */
|
---|
| 48 | typedef struct packet * packet_t;
|
---|
| 49 |
|
---|
| 50 | /** Type definition of the packet pointer.
|
---|
| 51 | * @see packet
|
---|
| 52 | */
|
---|
| 53 | typedef packet_t * packet_ref;
|
---|
| 54 |
|
---|
[91478aa] | 55 | /** Type definition of the packet dimension.
|
---|
| 56 | * @see packet_dimension
|
---|
| 57 | */
|
---|
| 58 | typedef struct packet_dimension packet_dimension_t;
|
---|
| 59 |
|
---|
| 60 | /** Type definition of the packet dimension pointer.
|
---|
| 61 | * @see packet_dimension
|
---|
| 62 | */
|
---|
| 63 | typedef packet_dimension_t * packet_dimension_ref;
|
---|
| 64 |
|
---|
| 65 | /** Packet dimension.
|
---|
| 66 | */
|
---|
| 67 | struct packet_dimension{
|
---|
| 68 | /** Reserved packet prefix length.
|
---|
| 69 | */
|
---|
| 70 | size_t prefix;
|
---|
| 71 | /** Maximal packet content length.
|
---|
| 72 | */
|
---|
| 73 | size_t content;
|
---|
| 74 | /** Reserved packet suffix length.
|
---|
| 75 | */
|
---|
| 76 | size_t suffix;
|
---|
| 77 | /** Maximal packet address length.
|
---|
| 78 | */
|
---|
| 79 | size_t addr_len;
|
---|
| 80 | };
|
---|
| 81 |
|
---|
[21580dd] | 82 | /** @name Packet management system interface
|
---|
| 83 | */
|
---|
| 84 | /*@{*/
|
---|
| 85 |
|
---|
| 86 | /** Finds the packet mapping.
|
---|
| 87 | * @param[in] packet_id The packet identifier to be found.
|
---|
| 88 | * @returns The found packet reference.
|
---|
| 89 | * @returns NULL if the mapping does not exist.
|
---|
| 90 | */
|
---|
| 91 | packet_t pm_find( packet_id_t packet_id );
|
---|
| 92 |
|
---|
| 93 | /** Adds the packet mapping.
|
---|
| 94 | * @param[in] packet The packet to be remembered.
|
---|
| 95 | * @returns EOK on success.
|
---|
| 96 | * @returns EINVAL if the packet is not valid.
|
---|
| 97 | * @returns EINVAL if the packet map is not initialized.
|
---|
| 98 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 99 | */
|
---|
| 100 | int pm_add( packet_t packet );
|
---|
| 101 |
|
---|
| 102 | /** Initializes the packet map.
|
---|
| 103 | * @returns EOK on success.
|
---|
| 104 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 105 | */
|
---|
| 106 | int pm_init( void );
|
---|
| 107 |
|
---|
| 108 | /** Releases the packet map.
|
---|
| 109 | */
|
---|
| 110 | void pm_destroy( void );
|
---|
| 111 |
|
---|
| 112 | /** Add packet to the sorted queue.
|
---|
| 113 | * The queue is sorted in the ascending order.
|
---|
| 114 | * The packet is inserted right before the packets of the same order value.
|
---|
[1e2e0c1e] | 115 | * @param[in,out] first The first packet of the queue. Sets the first packet of the queue. The original first packet may be shifted by the new packet.
|
---|
[21580dd] | 116 | * @param[in] packet The packet to be added.
|
---|
| 117 | * @param[in] order The packet order value.
|
---|
| 118 | * @param[in] metric The metric value of the packet.
|
---|
[1e2e0c1e] | 119 | * @returns EOK on success.
|
---|
| 120 | * @returns EINVAL if the first parameter is NULL.
|
---|
| 121 | * @returns EINVAL if the packet is not valid.
|
---|
[21580dd] | 122 | */
|
---|
[1e2e0c1e] | 123 | int pq_add( packet_t * first, packet_t packet, size_t order, size_t metric );
|
---|
[21580dd] | 124 |
|
---|
| 125 | /** Finds the packet with the given order.
|
---|
| 126 | * @param[in] first The first packet of the queue.
|
---|
| 127 | * @param[in] order The packet order value.
|
---|
| 128 | * @returns The packet with the given order.
|
---|
| 129 | * @returns NULL if the first packet is not valid.
|
---|
| 130 | * @returns NULL if the packet is not found.
|
---|
| 131 | */
|
---|
| 132 | packet_t pq_find( packet_t first, size_t order );
|
---|
| 133 |
|
---|
| 134 | /** Inserts packet after the given one.
|
---|
| 135 | * @param[in] packet The packet in the queue.
|
---|
| 136 | * @param[in] new_packet The new packet to be inserted.
|
---|
| 137 | * @returns EOK on success.
|
---|
| 138 | * @returns EINVAL if etiher of the packets is invalid.
|
---|
| 139 | */
|
---|
| 140 | int pq_insert_after( packet_t packet, packet_t new_packet );
|
---|
| 141 |
|
---|
| 142 | /** Detach the packet from the queue.
|
---|
| 143 | * @param[in] packet The packet to be detached.
|
---|
| 144 | * @returns The next packet in the queue. If the packet is the first one of the queue, this becomes the new first one.
|
---|
| 145 | * @returns NULL if there is no packet left.
|
---|
| 146 | * @returns NULL if the packet is not valid.
|
---|
| 147 | */
|
---|
| 148 | packet_t pq_detach( packet_t packet );
|
---|
| 149 |
|
---|
| 150 | /** Sets the packet order and metric attributes.
|
---|
| 151 | * @param[in] packet The packet to be set.
|
---|
| 152 | * @param[in] order The packet order value.
|
---|
| 153 | * @param[in] metric The metric value of the packet.
|
---|
| 154 | * @returns EOK on success.
|
---|
| 155 | * @returns EINVAL if the packet is invalid..
|
---|
| 156 | */
|
---|
| 157 | int pq_set_order( packet_t packet, size_t order, size_t metric );
|
---|
| 158 |
|
---|
| 159 | /** Sets the packet order and metric attributes.
|
---|
| 160 | * @param[in] packet The packet to be set.
|
---|
| 161 | * @param[out] order The packet order value.
|
---|
| 162 | * @param[out] metric The metric value of the packet.
|
---|
| 163 | * @returns EOK on success.
|
---|
| 164 | * @returns EINVAL if the packet is invalid..
|
---|
| 165 | */
|
---|
| 166 | int pq_get_order( packet_t packet, size_t * order, size_t * metric );
|
---|
| 167 |
|
---|
| 168 | /** Releases the whole queue.
|
---|
| 169 | * Detaches all packets of the queue and calls the packet_release() for each of them.
|
---|
| 170 | * @param[in] first The first packet of the queue.
|
---|
| 171 | * @param[in] packet_release The releasing function called for each of the packets after its detachment.
|
---|
| 172 | */
|
---|
| 173 | void pq_destroy( packet_t first, void ( * packet_release )( packet_t packet ));
|
---|
| 174 |
|
---|
| 175 | /** Returns the next packet in the queue.
|
---|
| 176 | * @param[in] packet The packet queue member.
|
---|
| 177 | * @returns The next packet in the queue.
|
---|
| 178 | * @returns NULL if there is no next packet.
|
---|
| 179 | * @returns NULL if the packet is not valid.
|
---|
| 180 | */
|
---|
| 181 | packet_t pq_next( packet_t packet );
|
---|
| 182 |
|
---|
| 183 | /** Returns the previous packet in the queue.
|
---|
| 184 | * @param[in] packet The packet queue member.
|
---|
| 185 | * @returns The previous packet in the queue.
|
---|
| 186 | * @returns NULL if there is no previous packet.
|
---|
| 187 | * @returns NULL if the packet is not valid.
|
---|
| 188 | */
|
---|
| 189 | packet_t pq_previous( packet_t packet );
|
---|
| 190 |
|
---|
| 191 | /*@}*/
|
---|
| 192 |
|
---|
| 193 | #endif
|
---|
| 194 |
|
---|
| 195 | /** @}
|
---|
| 196 | */
|
---|