9a074e31d21bda83f18a25ae108d174391fb9e70
[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 #define MAX_ISOTP_FRAMES 4096
33
34
35 /**
36  * FLAGS
37  */
38
39 #define INVALID_FLAG 0x0001
40 #define CAN_PROTOCOL 0x0002
41 #define J1939_PROTOCOL 0x0004
42 #define J1939_ADDR_CLAIM_PROTOCOL 0x0008
43 #define ISOTP_PROTOCOL 0x0010
44 #define ISOTP_SEND 0x0020
45 #define ISOTP_RECEIVE 0x0040
46 #define CAN_PROTOCOL_WITH_FD_FRAME 0x0080
47 #define FRAME_LAYOUT_IS_LE 0x0100
48
49 /// @class message_t
50 ///
51 /// @brief A compact representation of a single CAN message, meant to be used in in/out
52 /// buffers. It is a wrapper of a can_frame struct with some sugar around it for binding purposes.
53 class message_t {
54 protected:
55         uint32_t maxdlen_; ///< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/
56         uint32_t length_; ///< length_ - the length of the data array. */
57         uint32_t flags_; ///< format_ - the format mask of the message that control which socketcan will used*/
58         std::vector<uint8_t> data_; ///< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
59         uint64_t timestamp_; ///< timestamp_ - timestamp of the received message*/
60         int sub_id_; ///< sub_id_ - Subscription index. */
61
62 public:
63         message_t();
64         message_t(uint32_t maxdlen, uint32_t length, uint32_t flags, std::vector<uint8_t>& data, uint64_t timestamp);
65         virtual ~message_t() = default;
66
67         int get_sub_id() const;
68         const uint8_t* get_data() const;
69         const std::vector<uint8_t> get_data_vector() const;
70         const std::vector<uint8_t> get_data_vector(int start, int end) const;
71         uint32_t get_length() const;
72         uint64_t get_timestamp() const;
73
74         void set_data(std::vector<uint8_t> data);
75         void set_sub_id(int sub_id);
76         void set_timestamp(uint64_t timestamp);
77         virtual bool is_set() = 0;
78         virtual std::string get_debug_message() = 0;
79         virtual uint32_t get_id() const = 0;
80         virtual void set_id(canid_t id) = 0;
81         uint32_t get_flags();
82         void set_flags(uint32_t flags);
83         void erase_flags();
84         uint32_t get_maxdlen();
85         void set_maxdlen(uint32_t maxdlen);
86         void set_length(uint32_t length);
87 };