-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
222 lines (179 loc) · 5.38 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
#include "tcp_client.h"
#include "CGM_Display.h"
#include "hardware/watchdog.h"
int counter = 0;
long lastKnownVersion = 0;
bool watchDogReboot = false;
bool ReadBitmapStream();
extern "C" {
void tcp_debug_print_pcbs(void);
extern struct tcp_pcb *tcp_active_pcbs;
}
typedef struct tagWifiPoint
{
char ssid[48];
char password[32];
} WifiPoint;
WifiPoint wifiPoints[] =
{
{"WIFIHUB_d82510", "ntydtnkv"},
{"FRITZ!Box 7430 WO", "80031011246998990386"},
{"You will be hacked", ""},
{"HC", "nfwi6536"},
{""},
};
bool ReadBitmapStream()
{
tcp_debug_print_pcbs();
while (tcp_active_pcbs != nullptr)
{
printf("Waiting for all tcps to clear\r\n");
sleep_ms(1000);
}
const int imageSize = (128 / 8) * 250;
const int versionSize = sizeof(uint32_t);
bool hasErrored = false;
//TcpConnection *tcp = new TcpConnection("4.234.223.103", 5002, 30); // Azure Server
TcpConnection *tcp = new TcpConnection("81.187.255.239", 5002, 30); // Raspberry Pi at Toadhall
tcp->StartConnect();
unsigned int totalSize = imageSize + versionSize;
unsigned char* combinedBuffer = static_cast<unsigned char *>(malloc(totalSize));
unsigned int usedSize = 0;
while (true)
{
watchdog_update();
printf(watchDogReboot ? "¬" : ".");
TcpUserEvent* nextEvent;
nextEvent = tcp->DequeueEvent();
while (nextEvent != nullptr)
{
ConnectionEvents eventType = nextEvent->GetEvent();
printf("Picked event, type: %d, addr: %X\r\n", eventType, nextEvent);
switch (eventType)
{
case ConnectionEvents::ReceivedData :
{
unsigned char* partBuffer = nextEvent->GetBuffer();
int partLength = nextEvent->GetBufferLength();
if (usedSize + partLength > totalSize)
{
partLength = totalSize - usedSize;
}
memcpy(combinedBuffer + usedSize, partBuffer, partLength);
usedSize += partLength;
if (usedSize >= totalSize)
{
long version = *(reinterpret_cast<long*>(combinedBuffer));
printf("Version %ld", version);
if (lastKnownVersion != version)
{
lastKnownVersion = version;
CGM_ClearScreen();
CGM_DisplayBitmap(static_cast<unsigned char*>(combinedBuffer + versionSize));
}
tcp->Close();
}
}
break;
case ConnectionEvents::Errored :
CGM_ClearScreen();
CGM_printf("Errored %d, %d", nextEvent->GetError(), ++counter);
tcp->Close();
hasErrored = true;
break;
}
delete nextEvent;
if (tcp->IsClosed())
{
break;
}
sleep_ms(100);
nextEvent = tcp->DequeueEvent();
}
if (tcp->HasTimedOut())
{
CGM_ClearScreen();
CGM_printf("Timed out");
tcp->Close();
}
if (tcp->IsClosed())
{
delete tcp;
tcp = nullptr;
free(combinedBuffer);
return !hasErrored;
}
sleep_ms(100);
}
}
bool ConnectToWifi()
{
for (int i = 0; wifiPoints[i].ssid[0] != 0; ++i)
{
const char *ssid = wifiPoints[i].ssid;
const char *password = wifiPoints[i].password;
CGM_ClearScreen();
if (cyw43_arch_init())
{
CGM_DisplayText("failed to initialise");
return 1;
}
cyw43_arch_enable_sta_mode();
CGM_printf("Trying: %s", ssid);
uint32_t authLevel = CYW43_AUTH_WPA2_AES_PSK;
if (*password == 0)
{
authLevel = CYW43_AUTH_OPEN;
password = nullptr;
}
for (int i = 0; i < 30; ++i)
{
if (cyw43_arch_wifi_connect_timeout_ms(ssid, password, authLevel, 1000) == PICO_OK)
{
CGM_ClearScreen();
CGM_DisplayText("Wi-Fi Connected.");
return true;
}
watchdog_update();
}
cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
cyw43_arch_deinit();
}
return false;
}
int main()
{
stdio_init_all();
if (watchdog_caused_reboot())
{
printf("Rebooted by Watchdog!\n");
watchDogReboot = true;
}
else
{
printf("Clean boot\n");
}
watchdog_enable(8000, 1);
CGM_InitDisplay();
bool connected = false;
while (true)
{
if (!connected)
{
connected = ConnectToWifi();
}
else
{
if (!ReadBitmapStream())
{
connected = false;
}
else
{
sleep_ms(1000);
}
}
watchdog_update();
}
return 0;
}