2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <linux/can.h>
26 #define CAN_MESSAGE_SIZE 8
29 * @enum CanMessageFormat
30 * @brief The ID format for a CAN message.
32 enum CanMessageFormat {
33 STANDARD, /*!< STANDARD - standard 11-bit CAN arbitration ID. */
34 EXTENDED, /*!< EXTENDED - an extended frame, with a 29-bit arbitration ID. */
35 ERROR, /*!< ERROR - ERROR code used at initialization to signify that it isn't usable'*/
37 typedef enum CanMessageFormat CanMessageFormat;
40 * @class can_message_t
42 * @brief A compact representation of a single CAN message, meant to be used in in/out
46 /*************************
47 * old CanMessage struct *
48 *************************
51 CanMessageFormat format;
52 uint8_t data[CAN_MESSAGE_SIZE];
55 typedef struct CanMessage CanMessage;
59 uint32_t id_; /*!< uint32_t id - The ID of the message. */
60 uint8_t length_; /*!< uint8_t length - the length of the data array (max 8). */
61 CanMessageFormat format_; /*!< CanMessageFormat format - the format of the message's ID.*/
62 uint8_t data_[CAN_MESSAGE_SIZE]; /*!< uint8_t data - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
66 * @brief Class constructor
68 * Constructor about can_message_t class.
73 * @brief Retrieve id_ member value.
75 * @return uint32_t id_ class member
77 uint32_t get_id() const;
80 * @brief Retrieve format_ member value.
82 * @return CanMessageFormat format_ class member
84 int get_format() const;
87 * @brief Retrieve data_ member value.
89 * @return uint8_t data_ pointer class member
91 const uint8_t* get_data() const;
94 * @brief Retrieve length_ member value.
96 * @return uint8_t length_ class member
98 uint8_t get_length() const;
101 * @brief Control whether the object is correctly initialized
102 * to be sent over the CAN bus
104 * @return true if object correctly initialized and false if not...
106 bool is_correct_to_send();
109 * @brief Set id_ member value.
111 * Preferred way to initialize these members by using
112 * convert_from_canfd_frame method.
114 * @param uint32_t id_ class member
116 void set_id(const uint32_t new_id);
119 * @brief Set format_ member value.
121 * Preferred way to initialize these members by using
122 * convert_from_canfd_frame method.
124 * @param CanMessageFormat format_ class member
126 void set_format(const CanMessageFormat format);
129 * @brief Set data_ member value.
131 * Preferred way to initialize these members by using
132 * convert_from_canfd_frame method.
134 * @param uint8_t data_ array with a max size of 8 elements.
136 void set_data(const uint8_t new_data);
139 * @brief Set length_ member value.
141 * Preferred way to initialize these members by using
142 * convert_from_canfd_frame method.
144 * @param uint8_t length_ array with a max size of 8 elements.
146 void set_length(const uint8_t new_length);
149 * @brief Take a canfd_frame struct to initialize class members
151 * This is the preferred way to initialize class members.
153 * @param canfd_frame struct read from can bus device.
155 void convert_from_canfd_frame(const canfd_frame& frame);
158 * @brief Take all initialized class's members and build an
159 * canfd_frame struct that can be use to send a CAN message over
162 * @return canfd_frame struct built from class members.
164 canfd_frame convert_to_canfd_frame();
168 * @struct CanMessageDefinition
170 * @brief The definition of a CAN message. This includes a lot of metadata, so
171 * to save memory this struct should not be used for storing incoming and
172 * outgoing CAN messages.
174 struct CanMessageDefinition {
175 //can_bus_dev_t bus; /*!< bus - A pointer to the bus this message is on. */
176 uint32_t id; /*!< id - The ID of the message.*/
177 CanMessageFormat format; /*!< format - the format of the message's ID.*/
178 FrequencyClock frequencyClock; /*!< clock - an optional frequency clock to control the output of this
179 * message, if sent raw, or simply to mark the max frequency for custom
180 * handlers to retriec++ if ? syntaxve.*/
181 bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send CAN
182 * message if it has changed when using raw passthrough.*/
183 uint8_t lastValue[CAN_MESSAGE_SIZE]; /*!< lastValue - The last received value of the message. Defaults to undefined.
184 * This is required for the forceSendChanged functionality, as the stack
185 * needs to compare an incoming CAN message with the previous frame.*/
187 typedef struct CanMessageDefinition CanMessageDefinition;
190 * @struct CanMessageSet
192 * @brief A parent wrapper for a particular set of CAN messages and associated
193 * CAN buses(e.g. a vehicle or program).
196 uint8_t index; /*!<index - A numerical ID for the message set, ideally the index in an array
198 const std::string name; /*!< name - The name of the message set.*/
199 uint8_t busCount; /*!< busCount - The number of CAN buses defined for this message set.*/
200 unsigned short messageCount; /*!< messageCount - The number of CAN messages (across all buses) defined for
201 * this message set.*/
202 unsigned short signalCount; /*!< signalCount - The number of CAN signals (across all messages) defined for
203 * this message set.*/
204 unsigned short commandCount; /*!< commandCount - The number of CanCommmands defined for this message set.*/
207 /** Public: Return the currently active CAN configuration. */
208 CanMessageSet* getActiveMessageSet();
210 /** Public: Retrive a list of all possible CAN configurations.
212 * Returns a pointer to an array of all configurations.
214 CanMessageSet* getMessageSets();
216 /** Public: Return the length of the array returned by getMessageSets() */
217 int getMessageSetCount();
219 /* Public: Return an array of all CAN messages to be processed in the active
222 CanMessageDefinition* getMessages();
224 /* Public: Return the length of the array returned by getMessages(). */
225 int getMessageCount();