blob: 8f6dec641a01f82d25a178e556f4c5fb9358139e [file] [log] [blame]
Jeff Johnson295189b2012-06-20 16:38:30 -07001/*
Jeff Johnson32d95a32012-09-10 13:15:23 -07002 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
Jeff Johnson295189b2012-06-20 16:38:30 -07003 *
4 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5 *
6 *
7 * Permission to use, copy, modify, and/or distribute this software for
8 * any purpose with or without fee is hereby granted, provided that the
9 * above copyright notice and this permission notice appear in all
10 * copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 * PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * Woodside Networks, Inc proprietary. All rights reserved.
24 * File: $File: //depot/software/projects/feature_branches/nova_phase1/ap/apps/include/aniAsfPacket.h $
25 * Contains declarations for packet manipulation routines that make it
26 * easy to create and parse multi-layered network frames. This module
27 * minimizes buffer copies while adding or removing headers, and
28 * adding or removing payload.
29 *
30 * Author: Mayank D. Upadhyay
31 * Date: 19-June-2002
32 * History:-
33 * Date Modified by Modification Information
34 * ------------------------------------------------------
35 *
36 */
37#ifndef _ANI_ASF_PACKET_H_
38#define _ANI_ASF_PACKET_H_
39
40#include "vos_types.h"
41#include "palTypes.h"
42
43#define ANI_ETH_FRAME_LEN 1516
44#define ANI_DEFAULT_PACKET_SIZE (ANI_ETH_FRAME_LEN*2)
45
46/**
47 * Opaque packet structure with internal storage for raw bytes.
48 * Conceptually, a tPacket is a pre-allocated buffer that contains
49 * data in the middle and free space on either side. The start of the
50 * data is called the head. Routines are provided to add data at the
51 * front or at the rear. The length of the packet is the total number
52 * of valid data bytes contained in it. The size of the packet is the
53 * total number of preallocated bytes.
54 */
55typedef struct tAniPacket tAniPacket;
56
57/**
58 * aniAsfPacketAllocate
59 *
60 * FUNCTION:
61 * Create a packet of size 2*ANI_DEFAULT_PACKET_SIZE and positions the
62 * head of the packet in the center. The allocated storage can be free
63 * with a call to aniAsfPacketFree.
64 *
65 * LOGIC:
66 * Allocates storage for tPacket and its internal raw data
67 * buffer. Positions the head and tail pointers in the middle of the
68 * raw data buffer.
69 *
70 * @param packetPtr pointer that will be set to newly allocated
71 * tPacket if the operation succeeds.
72 *
73 * @return ANI_OK if the operation succeeds; ANI_E_MALLOC_FAILED if
74 * memory could not be allocated.
75 * @see aniAsfPacketFree
76 */
77int
78aniAsfPacketAllocate(tAniPacket **packetPtr);
79
80/**
81 * aniAsfPacketDuplicate
82 *
83 * Duplicates a given packet exactly. That is, the contents, the size
84 * of the packet, and the positions of the pointers are maintained in
85 * the new copy.
86 *
87 * @param newPacketPtr is set to a newly allocated packet that is a
88 * duplicate of oldPacket
89 * @param oldPacket the original packet that should be duplicated
90 *
91 * @return ANI_OK if the operation succeeds
92 */
93int
94aniAsfPacketDuplicate(tAniPacket **newPacketPtr, tAniPacket *oldPacket);
95
96/**
97 * aniAsfPacketAllocateExplicit
98 *
99 * FUNCTION:
100 * Create a packet of the desired size and position the head of the
101 * packet at the desired offset in the internal raw data buffer. An
102 * application would normally set this offset to the expected length
103 * of the protocol header, then append the payload, and finally,
104 * prepend the header. The allocated storage can be free with a call
105 * to aniAsfPacketFree.
106 *
107 * LOGIC:
108 * Allocates storage for tPacket and its internal raw data
109 * buffer. Positions the head and tail pointers at the given offset in
110 * the internal raw data buffer.
111 *
112 * @param packetPtr pointer that will be set to newly allocated
113 * tPacket if the operation succeeds.
114 * @param size the size of the internal raw data buffer
115 * @param offset the offset in the internal raw data buffer where the
116 * head of the packet will be positioned initially
117 *
118 * @return ANI_OK if the operation succeeds; ANI_E_MALLOC_FAILED if
119 * memory could not be allocated.
120 * @see aniAsfPacketFree
121 */
122int
123aniAsfPacketAllocateExplicit(tAniPacket **packetPtr,
124 v_U32_t size,
125 v_U32_t offset);
126
127/**
128 * aniAsfPacketFree
129 *
130 * FUNCTION:
131 * Free a previously allocated tPacket and its internal raw data
132 * buffer.
133 *
134 * @param packet the packet to free
135 *
136 * @return ANI_OK if the operation succeeds; ANI_E_NULL_VALUE if an
137 * unexpected NULL pointer is encountered
138 */
139int
140aniAsfPacketFree(tAniPacket *packet);
141
142/**
143 * aniAsfPacket2Str
144 *
145 * FUNCTION:
146 * Returns a printable representation of the data contained in the
147 * packet.
148 * Note: This function returns a static buffer used by aniAsfHexStr.
149 *
150 * @param packet the packet whose contents need to be printed
151 */
152v_U8_t *aniAsfPacket2Str(tAniPacket *packet);
153
154/**
155 * aniAsfPacketAppendBuffer
156 *
157 * FUNCTION:
158 * Appends the data contained in buf to the end of the data in
159 * destPacket. The head of destPacket remains unchanged, while its
160 * length increases by len.
161 *
162 * If there isn't enough free space in destPacket for all len bytes
163 * then the routine fails and the length of destPacket remains
164 * unchanged.
165 *
166 * LOGIC:
167 * Check that there is enough free space in the packet to append the
168 * buffer. If not, bail. Otherwise, copy bytes from the buffer into
169 * the packet's internal raw data buffer and increase the value of its
170 * length to reflect this.
171 *
172 * @param packet the packet to append to
173 * @param buf the buffer containing data to be appended to the packet
174 * @param len the number of bytes to append
175 *
176 * @return ANI_OK if the operation succeeds; ANI_E_FAILED if the
177 * packet does not have enough free space for the complete buffer
178 * @see aniAsfPacketPrependBuffer
179 */
180int
181aniAsfPacketAppendBuffer(tAniPacket *destPacket,
182 const v_U8_t *buf,
183 v_U32_t len);
184
185/**
186 * aniAsfPacketPrependBuffer
187 *
188 * FUNCTION:
189 * Prepends the data contained in buf to the start of the data in
190 * destPacket. The head of destPacket is repositioned and the length
191 * of destPacket increases by len.
192 *
193 * If there isn't enough free space in destPacket for all len bytes
194 * then the routine fails and the length of destPacket remains
195 * unchanged.
196 *
197 * LOGIC:
198 * Check that there is enough free space in the packet to prepend the
199 * buffer. If not, bail. Otherwise, copy bytes from the buffer into
200 * the packet's internal raw data buffer and increase the value of its
201 * length to reflect this.
202 *
203 * @param packet the packet to prepend to
204 * @param buf the buffer containing data to be prepended to the packet
205 * @param len the number of bytes to prepend
206 *
207 * @return ANI_OK if the operation succeeds; ANI_E_FAILED if the
208 * packet does not have enough free space for the complete buffer
209 * @see aniAsfPacketAppendBuffer
210 */
211int
212aniAsfPacketPrependBuffer(tAniPacket *destPacket,
213 const v_U8_t *buf,
214 v_U32_t len);
215
216/**
217 * aniAsfPacketCanAppendBuffer
218 *
219 * FUNCTION:
220 * Determines if len bytes can be safely appended to destPacket
221 * without overflowing.
222 *
223 * LOGIC:
224 * Current packet tail plus len of buffer should not exceed packet
225 * start plus packet size
226 *
227 * Note: This does not return a boolean value, but instead an integer
228 * code.
229 *
230 * @param packet the packet to append to
231 * @param len the number of bytes to append
232 *
233 * @return ANI_OK if the append operation would succeed; ANI_E_FAILED
234 * otherwise
235 */
236int
237aniAsfPacketCanAppendBuffer(tAniPacket *destPacket,
238 v_U32_t len);
239
240/**
241 * aniAsfPacketCanPrependBuffer
242 *
243 * FUNCTION:
244 * Determines if len bytes can be safely prepended to destPacket
245 * without overflowing.
246 *
247 * LOGIC:
248 * Current packet head minus len of buffer should not be less than
249 * start of packet.
250 *
251 * Note: This does not return a boolean value, but instead an integer
252 * code.
253 *
254 * @param packet the packet to prepend to
255 * @param len the number of bytes to prepend
256 *
257 * @return ANI_OK if the append operation would succeed; ANI_E_FAILED
258 * otherwise
259 */
260int
261aniAsfPacketCanPrependBuffer(tAniPacket *destPacket,
262 v_U32_t len);
263
264/**
265 * aniAsfPacketTruncateFromFront
266 *
267 * FUNCTION:
268 * Removes len bytes from the front of the packet by moving its
269 * head. The length of the packet is decremented by len.
270 *
271 * @param packet the packet to truncate from the front
272 * @param len the number of bytes to truncate
273 *
274 * @return ANI_OK if the append operation would succeed; ANI_E_FAILED
275 * otherwise
276 */
277int
278aniAsfPacketTruncateFromFront(tAniPacket *packet,
279 v_U32_t len);
280
281/**
282 * aniAsfPacketTruncateFromRear
283 *
284 * FUNCTION:
285 * Removes len bytes from the rear of the packet by moving its
286 * tail. The length of the packet is decremented by len.
287 *
288 * @param packet the packet to truncate from the rear
289 * @param len the number of bytes to truncate
290 *
291 * @return ANI_OK if the append operation would succeed; ANI_E_FAILED
292 * otherwise
293 */
294int
295aniAsfPacketTruncateFromRear(tAniPacket *packet,
296 v_U32_t len);
297
298/**
299 * aniAsfPacketGetLen
300 *
301 * FUNCTION:
302 * Returns the number of valid data bytes stored in the packet.
303 *
304 * @param packet the packet whose len we need
305 *
306 * @return the non-negative number of bytes stored in the packet
307 */
308int
309aniAsfPacketGetLen(tAniPacket *packet);
310
311/**
312 * aniAsfPacketGetBytes
313 *
314 * FUNCTION:
315 * Returns a pointer to the head of the valid data stored in the
316 * packet.
317 *
318 * @param packet the packet whose bytes we need
319 * @param rawBytesPtr the pointer that will be set the start of the
320 * raw bytes.
321 *
322 * @return The non-negative number of bytes stored in the packet if
323 * the operation succeeded. That is the same value as what would be
324 * returned by aniAsfPacketGetLen.
325 */
326int
327aniAsfPacketGetBytes(tAniPacket *packet, v_U8_t **rawBytesPtr);
328
329/**
330 * aniAsfPacketGetN
331 *
332 * Returns N bytes from the packet and moves the head of the packet
333 * beyond those bytes.
334 *
335 * @param packet the packet to read from
336 * @param n the number of bytes to read
337 * @param bytesPtr is set to the start of the octets
338 *
339 * @return ANI_OK if the operation succeeds; ANI_E_SHORT_PACKET if the
340 * packet does not have n bytes.
341 */
342int
343aniAsfPacketGetN(tAniPacket *packet, int n, v_U8_t **bytesPtr);
344
345/**
346 * aniAsfPacketEmpty
347 *
348 * FUNCTION:
349 * Re-initializes the packet by positioning the head to the middle and
350 * setting the length to zero.
351 *
352 * @param packet the packet to empty
353 *
354 * @return ANI_OK if the operation succeeded
355 */
356int
357aniAsfPacketEmpty(tAniPacket *packet);
358
359/**
360 * aniAsfPacketEmptyExplicit
361 *
362 * FUNCTION:
363 * Re-initializes the packet by positioning the head to the desired
364 * offset and setting the length to zero.
365 *
366 * @param packet the packet to empty
367 * @param offset the offset that the head of the packet should be set
368 * to. An application will be able to prepend and append data relative
369 * to this offset.
370 *
371 * @return ANI_OK if the operation succeeded
372 */
373int
374aniAsfPacketEmptyExplicit(tAniPacket *packet,
375 v_U32_t offset);
376
377
378/**
379 * aniAsfPacketPrependHdr
380 *
381 * FUNCTION:
382 * Prepends a tAniHdr at the start of the packet. All host to network
383 * byte order translation is also taken care of.
384 *
385 * @param packet the packet to write to
386 * @param msgType the message type to write as part of the header
387 *
388 * @return ANI_OK if the operation succeeds
389 */
390int
391aniAsfPacketPrependHdr(tAniPacket *packet, v_U16_t msgType);
392
393/**
394 * aniAsfPacketGet32
395 *
396 * FUNCTION:
397 * Reads a ANI_U32 out of the packet and returns it. The packet's head
398 * is advanced and its length decremented by the appropriate length.
399 * All network to host byte order translation is also taken care of.
400 *
401 * @param packet the packet to read from
402 * @param val the value to fill in
403 *
404 * @return ANI_OK if the operation succeeds
405 */
406int
407aniAsfPacketGet32(tAniPacket *packet, v_U32_t *val);
408
409/**
410 * aniAsfPacketAppend32
411 *
412 * FUNCTION:
413 * Appends a ANI_U32 to the end of the packet.
414 * All host to network byte order translation is also taken care of.
415 *
416 * @param packet the packet to write to
417 * @param val the value to append
418 *
419 * @return ANI_OK if the operation succeeds
420 */
421int
422aniAsfPacketAppend32(tAniPacket *packet, v_U32_t val);
423
424/**
425 * aniAsfPacketGet16
426 *
427 * FUNCTION:
428 * Reads a ANI_U16 out of the packet and returns it. The packet's head
429 * is advanced and its length decremented by the appropriate length.
430 * All network to host byte order translation is also taken care of.
431 *
432 * @param packet the packet to read from
433 * @param val the value to fill in
434 *
435 * @return ANI_OK if the operation succeeds
436 */
437int
438aniAsfPacketGet16(tAniPacket *packet, v_U16_t *val);
439
440/**
441 * aniAsfPacketPrepend16
442 *
443 * FUNCTION:
444 * Prepends a ANI_U16 to the start of the packet.
445 * All host to network byte order translation is also taken care of.
446 *
447 * @param packet the packet to write to
448 * @param val the value to prepend
449 *
450 * @return ANI_OK if the operation succeeds
451 */
452int
453aniAsfPacketPrepend16(tAniPacket *packet, v_U16_t val);
454
455/**
456 * aniAsfPacketAppend16
457 *
458 * FUNCTION:
459 * Appends a ANI_U16 to the end of the packet.
460 * All host to network byte order translation is also taken care of.
461 *
462 * @param packet the packet to write to
463 * @param val the value to append
464 *
465 * @return ANI_OK if the operation succeeds
466 */
467int
468aniAsfPacketAppend16(tAniPacket *packet, v_U16_t val);
469
470/**
471 * aniAsfPacketGet8
472 *
473 * FUNCTION:
474 * Reads a ANI_U8 out of the packet and returns it. The packet's head
475 * is advanced and its length decremented by the appropriate length.
476 * All network to host byte order translation is also taken care of.
477 *
478 * @param packet the packet to read from
479 * @param val the value to fill in
480 *
481 * @return ANI_OK if the operation succeeds
482 */
483int
484aniAsfPacketGet8(tAniPacket *packet, v_U8_t *val);
485
486/**
487 * aniAsfPacketPrepend8
488 *
489 * FUNCTION:
490 * Prepends a ANI_U8 to the start of the packet.
491 * All host to network byte order translation is also taken care of.
492 *
493 * @param packet the packet to read from
494 * @param val the value to prepend
495 *
496 * @return ANI_OK if the operation succeeds
497 */
498int
499aniAsfPacketPrepend8(tAniPacket *packet, v_U8_t val);
500
501/**
502 * aniAsfPacketAppend8
503 *
504 * FUNCTION:
505 * Appends a ANI_U8 to the end of the packet.
506 * All host to network byte order translation is also taken care of.
507 *
508 * @param packet the packet to write to
509 * @param val the value to append
510 *
511 * @return ANI_OK if the operation succeeds
512 */
513int
514aniAsfPacketAppend8(tAniPacket *packet, v_U8_t val);
515
516/**
517 * aniAsfPacketGetMac
518 *
519 * FUNCTION:
520 * Returns a tAniMacAddr from the start of the packet.
521 *
522 * @param packet the packet to read from
523 * @param macAddr the destination to copy the MAC address to
524 *
525 * @return ANI_OK if the operation succeeds. Also, the packet head
526 * pointer is advanced past the MAC address.
527 */
528int
529aniAsfPacketGetMac(tAniPacket *packet, tAniMacAddr macAddr);
530
531/**
532 * aniAsfPacketMoveLeft
533 *
534 * FUNCTION:
535 * Pretends that a certain number of bytes have been prepended to the
536 * packet, without actually copying any bytes in. The packet head and
537 * length are appropriately changed. This function is useful while
538 * interfacing with other libraries that only support byte array
539 * manipulation.
540 *
541 * WARNING:
542 * Applications are discouraged from using this function
543 * because correct usage is a two-step process - one: copy some bytes
544 * to the packet's internal buffer, two: move head and length. This
545 * violates the encapsulation the packet library aims to provide.
546 *
547 * @param packet the packet whose head and length needs to be modified
548 * @param count the number of bytes to modify by
549 *
550 * @return ANI_OK if the operation succeeds
551 */
552int
553aniAsfPacketMoveLeft(tAniPacket *packet, v_U32_t count);
554
555/**
556 * aniAsfPacketMoveRight
557 *
558 * FUNCTION:
559 * Pretends that a certain number of bytes have been appended to the
560 * packet, without actually copying any bytes in. The packet tail and
561 * length are appropriately changed. This function is useful while
562 * interfacing with other libraries that only support byte array
563 * manipulation.
564 *
565 * WARNING:
566 * Applications are discouraged from using this function
567 * because correct usage is a two-step process - one: copy some bytes
568 * to the packet's internal buffer, two: move tail and length. This
569 * violates the encapsulation the packet library aims to provide.
570 *
571 * @param packet the packet whose head and length needs to be modified
572 * @param count the number of bytes to modify by
573 *
574 * @return ANI_OK if the operation succeeds
575 */
576int
577aniAsfPacketMoveRight(tAniPacket *packet, v_U32_t count);
578
579/**
580 * aniAsfPacketGetBytesFromTail
581 *
582 * FUNCTION:
583 * Returns a pointer to the tail of the valid data stored
584 * in the packet.
585 *
586 * WARNING:
587 * Applications are discouraged from using this function
588 * because correct usage is a three-step process - one: call this
589 * routine to obtain a pointer to the current tail of the packet.
590 * two: treat this returned pointer like a simple array and copy
591 * some bytes to the packet's internal buffer, and finally
592 * three: move tail and length. This violates the encapsulation
593 * the packet library aims to provide.
594 *
595 * @param packet the packet whose bytes we need
596 * @param rawBytesPtr the pointer that will be set the start of the
597 * raw bytes.
598 *
599 * @return The non-negative number of bytes stored in the packet if
600 * the operation succeeded. That is the same value as what would be
601 * returned by aniAsfPacketGetLen.
602 */
603int
604aniAsfPacketGetBytesFromTail(tAniPacket *packet, v_U8_t **rawBytesPtr);
605
606
607#endif // _ANI_ASF_PACKET_H_