Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
If.cxx
Go to the documentation of this file.
1
35#include "openlcb/If.hxx"
36#include "openlcb/Convert.hxx"
37
41
42namespace openlcb
43{
44
46{
47 id = htobe64(id);
48 const char *src = reinterpret_cast<const char *>(&id);
49 return string(src + 2, 6);
50}
51
52void node_id_to_data(NodeID id, void* buf)
53{
54 id = htobe64(id);
55 const char *src = reinterpret_cast<const char *>(&id);
56 memcpy(buf, src + 2, 6);
57}
58
59NodeID data_to_node_id(const void* buf)
60{
61 uint64_t d = 0;
62 memcpy(reinterpret_cast<uint8_t *>(&d) + 2, buf, 6);
63 return be64toh(d);
64}
65
66NodeID buffer_to_node_id(const string &buf)
67{
68 HASSERT(buf.size() == 6);
69 return data_to_node_id(buf.data());
70}
71
72Payload eventid_to_buffer(uint64_t eventid)
73{
74 eventid = htobe64(eventid);
75 return string(reinterpret_cast<char*>(&eventid), 8);
76}
77
78void error_to_data(uint16_t error_code, void* data) {
79 uint8_t* p = (uint8_t*) data;
80 p[0] = error_code >> 8;
81 p[1] = error_code & 0xff;
82}
83
84uint16_t data_to_error(const void *data)
85{
86 const uint8_t *p = (const uint8_t *)data;
87 return (((uint16_t)p[0]) << 8) | p[1];
88}
89
90string error_to_buffer(uint16_t error_code, uint16_t mti)
91{
92 string ret(4, '\0');
93 error_to_data(error_code, &ret[0]);
94 ret[2] = mti >> 8;
95 ret[3] = mti & 0xff;
96 return ret;
97}
98
99string error_to_buffer(uint16_t error_code)
100{
101 string ret(2, '\0');
102 error_to_data(error_code, &ret[0]);
103 return ret;
104}
105
106void append_error_to_buffer(uint16_t error_code, Payload* p) {
107 p->push_back(error_code >> 8);
108 p->push_back(error_code & 0xff);
109}
110
111
112void buffer_to_error(const Payload &payload, uint16_t *error_code,
113 uint16_t *mti, string *error_message)
114{
115 if (mti)
116 *mti = 0;
117 if (error_code)
118 *error_code = Defs::ERROR_PERMANENT;
119 if (error_message)
120 error_message->clear();
121 if (payload.size() >= 2 && error_code)
122 {
123 *error_code = (((uint16_t)payload[0]) << 8) | (uint8_t)payload[1];
124 }
125 if (payload.size() >= 4 && mti)
126 {
127 *mti = (((uint16_t)payload[2]) << 8) | (uint8_t)payload[3];
128 }
129 if (payload.size() > 4 && error_message)
130 {
131 error_message->assign(&payload[4], payload.size() - 4);
132 }
133}
134
135Payload error_payload(uint16_t error_code, Defs::MTI incoming_mti)
136{
137 Payload p(4, 0);
138 error_to_data(error_code, &p[0]);
139 error_to_data(incoming_mti, &p[2]);
140 return p;
141}
142
143void send_event(Node* src_node, uint64_t event_id)
144{
145 auto *b = src_node->iface()->global_message_write_flow()->alloc();
146 b->data()->reset(Defs::MTI_EVENT_REPORT, src_node->node_id(),
147 eventid_to_buffer(event_id));
148 src_node->iface()->global_message_write_flow()->send(b);
149}
150
151
153
154/*Buffer *node_id_to_buffer(NodeID id)
155{
156 Buffer *ret = buffer_alloc(6);
157 id = htobe64(id);
158 uint8_t *src = reinterpret_cast<uint8_t *>(&id);
159 memcpy(ret->start(), src + 2, 6);
160 ret->advance(6);
161 return ret;
162}
163
164NodeID buffer_to_node_id(Buffer *buf)
165{
166 HASSERT(buf);
167 HASSERT(buf->used() == 6);
168 uint64_t d = 0;
169 memcpy(reinterpret_cast<uint8_t *>(&d) + 2, buf->start(), 6);
170 return be64toh(d);
171 }*/
172
174If::If(ExecutorBase *executor, int local_nodes_count)
175 : Service(executor)
176 , globalWriteFlow_(nullptr)
177 , addressedWriteFlow_(nullptr)
178 , dispatcher_(this)
179 , localNodes_(local_nodes_count)
180{
181}
182
183} // namespace openlcb
const unsigned LARGEST_BUFFERPOOL_BUCKET
Ensures that the largest bucket in the main buffer pool is exactly the size of a GenMessage.
Definition If.cxx:40
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
This class implements an execution of tasks pulled off an input queue.
Definition Executor.hxx:64
virtual void send(MessageType *message, unsigned priority=UINT_MAX)=0
Entry point to the flow.
MessageType * alloc()
Synchronously allocates a message buffer from the pool of this flow.
Collection of related state machines that pend on incoming messages.
MessageHandler * global_message_write_flow()
Definition If.hxx:200
If(ExecutorBase *executor, int local_nodes_count)
Constructs an NMRAnet interface.
Definition If.cxx:174
Base class for NMRAnet nodes conforming to the asynchronous interface.
Definition Node.hxx:52
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
string EMPTY_PAYLOAD
A global class / variable for empty or not-yet-initialized payloads.
Definition If.cxx:152
void node_id_to_data(NodeID id, void *data)
Convenience function to render a 48-bit NMRAnet node ID into an existing buffer.
Definition If.cxx:52
string node_id_to_buffer(NodeID id)
Convenience function to render a 48-bit NMRAnet node ID into a new buffer.
Definition If.cxx:45
NodeID data_to_node_id(const void *d)
Converts 6 bytes of big-endian data to a node ID.
Definition If.cxx:59
uint64_t NodeID
48-bit NMRAnet Node ID type
void send_event(Node *src_node, uint64_t event_id)
Helper function to send an event report to the bus.
Definition If.cxx:143
uint16_t data_to_error(const void *data)
Parses an error code from a payload object at a given pointer.
Definition If.cxx:84
void error_to_data(uint16_t error_code, void *data)
Writes an error code into a payload object at a given pointer.
Definition If.cxx:78
NodeID buffer_to_node_id(const string &buf)
Converts a 6-byte-long buffer to a node ID.
Definition If.cxx:66
void append_error_to_buffer(uint16_t error_code, Payload *p)
Appends an error to the end of an existing buffer.
Definition If.cxx:106
string Payload
Container that carries the data bytes in an NMRAnet message.
Payload eventid_to_buffer(uint64_t eventid)
Converts an Event ID to a Payload suitable to be sent as an event report.
Definition If.cxx:72
void buffer_to_error(const Payload &payload, uint16_t *error_code, uint16_t *mti, string *error_message)
Parses the payload of an Optional Interaction Rejected or Terminate Due To Error message.
Definition If.cxx:112
Payload error_payload(uint16_t error_code, Defs::MTI incoming_mti)
Generates the payload for an OIR or TDE message.
Definition If.cxx:135
string error_to_buffer(uint16_t error_code, uint16_t mti)
Formats a payload for response of error response messages such as OPtioanl Interaction Rejected or Te...
Definition If.cxx:90
MTI
Known Message type indicators.