Rev 4731 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3901 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3901 | 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 | |||
3912 | mejdrech | 29 | /** @addtogroup packet |
3901 | mejdrech | 30 | * @{ |
31 | */ |
||
32 | |||
33 | /** @file |
||
3912 | mejdrech | 34 | * Packet client. |
35 | * The hosting module has to be compiled with both the packet.c and the packet_client.c source files. |
||
36 | * To function correctly, initialization of the packet map by the pm_init() function has to happen at the first place. |
||
37 | * The module should not send the packet messages to the packet server but use the functions provided. |
||
38 | * The packet map should be released by the pm_destroy() function during the module termination. |
||
3914 | mejdrech | 39 | * The packets and the packet queues can't be locked at all. |
40 | * The processing modules should process them sequentially - by passing the packets to the next module and stopping using the passed ones. |
||
3912 | mejdrech | 41 | * @see packet.h |
3901 | mejdrech | 42 | */ |
43 | |||
44 | #ifndef __NET_PACKET_CLIENT_H__ |
||
45 | #define __NET_PACKET_CLIENT_H__ |
||
46 | |||
47 | #include "packet.h" |
||
48 | |||
4704 | mejdrech | 49 | /** @name Packet client interface |
50 | */ |
||
51 | /*@{*/ |
||
52 | |||
3912 | mejdrech | 53 | /** Allocates the specified type right before the actual packet content and returns its pointer. |
54 | * The wrapper of the packet_prepend() function. |
||
4756 | mejdrech | 55 | * @param[in] packet The packet to be used. |
56 | * @param[in] type The type to be allocated at the beginning of the packet content. |
||
3912 | mejdrech | 57 | * @returns The typed pointer to the allocated memory. |
58 | * @returns NULL if the packet is not valid. |
||
59 | * @returns NULL if there is not enough memory left. |
||
60 | */ |
||
3990 | mejdrech | 61 | #define PACKET_PREFIX( packet, type ) ( type * ) packet_prefix(( packet ), sizeof( type )) |
3901 | mejdrech | 62 | |
3912 | mejdrech | 63 | /** Allocates the specified type right after the actual packet content and returns its pointer. |
64 | * The wrapper of the packet_append() function. |
||
4756 | mejdrech | 65 | * @param[in] packet The packet to be used. |
66 | * @param[in] type The type to be allocated at the end of the packet content. |
||
3912 | mejdrech | 67 | * @returns The typed pointer to the allocated memory. |
68 | * @returns NULL if the packet is not valid. |
||
69 | * @returns NULL if there is not enough memory left. |
||
70 | */ |
||
3990 | mejdrech | 71 | #define PACKET_SUFFIX( packet, type ) ( type * ) packet_suffix(( packet ), sizeof( type )) |
3912 | mejdrech | 72 | |
73 | /** Trims the actual packet content by the specified prefix and suffix types. |
||
74 | * The wrapper of the packet_trim() function. |
||
4756 | mejdrech | 75 | * @param[in] packet The packet to be trimmed. |
76 | * @param[in] prefix The type of the prefix to be removed from the beginning of the packet content. |
||
77 | * @param[in] suffix The type of the suffix to be removed from the end of the packet content. |
||
3912 | mejdrech | 78 | * @returns EOK on success. |
79 | * @returns EINVAL if the packet is not valid. |
||
4705 | mejdrech | 80 | * @returns ENOMEM if there is not enough memory left. |
3912 | mejdrech | 81 | */ |
82 | #define PACKET_TRIM( packet, prefix, suffix ) packet_trim(( packet ), sizeof( prefix ), sizeof( suffix )) |
||
83 | |||
84 | /** Allocates the specified space right before the actual packet content and returns its pointer. |
||
4756 | mejdrech | 85 | * @param[in] packet The packet to be used. |
86 | * @param[in] length The space length to be allocated at the beginning of the packet content. |
||
3912 | mejdrech | 87 | * @returns The pointer to the allocated memory. |
88 | * @returns NULL if there is not enough memory left. |
||
89 | */ |
||
90 | void * packet_prefix( packet_t packet, size_t length ); |
||
91 | |||
92 | /** Allocates the specified space right after the actual packet content and returns its pointer. |
||
4756 | mejdrech | 93 | * @param[in] packet The packet to be used. |
94 | * @param[in] length The space length to be allocated at the end of the packet content. |
||
3912 | mejdrech | 95 | * @returns The pointer to the allocated memory. |
96 | * @returns NULL if there is not enough memory left. |
||
97 | */ |
||
98 | void * packet_suffix( packet_t packet, size_t length ); |
||
99 | |||
100 | /** Trims the actual packet content by the specified prefix and suffix lengths. |
||
4756 | mejdrech | 101 | * @param[in] packet The packet to be trimmed. |
102 | * @param[in] prefix The prefix length to be removed from the beginning of the packet content. |
||
103 | * @param[in] suffix The suffix length to be removed from the end of the packet content. |
||
3912 | mejdrech | 104 | * @returns EOK on success. |
105 | * @returns EINVAL if the packet is not valid. |
||
106 | * @returns ENOMEM if there is not enough memory left. |
||
107 | */ |
||
108 | int packet_trim( packet_t packet, size_t prefix, size_t suffix ); |
||
109 | |||
110 | /** Copies the specified data to the beginning of the actual packet content. |
||
111 | * Pushes the content end if needed. |
||
4756 | mejdrech | 112 | * @param[in] packet The packet to be filled. |
113 | * @param[in] data The data to be copied. |
||
114 | * @param[in] length The length of the copied data. |
||
3912 | mejdrech | 115 | * @returns EOK on success. |
116 | * @returns EINVAL if the packet is not valid. |
||
117 | * @returns ENOMEM if there is not enough memory left. |
||
118 | */ |
||
3901 | mejdrech | 119 | int packet_copy_data( packet_t packet, const void * data, size_t length ); |
3912 | mejdrech | 120 | |
121 | /** Returns the packet identifier. |
||
4756 | mejdrech | 122 | * @param[in] packet The packet. |
3912 | mejdrech | 123 | * @returns The packet identifier. |
124 | * @returns Zero (0) if the packet is not valid. |
||
125 | */ |
||
126 | packet_id_t packet_get_id( const packet_t packet ); |
||
127 | |||
128 | /** Returns the packet content length. |
||
4756 | mejdrech | 129 | * @param[in] packet The packet. |
3912 | mejdrech | 130 | * @returns The packet content length in bytes. |
131 | * @returns Zero (0) if the packet is not valid. |
||
132 | */ |
||
3901 | mejdrech | 133 | size_t packet_get_data_length( const packet_t packet ); |
3912 | mejdrech | 134 | |
135 | /** Returns the pointer to the beginning of the packet content. |
||
4756 | mejdrech | 136 | * @param[in] packet The packet. |
3912 | mejdrech | 137 | * @returns The pointer to the beginning of the packet content. |
138 | * @returns NULL if the packet is not valid. |
||
139 | */ |
||
3901 | mejdrech | 140 | void * packet_get_data( const packet_t packet ); |
3912 | mejdrech | 141 | |
142 | /** Returns the stored packet addresses and their length. |
||
4756 | mejdrech | 143 | * @param[in] packet The packet. |
144 | * @param[out] src The source address. May be NULL if not desired. |
||
145 | * @param[out] dest The destination address. May be NULL if not desired. |
||
4395 | mejdrech | 146 | * @returns The stored addresses length. |
3912 | mejdrech | 147 | * @returns Zero (0) if the addresses are not present. |
148 | * @returns EINVAL if the packet is not valid. |
||
149 | */ |
||
3901 | mejdrech | 150 | int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ); |
3912 | mejdrech | 151 | |
152 | /** Sets the packet addresses. |
||
4756 | mejdrech | 153 | * @param[in] packet The packet. |
154 | * @param[in] src The new source address. May be NULL. |
||
155 | * @param[in] dest The new destination address. May be NULL. |
||
156 | * @param[in] addr_len The addresses length. |
||
3912 | mejdrech | 157 | * @returns EOK on success. |
158 | * @returns EINVAL if the packet is not valid. |
||
159 | * @returns ENOMEM if there is not enough memory left. |
||
160 | */ |
||
3901 | mejdrech | 161 | int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ); |
3912 | mejdrech | 162 | |
163 | /** Translates the packet identifier to the packet reference. |
||
164 | * Tries to find mapping first. |
||
165 | * Contacts the packet server to share the packet if the mapping is not present. |
||
4756 | mejdrech | 166 | * @param[in] phone The packet server module phone. |
167 | * @param[out] packet The packet reference. |
||
168 | * @param[in] packet_id The packet identifier. |
||
3912 | mejdrech | 169 | * @returns EOK on success. |
170 | * @returns EINVAL if the packet parameter is NULL. |
||
171 | * @returns Other error codes as defined for the NET_PACKET_GET_SIZE message. |
||
4351 | mejdrech | 172 | * @returns Other error codes as defined for the packet_return() function. |
3912 | mejdrech | 173 | */ |
3901 | mejdrech | 174 | int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ); |
3912 | mejdrech | 175 | |
176 | /** Obtains the packet of the given dimensions. |
||
177 | * Contacts the packet server to return the appropriate packet. |
||
4756 | mejdrech | 178 | * @param[in] phone The packet server module phone. |
179 | * @param[in] addr_len The source and destination addresses maximal length in bytes. |
||
180 | * @param[in] max_prefix The maximal prefix length in bytes. |
||
181 | * @param[in] max_content The maximal content length in bytes. |
||
182 | * @param[in] max_suffix The maximal suffix length in bytes. |
||
3912 | mejdrech | 183 | * @returns The packet reference. |
184 | * @returns NULL on error. |
||
185 | */ |
||
3990 | mejdrech | 186 | packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ); |
3912 | mejdrech | 187 | |
188 | /** Obtains the packet of the given content size. |
||
189 | * Contacts the packet server to return the appropriate packet. |
||
4756 | mejdrech | 190 | * @param[in] phone The packet server module phone. |
191 | * @param[in] content The maximal content length in bytes. |
||
3912 | mejdrech | 192 | * @returns The packet reference. |
193 | * @returns NULL on error. |
||
194 | */ |
||
3990 | mejdrech | 195 | packet_t packet_get_1( int phone, size_t content ); |
3912 | mejdrech | 196 | |
4075 | mejdrech | 197 | /** Releases the packet queue. |
198 | * All packets in the queue are marked as free for use. |
||
199 | * The packet queue may be one packet only. |
||
200 | * The module should not use the packets after this point until they are received or obtained again. |
||
4756 | mejdrech | 201 | * @param[in] phone The packet server module phone. |
202 | * @param[in] packet_id The packet identifier. |
||
3912 | mejdrech | 203 | */ |
4192 | mejdrech | 204 | void pq_release( int phone, packet_id_t packet_id ); |
3901 | mejdrech | 205 | |
4756 | mejdrech | 206 | /** Returns the packet copy. |
207 | * Copies the addresses, data, order and metric values. |
||
208 | * Does not copy the queue placement. |
||
209 | * @param[in] phone The packet server module phone. |
||
210 | * @param[in] packet The original packet. |
||
211 | * @returns The packet copy. |
||
212 | * @returns NULL on error. |
||
4731 | mejdrech | 213 | */ |
214 | packet_t packet_get_copy( int phone, packet_t packet ); |
||
215 | |||
4704 | mejdrech | 216 | /*@}*/ |
217 | |||
3901 | mejdrech | 218 | #endif |
219 | |||
220 | /** @} |
||
221 | */ |