arduino-emulator
Loading...
Searching...
No Matches
CanMsg.h
1/*
2 CanMsg.h - Library for CAN message handling
3 Copyright (c) 2023 Arduino. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#ifndef ARDUINOCORE_API_CAN_MSG_H_
21#define ARDUINOCORE_API_CAN_MSG_H_
22
23/**************************************************************************************
24 * INCLUDE
25 **************************************************************************************/
26
27#include <inttypes.h>
28#include <string.h>
29
30#include "Print.h"
31#include "Printable.h"
32#include "Common.h"
33
34/**************************************************************************************
35 * NAMESPACE
36 **************************************************************************************/
37
38namespace arduino
39{
40
41/**************************************************************************************
42 * CLASS DECLARATION
43 **************************************************************************************/
44
45class CanMsg : public Printable
46{
47public:
48 static uint8_t constexpr MAX_DATA_LENGTH = 8;
49
50 static uint32_t constexpr CAN_EFF_FLAG = 0x80000000U;
51 static uint32_t constexpr CAN_SFF_MASK = 0x000007FFU; /* standard frame format (SFF) */
52 static uint32_t constexpr CAN_EFF_MASK = 0x1FFFFFFFU; /* extended frame format (EFF) */
53
54
55 CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr)
56 : id{can_id}
57 , data_length{min(can_data_len, MAX_DATA_LENGTH)}
58 , data{0}
59 {
60 if (data_length && can_data_ptr)
61 memcpy(data, can_data_ptr, data_length);
62 }
63
64 CanMsg() : CanMsg(0, 0, nullptr) { }
65
66 CanMsg(CanMsg const & other)
67 {
68 id = other.id;
69 data_length = other.data_length;
70 if (data_length > 0)
71 memcpy(data, other.data, data_length);
72 }
73
74 virtual ~CanMsg() { }
75
76 CanMsg & operator = (CanMsg const & other)
77 {
78 if (this != &other)
79 {
80 id = other.id;
81 data_length = other.data_length;
82 if (data_length > 0)
83 memcpy(data, other.data, data_length);
84 }
85 return (*this);
86 }
87
88 virtual size_t printTo(Print & p) const override
89 {
90 char buf[20] = {0};
91 size_t len = 0;
92
93 /* Print the header. */
94 if (isStandardId())
95 len = snprintf(buf, sizeof(buf), "[%03" PRIX32 "] (%d) : ", getStandardId(), data_length);
96 else
97 len = snprintf(buf, sizeof(buf), "[%08" PRIX32 "] (%d) : ", getExtendedId(), data_length);
98 size_t n = p.write(buf, len);
99
100 /* Print the data. */
101 for (size_t d = 0; d < data_length; d++)
102 {
103 len = snprintf(buf, sizeof(buf), "%02X", data[d]);
104 n += p.write(buf, len);
105 }
106
107 /* Wrap up. */
108 return n;
109 }
110
111
112 uint32_t getStandardId() const {
113 return (id & CAN_SFF_MASK);
114 }
115 uint32_t getExtendedId() const {
116 return (id & CAN_EFF_MASK);
117 }
118 bool isStandardId() const {
119 return ((id & CAN_EFF_FLAG) == 0);
120 }
121 bool isExtendedId() const {
122 return ((id & CAN_EFF_FLAG) == CAN_EFF_FLAG);
123 }
124
125
126 /*
127 * CAN ID semantics (mirroring definition by linux/can.h):
128 * |- Bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
129 * |- Bit 30 : reserved (future remote transmission request flag)
130 * |- Bit 29 : reserved (future error frame flag)
131 * |- Bit 0-28 : CAN identifier (11/29 bit)
132 */
133 uint32_t id;
134 uint8_t data_length;
135 uint8_t data[MAX_DATA_LENGTH];
136};
137
138/**************************************************************************************
139 * FREE FUNCTIONS
140 **************************************************************************************/
141
142inline uint32_t CanStandardId(uint32_t const id) {
143 return (id & CanMsg::CAN_SFF_MASK);
144}
145
146inline uint32_t CanExtendedId(uint32_t const id) {
147 return (CanMsg::CAN_EFF_FLAG | (id & CanMsg::CAN_EFF_MASK));
148}
149
150/**************************************************************************************
151 * NAMESPACE
152 **************************************************************************************/
153
154} /* arduino */
155
156#endif /* ARDUINOCORE_API_CAN_MSG_H_ */
Definition CanMsg.h:46
Definition Print.h:36
Definition Printable.h:35
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31