blob: c50b5c09200cbd538a164bb5ed8e95f25e9dbe24 [file] [log] [blame]
Austin Schuh82ea7382023-07-14 15:17:34 -07001#ifndef AOS_EVENTS_CONTEXT_H_
2#define AOS_EVENTS_CONTEXT_H_
3
4#include "aos/flatbuffers.h"
5#include "aos/time/time.h"
6#include "aos/uuid.h"
7
8namespace aos {
9
10// Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context
11// about the current message.
12struct Context {
13 // Time that the message was sent on this node, or the timer was triggered.
14 monotonic_clock::time_point monotonic_event_time;
15 // Realtime the message was sent on this node. This is set to min_time for
16 // Timers and PhasedLoops.
17 realtime_clock::time_point realtime_event_time;
18
19 // The rest are only valid for Watchers and Fetchers.
20
21 // For a single-node configuration, these two are identical to *_event_time.
22 // In a multinode configuration, these are the times that the message was
23 // sent on the original node.
24 monotonic_clock::time_point monotonic_remote_time;
25 realtime_clock::time_point realtime_remote_time;
26
Austin Schuhac6d89e2024-03-27 14:56:09 -070027 // Time that the message was published over the network on the remote node.
28 monotonic_clock::time_point monotonic_remote_transmit_time;
29
Austin Schuh82ea7382023-07-14 15:17:34 -070030 // Index in the queue.
31 uint32_t queue_index;
32 // Index into the remote queue. Useful to determine if data was lost. In a
33 // single-node configuration, this will match queue_index.
34 uint32_t remote_queue_index;
35
36 // Size of the data sent.
37 size_t size;
38 // Pointer to the data.
39 const void *data;
40
41 // Index of the message buffer. This will be in [0, NumberBuffers) on
42 // read_method=PIN channels, and -1 for other channels.
43 //
44 // This only tells you about the underlying storage for this message, not
45 // anything about its position in the queue. This is only useful for advanced
46 // zero-copy use cases, on read_method=PIN channels.
47 //
48 // This will uniquely identify a message on this channel at a point in time.
49 // For senders, this point in time is while the sender has the message. With
50 // read_method==PIN, this point in time includes while the caller has access
51 // to this context. For other read_methods, this point in time may be before
52 // the caller has access to this context, which makes this pretty useless.
53 int buffer_index;
54
55 // UUID of the remote node which sent this message, or this node in the case
56 // of events which are local to this node.
57 UUID source_boot_uuid = UUID::Zero();
58
59 // Efficiently copies the flatbuffer into a FlatbufferVector, allocating
60 // memory in the process. It is vital that T matches the type of the
61 // underlying flatbuffer.
62 template <typename T>
63 FlatbufferVector<T> CopyFlatBuffer() const {
64 ResizeableBuffer buffer;
65 buffer.resize(size);
66 memcpy(buffer.data(), data, size);
67 return FlatbufferVector<T>(std::move(buffer));
68 }
69};
70
71} // namespace aos
72
73#endif // AOS_EVENTS_CONTEXT_H_