Change CanMessageFormat name to make it
[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 "can/can-bus.hpp"
26 #include "utils/timer.hpp"
27
28 #define CAN_MESSAGE_SIZE 8
29
30 /**
31  * @enum can_message_format
32  * @brief The ID format for a CAN message.
33  */
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'*/
38 };
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 class can_message_t {
47         private:
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.*/
54
55                 uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/
56
57         public:
58                 can_message_t();
59
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;
66
67                 void set_format(const can_message_format new_format);
68
69                 bool is_correct_to_send();
70
71     static can_message_t convert_to_canfd_frame(const struct canfd_frame& frame, size_t nbytes);
72                 canfd_frame convert_to_canfd_frame();
73 };
74
75 /**
76  * @class can_message_definition_t
77  *
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.
81  */
82 class can_message_definition_t
83 {
84         private:
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.*/
96         
97         public:
98           can_message_definition_t();
99                 uint32_t get_id() const;
100 };
101
102 /**
103  * @class can_message_set_t
104  *
105  * @brief A parent wrapper for a particular set of CAN messages and associated
106  *      CAN buses(e.g. a vehicle or program).
107  */
108 class can_message_set_t
109 {
110         private:
111                 uint8_t index_; /*!<index_ - A numerical ID for the message set, ideally the index in an array
112                                                 *       for fast lookup*/
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.*/
121 };
122
123 /** Public: Retrive a list of all possible CAN configurations.
124  *
125  * Returns a pointer to an array of all configurations.
126  */
127 CanMessageSet* getMessageSets();
128
129 /** Public: Return the length of the array returned by getMessageSets() */
130 int getMessageSetCount();
131
132 /* Public: Return an array of all CAN messages to be processed in the active
133  * configuration.
134  */
135 CanMessageDefinition* getMessages();
136
137 /* Public: Return the length of the array returned by getMessages(). */
138 int getMessageCount();