-
Notifications
You must be signed in to change notification settings - Fork 6
/
wifihandler.ino
160 lines (144 loc) · 6.02 KB
/
wifihandler.ino
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
/* Hint: The ESP32 wifi library sources are here:
C:\Users\<username>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.13\libraries\WiFi
*/
static IPAddress broadcastAddr(255,255,255,255);
#define ALIVEBUFFERSIZE 50
uint8_t alivebuffer[ALIVEBUFFERSIZE];
uint32_t aliveCounter;
uint32_t debugNSendAttempts;
void wifihandler_cyclic(void) {
if (isWifiConnected && ((micros() - lastBroadcast) > 1000000ul) ) //every second send out a broadcast ping
{
uint8_t buff[4] = {0x1C,0xEF,0xAC,0xED};
lastBroadcast = micros();
Serial.println("Transmitting heartbeat");
wifiUDPServer.beginPacket(broadcastAddr, 17222);
wifiUDPServer.write(buff, 4);
wifiUDPServer.endPacket();
/* and also send an alive-message via telnet */
wifihandler_sendAliveToTelnet();
}
if (wifiTelnetServer.hasClient()) {
telnetClient = wifiTelnetServer.available();
Serial.print("New client: ");
//Serial.print(i); Serial.print(' ');
Serial.println(telnetClient.remoteIP());
//leds[SysSettings.LED_CONNECTION_STATUS] = CRGB::Blue;
//FastLED.show();
}
if (telnetClient) {
if (telnetClient.connected()) {
/* get data from the telnet client and push it to input processing */
while(telnetClient.available()) {
uint8_t inByt;
inByt = telnetClient.read();
//SysSettings.isWifiActive = true;
//Serial.println(inByt); /* echo to serial - just for debugging. Don't leave this on! */
wifiGVRET.processIncomingByte(inByt);
}
}
}
}
void wifihandler_sendBufferedData(void) {
size_t wifiLength = wifiGVRET.numAvailableBytes();
uint8_t* buff = wifiGVRET.getBufferedBytes();
if (telnetClient && telnetClient.connected())
{
telnetClient.write(buff, wifiLength);
}
wifiGVRET.clearBufferedBytes();
}
void wifihandler_sendAliveToTelnet(void) {
uint8_t i;
uint16_t aliveCanID;
i=0;
aliveCanID = 0x1000; /* simulated CAN ID. Real IDs are in range 0 to 0x7FF. */
#define whichBus 0
alivebuffer[i++] = 0xF1;
alivebuffer[i++] = 0; //0 = canbus frame sending
uint32_t now = micros();
alivebuffer[i++] = (uint8_t)(now & 0xFF);
alivebuffer[i++] = (uint8_t)(now >> 8);
alivebuffer[i++] = (uint8_t)(now >> 16);
alivebuffer[i++] = (uint8_t)(now >> 24);
alivebuffer[i++] = (uint8_t)(aliveCanID & 0xFF);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 8);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 16);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 24);
alivebuffer[i++] = 8 + (uint8_t)(whichBus << 4);
/* 8 data bytes */
alivebuffer[i++] = (uint8_t)(aliveCounter>>24);
alivebuffer[i++] = (uint8_t)(aliveCounter>>16);
alivebuffer[i++] = (uint8_t)(aliveCounter>>8);
alivebuffer[i++] = (uint8_t)(aliveCounter);
alivebuffer[i++] = (uint8_t)(expCounterMsg>>24);
alivebuffer[i++] = (uint8_t)(expCounterMsg>>16);
alivebuffer[i++] = (uint8_t)(expCounterMsg>>8);
alivebuffer[i++] = (uint8_t)expCounterMsg;
alivebuffer[i++] = 0; /* checksum, but not used. */
if (telnetClient && telnetClient.connected())
{
telnetClient.write(alivebuffer, i);
}
aliveCounter++;
/* another "virtual" message, with statistics data */
i=0;
aliveCanID = 0x1001; /* simulated CAN ID. Real IDs are in range 0 to 0x7FF. */
#define whichBus 0
alivebuffer[i++] = 0xF1;
alivebuffer[i++] = 0; //0 = canbus frame sending
now = micros();
alivebuffer[i++] = (uint8_t)(now & 0xFF);
alivebuffer[i++] = (uint8_t)(now >> 8);
alivebuffer[i++] = (uint8_t)(now >> 16);
alivebuffer[i++] = (uint8_t)(now >> 24);
alivebuffer[i++] = (uint8_t)(aliveCanID & 0xFF);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 8);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 16);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 24);
alivebuffer[i++] = 8 + (uint8_t)(whichBus << 4);
/* 8 data bytes */
alivebuffer[i++] = (uint8_t)(expCounterLostFrames>>24);
alivebuffer[i++] = (uint8_t)(expCounterLostFrames>>16);
alivebuffer[i++] = (uint8_t)(expCounterLostFrames>>8);
alivebuffer[i++] = (uint8_t)(expCounterLostFrames);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = 0; /* checksum, but not used. */
if (telnetClient && telnetClient.connected())
{
telnetClient.write(alivebuffer, i);
}
/* another "virtual" message, with statistics data */
i=0;
aliveCanID = 0x1002; /* simulated CAN ID. Real IDs are in range 0 to 0x7FF. */
#define whichBus 0
alivebuffer[i++] = 0xF1;
alivebuffer[i++] = 0; //0 = canbus frame sending
now = micros();
alivebuffer[i++] = (uint8_t)(now & 0xFF);
alivebuffer[i++] = (uint8_t)(now >> 8);
alivebuffer[i++] = (uint8_t)(now >> 16);
alivebuffer[i++] = (uint8_t)(now >> 24);
alivebuffer[i++] = (uint8_t)(aliveCanID & 0xFF);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 8);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 16);
alivebuffer[i++] = (uint8_t)(aliveCanID >> 24);
alivebuffer[i++] = 8 + (uint8_t)(whichBus << 4);
/* 8 data bytes */
alivebuffer[i++] = (uint8_t)(debugNSendAttempts>>24);
alivebuffer[i++] = (uint8_t)(debugNSendAttempts>>16);
alivebuffer[i++] = (uint8_t)(debugNSendAttempts>>8);
alivebuffer[i++] = (uint8_t)(debugNSendAttempts);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = (uint8_t)(0);
alivebuffer[i++] = 0; /* checksum, but not used. */
if (telnetClient && telnetClient.connected())
{
telnetClient.write(alivebuffer, i);
}
}