c5abd7fbafc5644c92357ba6611a5c916c85ad0f
[apps/low-level-can-service.git] / src / can / 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 "utils/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                 can_message_t();
71
72                 uint32_t get_id() const;
73                 bool get_rtr_flag_() const;
74                 int get_format() const;
75                 uint8_t get_flags() const;
76                 const uint8_t* get_data() const;
77                 uint8_t get_length() const;
78
79                 void set_max_data_length(size_t nbytes);
80                 void set_id_and_format(const uint32_t new_id);
81                 void set_format(const CanMessageFormat new_format);
82                 void set_format(const uint32_t can_id);
83                 void set_flags(const uint8_t flags);
84                 void set_data(const __u8* new_data);
85                 void set_length(const uint8_t new_length);
86
87                 bool is_correct_to_send();
88
89                 void convert_from_canfd_frame(const std::pair<struct canfd_frame&, size_t>args);
90                 canfd_frame convert_to_canfd_frame();
91 };
92
93 /**
94  * @struct CanMessageDefinition
95  *
96  * @brief The definition of a CAN message. This includes a lot of metadata, so
97  * to save memory this struct should not be used for storing incoming and
98  * outgoing CAN messages.
99  */
100 struct CanMessageDefinition {
101         //can_bus_dev_t bus; /*!< bus - A pointer to the bus this message is on. */
102         uint32_t id; /*!<  id - The ID of the message.*/
103         CanMessageFormat format; /*!< format - the format of the message's ID.*/
104         FrequencyClock frequencyClock; /*!<  clock - an optional frequency clock to control the output of this
105                                                                         *       message, if sent raw, or simply to mark the max frequency for custom
106                                                                         *       handlers to retriec++ if ? syntaxve.*/
107         bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send CAN
108                                                         *       message if it has changed when using raw passthrough.*/
109         uint8_t lastValue[CAN_MESSAGE_SIZE]; /*!< lastValue - The last received value of the message. Defaults to undefined.
110                                                                                 *       This is required for the forceSendChanged functionality, as the stack
111                                                                                 *       needs to compare an incoming CAN message with the previous frame.*/
112 };
113 typedef struct CanMessageDefinition CanMessageDefinition;
114
115 class can_message_definition_t
116 {
117         private:
118         can_bus_dev_t& bus_; /*!< bus - A pointer to the bus this message is on. */
119         uint32_t id_; /*!<  id - The ID of the message.*/
120         CanMessageFormat format_; /*!< format - the format of the message's ID.*/
121         FrequencyClock clock_; /*!<  clock - an optional frequency clock to control the output of this
122                                                         *       message, if sent raw, or simply to mark the max frequency for custom
123                                                         *       handlers to retriec++ if ? syntaxve.*/
124         bool forceSendChanged_; /*!< forceSendChanged - If true, regardless of the frequency, it will send CAN
125                                                          *      message if it has changed when using raw passthrough.*/
126         uint8_t lastValue_[CAN_MESSAGE_SIZE]; /*!< lastValue - The last received value of the message. Defaults to undefined.
127                                                                                   *     This is required for the forceSendChanged functionality, as the stack
128                                                                                   *     needs to compare an incoming CAN message with the previous frame.*/
129         
130         public:
131 };
132
133 /**
134  * @struct CanMessageSet
135  *
136  * @brief A parent wrapper for a particular set of CAN messages and associated
137  *      CAN buses(e.g. a vehicle or program).
138  */
139  typedef struct {
140         uint8_t index; /*!<index - A numerical ID for the message set, ideally the index in an array
141                                         *       for fast lookup*/
142         const std::string name; /*!< name - The name of the message set.*/
143         uint8_t busCount; /*!< busCount - The number of CAN buses defined for this message set.*/
144         unsigned short messageCount; /*!< messageCount - The number of CAN messages (across all buses) defined for
145                                                                         *       this message set.*/
146         unsigned short signalCount; /*!< signalCount - The number of CAN signals (across all messages) defined for
147                                                                 *       this message set.*/
148         unsigned short commandCount; /*!< commandCount - The number of CanCommmands defined for this message set.*/
149 } CanMessageSet;
150
151 class can_message_set_t
152 {
153         private:
154                 uint8_t index; /*!<index - A numerical ID for the message set, ideally the index in an array
155                                                 *       for fast lookup*/
156                 const std::string name; /*!< name - The name of the message set.*/
157                 uint8_t can_bus_count; /*!< busCount - The number of CAN buses defined for this message set.*/
158                 unsigned short can_message_count; /*!< messageCount - The number of CAN messages (across all buses) defined for
159                                                                                 *       this message set.*/
160                 unsigned short can_signal_count; /*!< signalCount - The number of CAN signals (across all messages) defined for
161                                                                         *       this message set.*/
162                 unsigned short can_command_count; /*!< commandCount - The number of CanCommmands defined for this message set.*/
163                 unsigned short obd2_signal_count; /*!< commandCount - The number of obd2 signals defined for this message set.*/
164 };
165
166 /** Public: Return the currently active CAN configuration. */
167 CanMessageSet* getActiveMessageSet();
168
169 /** Public: Retrive a list of all possible CAN configurations.
170  *
171  * Returns a pointer to an array of all configurations.
172  */
173 CanMessageSet* getMessageSets();
174
175 /** Public: Return the length of the array returned by getMessageSets() */
176 int getMessageSetCount();
177
178 /* Public: Return an array of all CAN messages to be processed in the active
179  * configuration.
180  */
181 CanMessageDefinition* getMessages();
182
183 /* Public: Return the length of the array returned by getMessages(). */
184 int getMessageCount();