ReduxLib C++ 2025.0.0-beta2
Loading...
Searching...
No Matches
CanandMessage.h
1// Copyright (c) Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the 3-clause BSD license.
3
4#pragma once
5#include <stdint.h>
6#include <units/time.h>
7#include <array>
8#include "CANBus.h"
9#include "CanandUtils.h"
10namespace redux::canand {
11
12/**
13 *
14 * Class that represents a CAN message received from the Redux CanandEventLoop
15 *
16 * This class is generally initialized by the event loop from a raw struct packet from the driver
17 * that gets read out and parsed. From there, it is then passed into CanandDevice::HandleMessage if it matches the address.
18 *
19 * <p>
20 * Of particular note are GetData() to get a handle to the packet's data and GetApiIndex() to see
21 * what type of packet it is.
22 * </p>
23 *
24 *
25 */
27 public:
28
29 /**
30 * Construct a new CanandMessage from a bunch of parts -- not intended to be instantiated directly
31 * @param busDescriptor bus descriptor value
32 * @param id can arbitration message id
33 * @param timestamp CAN message timestamp (milliseconds)
34 * @param dataLen data size (1-8)
35 * @param dataBuf pointer to data buffer
36 */
37 CanandMessage(uint8_t busDescriptor, uint32_t id, uint64_t timestamp, uint8_t dataLen, uint8_t* dataBuf) : \
38 id{id}, timestamp{timestamp}, bus{busDescriptor} {
39 dataSize = (dataLen > 8) ? 8 : dataLen;
40 memcpy(data, dataBuf, 8);
41 };
42 virtual ~CanandMessage() = default;
43
44
45 /**
46 * Gets the full 29-bit CAN message id.
47 *
48 * A summary of how the CAN message id works is described in {@link CanandAddress}.
49 * @return The full 29-bit message id.
50 */
51 inline uint32_t GetId() { return id; }
52
53 /**
54 * Gets the 8-bit CAN API index.
55 *
56 * This is the value that generally describes what type of CAN message was sent.
57 * @return the CAN API index.
58 */
59 inline uint8_t GetApiIndex() { return utils::getApiIndex(id); }
60
61 /**
62 * Gets the 6-bit CAN Device id.
63 *
64 * This is the user-adjustible "CAN Id" of the associated CAN device in question.
65 * @return the device id.
66 */
67 inline uint8_t GetDeviceId() { return utils::getDeviceId(id); }
68
69 /**
70 * Gets the 2-bit API page.
71 *
72 * API page distinguishes between different API index banks.
73 * @return the product id.
74 */
75 inline uint8_t GetApiPage() { return utils::getApiPage(id); }
76
77 /**
78 * Gets the 5-bit device type code
79 *
80 * Product ID/ device type combinations will be unique to a Redux product.
81 * @return the device type code.
82 */
83 inline uint8_t GetDeviceType() { return utils::getDeviceType(id); }
84
85 /**
86 * Gets the CAN message payload (up to 8 bytes).
87 *
88 * The length of the array is determined by how many bytes were in the original CAN packet.
89 * @return pointer to bytes that is 1-8 bytes long.
90 */
91 inline uint8_t* GetData() { return data; }
92
93 /**
94 * Gets the length of the CAN message's data in bytes.
95 * @return length (1-8)
96 */
97 inline uint8_t GetLength() { return dataSize; }
98
99 /**
100 * Gets the CAN message timestamp, in seconds.
101 * The time base is relative to the FPGA timestamp.
102 * @return timestamp in seconds.
103 */
104 inline units::second_t GetTimestamp() { return units::microsecond_t{static_cast<double>(timestamp)}; }
105
106 /**
107 * Gets an object representing the CAN bus that received the message
108 * @return CANBus object
109 */
110 inline CANBus GetBus() { return bus; }
111
112 private:
113 uint32_t id;
114 uint64_t timestamp;
115 uint8_t dataSize;
116 uint8_t data[8];
117 CANBus bus;
118};
119}
Definition: CANBus.h:14
Definition: CanandMessage.h:26
uint8_t GetApiPage()
Definition: CanandMessage.h:75
CanandMessage(uint8_t busDescriptor, uint32_t id, uint64_t timestamp, uint8_t dataLen, uint8_t *dataBuf)
Definition: CanandMessage.h:37
CANBus GetBus()
Definition: CanandMessage.h:110
uint32_t GetId()
Definition: CanandMessage.h:51
units::second_t GetTimestamp()
Definition: CanandMessage.h:104
uint8_t GetApiIndex()
Definition: CanandMessage.h:59
uint8_t * GetData()
Definition: CanandMessage.h:91
uint8_t GetDeviceId()
Definition: CanandMessage.h:67
uint8_t GetLength()
Definition: CanandMessage.h:97
uint8_t GetDeviceType()
Definition: CanandMessage.h:83
constexpr uint8_t getDeviceType(uint32_t fullId)
Definition: CanandUtils.h:24
constexpr uint8_t getDeviceId(uint32_t fullId)
Definition: CanandUtils.h:57
constexpr uint8_t getApiIndex(uint32_t fullId)
Definition: CanandUtils.h:46
constexpr uint8_t getApiPage(uint32_t fullId)
Definition: CanandUtils.h:35
Definition: CanandMessage.h:10