Libsockcanpp
A complete C++ wrapper around socketcan.
Loading...
Searching...
No Matches
CanXLMessage.hpp
Go to the documentation of this file.
1/**
2 * @file CanMessage.hpp
3 * @author Simon Cahill (contact@simonc.eu)
4 * @brief Contains the implementation of a CAN message representation in C++.
5 * @version 0.1
6 * @date 2025-03-25
7 *
8 * @copyright Copyright (c) 2020-2025 Simon Cahill
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#ifndef LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
24#define LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
25
26//////////////////////////////
27// SYSTEM INCLUDES //
28//////////////////////////////
29#include <linux/can.h>
30
31#include <cstring>
32#include <exception>
33#include <string>
34#include <system_error>
35#include <thread>
36
37//////////////////////////////
38// LOCAL INCLUDES //
39//////////////////////////////
40#include "CanId.hpp"
41
42namespace sockcanpp {
43
44 using std::error_code;
45 using std::generic_category;
46 using std::string;
47 using std::system_error;
48
49 /**
50 * @brief Represents a CAN message that was received.
51 */
52 class CanMessage {
53 public: // +++ Constructor / Destructor +++
54 CanMessage(const struct canxl_frame frame): _rawFrame(frame) { }
55
56 CanMessage(const CanId& priorityField, const CanId& acceptanceField, const string& frameData) {
57 if (frameData.size() > CANXL_MAX_DLEN) {
58 throw system_error(error_code(0xbadd1c, generic_category()), "Payload too big!");
59 }
60
61 struct canxl_frame rawFrame{};
62
63 std::copy(frameData.begin(), frameData.end(), rawFrame.data);
64
65 rawFrame.len = frameData.size();
66 rawFrame.prio = static_cast<uint16_t>(priorityField); // Ensure the CanId is truncated to 11 bits
67 rawFrame.af = static_cast<uint32_t>(acceptanceField); // Ensure the CanId is truncated to 32 bits
68 rawFrame.flags |= CANXL_XLF; // Set the mandatory CAN XL frame flag
69 rawFrame.sdt = 0; // Set the SDU type to 0 (default)
70 }
71
72 virtual ~CanMessage() = default;
73
74 public: // +++ Getters +++
75
76 private:
77 struct canxl_frame _rawFrame;
78 };
79
80}
81
82#endif // LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
Represents a CAN message that was received.
CanMessage(const CanId &priorityField, const CanId &acceptanceField, const string &frameData)
CanMessage(const struct canxl_frame frame)
struct can_frame _rawFrame
Main library namespace.
Definition CanDriver.cpp:56
Represents a CAN ID in a simple and easy-to-use manner.
Definition CanId.hpp:61