cfpkt.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
  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. /* Create a CAIF packet.
  17. * data Data to copy.
  18. * len Length of packet to be created
  19. * @return New packet.
  20. */
  21. struct cfpkt *cfpkt_create_uplink(const unsigned char *data, unsigned int len);
  22. /*
  23. * Destroy a CAIF Packet.
  24. * pkt Packet to be destoyed.
  25. */
  26. void cfpkt_destroy(struct cfpkt *pkt);
  27. /*
  28. * Extract header from packet.
  29. *
  30. * pkt Packet to extract header data from.
  31. * data Pointer to copy the header data into.
  32. * len Length of head data to copy.
  33. * @return zero on success and error code upon failure
  34. */
  35. int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len);
  36. /*
  37. * Peek header from packet.
  38. * Reads data from packet without changing packet.
  39. *
  40. * pkt Packet to extract header data from.
  41. * data Pointer to copy the header data into.
  42. * len Length of head data to copy.
  43. * @return zero on success and error code upon failure
  44. */
  45. int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len);
  46. /*
  47. * Extract header from trailer (end of packet).
  48. *
  49. * pkt Packet to extract header data from.
  50. * data Pointer to copy the trailer data into.
  51. * len Length of header data to copy.
  52. * @return zero on success and error code upon failure
  53. */
  54. int cfpkt_extr_trail(struct cfpkt *pkt, void *data, u16 len);
  55. /*
  56. * Add header to packet.
  57. *
  58. *
  59. * pkt Packet to add header data to.
  60. * data Pointer to data to copy into the header.
  61. * len Length of header data to copy.
  62. * @return zero on success and error code upon failure
  63. */
  64. int cfpkt_add_head(struct cfpkt *pkt, const void *data, u16 len);
  65. /*
  66. * Add trailer to packet.
  67. *
  68. *
  69. * pkt Packet to add trailer data to.
  70. * data Pointer to data to copy into the trailer.
  71. * len Length of trailer data to copy.
  72. * @return zero on success and error code upon failure
  73. */
  74. int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len);
  75. /*
  76. * Pad trailer on packet.
  77. * Moves data pointer in packet, no content copied.
  78. *
  79. * pkt Packet in which to pad trailer.
  80. * len Length of padding to add.
  81. * @return zero on success and error code upon failure
  82. */
  83. int cfpkt_pad_trail(struct cfpkt *pkt, u16 len);
  84. /*
  85. * Add a single byte to packet body (tail).
  86. *
  87. * pkt Packet in which to add byte.
  88. * data Byte to add.
  89. * @return zero on success and error code upon failure
  90. */
  91. int cfpkt_addbdy(struct cfpkt *pkt, const u8 data);
  92. /*
  93. * Add a data to packet body (tail).
  94. *
  95. * pkt Packet in which to add data.
  96. * data Pointer to data to copy into the packet body.
  97. * len Length of data to add.
  98. * @return zero on success and error code upon failure
  99. */
  100. int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len);
  101. /*
  102. * Checks whether there are more data to process in packet.
  103. * pkt Packet to check.
  104. * @return true if more data are available in packet false otherwise
  105. */
  106. bool cfpkt_more(struct cfpkt *pkt);
  107. /*
  108. * Checks whether the packet is erroneous,
  109. * i.e. if it has been attempted to extract more data than available in packet
  110. * or writing more data than has been allocated in cfpkt_create().
  111. * pkt Packet to check.
  112. * @return true on error false otherwise
  113. */
  114. bool cfpkt_erroneous(struct cfpkt *pkt);
  115. /*
  116. * Get the packet length.
  117. * pkt Packet to get length from.
  118. * @return Number of bytes in packet.
  119. */
  120. u16 cfpkt_getlen(struct cfpkt *pkt);
  121. /*
  122. * Set the packet length, by adjusting the trailer pointer according to length.
  123. * pkt Packet to set length.
  124. * len Packet length.
  125. * @return Number of bytes in packet.
  126. */
  127. int cfpkt_setlen(struct cfpkt *pkt, u16 len);
  128. /*
  129. * cfpkt_append - Appends a packet's data to another packet.
  130. * dstpkt: Packet to append data into, WILL BE FREED BY THIS FUNCTION
  131. * addpkt: Packet to be appended and automatically released,
  132. * WILL BE FREED BY THIS FUNCTION.
  133. * expectlen: Packet's expected total length. This should be considered
  134. * as a hint.
  135. * NB: Input packets will be destroyed after appending and cannot be used
  136. * after calling this function.
  137. * @return The new appended packet.
  138. */
  139. struct cfpkt *cfpkt_append(struct cfpkt *dstpkt, struct cfpkt *addpkt,
  140. u16 expectlen);
  141. /*
  142. * cfpkt_split - Split a packet into two packets at the specified split point.
  143. * pkt: Packet to be split (will contain the first part of the data on exit)
  144. * pos: Position to split packet in two parts.
  145. * @return The new packet, containing the second part of the data.
  146. */
  147. struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos);
  148. /*
  149. * Iteration function, iterates the packet buffers from start to end.
  150. *
  151. * Checksum iteration function used to iterate buffers
  152. * (we may have packets consisting of a chain of buffers)
  153. * pkt: Packet to calculate checksum for
  154. * iter_func: Function pointer to iteration function
  155. * chks: Checksum calculated so far.
  156. * buf: Pointer to the buffer to checksum
  157. * len: Length of buf.
  158. * data: Initial checksum value.
  159. * @return Checksum of buffer.
  160. */
  161. u16 cfpkt_iterate(struct cfpkt *pkt,
  162. u16 (*iter_func)(u16 chks, void *buf, u16 len),
  163. u16 data);
  164. /* Append by giving user access to packet buffer
  165. * cfpkt Packet to append to
  166. * buf Buffer inside pkt that user shall copy data into
  167. * buflen Length of buffer and number of bytes added to packet
  168. * @return 0 on error, 1 on success
  169. */
  170. int cfpkt_raw_append(struct cfpkt *cfpkt, void **buf, unsigned int buflen);
  171. /* Extract by giving user access to packet buffer
  172. * cfpkt Packet to extract from
  173. * buf Buffer inside pkt that user shall copy data from
  174. * buflen Length of buffer and number of bytes removed from packet
  175. * @return 0 on error, 1 on success
  176. */
  177. int cfpkt_raw_extract(struct cfpkt *cfpkt, void **buf, unsigned int buflen);
  178. /* Map from a "native" packet (e.g. Linux Socket Buffer) to a CAIF packet.
  179. * dir - Direction indicating whether this packet is to be sent or received.
  180. * nativepkt - The native packet to be transformed to a CAIF packet
  181. * @return The mapped CAIF Packet CFPKT.
  182. */
  183. struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt);
  184. /* Map from a CAIF packet to a "native" packet (e.g. Linux Socket Buffer).
  185. * pkt - The CAIF packet to be transformed into a "native" packet.
  186. * @return The native packet transformed from a CAIF packet.
  187. */
  188. void *cfpkt_tonative(struct cfpkt *pkt);
  189. /*
  190. * Insert a packet in the packet queue.
  191. * pktq Packet queue to insert into
  192. * pkt Packet to be inserted in queue
  193. * prio Priority of packet
  194. */
  195. void cfpkt_queue(struct cfpktq *pktq, struct cfpkt *pkt,
  196. unsigned short prio);
  197. /*
  198. * Remove a packet from the packet queue.
  199. * pktq Packet queue to fetch packets from.
  200. * @return Dequeued packet.
  201. */
  202. struct cfpkt *cfpkt_dequeue(struct cfpktq *pktq);
  203. /*
  204. * Peek into a packet from the packet queue.
  205. * pktq Packet queue to fetch packets from.
  206. * @return Peeked packet.
  207. */
  208. struct cfpkt *cfpkt_qpeek(struct cfpktq *pktq);
  209. /*
  210. * Initiates the packet queue.
  211. * @return Pointer to new packet queue.
  212. */
  213. struct cfpktq *cfpktq_create(void);
  214. /*
  215. * Get the number of packets in the queue.
  216. * pktq Packet queue to fetch count from.
  217. * @return Number of packets in queue.
  218. */
  219. int cfpkt_qcount(struct cfpktq *pktq);
  220. /*
  221. * Put content of packet into buffer for debuging purposes.
  222. * pkt Packet to copy data from
  223. * buf Buffer to copy data into
  224. * buflen Length of data to copy
  225. * @return Pointer to copied data
  226. */
  227. char *cfpkt_log_pkt(struct cfpkt *pkt, char *buf, int buflen);
  228. /*
  229. * Clones a packet and releases the original packet.
  230. * This is used for taking ownership of a packet e.g queueing.
  231. * pkt Packet to clone and release.
  232. * @return Cloned packet.
  233. */
  234. struct cfpkt *cfpkt_clone_release(struct cfpkt *pkt);
  235. /*
  236. * Returns packet information for a packet.
  237. * pkt Packet to get info from;
  238. * @return Packet information
  239. */
  240. struct caif_payload_info *cfpkt_info(struct cfpkt *pkt);
  241. /*! @} */
  242. #endif /* CFPKT_H_ */