1 /* 2 * Copyright (C) ST-Ericsson AB 2010 3 * Author: Sjur Brendeland 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 /* 20 * Destroy a CAIF Packet. 21 * pkt Packet to be destoyed. 22 */ 23 void cfpkt_destroy(struct cfpkt *pkt); 24 25 /* 26 * Extract header from packet. 27 * 28 * pkt Packet to extract header data from. 29 * data Pointer to copy the header data into. 30 * len Length of head data to copy. 31 * @return zero on success and error code upon failure 32 */ 33 int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len); 34 35 /* 36 * Peek header from packet. 37 * Reads data from packet without changing packet. 38 * 39 * pkt Packet to extract header data from. 40 * data Pointer to copy the header data into. 41 * len Length of head data to copy. 42 * @return zero on success and error code upon failure 43 */ 44 int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len); 45 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 /* 57 * Add header to packet. 58 * 59 * 60 * pkt Packet to add header data to. 61 * data Pointer to data to copy into the header. 62 * len Length of header data to copy. 63 * @return zero on success and error code upon failure 64 */ 65 int cfpkt_add_head(struct cfpkt *pkt, const void *data, u16 len); 66 67 /* 68 * Add trailer to packet. 69 * 70 * 71 * pkt Packet to add trailer data to. 72 * data Pointer to data to copy into the trailer. 73 * len Length of trailer data to copy. 74 * @return zero on success and error code upon failure 75 */ 76 int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len); 77 78 /* 79 * Pad trailer on packet. 80 * Moves data pointer in packet, no content copied. 81 * 82 * pkt Packet in which to pad trailer. 83 * len Length of padding to add. 84 * @return zero on success and error code upon failure 85 */ 86 int cfpkt_pad_trail(struct cfpkt *pkt, u16 len); 87 88 /* 89 * Add a single byte to packet body (tail). 90 * 91 * pkt Packet in which to add byte. 92 * data Byte to add. 93 * @return zero on success and error code upon failure 94 */ 95 int cfpkt_addbdy(struct cfpkt *pkt, const u8 data); 96 97 /* 98 * Add a data to packet body (tail). 99 * 100 * pkt Packet in which to add data. 101 * data Pointer to data to copy into the packet body. 102 * len Length of data to add. 103 * @return zero on success and error code upon failure 104 */ 105 int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len); 106 107 /* 108 * Checks whether there are more data to process in packet. 109 * pkt Packet to check. 110 * @return true if more data are available in packet false otherwise 111 */ 112 bool cfpkt_more(struct cfpkt *pkt); 113 114 /* 115 * Checks whether the packet is erroneous, 116 * i.e. if it has been attempted to extract more data than available in packet 117 * or writing more data than has been allocated in cfpkt_create(). 118 * pkt Packet to check. 119 * @return true on error false otherwise 120 */ 121 bool cfpkt_erroneous(struct cfpkt *pkt); 122 123 /* 124 * Get the packet length. 125 * pkt Packet to get length from. 126 * @return Number of bytes in packet. 127 */ 128 u16 cfpkt_getlen(struct cfpkt *pkt); 129 130 /* 131 * Set the packet length, by adjusting the trailer pointer according to length. 132 * pkt Packet to set length. 133 * len Packet length. 134 * @return Number of bytes in packet. 135 */ 136 int cfpkt_setlen(struct cfpkt *pkt, u16 len); 137 138 /* 139 * cfpkt_append - Appends a packet's data to another packet. 140 * dstpkt: Packet to append data into, WILL BE FREED BY THIS FUNCTION 141 * addpkt: Packet to be appended and automatically released, 142 * WILL BE FREED BY THIS FUNCTION. 143 * expectlen: Packet's expected total length. This should be considered 144 * as a hint. 145 * NB: Input packets will be destroyed after appending and cannot be used 146 * after calling this function. 147 * @return The new appended packet. 148 */ 149 struct cfpkt *cfpkt_append(struct cfpkt *dstpkt, struct cfpkt *addpkt, 150 u16 expectlen); 151 152 /* 153 * cfpkt_split - Split a packet into two packets at the specified split point. 154 * pkt: Packet to be split (will contain the first part of the data on exit) 155 * pos: Position to split packet in two parts. 156 * @return The new packet, containing the second part of the data. 157 */ 158 struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos); 159 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 174 int cfpkt_iterate(struct cfpkt *pkt, 175 u16 (*iter_func)(u16 chks, void *buf, u16 len), 176 u16 data); 177 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 185 /* Map from a CAIF packet to a "native" packet (e.g. Linux Socket Buffer). 186 * pkt - The CAIF packet to be transformed into a "native" packet. 187 * @return The native packet transformed from a CAIF packet. 188 */ 189 void *cfpkt_tonative(struct cfpkt *pkt); 190 191 /* 192 * Returns packet information for a packet. 193 * pkt Packet to get info from; 194 * @return Packet information 195 */ 196 struct caif_payload_info *cfpkt_info(struct cfpkt *pkt); 197 198 /** cfpkt_set_prio - set priority for a CAIF packet. 199 * 200 * @pkt: The CAIF packet to be adjusted. 201 * @prio: one of TC_PRIO_ constants. 202 */ 203 void cfpkt_set_prio(struct cfpkt *pkt, int prio); 204 205 #endif /* CFPKT_H_ */ 206