Change data_ member of can_message_t object
[apps/agl-service-can-low-level.git] / src / can-message.hpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #pragma once
19
20 #include <vector>
21 #include <string>
22 #include <cstdint>
23 #include <linux/can.h>
24
25 #include "timer.hpp"
26
27 #define CAN_MESSAGE_SIZE 8
28
29 /**
30  * @enum CanMessageFormat
31  * @brief The ID format for a CAN message.
32  */
33 enum CanMessageFormat {
34         STANDARD, /*!< STANDARD - standard 11-bit CAN arbitration ID. */
35         EXTENDED, /*!< EXTENDED - an extended frame, with a 29-bit arbitration ID. */
36         ERROR,    /*!< ERROR - ERROR code used at initialization to signify that it isn't usable'*/
37 };
38 typedef enum CanMessageFormat CanMessageFormat;
39
40 /**
41  * @class can_message_t
42  *
43  * @brief A compact representation of a single CAN message, meant to be used in in/out
44  * buffers.
45  */
46
47 /*************************
48  * old CanMessage struct *
49  *************************
50 struct CanMessage {
51         uint32_t id;
52         CanMessageFormat format;
53         uint8_t data[CAN_MESSAGE_SIZE];
54         uint8_t length;
55 };
56 typedef struct CanMessage CanMessage;
57 */
58 class can_message_t {
59         private:
60                 uint32_t id_; /*!< uint32_t id - The ID of the message. */
61                 bool rtr_flag_; /*!< bool rtr_flag - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/
62                 uint8_t length_; /*!<  uint8_t length - the length of the data array (max 8). */
63                 uint8_t flags_; /*!< unint8_t flags of a CAN FD frame. Needed if we catch FD frames.*/
64                 CanMessageFormat format_; /*!< CanMessageFormat format - the format of the message's ID.*/
65                 std::vector<uint8_t> data_; /*!< uint8_t data  - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
66
67                 uint8_t maxdlen_;
68
69         public:
70                 /**
71                  * @brief Class constructor
72                  *
73                  * Constructor about can_message_t class.
74                  */
75                 can_message_t();
76
77                 /**
78                  * @brief Retrieve id_ member value.
79                  *
80                  * @return uint32_t id_ class member
81                  */
82                 uint32_t get_id() const;
83                 
84                 /**
85                  * @brief Retrieve RTR flag member.
86                  *
87                  * @return bool rtr_flags_ class member
88                  */
89                 bool get_rtr_flag_() const;
90
91                 /**
92                  * @brief Retrieve format_ member value.
93                  *
94                  * @return CanMessageFormat format_ class member
95                  */
96                 int get_format() const;
97                 
98                 /**
99                  * @brief Retrieve format_ member value.
100                  *
101                  * @return CanMessageFormat format_ class member
102                  */
103                 uint8_t get_flags() const;
104                 
105                 /**
106                  * @brief Retrieve data_ member value.
107                  *
108                  * @return uint8_t data_ pointer to the first element 
109                  *  of class member data_
110                  */
111                 const uint8_t* get_data() const;
112                 
113                 /**
114                  * @brief Retrieve length_ member value.
115                  *
116                  * @return uint8_t length_ class member
117                  */
118                 uint8_t get_length() const;
119                 
120                 void set_max_data_length(const struct canfd_frame& frame);
121
122                 /**
123                  * @brief Control whether the object is correctly initialized
124                  *  to be sent over the CAN bus
125                  *
126                  * @return true if object correctly initialized and false if not...
127                  */
128                 bool is_correct_to_send();
129                 
130                 /**
131                  * @brief Set id_ member value.
132                  *
133                  * Preferred way to initialize these members by using 
134                  * convert_from_canfd_frame method.
135                  *
136                  * @param uint32_t id_ class member
137                  */
138                 void set_id_and_format(const uint32_t new_id);
139                 
140                 /**
141                  * @brief Set format_ member value.
142                  *
143                  * Preferred way to initialize these members by using 
144                  * convert_from_canfd_frame method.
145                  *
146                  * @param CanMessageFormat format_ class member
147                  */
148                 void set_format(const CanMessageFormat new_format);
149                 
150                 /**
151                  * @brief Set format_ member value. Deducing from the can_id
152                  *  of a canfd_frame.
153                  *
154                  * Preferred way to initialize these members by using 
155                  * convert_from_canfd_frame method.
156                  *
157                  * @param uint32_t can_id integer from a canfd_frame
158                  */
159                 void set_format(const uint32_t can_id);
160
161                 /**
162                  * @brief Set format_ member value.
163                  *
164                  * Preferred way to initialize these members by using 
165                  * convert_from_canfd_frame method.
166                  *
167                  * @param CanMessageFormat format_ class member
168                  */
169                 void set_flags(const uint8_t flags);
170
171                 /**
172                  * @brief Set data_ member value.
173                  *
174                  * Preferred way to initialize these members by using 
175                  * convert_from_canfd_frame method.
176                  *
177                  * @param uint8_t data_ array with a max size of 8 elements.
178                  */
179                 void set_data(const __u8 new_data[], size_t dlen);
180                 
181                 /**
182                  * @brief Set length_ member value.
183                  *
184                  * Preferred way to initialize these members by using 
185                  * convert_from_canfd_frame method.
186                  *
187                  * @param uint8_t length_ array with a max size of 8 elements.
188                  */
189                 void set_length(const uint8_t new_length);
190
191                 /**
192                  * @brief Take a canfd_frame struct to initialize class members
193                  *
194                  * This is the preferred way to initialize class members.
195                  *
196                  * @param canfd_frame struct read from can bus device.
197                  */
198                 void convert_from_canfd_frame(const struct canfd_frame& frame);
199                 
200                 /**
201                  * @brief Take all initialized class's members and build an 
202                  * canfd_frame struct that can be use to send a CAN message over
203                  * the bus.
204                  *
205                  * @return canfd_frame struct built from class members.
206                  */
207                 canfd_frame convert_to_canfd_frame();
208 };
209
210 /**
211  * @struct CanMessageDefinition
212  *
213  * @brief The definition of a CAN message. This includes a lot of metadata, so
214  * to save memory this struct should not be used for storing incoming and
215  * outgoing CAN messages.
216  */
217 struct CanMessageDefinition {
218         //can_bus_dev_t bus; /*!< bus - A pointer to the bus this message is on. */
219         uint32_t id; /*!<  id - The ID of the message.*/
220         CanMessageFormat format; /*!< format - the format of the message's ID.*/
221         FrequencyClock frequencyClock; /*!<  clock - an optional frequency clock to control the output of this
222                                                                         *       message, if sent raw, or simply to mark the max frequency for custom
223                                                                         *       handlers to retriec++ if ? syntaxve.*/
224         bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send CAN
225                                                         *       message if it has changed when using raw passthrough.*/
226         uint8_t lastValue[CAN_MESSAGE_SIZE]; /*!< lastValue - The last received value of the message. Defaults to undefined.
227                                                                                 *       This is required for the forceSendChanged functionality, as the stack
228                                                                                 *       needs to compare an incoming CAN message with the previous frame.*/
229 };
230 typedef struct CanMessageDefinition CanMessageDefinition;
231
232 /**
233  * @struct CanMessageSet
234  *
235  * @brief A parent wrapper for a particular set of CAN messages and associated
236  *      CAN buses(e.g. a vehicle or program).
237  */
238  typedef struct {
239         uint8_t index; /*!<index - A numerical ID for the message set, ideally the index in an array
240                                         *       for fast lookup*/
241         const std::string name; /*!< name - The name of the message set.*/
242         uint8_t busCount; /*!< busCount - The number of CAN buses defined for this message set.*/
243         unsigned short messageCount; /*!< messageCount - The number of CAN messages (across all buses) defined for
244                                                                         *       this message set.*/
245         unsigned short signalCount; /*!< signalCount - The number of CAN signals (across all messages) defined for
246                                                                 *       this message set.*/
247         unsigned short commandCount; /*!< commandCount - The number of CanCommmands defined for this message set.*/
248 } CanMessageSet;
249
250 /** Public: Return the currently active CAN configuration. */
251 CanMessageSet* getActiveMessageSet();
252
253 /** Public: Retrive a list of all possible CAN configurations.
254  *
255  * Returns a pointer to an array of all configurations.
256  */
257 CanMessageSet* getMessageSets();
258
259 /** Public: Return the length of the array returned by getMessageSets() */
260 int getMessageSetCount();
261
262 /* Public: Return an array of all CAN messages to be processed in the active
263  * configuration.
264  */
265 CanMessageDefinition* getMessages();
266
267 /* Public: Return the length of the array returned by getMessages(). */
268 int getMessageCount();