6e0daadaff9b2290403c30130ab0820242e0f409
[apps/agl-service-can-low-level.git] / low-can-binding / can / message / 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 <iostream>
24 #include <memory>
25 #include <linux/can.h>
26 #include <linux/can/bcm.h>
27 #include "../../utils/timer.hpp"
28
29 #define CAN_MESSAGE_SIZE 8
30
31 #define MAX_BCM_CAN_FRAMES 257
32
33 struct bcm_msg
34 {
35         struct bcm_msg_head msg_head;
36         union {
37                 struct canfd_frame fd_frames[MAX_BCM_CAN_FRAMES];
38                 struct can_frame frames[MAX_BCM_CAN_FRAMES];
39         };
40 };
41
42 /**
43  * @enum can_message_format_t
44  * @brief The ID format for a CAN message.
45  */
46 enum class can_message_format_t {
47         STANDARD, ///< STANDARD - standard 11-bit CAN arbitration ID. */
48         EXTENDED, ///< EXTENDED - an extended frame, with a 29-bit arbitration ID. */
49         J1939,    ///< J1939    - Format for j1939 messages
50         INVALID,  ///< INVALID  - INVALID code used at initialization to signify that it isn't usable'*/
51 };
52
53
54 /// @class message_t
55 ///
56 /// @brief A compact representation of a single CAN message, meant to be used in in/out
57 /// buffers. It is a wrapper of a can_frame struct with some sugar around it for binding purposes.
58 class message_t {
59 protected:
60         uint8_t length_; ///< length_ - the length of the data array (max 8). */
61         can_message_format_t format_; ///< format_ - the format of the message's ID.*/
62         std::vector<uint8_t> data_; ///< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
63         uint64_t timestamp_; ///< timestamp_ - timestamp of the received message*/
64         int sub_id_; ///< sub_id_ - Subscription index. */
65
66
67 public:
68         message_t();
69         message_t(uint8_t length, can_message_format_t format, std::vector<uint8_t>& data, uint64_t timestamp);
70
71         int get_sub_id() const;
72         const uint8_t* get_data() const;
73         const std::vector<uint8_t> get_data_vector() const;
74         uint8_t get_length() const;
75         uint64_t get_timestamp() const;
76
77         void set_sub_id(int sub_id);
78         void set_timestamp(uint64_t timestamp);
79         can_message_format_t get_msg_format();
80         virtual bool is_set() = 0;
81         virtual std::string get_debug_message() = 0;
82         virtual uint32_t get_id() const = 0;
83         virtual struct bcm_msg get_bcm_msg() = 0;
84         virtual void set_bcm_msg(struct bcm_msg bcm_msg) = 0;
85
86 };