forked from attermann/microReticulum_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPages.h
More file actions
286 lines (265 loc) · 13.2 KB
/
Copy pathPages.h
File metadata and controls
286 lines (265 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (C) 2026, Chad Attermann
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <Arduino.h>
#include "Config.h"
#include <microReticulum/Interface.h>
#include <microReticulum/Identity.h>
#include <microReticulum/Utilities/OS.h>
#include <microReticulum/Bytes.h>
// CBA NOTE Thge following <MsgPack.h> include *MUST* precede the "Utilities.h" include
#include <MsgPack.h>
#include <string>
extern RNS::Interface lora_interface;
#if HAS_WIFI && defined(UDP_TRANSPORT)
extern RNS::Interface udp_interface;
extern IPAddress wr_device_ip;
extern uint16_t udp_port;
extern uint8_t wifi_mode;
extern char wr_ssid[];
#endif
void add_interface_details(RNS::Bytes& content, const RNS::Interface& interface) {
content << " \"mode\": \"";
switch (interface.mode()) {
case RNS::Type::Interface::MODE_ACCESS_POINT:
content << "ACCESS_POINT";
break;
case RNS::Type::Interface::MODE_BOUNDARY:
content << "BOUNDARY";
break;
case RNS::Type::Interface::MODE_FULL:
content << "FULL";
break;
case RNS::Type::Interface::MODE_GATEWAY:
content << "GATEWAY";
break;
case RNS::Type::Interface::MODE_POINT_TO_POINT:
content << "POINT_TO_POINT";
break;
case RNS::Type::Interface::MODE_ROAMING:
content << "ROAMING";
break;
case RNS::Type::Interface::MODE_NONE:
content << "NONE";
break;
default:
break;
}
content << "\",\n";
content << " \"bitrate\": " << std::to_string(interface.bitrate()) << ",\n";
content << " \"packets_sent\": " << std::to_string(interface.tx()) << ",\n";
content << " \"packets_received\": " << std::to_string(interface.rx()) << ",\n";
content << " \"bytes_sent\": " << std::to_string(interface.txbytes()) << ",\n";
content << " \"bytes_received\": " << std::to_string(interface.rxbytes()) << ",\n";
content << " \"queued_announces\": " << std::to_string(interface.announce_queue().size()) << ",\n";
}
// Request handler for NomadNet pages. Signature is fixed by
// RNS::RequestHandler::response_generator (see src/Destination.h).
//
// The Link::request contract is that the return value must already be
// msgpack-encoded bytes — Link::handle_request splices it verbatim into
// the response envelope (Link.cpp:994). Python RNS auto-encodes
// arbitrary return values; in C++ we encode manually here.
RNS::Bytes serve_page(
const RNS::Bytes& path,
const RNS::Bytes& data,
const RNS::Bytes& request_id,
const RNS::Bytes& link_id,
const RNS::Identity& remote_identity,
double requested_at
) {
std::string category;
{
MsgPack::Unpacker u;
u.feed(data.data(), data.size());
std::map<String, String> params;
if (u.isMap()) {
u.deserialize(params);
for (const auto& [key, value] : params) {
TRACEF("Param: key=%s, value=%s", key.c_str(), value.c_str());
}
}
auto iter = params.find("var_c");
if (iter != params.end()) {
category = (*iter).second.c_str();
}
}
NOTICEF("Serving %s with category \"%s\" to link <%s>", path.toString().c_str(), category.c_str(), link_id.toHex().c_str());
MsgPack::Packer packer;
{
RNS::Bytes content;
if (path == "/page/index.mu") {
content = "> microReticulum Stats\n";
content << ">> Memory\n";
content << "`!`[• Heap Memory`:/page/stack.mu`c=heap]`\n";
content << "`!`[• Memory Pools`:/page/stack.mu`c=pool]`\n";
content << "`!`[• Memory Allocators`:/page/stack.mu`c=alloc]`\n";
content << ">> Storage\n";
content << "`!`[• Flash Memory`:/page/stack.mu`c=flash]`\n";
content << "`!`[• Object Stores`:/page/stack.mu`c=store]`\n";
content << "`!`[• Transport Metrics`:/page/stack.mu`c=metrics]`\n";
content << ">> Device\n";
content << "`!`[• General`:/page/device.mu`c=general]`\n";
content << "`!`[• Interfaces`:/page/device.mu`c=interfaces]`\n";
}
else if (path == "/page/stack.mu") {
if (category == "heap") {
content = "{\n";
content << " \"heap_size\": " << std::to_string(RNS::Utilities::Memory::heap_size()) << ",\n";
content << " \"heap_free\": " << std::to_string(RNS::Utilities::Memory::heap_available()) << ",\n";
content << " \"heap_freepct\": " << std::to_string((unsigned)((double)RNS::Utilities::Memory::heap_available() / (double)RNS::Utilities::Memory::heap_size() * 100.0)) << ",\n";
#if defined(ESP32)
content << " \"heap_minfree\": " << std::to_string(ESP.getMinFreeHeap()) << ",\n";
content << " \"heap_maxalloc\": " << std::to_string(ESP.getMaxAllocHeap()) << ",\n";
content << " \"heap_fragmented\": " << std::to_string((unsigned)(100.0 - (double)ESP.getMaxAllocHeap() / (double)ESP.getFreeHeap() * 100.0)) << ",\n";
content << " \"psram_size\": " << std::to_string(ESP.getPsramSize()) << ",\n";
content << " \"psram_free\": " << std::to_string(ESP.getFreePsram()) << ",\n";
content << " \"psram_freepct\": " << std::to_string((ESP.getPsramSize() > 0) ? (unsigned)((double)ESP.getFreePsram() / (double)ESP.getPsramSize() * 100.0) : 0) << ",\n";
content << " \"psram_minfree\": " << std::to_string(ESP.getMinFreePsram()) << ",\n";
content << " \"psram_maxalloc\": " << std::to_string(ESP.getMaxAllocPsram()) << ",\n";
content << " \"psram_fragmented\": " << std::to_string((ESP.getFreePsram() > 0) ? (unsigned)(100.0 - (double)ESP.getMaxAllocPsram() / (double)ESP.getFreePsram() * 100.0) : 0) << ",\n";
#elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT)
//HEAD("Heap Stats", LOG_TRACE);
//if (loglevel() == LOG_TRACE) {
// dbgMemInfo();
//}
#endif
content << "}";
}
else if (category == "flash") {
size_t flash_size = RNS::Utilities::OS::storage_size();
size_t flash_free = RNS::Utilities::OS::storage_available();
uint8_t flash_freepct = 0;
if (flash_size > 0) flash_freepct = (uint8_t)((double)flash_free / (double)flash_size * 100.0);
//size_t fs_size = RNS::Utilities::OS::get_filesystem().storageSize();
//size_t fs_free = RNS::Utilities::OS::get_filesystem().storageAvailable();
//uint8_t fs_freepct = 0;
//if (fs_size > 0) fs_freepct = (uint8_t)((double)fs_free / (double)fs_size * 100.0);
content = "{\n";
content << " \"flash_size\": " << std::to_string(flash_size) << ",\n";
content << " \"flash_free\": " << std::to_string(flash_free) << ",\n";
content << " \"flash_freepct\": " << std::to_string(flash_freepct) << ",\n";
//content << " \"fs_size\": " << std::to_string(fs_size) << ",\n";
//content << " \"fs_free\": " << std::to_string(fs_free) << ",\n";
//content << " \"fs_freepct\": " << std::to_string(fs_freepct) << ",\n";
content << "}";
}
else if (category == "pool") {
content = "NOT YET IMPLEMENTED\n";
}
else if (category == "alloc") {
content = "NOT YET IMPLEMENTED\n";
}
else if (category == "store") {
uint32_t destination_path_responses = 0;
for (auto& [destination_hash, destination] : RNS::Transport::destinations()) {
destination_path_responses += destination.path_responses().size();
}
uint32_t interface_announces = 0;
for (auto& [interface_hash, interface] : RNS::Transport::get_interfaces()) {
interface_announces += interface.announce_queue().size();
}
content = "{\n";
content << " \"paths\": " << std::to_string(RNS::Transport::new_path_table().size()) << ",\n";
content << " \"destinations\": " << std::to_string(RNS::Transport::destinations().size()) << ",\n";
content << " \"announces\": " << std::to_string(RNS::Transport::announce_table().size()) << ",\n";
content << " \"held_announces\": " << std::to_string(RNS::Transport::held_announces().size()) << ",\n";
content << " \"path_requests\": " << std::to_string(RNS::Transport::path_requests().size()) << ",\n";
content << " \"discovery_path_requests\": " << std::to_string(RNS::Transport::discovery_path_requests().size()) << ",\n";
content << " \"pending_local_path_requests\": " << std::to_string(RNS::Transport::pending_local_path_requests().size()) << ",\n";
content << " \"discovery_pr_tags\": " << std::to_string(RNS::Transport::discovery_pr_tags().size()) << ",\n";
content << " \"control_destinations\": " << std::to_string(RNS::Transport::control_destinations().size()) << ",\n";
content << " \"control_hashes\": " << std::to_string(RNS::Transport::control_hashes().size()) << ",\n";
content << " \"packet_hashes\": " << std::to_string(RNS::Transport::packet_hashlist().size()) << ",\n";
content << " \"reverse_hashes\": " << std::to_string(RNS::Transport::reverse_table().size()) << ",\n";
content << " \"receipts\": " << std::to_string(RNS::Transport::receipts().size()) << ",\n";
content << " \"links\": " << std::to_string(RNS::Transport::link_table().size()) << ",\n";
content << " \"pending_links\": " << std::to_string(RNS::Transport::pending_links().size()) << ",\n";
content << " \"active_links\": " << std::to_string(RNS::Transport::active_links().size()) << ",\n";
content << " \"tunnels\": " << std::to_string(RNS::Transport::tunnels().size()) << ",\n";
content << " \"known_destinations\": " << std::to_string(RNS::Identity::known_destinations().size()) << ",\n";
content << " \"destination_path_responses\": " << std::to_string(destination_path_responses) << ",\n";
content << " \"queued_announces\": " << std::to_string(interface_announces) << ",\n";
content << "}";
}
else if (category == "metrics") {
content = "{\n";
content << " \"packets_sent\": " << std::to_string(RNS::Transport::packets_sent()) << ",\n";
content << " \"packets_received\": " << std::to_string(RNS::Transport::packets_received()) << ",\n";
content << " \"destinations_added\": " << std::to_string(RNS::Transport::destinations_added()) << ",\n";
content << "}";
}
else {
content = "CATEGORY NOT FOUND\n";
}
}
else if (path == "/page/device.mu") {
if (category == "general") {
content = "{\n";
content << " \"firmware_version\": \"" << std::to_string(MAJ_VERS) << "." << std::to_string(MIN_VERS) << "\",\n";
content << " \"battery_voltage\": " << std::to_string((float)((uint16_t)(battery_voltage*100)) / 100.0) << ",\n";
content << " \"battery_percent\": " << std::to_string(((uint8_t)battery_percent)) << ",\n";
content << " \"battery_state\": \"";
switch (battery_state) {
case BATTERY_STATE_CHARGING:
content << "CHARGING";
break;
case BATTERY_STATE_CHARGED:
content << "CHARGED";
break;
case BATTERY_STATE_DISCHARGING:
content << "DISCHARGING";
break;
case BATTERY_STATE_UNKNOWN:
content << "UNKNOWN";
break;
default:
break;
}
content << "\",\n";
content << "}";
}
else if (category == "interfaces") {
content = "{\n";
content << " \"" << lora_interface.name().c_str() << "\": {\n";
content << " \"frequency\": " << std::to_string(lora_freq) << ",\n";
content << " \"bandwidth\": " << std::to_string(lora_bw) << ",\n";
content << " \"tx_power\": " << std::to_string(lora_txp) << ",\n";
content << " \"spreading_factor\": " << std::to_string(lora_sf) << ",\n";
content << " \"coding_rate\": " << std::to_string(lora_cr) << ",\n";
content << " \"current_rssi\": " << std::to_string(last_rssi+rssi_offset) << ",\n";
content << " \"current_snr\": " << std::to_string(last_snr_raw) << ",\n";
add_interface_details(content, lora_interface);
content << " },\n";
#if defined(UDP_TRANSPORT)
if (wifi_mode != WR_WIFI_OFF && udp_interface) {
content << " \"" << udp_interface.name().c_str() << "\": {\n";
content << " \"ip_addr\": \"" << wr_device_ip.toString().c_str() << "\",\n";
content << " \"udp_port\": " << std::to_string(udp_port) << ",\n";
content << " \"wifi_ssid\": \"" << wr_ssid << "\",\n";
add_interface_details(content, udp_interface);
content << " },\n";
}
#endif
content << "}";
}
else {
content = "CATEGORY NOT FOUND\n";
}
}
else {
content = "PATH NOT FOUND\n";
}
packer.packBinary(content.data(), content.size());
}
return RNS::Bytes(packer.data(), packer.size());
}