MeshProtocolSimulator
1.0.0
|
8 #include "rapidjson/filereadstream.h"
9 #include "rapidjson/schema.h"
10 #include "rapidjson/error/en.h"
11 #include "JSONScenarioSchemaHelper.h"
16 bool auto_pause =
false;
17 std::chrono::seconds simulation_limit;
18 std::vector<NodeConnections> node_connections;
19 std::vector<NodeSettings> node_settings;
40 inline std::string getVersion() {
48 bool parse(std::string path);
50 inline rapidjson::ParseResult getLastParseResult() {
51 return lastParseResult;
54 inline rapidjson::ParseResult getLastSchemaParseResult() {
55 return lastSchemaParseResult;
58 static bool pathExists(
const char* path);
70 bool verified =
false;
74 rapidjson::ParseResult lastParseResult;
75 rapidjson::ParseResult lastSchemaParseResult;
77 bool ParseJSON(std::string filePath);
79 struct JSONReaderHandler :
public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JSONReaderHandler> {
83 reader->verified =
false;
84 reader->settings.
reset();
91 if (lastKeys.size() == 2 && lastKeys[1] ==
"settings") {
92 if (currentKey ==
"auto_pause") {
93 reader->settings.auto_pause = b;
96 else if (lastKeys.size() == 4 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_settings" && currentKey ==
"is_gateway") {
97 currentNodeSettings.isGateway = b;
103 return AnyNumber(i, i,
false);
105 bool Uint(
unsigned u) {
106 return AnyNumber(u, u,
true);
108 bool Int64(int64_t i) {
109 return AnyNumber(i, i,
false);
111 bool Uint64(uint64_t u) {
112 return AnyNumber(u, u,
true);
115 bool Double(
double d) {
116 if (lastKeys.size() == 4 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_connections") {
117 if (currentKey ==
"RSSI") {
118 if (d >= -120.00f && d <= 0.00f) {
119 currentNodeConnection.
RSSI = d;
122 else if (currentKey ==
"RSSI_tolerance") {
123 if (d >= 0.00f && d <= 120.00f) {
127 else if (currentKey ==
"quality") {
128 if (d >= 0.00f && d <= 1.00f) {
129 currentNodeConnection.
Quality = d;
137 std::string value = std::string(str, length);
138 if (lastKeys.size() == 1) {
139 if (currentKey ==
"type") {
140 if (value ==
"Simulation scenario") {
141 reader->verified =
true;
145 else if (currentKey ==
"version") {
149 else if (lastKeys.size() == 4 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_settings" && currentKey ==
"SSID") {
150 currentNodeSettings.SSID = std::string(str, length);
157 lastKeys.push_back(currentKey);
158 if (lastKeys.size() == 4) {
160 currentNodeSettings.
clear();
165 currentKey = std::string(str, length);
170 if (lastKeys.size() == 3 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_connections") {
171 reader->settings.node_connections.push_back(currentNodeConnection);
173 else if (lastKeys.size() == 3 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_settings" && currentNodeSettings.MACAddress != 0) {
174 reader->settings.node_settings.push_back(currentNodeSettings);
180 lastKeys.push_back(currentKey);
189 bool AnyNumber(uint64_t valu, int64_t val,
bool isUnsinged) {
190 if (lastKeys.size() == 2 && lastKeys[1] ==
"settings") {
191 if (currentKey ==
"simulation_limit") {
192 reader->settings.simulation_limit = std::chrono::seconds(valu);
195 else if (lastKeys.size() == 4 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_connections") {
196 if (currentKey ==
"node1") {
197 if (valu > 0 && valu <= UINT32_MAX) {
198 currentNodeConnection.
Node1 = valu;
201 else if (currentKey ==
"node2") {
202 if (valu > 0 && valu <= UINT32_MAX) {
203 currentNodeConnection.
Node2 = valu;
207 else if (lastKeys.size() == 4 && lastKeys[1] ==
"settings" && lastKeys[2] ==
"node_settings") {
208 if (currentKey ==
"node") {
209 currentNodeSettings.MACAddress = (uint32_t)valu;
211 else if (currentKey ==
"MCU_frequency") {
214 else if (currentKey ==
"pre_send_cycles_delay") {
217 else if (currentKey ==
"pre_send_delay") {
218 currentNodeSettings.
preSendDelay = std::chrono::nanoseconds(valu);
220 else if (currentKey ==
"sending_speed") {
223 else if (currentKey ==
"extra_bits_sent_count") {
226 else if (currentKey ==
"post_send_cycles_delay") {
229 else if (currentKey ==
"post_send_delay") {
230 currentNodeSettings.
postSendDelay = std::chrono::nanoseconds(valu);
232 else if (currentKey ==
"pre_receive_cycles_delay") {
235 else if (currentKey ==
"pre_receive_delay") {
238 else if (currentKey ==
"post_receive_cycles_delay") {
241 else if (currentKey ==
"post_receive_delay") {
244 else if (currentKey ==
"available_cycles_delay") {
247 else if (currentKey ==
"available_delay") {
248 currentNodeSettings.
availableDelay = std::chrono::nanoseconds(valu);
250 else if (currentKey ==
"interference_check_cycles_delay") {
253 else if (currentKey ==
"interference_check_delay") {
256 else if (currentKey ==
"max_frame_size") {
257 if (valu > 255) valu = 255;
260 else if (currentKey ==
"input_fifo_size") {
263 else if (currentKey ==
"start_delay") {
271 std::string currentKey;
272 std::vector<std::string> lastKeys;
315 void end(
int errorCode, std::string errorText);
324 void exceptionEnd(
int errorCode, std::string errorText);
362 void logReceiveInterference(MeshMAC node, std::chrono::nanoseconds simulationTime, MeshMAC sender_node1, uint64_t sender_node1_UID,
float rssi1, MeshMAC sender_node2, uint64_t sender_node2_UID,
float rssi2);
374 void logTransmitInterference(MeshMAC node, uint64_t node_UID, std::chrono::nanoseconds simulationTime, MeshMAC sender_node, uint64_t sender_node_UID,
float RSSI);
385 void logMessageLost(MeshMAC sender, MeshMAC receiver, uint64_t node_UID, std::chrono::nanoseconds simulationTime,
float RSSI);
396 void logAPLReceived(MeshMAC node, MeshMAC sender_node, std::chrono::nanoseconds simulationTime,
char* messageData,
size_t length);
408 void logTableChange(MeshMAC node, std::string& descriptor, TableChangeType changeType, std::chrono::nanoseconds simulationTime,
const char* data,
size_t length);
437 bool beginCalled =
false;
438 std::chrono::steady_clock::time_point simulationStartTime;
439 std::ofstream outFile;
440 bool anyLogWritten =
false;
void validateAndSort()
Removes extra settings or duplicate settings and sorts them by MAC address. If settings for some node...
Definition: JSON_helper.cpp:11
std::string version
Emulator version.
Definition: JSON_helper.h:68
void logAPLReceived(MeshMAC node, MeshMAC sender_node, std::chrono::nanoseconds simulationTime, char *messageData, size_t length)
Logs application layer received message to output log file.
Definition: JSON_helper.cpp:413
Settings for each node in simulation. Those settings can be get from scenario file.
Definition: Emulator_helpers.h:124
MeshMAC Node2
Second node.
Definition: Emulator_helpers.h:71
uint32_t MCUFrequency
Simulation MCU frequency in Hz. Defaultly set to 16 MHz.
Definition: Emulator_helpers.h:148
static std::string EncodeSpecialCharacters(const char *text, size_t inLength)
Encodes special characters in json string value. Escaped characters are '', '\f', ' ',...
Definition: JSON_helper.cpp:462
void logTableChange(MeshMAC node, std::string &descriptor, TableChangeType changeType, std::chrono::nanoseconds simulationTime, const char *data, size_t length)
Logs application layer received message to output log file.
Definition: JSON_helper.cpp:433
std::chrono::nanoseconds preReceiveDelay
Delay before message is received in nanoseconds. Defaultly set to 5µs.
Definition: Emulator_helpers.h:188
std::chrono::nanoseconds availableDelay
Delay when checking message avaiability in nanoseconds. Defaultly set to 5µs.
Definition: Emulator_helpers.h:208
static std::string EncodeSpecialCharacters(std::string &text)
Encodes special characters in json string value. Escaped characters are '', '\f', ' ',...
Definition: JSON_helper.h:425
void terminate()
Terminates logging and closes file. Output file "status" property will be set to "terminated".
Definition: JSON_helper.cpp:232
uint32_t sendingSpeed
Speed of send in bits per second. Defaultly set to 1 Mbps.
Definition: Emulator_helpers.h:163
void clear()
Clears settings.
Definition: Emulator_helpers.h:233
common definitions and configuration
Definition: JSON_helper.h:15
This file contains helper classes or structured for Emulator class.
uint32_t preSendCyclesDelay
Delay before message sending in cycles. Defaultly set to 50.
Definition: Emulator_helpers.h:153
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:380
int InputFiFoSize
Size of input FiFo on specified radio. Defaultly set to 3.
Definition: Emulator_helpers.h:228
void logMessageReceive(MeshMAC node, std::chrono::nanoseconds receiveTime, RawMessage &message, float RSSI)
Logs message receiving to output log file.
Definition: JSON_helper.cpp:330
void logMessageLost(MeshMAC sender, MeshMAC receiver, uint64_t node_UID, std::chrono::nanoseconds simulationTime, float RSSI)
Logs lost message due to bad link quality to output log file.
Definition: JSON_helper.cpp:392
uint32_t postReceiveCyclesDelay
Delay after message is received in cycles. Defaultly set to 10.
Definition: Emulator_helpers.h:193
uint32_t postSendCyclesDelay
Delay after message sending in cycles. Defaultly set to 50.
Definition: Emulator_helpers.h:173
uint32_t availableCyclesDelay
Delay when checking message avaiability in cycles. Defaultly set to 10.
Definition: Emulator_helpers.h:203
float RSSI
RSSI of current link connection in dBm. This value has to be negative and in range from 0dBm to -120d...
Definition: Emulator_helpers.h:76
uint32_t preReceiveCyclesDelay
Delay before message is received in cycles. Defaultly set to 10.
Definition: Emulator_helpers.h:183
void logMessageTransmit(RawMessage &message)
Logs message transmission to output log file.
Definition: JSON_helper.cpp:310
Definition: Simulation.h:220
float RSSI_tolerance
Absolute value, that can be added or subtracted from RSSI during simulation. When set to 0,...
Definition: Emulator_helpers.h:83
std::chrono::nanoseconds postReceiveDelay
Delay after message is received in nanoseconds. Defaultly set to 5µs.
Definition: Emulator_helpers.h:198
This structure represents raw message, that was sent between two virtual radios. It has no header and...
Definition: Emulator_helpers.h:262
std::string version
Emulator version.
Definition: JSON_helper.h:291
void logTransmitInterference(MeshMAC node, uint64_t node_UID, std::chrono::nanoseconds simulationTime, MeshMAC sender_node, uint64_t sender_node_UID, float RSSI)
Logs transmit interference (one node is sending message and another wants to send him message) to out...
Definition: JSON_helper.cpp:371
void end()
Ends logging and closes file. Output file "status" property will be set to "success".
Definition: JSON_helper.cpp:295
static std::string GetCurrentTimeISO8601()
Gets current pc time and converts it to ISO 8601.
Definition: JSON_helper.cpp:504
Definition: JSON_helper.h:79
std::chrono::nanoseconds simulationOffset
Simulation start offset.
Definition: Emulator_helpers.h:143
void logReceiveInterference(MeshMAC node, std::chrono::nanoseconds simulationTime, MeshMAC sender_node1, uint64_t sender_node1_UID, float rssi1, MeshMAC sender_node2, uint64_t sender_node2_UID, float rssi2)
Logs receive interference (two nodes sends simultaneously and message cannot be received by "node") t...
Definition: JSON_helper.cpp:350
uint8_t maxFrameSize
Maximum size of frame, that can be sent. Value can be from 24 to 255.
Definition: Emulator_helpers.h:223
MeshMAC Node1
First node.
Definition: Emulator_helpers.h:66
uint32_t interferenceCheckCyclesDelay
Delay when checking interference in cycles. Defaultly set to 10.
Definition: Emulator_helpers.h:213
Definition: JSON_helper.h:37
Definition: JSON_helper.h:280
This structure represents link connection between two nodes. Nodes are indentified by it's MeshMAC ad...
Definition: Emulator_helpers.h:47
std::chrono::nanoseconds preSendDelay
Delay before message sending in nanoseconds. Defaultly set to 13µs.
Definition: Emulator_helpers.h:158
bool begin(const JSONSettings &settings, std::string path)
Begins simulation logging.
Definition: JSON_helper.cpp:146
float Quality
Link quality, represented as probability, that sent packet through this link will be received....
Definition: Emulator_helpers.h:94
std::chrono::nanoseconds postSendDelay
Delay after message sending in nanoseconds. Defaultly set to 13µs.
Definition: Emulator_helpers.h:178
void reset()
Completelly clears settings to it's default state.
Definition: JSON_helper.cpp:4
std::chrono::nanoseconds interferenceCheckDelay
Delay when checking interference in nanoseconds. Defaultly set to 5µs.
Definition: Emulator_helpers.h:218
void exceptionEnd(int errorCode, std::string errorText)
Ends logging and closes file. Output file "status" property will be set to "success" or "exception" d...
Definition: JSON_helper.cpp:271
uint32_t extraBitsSentCount
Count of extra bits sent with message (preamble, CRC, ...)
Definition: Emulator_helpers.h:168