-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.cpp
167 lines (149 loc) · 4.54 KB
/
monitor.cpp
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
/**
* This file is part of Wio Helium Monitor.
*
* Wio Helium Monitor is free software created by Paul Pinault aka disk91.
* 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
* any later version.
*
* Wio Helium Monitor 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 Wio LoRaWan Field Tester. If not, see <https://www.gnu.org/licenses/>.
*
* Author : Paul Pinault (disk91.com)
*/
#include <Arduino.h>
#include "config.h"
#include "keys.h"
#include "state.h"
#include "wifi.h"
#include "watchdog.h"
#include "loraCom.h"
void initState() {
state.isLoRaSet = false;
if ( readConfig() ) {
LOGF(("SSID : %s\r\n",state.ssid));
LOGF(("PASS : %s\r\n",state.password));
LOGF(("INTIP : %s\r\n",state.intPingIp));
LOGF(("ETXIP : %s\r\n",state.extPingIp));
} else {
LOGLN(("No Config found"));
#if defined __WIFISSID && defined __WIFIKEY
strcpy((char*)state.ssid,__WIFISSID);
strcpy((char*)state.password,__WIFIKEY);
strcpy((char*)state.extPingIp,__EXTIP);
strcpy((char*)state.intPingIp,__HSIP);
storeConfig();
#else
wifiConfig();
#endif
NVIC_SystemReset();
}
// Manage statis LoRaConfig
uint8_t deveui[] = __DEVEUI;
uint8_t appeui[] = __APPEUI;
uint8_t appkey[] = __APPKEY;
bool zero = true;
for ( int i = 0 ; i < 8 ; i++ ) {
if ( deveui[i] != 0 ) zero = false;
}
if ( !zero ) {
// we have a static configuration
bcopy(deveui,loraConf.deveui,DEVEUI_SZ);
bcopy(appeui,loraConf.appeui,APPEUI_SZ);
bcopy(appkey,loraConf.appkey,APPKEY_SZ);
loraConf.zone = __ZONE;
state.isLoRaSet = true;
LOGLN(("LoRa config Set"));
}
state.readPtr = 0;
state.writePtr = 0;
state.elements = 0;
state.intState = 0;
state.extState = 0;
state.hsName[0] = '\0';
state.hsState = HSSTATE_UNKN;
state.hasRefreshed = false;
state.withSound = true;
state.e5Detected = false;
state.isRegistered = false;
state.isLoRaInit = false;
state.isLoRaConnected = false;
state.uptime = 0;
}
// ---
// manage the historical data
// ---
void addInBuffer(uint32_t rttExt, uint32_t rttInt ) {
state.avgExtPing[state.writePtr] = (rttExt < (8*255))?rttExt/8:255; // storing step is 8ms, max 255*8ms
state.avgIntPing[state.writePtr] = (rttInt < (8*255))?rttInt/8:255; // storing step is 8ms, max 255*8ms
state.writePtr = (state.writePtr+1) & (MAXBUFFER-1);
if ( state.elements == MAXBUFFER ) {
state.readPtr = (state.readPtr+1) & (MAXBUFFER-1);
} else {
state.elements++;
}
state.hasRefreshed = true;
}
uint8_t getIndexInBuffer(int i) {
int t = state.readPtr;
if ( i > state.elements ) return MAXBUFFER;
for ( int k = 0 ; k < i ; k++ ) {
t = (t+1) & (MAXBUFFER-1);
}
return t;
}
uint8_t getLastIndexWritten() {
if ( state.elements == 0 ) return MAXBUFFER;
if ( state.writePtr == 0 ) return MAXBUFFER-1;
return state.writePtr-1;
}
void runMonitor() {
uint32_t rttE = pingIP((char*)state.extPingIp);
iAmAlive();
uint32_t rttI = pingIP((char*)state.intPingIp);
iAmAlive();
addInBuffer(rttE,rttI);
if ( rttE > 0 ) state.extState = 5;
else if ( state.extState > 0 ) state.extState--;
if ( rttI > 0 ) state.intState = 5;
else if ( state.intState > 0 ) state.intState--;
}
void runLoRaPing() {
uint8_t data[8];
for ( int i = 0 ; i < 7 ; i ++ ) {
data[i] = state.uidi[i];
}
data[7] = ((state.intState << 4) & 0xF0) | (state.extState & 0x0F);
do_send(1, data, 8, 7, 4, false, 0);
}
bool reportData() {
if (state.extState == 5) {
// network looks available
bool r = reportWatchium();
iAmAlive();
if ( r && state.e5Detected && state.isRegistered ) {
getLoRaConfig();
iAmAlive();
// make sure we init LoRa also when the setup is static
loraInit();
iAmAlive();
}
return true;
} else {
// prefer report over LoRaWan
uint8_t data[8];
for ( int i = 0 ; i < 7 ; i ++ ) {
data[i] = state.uidi[i];
}
data[7] = ((state.intState << 4) & 0xF0) | (state.extState & 0x0F);
// the maximum SF/DR possible in the zone will be automatically set
do_send(1, data, 8, 12, 30, false, 1);
return false;
}
}