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 can_message_format
32 * @brief The ID format for a CAN message.
34 enum class can_message_format {
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'*/
41 * @class can_message_t
43 * @brief A compact representation of a single CAN message, meant to be used in in/out
48 uint32_t id_; /*!< id_ - The ID of the message. */
49 bool rtr_flag_; /*!< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/
50 uint8_t length_; /*!< length_ - the length of the data array (max 8). */
51 uint8_t flags_; /*!< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/
52 can_message_format format_; /*!< format_ - the format of the message's ID.*/
53 std::vector<uint8_t> data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
55 uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/
60 uint32_t get_id() const;
61 bool get_rtr_flag_() const;
62 int get_format() const;
63 uint8_t get_flags() const;
64 const uint8_t* get_data() const;
65 uint8_t get_length() const;
67 void set_format(const can_message_format new_format);
69 bool is_correct_to_send();
71 static can_message_t convert_to_canfd_frame(const struct canfd_frame& frame, size_t nbytes);
72 canfd_frame convert_to_canfd_frame();
76 * @class can_message_definition_t
78 * @brief The definition of a CAN message. This includes a lot of metadata, so
79 * to save memory this struct should not be used for storing incoming and
80 * outgoing CAN messages.
82 class can_message_definition_t
85 can_bus_dev_t& bus_; /*!< bus_ - A pointer to the bus this message is on. */
86 uint32_t id_; /*!< id_ - The ID of the message.*/
87 can_message_format format_; /*!< format_ - the format of the message's ID.*/
88 FrequencyClock clock_; /*!< clock_ - an optional frequency clock to control the output of this
89 * message, if sent raw, or simply to mark the max frequency for custom
90 * handlers to retrieve.*/
91 bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send CAN
92 * message if it has changed when using raw passthrough.*/
93 std::vector<uint8_t> last_value_; /*!< last_value_ - The last received value of the message. Defaults to undefined.
94 * This is required for the forceSendChanged functionality, as the stack
95 * needs to compare an incoming CAN message with the previous frame.*/
98 can_message_definition_t();
99 uint32_t get_id() const;
103 * @class can_message_set_t
105 * @brief A parent wrapper for a particular set of CAN messages and associated
106 * CAN buses(e.g. a vehicle or program).
108 class can_message_set_t
111 uint8_t index_; /*!<index_ - A numerical ID for the message set, ideally the index in an array
113 const std::string name_; /*!< name_ - The name of the message set.*/
114 uint8_t can_bus_count_; /*!< can_bus_count_ - The number of CAN buses defined for this message set.*/
115 unsigned short can_message_count_; /*!< can_message_count_ - The number of CAN messages (across all buses) defined for
116 * this message set.*/
117 unsigned short can_signal_count_; /*!< can_signal_count_ - The number of CAN signals (across all messages) defined for
118 * this message set.*/
119 unsigned short can_command_count_; /*!< can_command_count_ - The number of CanCommmands defined for this message set.*/
120 unsigned short obd2_signal_count_; /*!< obd2_signal_count_ - The number of obd2 signals defined for this message set.*/
123 /** Public: Retrive a list of all possible CAN configurations.
125 * Returns a pointer to an array of all configurations.
127 CanMessageSet* getMessageSets();
129 /** Public: Return the length of the array returned by getMessageSets() */
130 int getMessageSetCount();
132 /* Public: Return an array of all CAN messages to be processed in the active
135 CanMessageDefinition* getMessages();
137 /* Public: Return the length of the array returned by getMessages(). */
138 int getMessageCount();