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