-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
243 lines (189 loc) · 5.46 KB
/
main.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
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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <pins_arduino.h>
#include <Sys.h>
#include <Wifi.h>
#include <LedBlinker.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <mDNS.h>
#include <ctype.h>
#include <uart.h>
#include <Udp.h>
#include <ctype.h>
#include <cstdlib>
#include <Base64.h>
#include <Config.h>
#include <PubSubClient.h>
#include <EventBus.h>
#include <Mqtt.h>
#include <MqttJson.h>
#include <System.h>
#include <MqttJson.h>
#include <DWM1000_Tag.h>
#include <DWM1000_Anchor.h>
#include <Memory.h>
#include <SpiTester.h>
#include <IrqTester.h>
#include <Configurator.h>
#include <Metric.h>
#include <WiFiUdp.h>
#include <SysLog.h>
bool stopHeap(Actor* a,const char* s);
void startHeap();
extern "C" {
#include "user_interface.h"
};
uint32_t BAUDRATE = 230400;
Uid uid(200);
EventBus eb(4096,1024);
Log logger(256);
//________________________________________________Se_________________
Str line(20);
#include <main_labels.h>
Wifi wifi("wifi");
mDNS mdns("mdns");
LedBlinker led;
Mqtt mqtt("mqtt",1024);
System systm("system");
MqttJson router("mqttJson",1024);
DWM1000_Tag dwm1000Tag("dwm1000");
DWM1000_Anchor dwm1000Anchor("dwm1000");
Memory memory("memory");
Configurator configurator("config");
//SpiTester spiTester("spiTester");
//IrqTester irqTester("irqTester");
Config config;
extern void waitConfig();
/*
// A UDP instance to let us send and receive packets over UDP*/
// WiFiUDP udpClient;
// Create a new syslog instance with LOG_KERN facility
Syslog *syslog;
Str udpHost(20);
uint32_t udpPort;
void syslogger(char* start,uint32_t length)
{
if ( syslog==0) {
// syslog = new Syslog(udpClient,udpHost.c_str() , udpPort, Sys::hostname(), "system", LOG_KERN);
}
start[length]='\0';
syslog->log(start);
Log::serialLog(start,length);
}
enum { NONE,TAG,ANCHOR } DWM1000Role=NONE;
#include <vector.h>
#include <error_handler.h>
//etl::vector<uint32_t,100> v;
#include <malloc.h>
void report_memory() {
malloc_stats();
}
void free_error_handler(const etl::exception& e)
{
ERROR( "The error was %s in %s at %d ",e.what() ,e.file_name() ,e.line_number() );
}
Str strLog(256);
void setup()
{
Serial.begin(BAUDRATE, SerialConfig::SERIAL_8E1, SerialMode::SERIAL_FULL); // 8E1 for STM32
Serial.setDebugOutput(false);
Sys::delay(1000);
// v.push_back(3);
/* WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false);
WiFi.enableAP(false);
WiFi.enableSTA(false);
WiFi.mode(WIFI_OFF); */
etl::error_handler::free_function callback(free_error_handler);
etl::error_handler::set_callback(callback);
config.load();
waitConfig();
eb.onAny().call([](Cbor& msg) { // Log all events -> first handler
eb.log(strLog,msg);
DEBUG("%s",strLog.c_str());
});
Str strHostname(30),logLevel(5),logOutput(5);
Sys::init();
char hn[20];
sprintf(hn,"ESP%X",ESP.getChipId());
config.setNameSpace("sys");
config.get("host",strHostname,hn);
// system_update_cpu_freq(160);
Sys::hostname(strHostname.c_str());
logger.level(Log::LOG_INFO);
INFO("version : " __DATE__ " " __TIME__ " host %s",hn);
config.setNameSpace("log");
config.get("level",logLevel,"I");
logger.setLogLevel(logLevel.peek(0));
config.get("output",logOutput,"S");
/* cannot log through UDP too much overhead
if ( logOutput.peek(0)=='U') {
config.setNameSpace("syslog");
config.get("host",udpHost,"192.168.0.150");
config.get("port",udpPort,514);
logger.setOutput(syslogger);
};
/* Cannot log through MQTT too much overhead or DWM1000 protocol
* if ( logOutput.peek(0)=='M') {
mqtt.setLog(true);
}
*/
wifi.setup();
INFO(" starting Wifi host : '%s' on SSID : '%s' '%s' ", wifi.getHostname(),
wifi.getSSID(), wifi.getPassword());
uid.add(labels,LABEL_COUNT);
led.setMqtt(mqtt.id());
led.setWifi(wifi.id());
mqtt.setWifi(wifi.id());
// wifi.setup();
mdns.setWifi(wifi.id());
mdns.setup();
// memory.setup();
// spiTester.setup();
// irqTester.setup();
configurator.setup();
Str role(3);
config.setNameSpace("lpos");
config.get("role",role,"A");
uint8_t mac[8];
memset(mac,0,8);
WiFi.macAddress(&mac[2]);
if ( strcmp(role.c_str(), "T") == 0) {
DWM1000Role = TAG;
dwm1000Tag.setShortAddress(ESP.getChipId() & 0xFFFF);
dwm1000Tag.setLongAddress(mac);
dwm1000Tag.setup();
} else { // otherwise an anchor
DWM1000Role=ANCHOR;
dwm1000Anchor.setShortAddress(ESP.getChipId() & 0xFFFF);
dwm1000Anchor.setLongAddress(mac);
// dwm1000Anchor.setup();
}
// mqtt.setHost("limero.ddns.net")
mqtt.setup();
router.setMqttId(mqtt.id());
router.setup();
led.setup();
systm.setup();
config.save();
return;
}
extern "C" void tcpCleanup();
extern "C" void loop()
{
startHeap();
eb.eventLoop();
stopHeap(0,"after eventLoop" );
wifi.loop();
stopHeap(0,"after wifi loop" );
mqtt.loop();
stopHeap(0,"after mqtt loop" );
tcpCleanup();
mdns.loop();
if ( DWM1000Role == TAG ) {
dwm1000Tag.loop();
} else if ( DWM1000Role==ANCHOR) {
dwm1000Anchor.loop();
}
}