-
Notifications
You must be signed in to change notification settings - Fork 0
/
cupdate.h
278 lines (230 loc) · 6.95 KB
/
cupdate.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
void download_done(void *optParm, AsyncHTTPSRequest *request, int readyState);
void download_data(void *xo, AsyncHTTPSRequest *request, int available);
void sendDownloadReq(char *theURL,void (*stateCB)(void* optParm, AsyncHTTPSRequest* request, int readyState),void (*dataCB)(void* optParm, AsyncHTTPSRequest* request, int readyState));
int getlocalversion()
{
char versionstr[32];
int theversion;
FsFile vfile;
vfile.open(".version",O_RDWR);
if (vfile){
vfile.read((uint8_t * )versionstr,31);
vfile.close();
//log(versionstr);
} else {
strcpy(versionstr,"-1");
}
theversion = atoi(versionstr);
return(theversion);
}
void sendHttpRequest(char *theURL,void (*pFunc)(void* optParm, AsyncHTTPSRequest* request, int readyState))
{
static bool requestOpenResult;
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
{
request.setTimeout(100000);
request.onReadyStateChange(pFunc);
request.setDebug(true);
requestOpenResult = request.open("GET", theURL);
if (requestOpenResult)
{
// Only send() if open() returns true, or crash
//Serial.println("Sending request");
//Serial.println(theURL);
request.send();
}
else
{
log("Can't send bad request");
log(theURL);
}
}
else
{
log("Can't send request");
log(theURL);
}
}
void requestCB(void* optParm, AsyncHTTPSRequest* request, int readyState)
{
(void) optParm;
if (readyState == readyStateDone)
{
Serial.println("\n**************************************");
Serial.println(request->responseLongText());
Serial.println("**************************************");
request->setDebug(false);
}
}
void add_download(char *filename)
{
struct content_entry *dl;
if (strlen(filename) == 0){
log("Ignoring empty filename");
return;
}
dl = (struct content_entry *)malloc(sizeof(struct content_entry));
if (dl == NULL){
log("Malloc Failed for add_download");
return;
}
strcpy(dl->filename,filename);
dl->state = DL_STATE_DOWNLOAD_NEEDED;
queue(&dl_q,&dl->qe);
}
void start_downloading()
{
char theurl[256];
char dirpath[256];
char message[256];
cur_dl = (struct content_entry *)dl_q.tail;
if (cur_dl == NULL){
log("No Downloads");
return;
}
if (cur_dl->state != DL_STATE_DOWNLOAD_NEEDED){
log("Wrong Download State to start");
return;
}
sprintf(message,"Starting download for: %s\n",cur_dl->filename);
log(message);
strcpy(theurl,"https://raw.githubusercontent.com/glennswest/i2cgateha/main/contents/");
strcat(theurl,cur_dl->filename);
//strcpy(dirpath,cur_dl->filename);
if (sd.exists(cur_dl->filename)){
sprintf(message,"Removing %s",cur_dl->filename);
log(message);
sd.remove(cur_dl->filename);
log("Remove done");
}
log("Opening file");
if (dfile.open(cur_dl->filename, O_WRONLY | O_CREAT)){
log("File Open");
} else {
sprintf(message,"Open Failed: %s(%d)",cur_dl->filename,strlen(cur_dl->filename));
log(message);
return;
}
log("Send Download Request");
sendDownloadReq(theurl,download_done,download_data);
}
void download_done(void *optParm, AsyncHTTPSRequest *request, int readyState)
{
char message[256];
int remain_length;
sprintf(message,"Download done callback %s - state %d\n",cur_dl->filename,readyState);
Serial.print(message);
if (readyState == readyStateDone){
remain_length = request->available();
sprintf(message,"Download complete: %s - Remainer: %d\n", cur_dl->filename,remain_length);
Serial.print(message);
dfile.write((uint8_t *)request->responseLongText(),remain_length);
dfile.close();
if (cur_dl == (struct content_entry *)dl_q.tail){
unqueue(&dl_q);
}
start_downloading();
}
}
void download_data(void *xo, AsyncHTTPSRequest *request, int available)
{
char message[128];
//Serial.print("download_data\n");
if (available > 0){
//sprintf(message,"Downloading %s - %d bytes",cur_dl->filename,available);
//Serial.print(message);
dfile.write((uint8_t *)request->responseLongText(),available);
}
}
void get_content_list(void* optParm, AsyncHTTPSRequest* request, int readyState)
{
(void) optParm;
int tsize;
char filename[64];
char work[2048];
char *fptr;
char *eptr;
if (readyState == readyStateDone)
{
Serial.print("Got content list\n");
strcpy(work,request->responseLongText());
tsize = strlen(work);
Serial.printf("Size = %d\n\r",tsize);
fptr = work;
eptr = strchr(fptr,0x0A);
if (eptr == NULL){
log("No Data");
return;
}
while(eptr != NULL){
*eptr = 0;
eptr++;
strcpy(filename,fptr);
Serial.printf("Filename: %s\n\r",filename);
add_download(filename);
fptr = eptr;
eptr = strchr(fptr,0x0A);
}
strcpy(filename,fptr);
Serial.printf("Filename: %s\n\r",filename);
add_download(filename);
start_downloading();
}
}
void start_content_update()
{
log("Getting List of Updated Content");
sendHttpRequest("https://raw.githubusercontent.com/glennswest/i2cgateha/main/contents/.content",get_content_list);
//sendHttpRequest("https://raw.githubusercontent.com/glennswest/i2cgateha/main/contents/.content",requestCB);
}
void remote_version_check(void* optParm, AsyncHTTPSRequest* request, int readyState)
{
(void) optParm;
char message[256];
if (readyState == readyStateDone)
{
remote_version = request->responseText().toInt();
sprintf(message,"Remote Content Version: %d",remote_version);
log(message);
request->setDebug(false);
if (local_version < remote_version){
log("Content Update Needed - Starting");
start_content_update();
}
}
}
// Start the content update process
void content_check()
{
char message[256];
log("Getting Remote Version");
sendHttpRequest("https://raw.githubusercontent.com/glennswest/i2cgateha/main/contents/.version",remote_version_check);
}
void sendDownloadReq(char *theURL,void (*stateCB)(void* optParm, AsyncHTTPSRequest* request, int readyState),void (*dataCB)(void* optParm, AsyncHTTPSRequest* request, int readyState))
{
static bool requestOpenResult;
Serial.print("sendDownloadReq\n");
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
{
request.setTimeout(5);
request.onReadyStateChange(stateCB);
request.onData(dataCB);
//request.setDebug(true);
requestOpenResult = request.open("GET", theURL);
if (requestOpenResult)
{
// Only send() if open() returns true, or crash
Serial.println("Sending request");
Serial.println(theURL);
request.send();
}
else
{
Serial.println("Can't send bad request");
}
}
else
{
Serial.println("Can't send request");
}
}