-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopgpdevice.cpp
448 lines (363 loc) · 9.05 KB
/
opgpdevice.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
Copyright 2019 SoloKeys Developers
Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
http://opensource.org/licenses/MIT>, at your option. This file may not be
copied, modified, or distributed except according to those terms.
*/
#include <sys/time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <fnmatch.h>
#include "opgpdevice.h"
#define SPIFFS_MODE
#ifdef SPIFFS_MODE
#include <spiffs.h>
static spiffs fs;
static uint8_t fsbuf[2048*10] = {0};
static char *SpiffsFileName = (char *)"filesystem.spiffs";
#endif
#define LOG_PAGE_SIZE 64
bool ifileexist(char* name);
int ireadfile(char* name, uint8_t * buf, size_t max_size, size_t *size);
int iwritefile(char* name, uint8_t * buf, size_t size);
int sprintfs();
static u8_t spiffs_work_buf[LOG_PAGE_SIZE * 2];
static u8_t spiffs_fds[32 * 4];
static u8_t spiffs_cache_buf[(LOG_PAGE_SIZE + 32) * 4];
static s32_t hw_spiffs_read(u32_t addr, u32_t size, u8_t *dst) {
memcpy(dst, fsbuf + addr, size);
return SPIFFS_OK;
}
static s32_t hw_spiffs_write(u32_t addr, u32_t size, u8_t *src) {
memcpy(fsbuf + addr, src, size);
return SPIFFS_OK;
}
static s32_t hw_spiffs_erase(u32_t addr, u32_t size) {
memset(fsbuf + addr, 0xff, size);
return SPIFFS_OK;
}
void hw_spiffs_mount() {
spiffs_config cfg;
cfg.phys_size = 10*2048; // use all spi flash
cfg.phys_addr = 0; // start spiffs at start of spi flash
cfg.phys_erase_block = 2048; // according to datasheet
cfg.log_block_size = 2048; // let us not complicate things
cfg.log_page_size = LOG_PAGE_SIZE; // as we said
cfg.hal_read_f = hw_spiffs_read;
cfg.hal_write_f = hw_spiffs_write;
cfg.hal_erase_f = hw_spiffs_erase;
int res = SPIFFS_mount(&fs,
&cfg,
spiffs_work_buf,
spiffs_fds,
sizeof(spiffs_fds),
spiffs_cache_buf,
sizeof(spiffs_cache_buf),
0);
printf("mount res: %i\n", res);
if (res || !SPIFFS_mounted(&fs)) {
res = SPIFFS_format(&fs);
printf("format res: %i\n", res);
}
uint32_t total = 0;
uint32_t used = 0;
SPIFFS_info(&fs, &total, &used);
printf("Mounted OK. Memory total: %d used: %d\n", total, used);
sprintfs();
}
int hwinit() {
#ifdef SPIFFS_MODE
memset(fsbuf, 0xff, sizeof(fsbuf));
if (ifileexist(SpiffsFileName)) {
size_t size = 0;
ireadfile(SpiffsFileName, fsbuf, sizeof(fsbuf), &size);
if (size != sizeof(fsbuf))
memset(fsbuf, 0xff, sizeof(fsbuf));
printf("Loaded OK\n");
}
hw_spiffs_mount();
#endif
return 0;
}
int spiffs_save() {
return iwritefile(SpiffsFileName, fsbuf, sizeof(fsbuf));
}
int udp_server()
{
static int run_already = 0;
static int fd = -1;
if (run_already && fd >= 0) return fd;
run_already = 1;
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror( "socket failed" );
return 1;
}
struct timeval read_timeout;
read_timeout.tv_sec = 0;
read_timeout.tv_usec = 10;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof(struct timeval)) != 0)
{
perror( "setsockopt" );
exit(1);
}
struct sockaddr_in serveraddr;
memset( &serveraddr, 0, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( 8111 );
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
if ( bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
perror( "bind failed" );
exit(1);
}
return fd;
}
int udp_recv(int fd, uint8_t * buf, int size)
{
fd_set input;
FD_ZERO(&input);
FD_SET(fd, &input);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
int n = select(fd + 1, &input, NULL, NULL, &timeout);
if (n == -1) {
perror("select\n");
exit(1);
} else if (n == 0)
return 0;
if (!FD_ISSET(fd, &input))
{
}
int length = recvfrom( fd, buf, size, 0, NULL, 0 );
if ( length < 0 ) {
perror( "recvfrom failed" );
exit(1);
}
return length;
}
void udp_send(int fd, uint8_t * buf, int size)
{
struct sockaddr_in serveraddr;
memset( &serveraddr, 0, sizeof(serveraddr) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons( 7112 );
serveraddr.sin_addr.s_addr = htonl( 0x7f000001 ); // (127.0.0.1)
if (sendto( fd, buf, size, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) {
perror( "sendto failed" );
exit(1);
}
}
static int fd = 0;
void ccid_init()
{
fd = udp_server();
}
uint32_t ccid_recv(uint8_t * msg)
{
int l;
l = udp_recv(fd, msg, 1024);
return l;
}
void ccid_send(uint8_t * msg, uint32_t sz)
{
udp_send(fd, msg, sz);
}
void make_work_directory(char* dir) {
if (access(dir, F_OK) != 0) {
mkdir(dir, 0777);
}
}
bool ifileexist(char* name) {
char fname[100] = {0};
char dir[] = "./data/";
make_work_directory(dir);
strcpy(fname, dir);
strcat(fname, name);
// check if it exist and have read permission
if (access(fname, R_OK) != 0)
return false;
return true;
}
bool sfileexist(char* name) {
spiffs_DIR d;
struct spiffs_dirent e;
struct spiffs_dirent *pe = &e;
SPIFFS_opendir(&fs, "/", &d);
while ((pe = SPIFFS_readdir(&d, pe))) {
if (0 == strcmp(name, (char *)pe->name)) {
return true;
}
}
return false;
}
bool fileexist(char* name) {
#ifdef SPIFFS_MODE
return sfileexist(name);
#else
return ifileexist(name);
#endif
}
int ireadfile(char* name, uint8_t * buf, size_t max_size, size_t *size) {
char fname[100] = {0};
char dir[] = "./data/";
make_work_directory(dir);
strcpy(fname, dir);
strcat(fname, name);
// check if it exist and have read permission
if (access(fname, R_OK) != 0)
return 1;
FILE *f = fopen(fname, "r");
if (f <= 0)
return 2;
*size = fread(buf, 1, max_size, f);
fclose(f);
return 0;
}
int sreadfile(char* name, uint8_t * buf, size_t max_size, size_t *size) {
*size = 0;
spiffs_file fd = SPIFFS_open(&fs, name, SPIFFS_RDWR, 0);
if (fd < 0)
return fd;
int res = SPIFFS_read(&fs, fd, buf, max_size);
*size = res;
int cres = SPIFFS_close(&fs, fd) < 0;
if (cres < 0)
return cres;
return (res >= 0) ? 0 : res;
}
int readfile(char* name, uint8_t * buf, size_t max_size, size_t *size) {
#ifdef SPIFFS_MODE
return sreadfile(name, buf, max_size, size);
#else
return ireadfile(name, buf, max_size, size);
#endif
}
int iwritefile(char* name, uint8_t * buf, size_t size) {
char fname[100] = {0};
char dir[] = "./data/";
make_work_directory(dir);
strcpy(fname, dir);
strcat(fname, name);
FILE *f = fopen(fname, "w");
if (f <= 0)
return 2;
size_t sz = fwrite(buf, 1, size, f);
fclose(f);
if (sz != size)
return 3;
return 0;
}
int swritefile(char* name, uint8_t * buf, size_t size) {
spiffs_file fd = SPIFFS_open(&fs, name, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
if (fd < 0)
return fd;
int res = SPIFFS_write(&fs, fd, buf, size);
int cres = SPIFFS_close(&fs, fd) < 0;
if (cres < 0)
return cres;
// debug only!
spiffs_save();
return (res >= 0) ? 0 : res;
}
int writefile(char* name, uint8_t * buf, size_t size) {
#ifdef SPIFFS_MODE
return swritefile(name, buf, size);
#else
return iwritefile(name, buf, size);
#endif
}
int ideletefile(char* name) {
char fname[100] = {0};
char dir[] = "./data/";
make_work_directory(dir);
strcpy(fname, dir);
strcat(fname, name);
remove(fname);
return 0;
}
int deletefile(char* name) {
#ifdef SPIFFS_MODE
return SPIFFS_remove(&fs, name);
#else
return ideletefile(name);
#endif
}
int ideletefiles(char* name) {
char fname[100] = {0};
char dir[] = "./data/";
make_work_directory(dir);
DIR *dirp=opendir(dir);
struct dirent entry;
struct dirent *dp=&entry;
while((dp = readdir(dirp)))
{
if((fnmatch(name, dp->d_name,0)) == 0)
{
strcpy(fname, dir);
strcat(fname, dp->d_name);
remove(fname);
}
}
closedir(dirp);
strcpy(fname, dir);
strcat(fname, name);
return 0;
}
int sprintfs() {
spiffs_DIR d;
struct spiffs_dirent e;
struct spiffs_dirent *pe = &e;
uint32_t total = 0;
uint32_t used = 0;
SPIFFS_info(&fs, &total, &used);
printf_device("Memory total: %d used: %d\n", total, used);
SPIFFS_opendir(&fs, "/", &d);
while ((pe = SPIFFS_readdir(&d, pe))) {
printf_device(" [%4d] %s\n", pe->size, pe->name);
}
SPIFFS_closedir(&d);
return 0;
}
int sdeletefiles(char* name) {
spiffs_DIR d;
struct spiffs_dirent e;
struct spiffs_dirent *pe = &e;
int res;
sprintfs();
SPIFFS_opendir(&fs, "/", &d);
while ((pe = SPIFFS_readdir(&d, pe))) {
if ((fnmatch(name, (char *)pe->name, 0)) == 0) {
fd = SPIFFS_open_by_dirent(&fs, pe, SPIFFS_RDWR, 0);
if (fd < 0)
return SPIFFS_errno(&fs);
res = SPIFFS_fremove(&fs, fd);
if (res < 0)
return SPIFFS_errno(&fs);
}
}
SPIFFS_closedir(&d);
return 0;
}
int deletefiles(char* name) {
#ifdef SPIFFS_MODE
return sdeletefiles(name);
#else
return ideletefiles(name);
#endif
}
int hwreboot() {
return 0;
}