2 * @file frescan_packets.h
4 * @brief FRESCAN packets definition and pool
15 * This file contains the FRESCAN packets definition and functions to
16 * allocate and free them from a global pool of packets statically
21 * See MaRTE OS license
25 #ifndef _MARTE_FRESCAN_PACKETS_H_
26 #define _MARTE_FRESCAN_PACKETS_H_
28 #include <stdint.h> // uint8_t ...
29 #include "frescan.h" // frescan_flags_t
30 #include <misc/linux_list.h> // struct list_head
31 #include <drivers/can.h> // can_frame_t
34 * frescan_packet_t - a frescan packet
36 * This structure is very important and it is used to store a FRESCAN packet.
37 * As we support fragmentation, a FRESCAN packet can be composed of several
38 * CAN frames. This 'frescan_packet_t' structure is used in two main cases:
40 * 1.- When we are sending data. In this case, the buffer pointers store the
41 * real data we want to sent and we use a 'buffer_read_pointer' to know
42 * how many bytes of the buffer we already sent. In 'frame', we store the
43 * last sent frame (with the corresponding CAN id fields). We will have
44 * to update the fragmentation fields as long as we send more packets.
45 * The 'fifo_list' is used to chained frescan packets of the same priority
46 * or that belong to the same sporadic server. Finally, 'flags', specify
47 * if we are sending ASYNC or SYNC. If we are sending SYNC the buffer
48 * pointers are pointing to the buffer sent by the user (zero copying),
49 * while if we use ASYNC, a copy of the data is done to the buffer.
51 * 2.- When we are receiving data, we only use 'frame' and 'fifo_list' fields.
52 * The IRQ handler of the chip allocates a CAN frame and calls to our hook.
53 * We store the pointer to that frame in 'frame' and we make a chain with
54 * frames of the same message (using the fragmentation fields). When we
55 * have all of them, we move the packet list to the corresponding
56 * receiving channel to wait for the user to perform a receive operation
57 * when we will copy the data and free both the packets and the frames.
59 * @flags: to know if the packet is to be sent SYNC or ASYNC, FP or SERVER...
60 * @frame: pointer to the last sent frame or the received frame
61 * @fifo_list: list to put several packets together
62 * @msg_list: list to put packets of the same message together
63 * @buffer_head: pointer to first byte of the buffer that is going to be sent
64 * @buffer_read_pointer: pointer to the part of the buffer being read
65 * @buffer_pending_bytes: bytes waiting to be sent
66 * @pool_pos: position in the packets pool to know how to free it
68 * NOTE: the buffers could also be used on the receiving part to support
69 * sequential reads, instead of reading the whole message at once.
74 frescan_flags_t flags;
75 struct can_frame_t *frame;
76 struct list_head fifo_list;
77 struct list_head msg_list;
78 uint8_t *buffer_head; // only for sending packets
79 uint8_t *buffer_read_pointer; // only for sending packets
80 uint32_t buffer_pending_bytes; // only for sending packets
84 #define FRESCAN_MX_PACKETS 100
87 * frescan_packets_init
89 * Initializes a pool of packets that will be managed internally
92 extern int frescan_packets_init();
95 * frescan_packets_alloc
97 * Allocates a frame from the pool of packets. On error it returns NULL
100 extern frescan_packet_t *frescan_packets_alloc();
103 * frescan_packets_free
105 * Frees a frame and returns it to the pool of packets.
108 extern int frescan_packets_free(frescan_packet_t *packet);
110 #endif // _MARTE_FRESCAN_PACKETS_H_