cfpkt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland
  4. * License terms: GNU General Public License (GPL) version 2
  5. */
  6. #ifndef CFPKT_H_
  7. #define CFPKT_H_
  8. #include <net/caif/caif_layer.h>
  9. #include <linux/types.h>
  10. struct cfpkt;
  11. /* Create a CAIF packet.
  12. * len: Length of packet to be created
  13. * @return New packet.
  14. */
  15. struct cfpkt *cfpkt_create(u16 len);
  16. /*
  17. * Destroy a CAIF Packet.
  18. * pkt Packet to be destoyed.
  19. */
  20. void cfpkt_destroy(struct cfpkt *pkt);
  21. /*
  22. * Extract header from packet.
  23. *
  24. * pkt Packet to extract header data from.
  25. * data Pointer to copy the header data into.
  26. * len Length of head data to copy.
  27. * @return zero on success and error code upon failure
  28. */
  29. int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len);
  30. /*
  31. * Peek header from packet.
  32. * Reads data from packet without changing packet.
  33. *
  34. * pkt Packet to extract header data from.
  35. * data Pointer to copy the header data into.
  36. * len Length of head data to copy.
  37. * @return zero on success and error code upon failure
  38. */
  39. int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len);
  40. /*
  41. * Extract header from trailer (end of packet).
  42. *
  43. * pkt Packet to extract header data from.
  44. * data Pointer to copy the trailer data into.
  45. * len Length of header data to copy.
  46. * @return zero on success and error code upon failure
  47. */
  48. int cfpkt_extr_trail(struct cfpkt *pkt, void *data, u16 len);
  49. /*
  50. * Add header to packet.
  51. *
  52. *
  53. * pkt Packet to add header data to.
  54. * data Pointer to data to copy into the header.
  55. * len Length of header data to copy.
  56. * @return zero on success and error code upon failure
  57. */
  58. int cfpkt_add_head(struct cfpkt *pkt, const void *data, u16 len);
  59. /*
  60. * Add trailer to packet.
  61. *
  62. *
  63. * pkt Packet to add trailer data to.
  64. * data Pointer to data to copy into the trailer.
  65. * len Length of trailer data to copy.
  66. * @return zero on success and error code upon failure
  67. */
  68. int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len);
  69. /*
  70. * Pad trailer on packet.
  71. * Moves data pointer in packet, no content copied.
  72. *
  73. * pkt Packet in which to pad trailer.
  74. * len Length of padding to add.
  75. * @return zero on success and error code upon failure
  76. */
  77. int cfpkt_pad_trail(struct cfpkt *pkt, u16 len);
  78. /*
  79. * Add a single byte to packet body (tail).
  80. *
  81. * pkt Packet in which to add byte.
  82. * data Byte to add.
  83. * @return zero on success and error code upon failure
  84. */
  85. int cfpkt_addbdy(struct cfpkt *pkt, const u8 data);
  86. /*
  87. * Add a data to packet body (tail).
  88. *
  89. * pkt Packet in which to add data.
  90. * data Pointer to data to copy into the packet body.
  91. * len Length of data to add.
  92. * @return zero on success and error code upon failure
  93. */
  94. int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len);
  95. /*
  96. * Checks whether there are more data to process in packet.
  97. * pkt Packet to check.
  98. * @return true if more data are available in packet false otherwise
  99. */
  100. bool cfpkt_more(struct cfpkt *pkt);
  101. /*
  102. * Checks whether the packet is erroneous,
  103. * i.e. if it has been attempted to extract more data than available in packet
  104. * or writing more data than has been allocated in cfpkt_create().
  105. * pkt Packet to check.
  106. * @return true on error false otherwise
  107. */
  108. bool cfpkt_erroneous(struct cfpkt *pkt);
  109. /*
  110. * Get the packet length.
  111. * pkt Packet to get length from.
  112. * @return Number of bytes in packet.
  113. */
  114. u16 cfpkt_getlen(struct cfpkt *pkt);
  115. /*
  116. * Set the packet length, by adjusting the trailer pointer according to length.
  117. * pkt Packet to set length.
  118. * len Packet length.
  119. * @return Number of bytes in packet.
  120. */
  121. int cfpkt_setlen(struct cfpkt *pkt, u16 len);
  122. /*
  123. * cfpkt_append - Appends a packet's data to another packet.
  124. * dstpkt: Packet to append data into, WILL BE FREED BY THIS FUNCTION
  125. * addpkt: Packet to be appended and automatically released,
  126. * WILL BE FREED BY THIS FUNCTION.
  127. * expectlen: Packet's expected total length. This should be considered
  128. * as a hint.
  129. * NB: Input packets will be destroyed after appending and cannot be used
  130. * after calling this function.
  131. * @return The new appended packet.
  132. */
  133. struct cfpkt *cfpkt_append(struct cfpkt *dstpkt, struct cfpkt *addpkt,
  134. u16 expectlen);
  135. /*
  136. * cfpkt_split - Split a packet into two packets at the specified split point.
  137. * pkt: Packet to be split (will contain the first part of the data on exit)
  138. * pos: Position to split packet in two parts.
  139. * @return The new packet, containing the second part of the data.
  140. */
  141. struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos);
  142. /*
  143. * Iteration function, iterates the packet buffers from start to end.
  144. *
  145. * Checksum iteration function used to iterate buffers
  146. * (we may have packets consisting of a chain of buffers)
  147. * pkt: Packet to calculate checksum for
  148. * iter_func: Function pointer to iteration function
  149. * chks: Checksum calculated so far.
  150. * buf: Pointer to the buffer to checksum
  151. * len: Length of buf.
  152. * data: Initial checksum value.
  153. * @return Checksum of buffer.
  154. */
  155. int cfpkt_iterate(struct cfpkt *pkt,
  156. u16 (*iter_func)(u16 chks, void *buf, u16 len),
  157. u16 data);
  158. /* Map from a "native" packet (e.g. Linux Socket Buffer) to a CAIF packet.
  159. * dir - Direction indicating whether this packet is to be sent or received.
  160. * nativepkt - The native packet to be transformed to a CAIF packet
  161. * @return The mapped CAIF Packet CFPKT.
  162. */
  163. struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt);
  164. /* Map from a CAIF packet to a "native" packet (e.g. Linux Socket Buffer).
  165. * pkt - The CAIF packet to be transformed into a "native" packet.
  166. * @return The native packet transformed from a CAIF packet.
  167. */
  168. void *cfpkt_tonative(struct cfpkt *pkt);
  169. /*
  170. * Returns packet information for a packet.
  171. * pkt Packet to get info from;
  172. * @return Packet information
  173. */
  174. struct caif_payload_info *cfpkt_info(struct cfpkt *pkt);
  175. /** cfpkt_set_prio - set priority for a CAIF packet.
  176. *
  177. * @pkt: The CAIF packet to be adjusted.
  178. * @prio: one of TC_PRIO_ constants.
  179. */
  180. void cfpkt_set_prio(struct cfpkt *pkt, int prio);
  181. #endif /* CFPKT_H_ */