cfpkt.h 6.3 KB

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