forked from meshtastic/firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaxcounterModule.cpp
More file actions
183 lines (155 loc) · 6.84 KB
/
Copy pathPaxcounterModule.cpp
File metadata and controls
183 lines (155 loc) · 6.84 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
#include "configuration.h"
#if defined(ARCH_ESP32) && !MESHTASTIC_EXCLUDE_PAXCOUNTER
#include "Default.h"
#include "MeshService.h"
#include "PaxcounterModule.h"
#include "graphics/ScreenFonts.h"
#include "graphics/SharedUIDisplay.h"
#include "graphics/images.h"
#include <esp_event.h>
#include <freertos/timers.h>
// Arduino WiFi has a wifiscan.h too, so declare the libpax scanner hooks here.
void set_wifi_channels(uint16_t channels_map);
void wifi_sniffer_init(uint16_t wifi_channel_switch_interval);
void switchWifiChannel(TimerHandle_t xTimer);
extern TimerHandle_t WifiChanTimer;
static void startWifiChannelTimer(uint16_t wifi_channel_switch_interval)
{
if (wifi_channel_switch_interval == 0) {
return;
}
WifiChanTimer =
xTimerCreate("WifiChannelTimer", pdMS_TO_TICKS(wifi_channel_switch_interval * 10), pdTRUE, (void *)0, switchWifiChannel);
if (!WifiChanTimer) {
LOG_WARN("Paxcounter could not create WiFi channel switch timer");
return;
}
xTimerStart(WifiChanTimer, 0);
}
static void ensureDefaultEventLoop()
{
esp_err_t result = esp_event_loop_create_default();
if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) {
LOG_WARN("Paxcounter could not create ESP event loop: %d", result);
}
}
PaxcounterModule *paxcounterModule;
/**
* Callback function for libpax.
* We only clear our sent flag here, since this function is called from another thread, so we
* cannot send to the mesh directly.
*/
void PaxcounterModule::handlePaxCounterReportRequest()
{
// The libpax library already updated our data structure, just before invoking this callback.
LOG_INFO("PaxcounterModule: libpax reported new data: wifi=%d; ble=%d; uptime=%lu",
paxcounterModule->count_from_libpax.wifi_count, paxcounterModule->count_from_libpax.ble_count, millis() / 1000);
paxcounterModule->reportedDataSent = false;
paxcounterModule->setIntervalFromNow(0);
}
PaxcounterModule::PaxcounterModule()
: concurrency::OSThread("Paxcounter"),
ProtobufModule("paxcounter", meshtastic_PortNum_PAXCOUNTER_APP, &meshtastic_Paxcount_msg)
{
}
/**
* Send the Pax information to the mesh if we got new data from libpax.
* This is called periodically from our runOnce() method and will actually send the data to the mesh
* if libpax updated it since the last transmission through the callback.
* @param dest - destination node (usually NODENUM_BROADCAST)
* @return false if sending is unnecessary, true if information was sent
*/
bool PaxcounterModule::sendInfo(NodeNum dest)
{
if (paxcounterModule->reportedDataSent)
return false;
LOG_INFO("PaxcounterModule: send pax info wifi=%d; ble=%d; uptime=%lu", count_from_libpax.wifi_count,
count_from_libpax.ble_count, millis() / 1000);
meshtastic_Paxcount pl = meshtastic_Paxcount_init_default;
pl.wifi = count_from_libpax.wifi_count;
pl.ble = count_from_libpax.ble_count;
pl.uptime = millis() / 1000;
meshtastic_MeshPacket *p = allocDataProtobuf(pl);
p->to = dest;
p->decoded.want_response = false;
p->priority = meshtastic_MeshPacket_Priority_BACKGROUND;
service->sendToMesh(p, RX_SRC_LOCAL, true);
paxcounterModule->reportedDataSent = true;
return true;
}
bool PaxcounterModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_Paxcount *p)
{
return false; // Let others look at this message also if they want. We don't do anything with received packets.
}
meshtastic_MeshPacket *PaxcounterModule::allocReply()
{
meshtastic_Paxcount pl = meshtastic_Paxcount_init_default;
pl.wifi = count_from_libpax.wifi_count;
pl.ble = count_from_libpax.ble_count;
pl.uptime = millis() / 1000;
return allocDataProtobuf(pl);
}
int32_t PaxcounterModule::runOnce()
{
if (isActive()) {
if (firstTime) {
firstTime = false;
LOG_DEBUG("Paxcounter starting up with interval of %d seconds",
Default::getConfiguredOrDefault(moduleConfig.paxcounter.paxcounter_update_interval,
default_telemetry_broadcast_interval_secs));
struct libpax_config_t configuration;
libpax_default_config(&configuration);
configuration.blecounter = 1;
configuration.blescantime = 0; // infinite
configuration.wificounter = 1;
configuration.LIBPAX_WIFI_CHANNEL_map = LIBPAX_WIFI_CHANNEL_ALL;
configuration.LIBPAX_WIFI_CHANNEL_switch_interval = 50;
configuration.wifi_rssi_threshold = Default::getConfiguredOrDefault(moduleConfig.paxcounter.wifi_threshold, -80);
configuration.ble_rssi_threshold = Default::getConfiguredOrDefault(moduleConfig.paxcounter.ble_threshold, -80);
libpax_update_config(&configuration);
// internal processing initialization
libpax_counter_init(handlePaxCounterReportRequest, &count_from_libpax,
Default::getConfiguredOrDefault(moduleConfig.paxcounter.paxcounter_update_interval,
default_telemetry_broadcast_interval_secs),
0);
// libpax sets the WiFi country in counter_start(), so start channel rotation after that.
ensureDefaultEventLoop();
set_wifi_channels(configuration.LIBPAX_WIFI_CHANNEL_map);
wifi_sniffer_init(0);
libpax_counter_start();
startWifiChannelTimer(configuration.LIBPAX_WIFI_CHANNEL_switch_interval);
} else {
sendInfo(NODENUM_BROADCAST);
}
return Default::getConfiguredOrDefaultMsScaled(moduleConfig.paxcounter.paxcounter_update_interval,
default_telemetry_broadcast_interval_secs, numOnlineNodes);
} else {
return disable();
}
}
#if HAS_SCREEN
#include "graphics/ScreenFonts.h"
#include "graphics/SharedUIDisplay.h"
void PaxcounterModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
display->clear();
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(FONT_SMALL);
int line = 1;
// === Set Title
const char *titleStr = "Pax";
// === Header ===
graphics::drawCommonHeader(display, x, y, titleStr);
char buffer[50];
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(FONT_SMALL);
libpax_counter_count(&count_from_libpax);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(FONT_SMALL);
display->drawStringf(display->getWidth() / 2 + x, graphics::getTextPositions(display)[line++], buffer,
"WiFi: %d\nBLE: %d\nUptime: %ds", count_from_libpax.wifi_count, count_from_libpax.ble_count,
millis() / 1000);
graphics::drawCommonFooter(display, x, y);
}
#endif // HAS_SCREEN
#endif