|
| 1 | +/* |
| 2 | + * This file is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of either the GNU General Public License version 2 |
| 4 | + * or the GNU Lesser General Public License version 2.1, both as |
| 5 | + * published by the Free Software Foundation. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef ARDUINOCORE_API_CAN_MSG_H_ |
| 9 | +#define ARDUINOCORE_API_CAN_MSG_H_ |
| 10 | + |
| 11 | +/************************************************************************************** |
| 12 | + * INCLUDE |
| 13 | + **************************************************************************************/ |
| 14 | + |
| 15 | +#include <inttypes.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +#include "Print.h" |
| 19 | +#include "Printable.h" |
| 20 | +#include "Common.h" |
| 21 | + |
| 22 | +/************************************************************************************** |
| 23 | + * NAMESPACE |
| 24 | + **************************************************************************************/ |
| 25 | + |
| 26 | +namespace arduino |
| 27 | +{ |
| 28 | + |
| 29 | +/************************************************************************************** |
| 30 | + * CLASS DECLARATION |
| 31 | + **************************************************************************************/ |
| 32 | + |
| 33 | +class CanMsg : public Printable |
| 34 | +{ |
| 35 | +public: |
| 36 | + static uint8_t constexpr MAX_DATA_LENGTH = 8; |
| 37 | + |
| 38 | + static uint32_t constexpr CAN_EFF_FLAG = 0x80000000U; |
| 39 | + static uint32_t constexpr CAN_SFF_MASK = 0x000007FFU; /* standard frame format (SFF) */ |
| 40 | + static uint32_t constexpr CAN_EFF_MASK = 0x1FFFFFFFU; /* extended frame format (EFF) */ |
| 41 | + |
| 42 | + |
| 43 | + CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr) |
| 44 | + : id{can_id} |
| 45 | + , data_length{min(can_data_len, MAX_DATA_LENGTH)} |
| 46 | + , data{0} |
| 47 | + { |
| 48 | + if (data_length && can_data_ptr) |
| 49 | + memcpy(data, can_data_ptr, data_length); |
| 50 | + } |
| 51 | + |
| 52 | + CanMsg() : CanMsg(0, 0, nullptr) { } |
| 53 | + |
| 54 | + CanMsg(CanMsg const & other) |
| 55 | + { |
| 56 | + id = other.id; |
| 57 | + data_length = other.data_length; |
| 58 | + if (data_length > 0) |
| 59 | + memcpy(data, other.data, data_length); |
| 60 | + } |
| 61 | + |
| 62 | + virtual ~CanMsg() { } |
| 63 | + |
| 64 | + CanMsg & operator = (CanMsg const & other) |
| 65 | + { |
| 66 | + if (this != &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 | + return (*this); |
| 74 | + } |
| 75 | + |
| 76 | + virtual size_t printTo(Print & p) const override |
| 77 | + { |
| 78 | + char buf[20] = {0}; |
| 79 | + size_t len = 0; |
| 80 | + |
| 81 | + /* Print the header. */ |
| 82 | + if (isStandardId()) |
| 83 | + len = snprintf(buf, sizeof(buf), "[%03" PRIX32 "] (%d) : ", getStandardId(), data_length); |
| 84 | + else |
| 85 | + len = snprintf(buf, sizeof(buf), "[%08" PRIX32 "] (%d) : ", getExtendedId(), data_length); |
| 86 | + size_t n = p.write(buf, len); |
| 87 | + |
| 88 | + /* Print the data. */ |
| 89 | + for (size_t d = 0; d < data_length; d++) |
| 90 | + { |
| 91 | + len = snprintf(buf, sizeof(buf), "%02X", data[d]); |
| 92 | + n += p.write(buf, len); |
| 93 | + } |
| 94 | + |
| 95 | + /* Wrap up. */ |
| 96 | + return n; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + uint32_t getStandardId() const { |
| 101 | + return (id & CAN_SFF_MASK); |
| 102 | + } |
| 103 | + uint32_t getExtendedId() const { |
| 104 | + return (id & CAN_EFF_MASK); |
| 105 | + } |
| 106 | + bool isStandardId() const { |
| 107 | + return ((id & CAN_EFF_FLAG) == 0); |
| 108 | + } |
| 109 | + bool isExtendedId() const { |
| 110 | + return ((id & CAN_EFF_FLAG) == CAN_EFF_FLAG); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /* |
| 115 | + * CAN ID semantics (mirroring definition by linux/can.h): |
| 116 | + * |- Bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) |
| 117 | + * |- Bit 30 : reserved (future remote transmission request flag) |
| 118 | + * |- Bit 29 : reserved (future error frame flag) |
| 119 | + * |- Bit 0-28 : CAN identifier (11/29 bit) |
| 120 | + */ |
| 121 | + uint32_t id; |
| 122 | + uint8_t data_length; |
| 123 | + uint8_t data[MAX_DATA_LENGTH]; |
| 124 | +}; |
| 125 | + |
| 126 | +/************************************************************************************** |
| 127 | + * FREE FUNCTIONS |
| 128 | + **************************************************************************************/ |
| 129 | + |
| 130 | +inline uint32_t CanStandardId(uint32_t const id) { |
| 131 | + return (id & CanMsg::CAN_SFF_MASK); |
| 132 | +} |
| 133 | + |
| 134 | +inline uint32_t CanExtendedId(uint32_t const id) { |
| 135 | + return (CanMsg::CAN_EFF_FLAG | (id & CanMsg::CAN_EFF_MASK)); |
| 136 | +} |
| 137 | + |
| 138 | +/************************************************************************************** |
| 139 | + * NAMESPACE |
| 140 | + **************************************************************************************/ |
| 141 | + |
| 142 | +} /* arduino */ |
| 143 | + |
| 144 | +#endif /* ARDUINOCORE_API_CAN_MSG_H_ */ |
0 commit comments