Keep subscription to signal simplier without doubling
[apps/agl-service-can-low-level.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 #include "can-message-set.hpp"
27
28 #define CAN_MESSAGE_SIZE 8
29
30 class can_bus_dev_t;
31
32 /**
33  * @enum can_message_format_t
34  * @brief The ID format for a CAN message.
35  */
36 enum class can_message_format_t {
37         STANDARD, /*!< STANDARD - standard 11-bit CAN arbitration ID. */
38         EXTENDED, /*!< EXTENDED - an extended frame, with a 29-bit arbitration ID. */
39         ERROR,    /*!< ERROR - ERROR code used at initialization to signify that it isn't usable'*/
40 };
41
42 /**
43  * @class can_message_t
44  *
45  * @brief A compact representation of a single CAN message, meant to be used in in/out
46  * buffers.
47  */
48 class can_message_t {
49 private:
50         uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/
51         uint32_t id_; /*!< id_ - The ID of the message. */
52         uint8_t length_; /*!< length_ - the length of the data array (max 8). */
53         can_message_format_t format_; /*!< format_ - the format of the message's ID.*/
54         bool rtr_flag_; /*!< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/
55         uint8_t flags_; /*!< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/
56         std::vector<uint8_t> data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
57
58 public:
59         can_message_t();
60         can_message_t(uint8_t maxdlen, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag_, uint8_t flags, std::vector<uint8_t> data);
61
62         uint32_t get_id() const;
63         bool get_rtr_flag_() const;
64         can_message_format_t get_format() const;
65         uint8_t get_flags() const;
66         const uint8_t* get_data() const;
67         uint8_t get_length() const;
68
69         void set_format(const can_message_format_t new_format);
70
71         bool is_correct_to_send();
72
73 static can_message_t convert_from_canfd_frame(const struct canfd_frame& frame, size_t nbytes);
74         canfd_frame convert_to_canfd_frame();
75 };
76
77 /**
78  * @class can_message_definition_t
79  *
80  * @brief The definition of a CAN message. This includes a lot of metadata, so
81  * to save memory this struct should not be used for storing incoming and
82  * outgoing CAN messages.
83  */
84 class can_message_definition_t
85 {
86         private:
87         can_bus_dev_t& bus_; /*!< bus_ - A pointer to the bus this message is on. */
88         uint32_t id_; /*!< id_ - The ID of the message.*/
89         can_message_format_t format_; /*!< format_ - the format of the message's ID.*/
90         frequency_clock_t clock_; /*!<  clock_ - an optional frequency clock to control the output of this
91                                                         *      message, if sent raw, or simply to mark the max frequency for custom
92                                                         *      handlers to retrieve.*/
93         bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send CAN
94                                                          *      message if it has changed when using raw passthrough.*/
95         std::vector<uint8_t> last_value_; /*!< last_value_ - The last received value of the message. Defaults to undefined.
96                                                                                   *     This is required for the forceSendChanged functionality, as the stack
97                                                                                   *     needs to compare an incoming CAN message with the previous frame.*/
98         
99         public:
100           can_message_definition_t(can_bus_dev_t& cbd);
101                 uint32_t get_id() const;
102 };
103
104 /** Public: Retrive a list of all possible CAN configurations.
105  *
106  * Returns a pointer to an array of all configurations.
107  */
108 can_message_set_t* getMessageSets();
109
110 /** Public: Return the length of the array returned by getMessageSets() */
111 int getMessageSetCount();
112
113 /* Public: Return an array of all CAN messages to be processed in the active
114  * configuration.
115  */
116 can_message_definition_t* getMessages();
117
118 /* Public: Return the length of the array returned by getMessages(). */
119 int getMessageCount();