-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
parser.h
62 lines (55 loc) · 2.54 KB
/
parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef PARSER_H_
#define PARSER_H_
#include "cannelloni.h"
#include <linux/can.h>
#include <sys/types.h>
#include <functional>
#include <list>
/**
* Parses Cannelloni packet and extracts CAN frames
* If frameAllocator allocates heap memory or reserves resources in some preallocated buffer
* you need to remember to free those resources up in the frameReceiver implementation.
* parseFrames function does not claim ownership of resources allocated by frameAllocator.
* In the case when incomplete packet is received frameReceiver will be passed a frame
* with len set to 0, so that proper deallocation of resources can be done there.
*
* @param len Buffer length
* @param buffer Pointer to buffer containing Cannelloni packet
* @param frameAllocator Callback responsible for providing memory for CAN frame. Returns pointer
* to allocated frame.
* @param frameReceiver Callback responsible for handling newly read CAN frame. First argument
* is pointer to frame allocated by frameAllocator and filled with data by parseFrames.
* Second argument tells whether frame was read with success and is correct (can be processed
* further), or whether there was some error and it should not be processed (apart from
* being deallocated)
*/
void parseFrames(uint16_t len, const uint8_t* buffer,
std::function<canfd_frame*()> frameAllocator,
std::function<void(canfd_frame*, bool)> frameReceiver);
/**
* Encodes a CAN frame into its binary data format.
*
* @param data Pointer to the data buffer where the encoded frame will be
stored.
* @param frame Pointer to the CAN frame structure to encode.
*
* @return The size of the encoded frame.
*/
size_t encodeFrame(uint8_t *data, canfd_frame *frame);
/**
* Builds Cannelloni packet from provided list of CAN frames
* @param len Buffer length
* @param packetBuffer Pointer to buffer that will contain Cannelloni packet
* @param frames Reference to list of pointers to CAN frames
* @param seqNo Packet sequence number
* @param handleOverflow Callback responsible for handling CAN frames that
* did't fit into Cannelloni package. First argument is a frames list
* reference, second argument is iterator to the first not handled frame.
* @return
*/
uint8_t *buildPacket(uint16_t len, uint8_t *packetBuffer,
std::list<canfd_frame *> &frames, uint8_t seqNo,
std::function<void(std::list<canfd_frame *> &,
std::list<canfd_frame *>::iterator)>
handleOverflow);
#endif /* PARSER_H_ */