Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SimpleNodeInfo.cxx
1
36
37#include "openmrn_features.h"
39
40namespace openlcb
41{
42
43extern const SimpleNodeStaticValues __attribute__((weak)) SNIP_STATIC_DATA = {
44 4, "OpenMRN", "Undefined model", "Undefined HW version", "0.9"};
45
46void init_snip_user_file(int fd, const char *user_name,
47 const char *user_description)
48{
49 ::lseek(fd, 0, SEEK_SET);
51 memset(&data, 0, sizeof(data));
52 data.version = 2;
53 str_populate(data.user_name, user_name);
54 str_populate(data.user_description, user_description);
55 int ofs = 0;
56 auto *p = (const uint8_t *)&data;
57 const int len = sizeof(data);
58 while (ofs < len)
59 {
60 int ret = ::write(fd, p, len - ofs);
61 if (ret < 0)
62 {
63 LOG(FATAL, "Init SNIP file: Could not write to fd %d: %s", fd,
64 strerror(errno));
65 }
66 ofs += ret;
67 }
68}
69
70static size_t find_string_at(const openlcb::Payload& payload, size_t start_pos, string* output) {
71 if (start_pos >= payload.size()) {
72 output->clear();
73 return start_pos;
74 }
75 size_t epos = payload.find('\0', start_pos);
76 output->assign(payload.substr(start_pos, epos - start_pos));
77 if (epos == string::npos) {
78 return epos;
79 } else {
80 return epos + 1;
81 }
82}
83
85 const openlcb::Payload &payload, SnipDecodedData *output)
86{
87 output->clear();
88 if (payload.empty())
89 {
90 return;
91 }
92 char sys_ver = payload[0];
93 size_t pos = 1;
94 pos = find_string_at(payload, pos, &output->manufacturer_name);
95 pos = find_string_at(payload, pos, &output->model_name);
96 pos = find_string_at(payload, pos, &output->hardware_version);
97 pos = find_string_at(payload, pos, &output->software_version);
98 // Future-proof with the version handling.
99 for (int i = 4; i < sys_ver; ++i)
100 {
101 string discard;
102 pos = find_string_at(payload, pos, &discard);
103 }
104 if (pos == string::npos)
105 {
106 return;
107 }
108 // char usr_ver = payload[pos];
109 pos++;
110 pos = find_string_at(payload, pos, &output->user_name);
111 pos = find_string_at(payload, pos, &output->user_description);
112}
113
114} // namespace nrmanet
void str_populate(char(&dst)[N], const char *src)
Populates a character array with a C string.
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int FATAL
Loglevel that kills the current process.
Definition logging.h:51
const SimpleNodeStaticValues SNIP_STATIC_DATA
This static data will be exported as the first block of SNIP.
void decode_snip_response(const openlcb::Payload &payload, SnipDecodedData *output)
Takes an NMRANet SNIP repsonse message paylaod and splits it up into individual fields of the respons...
void init_snip_user_file(int fd, const char *user_name, const char *user_description)
Helper function for test nodes.
string Payload
Container that carries the data bytes in an NMRAnet message.
Structure representing the layout of the memory space for Simple Node Identification user-editable da...
Structure representing the layout of the memory space for Simple Node Identification manufacturer-spe...
Holds the data we decoded from a SNIP response.
void clear()
Resets all entries to empty.
string hardware_version
SNIP response field.
string manufacturer_name
SNIP response field.
string user_description
SNIP response field.
string software_version
SNIP response field.
string user_name
SNIP response field.
string model_name
SNIP response field.