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