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.
23 #include <linux/can.h>
25 #include "can/can-bus.hpp"
26 #include "utils/timer.hpp"
28 #define CAN_MESSAGE_SIZE 8
31 * @enum CanMessageFormat
32 * @brief The ID format for a CAN message.
34 enum CanMessageFormat {
35 STANDARD, /*!< STANDARD - standard 11-bit CAN arbitration ID. */
36 EXTENDED, /*!< EXTENDED - an extended frame, with a 29-bit arbitration ID. */
37 ERROR, /*!< ERROR - ERROR code used at initialization to signify that it isn't usable'*/
39 typedef enum CanMessageFormat CanMessageFormat;
42 * @class can_message_t
44 * @brief A compact representation of a single CAN message, meant to be used in in/out
48 /*************************
49 * old CanMessage struct *
50 *************************
53 CanMessageFormat format;
54 uint8_t data[CAN_MESSAGE_SIZE];
57 typedef struct CanMessage CanMessage;
61 uint32_t id_; /*!< id_ - The ID of the message. */
62 bool rtr_flag_; /*!< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/
63 uint8_t length_; /*!< length_ - the length of the data array (max 8). */
64 uint8_t flags_; /*!< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/
65 CanMessageFormat format_; /*!< format_ - the format of the message's ID.*/
66 std::vector<uint8_t> data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
68 uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/
73 uint32_t get_id() const;
74 bool get_rtr_flag_() const;
75 int get_format() const;
76 uint8_t get_flags() const;
77 const uint8_t* get_data() const;
78 uint8_t get_length() const;
80 void set_max_data_length(size_t nbytes);
81 void set_id_and_format(const uint32_t new_id);
82 void set_format(const CanMessageFormat new_format);
83 void set_format(const uint32_t can_id);
84 void set_flags(const uint8_t flags);
85 void set_data(const __u8* new_data);
86 void set_length(const uint8_t new_length);
88 bool is_correct_to_send();
90 void convert_from_canfd_frame(const std::pair<struct canfd_frame&, size_t>args);
91 canfd_frame convert_to_canfd_frame();
95 * @struct CanMessageDefinition
97 * @brief The definition of a CAN message. This includes a lot of metadata, so
98 * to save memory this struct should not be used for storing incoming and
99 * outgoing CAN messages.
101 struct CanMessageDefinition {
102 //can_bus_dev_t bus; /*!< bus - A pointer to the bus this message is on. */
103 uint32_t id; /*!< id - The ID of the message.*/
104 CanMessageFormat format; /*!< format - the format of the message's ID.*/
105 FrequencyClock frequencyClock; /*!< clock - an optional frequency clock to control the output of this
106 * message, if sent raw, or simply to mark the max frequency for custom
107 * handlers to retriec++ if ? syntaxve.*/
108 bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send CAN
109 * message if it has changed when using raw passthrough.*/
110 uint8_t lastValue[CAN_MESSAGE_SIZE]; /*!< lastValue - The last received value of the message. Defaults to undefined.
111 * This is required for the forceSendChanged functionality, as the stack
112 * needs to compare an incoming CAN message with the previous frame.*/
114 typedef struct CanMessageDefinition CanMessageDefinition;
116 class can_message_definition_t
119 can_bus_dev_t& bus_; /*!< bus_ - A pointer to the bus this message is on. */
120 uint32_t id_; /*!< id_ - The ID of the message.*/
121 CanMessageFormat format_; /*!< format_ - the format of the message's ID.*/
122 FrequencyClock clock_; /*!< clock_ - an optional frequency clock to control the output of this
123 * message, if sent raw, or simply to mark the max frequency for custom
124 * handlers to retriec++ if ? syntaxve.*/
125 bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send CAN
126 * message if it has changed when using raw passthrough.*/
127 uint8_t last_value_[CAN_MESSAGE_SIZE]; /*!< last_value_ - The last received value of the message. Defaults to undefined.
128 * This is required for the forceSendChanged functionality, as the stack
129 * needs to compare an incoming CAN message with the previous frame.*/
136 * @struct CanMessageSet
138 * @brief A parent wrapper for a particular set of CAN messages and associated
139 * CAN buses(e.g. a vehicle or program).
142 uint8_t index; /*!<index - A numerical ID for the message set, ideally the index in an array
144 const std::string name; /*!< name - The name of the message set.*/
145 uint8_t busCount; /*!< busCount - The number of CAN buses defined for this message set.*/
146 unsigned short messageCount; /*!< messageCount - The number of CAN messages (across all buses) defined for
147 * this message set.*/
148 unsigned short signalCount; /*!< signalCount - The number of CAN signals (across all messages) defined for
149 * this message set.*/
150 unsigned short commandCount; /*!< commandCount - The number of CanCommmands defined for this message set.*/
153 class can_message_set_t
156 uint8_t index_; /*!<index_ - A numerical ID for the message set, ideally the index in an array
158 const std::string name_; /*!< name_ - The name of the message set.*/
159 uint8_t can_bus_count_; /*!< can_bus_count_ - The number of CAN buses defined for this message set.*/
160 unsigned short can_message_count_; /*!< can_message_count_ - The number of CAN messages (across all buses) defined for
161 * this message set.*/
162 unsigned short can_signal_count_; /*!< can_signal_count_ - The number of CAN signals (across all messages) defined for
163 * this message set.*/
164 unsigned short can_command_count_; /*!< can_command_count_ - The number of CanCommmands defined for this message set.*/
165 unsigned short obd2_signal_count_; /*!< obd2_signal_count_ - The number of obd2 signals defined for this message set.*/
168 /** Public: Return the currently active CAN configuration. */
169 CanMessageSet* getActiveMessageSet();
171 /** Public: Retrive a list of all possible CAN configurations.
173 * Returns a pointer to an array of all configurations.
175 CanMessageSet* getMessageSets();
177 /** Public: Return the length of the array returned by getMessageSets() */
178 int getMessageSetCount();
180 /* Public: Return an array of all CAN messages to be processed in the active
183 CanMessageDefinition* getMessages();
185 /* Public: Return the length of the array returned by getMessages(). */
186 int getMessageCount();