|
| 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 <cstdlib> |
| 16 | +#include <cstdint> |
| 17 | +#include <cstring> |
| 18 | + |
| 19 | +#include <Arduino.h> |
| 20 | + |
| 21 | +/************************************************************************************** |
| 22 | + * NAMESPACE |
| 23 | + **************************************************************************************/ |
| 24 | + |
| 25 | +namespace arduino |
| 26 | +{ |
| 27 | + |
| 28 | +/************************************************************************************** |
| 29 | + * CLASS DECLARATION |
| 30 | + **************************************************************************************/ |
| 31 | + |
| 32 | +class CanMsg : public Printable |
| 33 | +{ |
| 34 | +public: |
| 35 | + static size_t constexpr MAX_DATA_LENGTH = 8; |
| 36 | + |
| 37 | + CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr) |
| 38 | + : id{can_id} |
| 39 | + , data_length{can_data_len} |
| 40 | + , data{0} |
| 41 | + { |
| 42 | + memcpy(data, can_data_ptr, min(can_data_len, MAX_DATA_LENGTH)); |
| 43 | + } |
| 44 | + |
| 45 | + CanMsg() : CanMsg(0, 0, nullptr) { } |
| 46 | + |
| 47 | + CanMsg(CanMsg const & other) |
| 48 | + { |
| 49 | + this->id = other.id; |
| 50 | + this->data_length = other.data_length; |
| 51 | + memcpy(this->data, other.data, this->data_length); |
| 52 | + } |
| 53 | + |
| 54 | + virtual ~CanMsg() { } |
| 55 | + |
| 56 | + void operator = (CanMsg const & other) |
| 57 | + { |
| 58 | + if (this == &other) |
| 59 | + return; |
| 60 | + |
| 61 | + this->id = other.id; |
| 62 | + this->data_length = other.data_length; |
| 63 | + memcpy(this->data, other.data, this->data_length); |
| 64 | + } |
| 65 | + |
| 66 | + virtual size_t printTo(Print & p) const override |
| 67 | + { |
| 68 | + char buf[20] = {0}; |
| 69 | + size_t len = 0; |
| 70 | + |
| 71 | + /* Print the header. */ |
| 72 | + len = snprintf(buf, sizeof(buf), "[%08X] (%d) : ", id, data_length); |
| 73 | + size_t n = p.write(buf, len); |
| 74 | + |
| 75 | + /* Print the data. */ |
| 76 | + for (size_t d = 0; d < data_length; d++) |
| 77 | + { |
| 78 | + len = snprintf(buf, sizeof(buf), "%02X", data[d]); |
| 79 | + n += p.write(buf, len); |
| 80 | + } |
| 81 | + |
| 82 | + /* Wrap up. */ |
| 83 | + return n; |
| 84 | + } |
| 85 | + |
| 86 | + uint32_t id; |
| 87 | + uint8_t data_length; |
| 88 | + uint8_t data[MAX_DATA_LENGTH]; |
| 89 | +}; |
| 90 | + |
| 91 | +/************************************************************************************** |
| 92 | + * NAMESPACE |
| 93 | + **************************************************************************************/ |
| 94 | + |
| 95 | +} /* arduino */ |
| 96 | + |
| 97 | +#endif /* ARDUINOCORE_API_CAN_MSG_H_ */ |
0 commit comments