-
Notifications
You must be signed in to change notification settings - Fork 64
/
fuji.cpp
executable file
·1432 lines (1163 loc) · 38.3 KB
/
fuji.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef BUILD_COCO
#include "fuji.h"
#ifdef ESP_PLATFORM
#include <driver/ledc.h>
#else
#include <libgen.h>
#endif
#include <cstdint>
#include <cstring>
#include "../../../include/debug.h"
#include "fnSystem.h"
#include "fnConfig.h"
#include "fsFlash.h"
#include "fnWiFi.h"
#include "led.h"
#include "utils.h"
#include "string_utils.h"
#define ADDITIONAL_DETAILS_BYTES 13
drivewireFuji theFuji; // global fuji device object
// drivewireDisk drivewireDiskDevs[MAX_HOSTS];
drivewireNetwork drivewireNetDevs[MAX_NETWORK_DEVICES];
bool _validate_host_slot(uint8_t slot, const char *dmsg = nullptr);
bool _validate_device_slot(uint8_t slot, const char *dmsg = nullptr);
bool _validate_host_slot(uint8_t slot, const char *dmsg)
{
if (slot < MAX_HOSTS)
return true;
if (dmsg == NULL)
{
Debug_printf("!! Invalid host slot %hu\n", slot);
}
else
{
Debug_printf("!! Invalid host slot %hu @ %s\n", slot, dmsg);
}
return false;
}
bool _validate_device_slot(uint8_t slot, const char *dmsg)
{
if (slot < MAX_DISK_DEVICES)
return true;
if (dmsg == NULL)
{
Debug_printf("!! Invalid device slot %hu\n", slot);
}
else
{
Debug_printf("!! Invalid device slot %hu @ %s\n", slot, dmsg);
}
return false;
}
/**
* Say the numbers 1-8 using phonetic tweaks.
* @param n The number to say.
*/
void say_number(unsigned char n)
{
#ifdef TODO_SPEECH
switch (n)
{
case 1:
util_sam_say("WAH7NQ", true);
break;
case 2:
util_sam_say("TUW7", true);
break;
case 3:
util_sam_say("THRIYY7Q", true);
break;
case 4:
util_sam_say("FOH7R", true);
break;
case 5:
util_sam_say("F7AYVQ", true);
break;
case 6:
util_sam_say("SIH7IHKSQ", true);
break;
case 7:
util_sam_say("SEHV7EHNQ", true);
break;
case 8:
util_sam_say("AEY74Q", true);
break;
default:
Debug_printf("say_number() - Uncaught number %d", n);
}
#endif
}
/**
* Say swap label
*/
void say_swap_label()
{
#ifdef TODO_SPEECH
// DISK
util_sam_say("DIHSK7Q ", true);
#endif
}
// Constructor
drivewireFuji::drivewireFuji()
{
// Helpful for debugging
for (int i = 0; i < MAX_HOSTS; i++)
_fnHosts[i].slotid = i;
}
// Reset FujiNet
void drivewireFuji::reset_fujinet()
{
Debug_println("Fuji cmd: REBOOT");
// drivewire_complete();
fnSystem.reboot();
}
// Scan for networks
void drivewireFuji::net_scan_networks()
{
Debug_println("Fuji cmd: SCAN NETWORKS");
if (!wifiScanStarted)
{
wifiScanStarted = true;
_countScannedSSIDs = fnWiFi.scan_networks();
}
response.clear();
response.shrink_to_fit();
response += _countScannedSSIDs;
}
// Return scanned network entry
void drivewireFuji::net_scan_result()
{
Debug_println("Fuji cmd: GET SCAN RESULT");
uint8_t n = fnUartBUS.read();
wifiScanStarted = false;
// Response to FUJICMD_GET_SCAN_RESULT
struct
{
char ssid[MAX_SSID_LEN + 1];
uint8_t rssi;
} detail;
bool err = false;
if (n < _countScannedSSIDs)
fnWiFi.get_scan_result(n, detail.ssid, &detail.rssi);
else
{
memset(&detail, 0, sizeof(detail));
err = true;
}
response.clear();
response.shrink_to_fit();
response = std::string((const char *)&detail, sizeof(detail));
}
// Get SSID
void drivewireFuji::net_get_ssid()
{
Debug_println("Fuji cmd: GET SSID");
// Response to FUJICMD_GET_SSID
struct
{
char ssid[MAX_SSID_LEN + 1];
char password[MAX_WIFI_PASS_LEN];
} cfg;
memset(&cfg, 0, sizeof(cfg));
/*
We memcpy instead of strcpy because technically the SSID and phasephras aren't strings and aren't null terminated,
they're arrays of bytes officially and can contain any byte value - including a zero - at any point in the array.
However, we're not consistent about how we treat this in the different parts of the code.
*/
std::string s = Config.get_wifi_ssid();
memcpy(cfg.ssid, s.c_str(),
s.length() > sizeof(cfg.ssid) ? sizeof(cfg.ssid) : s.length());
s = Config.get_wifi_passphrase();
memcpy(cfg.password, s.c_str(),
s.length() > sizeof(cfg.password) ? sizeof(cfg.password) : s.length());
response.clear();
response.shrink_to_fit();
response = std::string((const char *)&cfg, sizeof(cfg));
}
// Set SSID
void drivewireFuji::net_set_ssid()
{
Debug_printf("\r\nFuji cmd: SET SSID");
struct
{
char ssid[MAX_SSID_LEN + 1];
char password[MAX_WIFI_PASS_LEN];
} cfg;
fnUartBUS.readBytes((uint8_t *)&cfg, sizeof(cfg));
bool save = false; // for now don't save - to do save if connection was succesful
// URL Decode SSID/PASSWORD to handle special chars (FIXME)
//mstr::urlDecode(cfg.ssid, sizeof(cfg.ssid));
//mstr::urlDecode(cfg.password, sizeof(cfg.password));
Debug_printf("\r\nConnecting to net: %s password: %s\n", cfg.ssid, cfg.password);
if (fnWiFi.connect(cfg.ssid, cfg.password) == ESP_OK)
{
Config.store_wifi_ssid(cfg.ssid, sizeof(cfg.ssid));
Config.store_wifi_passphrase(cfg.password, sizeof(cfg.password));
}
// Only save these if we're asked to, otherwise assume it was a test for connectivity
// should only save if connection was successful - i think
if (save)
{
Config.save();
}
}
// Get WiFi Status
void drivewireFuji::net_get_wifi_status()
{
uint8_t wifiStatus = fnWiFi.connected() ? 3 : 6;
Debug_printv("Fuji cmd: GET WIFI STATUS: %u", wifiStatus);
response.clear();
response.shrink_to_fit();
response += wifiStatus;
}
// Check if Wifi is enabled
void drivewireFuji::net_get_wifi_enabled()
{
uint8_t e = Config.get_wifi_enabled() ? 1 : 0;
Debug_printv("Fuji cmd: GET WIFI ENABLED: %u", e);
response.clear();
response.shrink_to_fit();
response += e;
}
// Mount Server
void drivewireFuji::mount_host()
{
Debug_println("Fuji cmd: MOUNT HOST");
unsigned char hostSlot = fnUartBUS.read();
_fnHosts[hostSlot].mount();
}
// Disk Image Mount
void drivewireFuji::disk_image_mount()
{
// TAPE or CASSETTE handling: this function can also mount CAS and WAV files
// to the C: device. Everything stays the same here and the mounting
// where all the magic happens is done in the drivewireDisk::mount() function.
// This function opens the file, so cassette does not need to open the file.
// Cassette needs the file pointer and file size.
Debug_println("Fuji cmd: MOUNT IMAGE");
uint8_t deviceSlot = fnUartBUS.read();
uint8_t options = fnUartBUS.read(); // DISK_ACCESS_MODE
// TODO: Implement FETCH?
char flag[3] = {'r', 0, 0};
if (options == DISK_ACCESS_MODE_WRITE)
flag[1] = '+';
// A couple of reference variables to make things much easier to read...
fujiDisk &disk = _fnDisks[deviceSlot];
fujiHost &host = _fnHosts[disk.host_slot];
Debug_printf("Selecting '%s' from host #%u as %s on D%u:\n",
disk.filename, disk.host_slot, flag, deviceSlot + 1);
// TODO: Refactor along with mount disk image.
disk.disk_dev.host = &host;
disk.fileh = host.fnfile_open(disk.filename, disk.filename, sizeof(disk.filename), flag);
// We've gotten this far, so make sure our bootable CONFIG disk is disabled
boot_config = false;
// We need the file size for loading XEX files and for CASSETTE, so get that too
disk.disk_size = host.file_size(disk.fileh);
// And now mount it
disk.disk_type = disk.disk_dev.mount(disk.fileh, disk.filename, disk.disk_size);
}
// Toggle boot config on/off, aux1=0 is disabled, aux1=1 is enabled
void drivewireFuji::set_boot_config()
{
// boot_config = cmdFrame.aux1;
// drivewire_complete();
}
// Do DRIVEWIRE copy
void drivewireFuji::copy_file()
{
// uint8_t csBuf[256];
// string copySpec;
// string sourcePath;
// string destPath;
// uint8_t ck;
// FILE *sourceFile;
// FILE *destFile;
// char *dataBuf;
// unsigned char sourceSlot;
// unsigned char destSlot;
// dataBuf = (char *)malloc(532);
// if (dataBuf == nullptr)
// {
// drivewire_error();
// return;
// }
// memset(&csBuf, 0, sizeof(csBuf));
// ck = bus_to_peripheral(csBuf, sizeof(csBuf));
// if (ck != drivewire_checksum(csBuf, sizeof(csBuf)))
// {
// drivewire_error();
// return;
// }
// copySpec = string((char *)csBuf);
// Debug_printf("copySpec: %s\n", copySpec.c_str());
// // Check for malformed copyspec.
// if (copySpec.empty() || copySpec.find_first_of("|") == string::npos)
// {
// drivewire_error();
// return;
// }
// if (cmdFrame.aux1 < 1 || cmdFrame.aux1 > 8)
// {
// drivewire_error();
// return;
// }
// if (cmdFrame.aux2 < 1 || cmdFrame.aux2 > 8)
// {
// drivewire_error();
// return;
// }
// sourceSlot = cmdFrame.aux1 - 1;
// destSlot = cmdFrame.aux2 - 1;
// // All good, after this point...
// // Chop up copyspec.
// sourcePath = copySpec.substr(0, copySpec.find_first_of("|"));
// destPath = copySpec.substr(copySpec.find_first_of("|") + 1);
// // At this point, if last part of dest path is / then copy filename from source.
// if (destPath.back() == '/')
// {
// Debug_printf("append source file\n");
// string sourceFilename = sourcePath.substr(sourcePath.find_last_of("/") + 1);
// destPath += sourceFilename;
// }
// // Mount hosts, if needed.
// _fnHosts[sourceSlot].mount();
// _fnHosts[destSlot].mount();
// // Open files...
// sourceFile = _fnHosts[sourceSlot].file_open(sourcePath.c_str(), (char *)sourcePath.c_str(), sourcePath.size() + 1, "r");
// if (sourceFile == nullptr)
// {
// drivewire_error();
// return;
// }
// destFile = _fnHosts[destSlot].file_open(destPath.c_str(), (char *)destPath.c_str(), destPath.size() + 1, "w");
// if (destFile == nullptr)
// {
// drivewire_error();
// return;
// }
// size_t readCount = 0;
// size_t readTotal = 0;
// size_t writeCount = 0;
// size_t expected = _fnHosts[sourceSlot].file_size(sourceFile); // get the filesize
// bool err = false;
// do
// {
// readCount = fread(dataBuf, 1, 532, sourceFile);
// readTotal += readCount;
// // Check if we got enough bytes on the read
// if(readCount < 532 && readTotal != expected)
// {
// err = true;
// break;
// }
// writeCount = fwrite(dataBuf, 1, readCount, destFile);
// // Check if we sent enough bytes on the write
// if (writeCount != readCount)
// {
// err = true;
// break;
// }
// Debug_printf("Copy File: %d bytes of %d\n", readTotal, expected);
// } while (readTotal < expected);
// if (err == true)
// {
// // Remove the destination file and error
// _fnHosts[destSlot].file_remove((char *)destPath.c_str());
// drivewire_error();
// Debug_printf("Copy File Error! wCount: %d, rCount: %d, rTotal: %d, Expect: %d\n", writeCount, readCount, readTotal, expected);
// }
// else
// {
// drivewire_complete();
// }
// // copyEnd:
// fclose(sourceFile);
// fclose(destFile);
// free(dataBuf);
}
// Mount all
void drivewireFuji::mount_all()
{
bool nodisks = true; // Check at the end if no disks are in a slot and disable config
for (int i = 0; i < 8; i++)
{
fujiDisk &disk = _fnDisks[i];
fujiHost &host = _fnHosts[disk.host_slot];
char flag[3] = {'r', 0, 0};
if (disk.access_mode == DISK_ACCESS_MODE_WRITE)
flag[1] = '+';
if (disk.host_slot != INVALID_HOST_SLOT)
{
nodisks = false; // We have a disk in a slot
if (host.mount() == false)
{
return;
}
Debug_printf("Selecting '%s' from host #%u as %s on D%u:\n",
disk.filename, disk.host_slot, flag, i + 1);
disk.fileh = host.fnfile_open(disk.filename, disk.filename, sizeof(disk.filename), flag);
if (disk.fileh == nullptr)
{
return;
}
// We've gotten this far, so make sure our bootable CONFIG disk is disabled
boot_config = false;
// We need the file size for loading XEX files and for CASSETTE, so get that too
disk.disk_size = host.file_size(disk.fileh);
// Set the host slot for high score mode
// TODO: Refactor along with mount disk image.
disk.disk_dev.host = &host;
// And now mount it
disk.disk_type = disk.disk_dev.mount(disk.fileh, disk.filename, disk.disk_size);
}
}
if (nodisks)
{
// No disks in a slot, disable config
boot_config = false;
}
}
// Set boot mode
void drivewireFuji::set_boot_mode()
{
// insert_boot_device(cmdFrame.aux1);
// boot_config = true;
// drivewire_complete();
}
char *_generate_appkey_filename(appkey *info)
{
static char filenamebuf[30];
snprintf(filenamebuf, sizeof(filenamebuf), "/FujiNet/%04hx%02hhx%02hhx.key", info->creator, info->app, info->key);
return filenamebuf;
}
/*
Opens an "app key". This just sets the needed app key parameters (creator, app, key, mode)
for the subsequent expected read/write command. We could've added this information as part
of the payload in a WRITE_APPKEY command, but there was no way to do this for READ_APPKEY.
Requiring a separate OPEN command makes both the read and write commands behave similarly
and leaves the possibity for a more robust/general file read/write function later.
*/
void drivewireFuji::open_app_key()
{
// Debug_print("Fuji cmd: OPEN APPKEY\n");
// // The data expected for this command
// uint8_t ck = bus_to_peripheral((uint8_t *)&_current_appkey, sizeof(_current_appkey));
// if (drivewire_checksum((uint8_t *)&_current_appkey, sizeof(_current_appkey)) != ck)
// {
// drivewire_error();
// return;
// }
// // We're only supporting writing to SD, so return an error if there's no SD mounted
// if (fnSDFAT.running() == false)
// {
// Debug_println("No SD mounted - returning error");
// drivewire_error();
// return;
// }
// // Basic check for valid data
// if (_current_appkey.creator == 0 || _current_appkey.mode == APPKEYMODE_INVALID)
// {
// Debug_println("Invalid app key data");
// drivewire_error();
// return;
// }
// Debug_printf("App key creator = 0x%04hx, app = 0x%02hhx, key = 0x%02hhx, mode = %hhu, filename = \"%s\"\n",
// _current_appkey.creator, _current_appkey.app, _current_appkey.key, _current_appkey.mode,
// _generate_appkey_filename(&_current_appkey));
// drivewire_complete();
}
/*
The app key close operation is a placeholder in case we want to provide more robust file
read/write operations. Currently, the file is closed immediately after the read or write operation.
*/
void drivewireFuji::close_app_key()
{
Debug_print("Fuji cmd: CLOSE APPKEY\n");
_current_appkey.creator = 0;
_current_appkey.mode = APPKEYMODE_INVALID;
// drivewire_complete();
}
/*
Write an "app key" to SD (ONLY!) storage.
*/
void drivewireFuji::write_app_key()
{
// uint16_t keylen = UINT16_FROM_HILOBYTES(cmdFrame.aux2, cmdFrame.aux1);
// Debug_printf("Fuji cmd: WRITE APPKEY (keylen = %hu)\n", keylen);
// // Data for FUJICMD_WRITE_APPKEY
// uint8_t value[MAX_APPKEY_LEN];
// uint8_t ck = bus_to_peripheral((uint8_t *)value, sizeof(value));
// if (drivewire_checksum((uint8_t *)value, sizeof(value)) != ck)
// {
// drivewire_error();
// return;
// }
// // Make sure we have valid app key information
// if (_current_appkey.creator == 0 || _current_appkey.mode != APPKEYMODE_WRITE)
// {
// Debug_println("Invalid app key metadata - aborting");
// drivewire_error();
// return;
// }
// // Make sure we have an SD card mounted
// if (fnSDFAT.running() == false)
// {
// Debug_println("No SD mounted - can't write app key");
// drivewire_error();
// return;
// }
// char *filename = _generate_appkey_filename(&_current_appkey);
// // Reset the app key data so we require calling APPKEY OPEN before another attempt
// _current_appkey.creator = 0;
// _current_appkey.mode = APPKEYMODE_INVALID;
// Debug_printf("Writing appkey to \"%s\"\n", filename);
// // Make sure we have a "/FujiNet" directory, since that's where we're putting these files
// fnSDFAT.create_path("/FujiNet");
// FILE *fOut = fnSDFAT.file_open(filename, "w");
// if (fOut == nullptr)
// {
// Debug_printf("Failed to open/create output file: errno=%d\n", errno);
// drivewire_error();
// return;
// }
// size_t count = fwrite(value, 1, keylen, fOut);
// int e = errno;
// fclose(fOut);
// if (count != keylen)
// {
// Debug_printf("Only wrote %u bytes of expected %hu, errno=%d\n", count, keylen, e);
// drivewire_error();
// }
// drivewire_complete();
}
/*
Read an "app key" from SD (ONLY!) storage
*/
void drivewireFuji::read_app_key()
{
// Debug_println("Fuji cmd: READ APPKEY");
// // Make sure we have an SD card mounted
// if (fnSDFAT.running() == false)
// {
// Debug_println("No SD mounted - can't read app key");
// drivewire_error();
// return;
// }
// // Make sure we have valid app key information
// if (_current_appkey.creator == 0 || _current_appkey.mode != APPKEYMODE_READ)
// {
// Debug_println("Invalid app key metadata - aborting");
// drivewire_error();
// return;
// }
// char *filename = _generate_appkey_filename(&_current_appkey);
// Debug_printf("Reading appkey from \"%s\"\n", filename);
// FILE *fIn = fnSDFAT.file_open(filename, "r");
// if (fIn == nullptr)
// {
// Debug_printf("Failed to open input file: errno=%d\n", errno);
// drivewire_error();
// return;
// }
// struct
// {
// uint16_t size;
// uint8_t value[MAX_APPKEY_LEN];
// } __attribute__((packed)) response;
// memset(&response, 0, sizeof(response));
// size_t count = fread(response.value, 1, sizeof(response.value), fIn);
// fclose(fIn);
// Debug_printf("Read %d bytes from input file\n", count);
// response.size = count;
// bus_to_computer((uint8_t *)&response, sizeof(response), false);
}
// Disk Image Unmount
void drivewireFuji::disk_image_umount()
{
uint8_t deviceSlot = fnUartBUS.read();
Debug_printf("Fuji cmd: UNMOUNT IMAGE 0x%02X\n", deviceSlot);
// Handle disk slots
if (deviceSlot < MAX_DISK_DEVICES)
{
_fnDisks[deviceSlot].disk_dev.unmount();
_fnDisks[deviceSlot].disk_dev.device_active = false;
_fnDisks[deviceSlot].reset();
}
}
// Disk Image Rotate
/*
We rotate disks my changing their disk device ID's. That prevents
us from having to unmount and re-mount devices.
*/
void drivewireFuji::image_rotate()
{
// Debug_println("Fuji cmd: IMAGE ROTATE");
// int count = 0;
// // Find the first empty slot
// while (_fnDisks[count].fileh != nullptr)
// count++;
// if (count > 1)
// {
// count--;
// // Save the device ID of the disk in the last slot
// int last_id = _fnDisks[count].disk_dev.id();
// for (int n = count; n > 0; n--)
// {
// int swap = _fnDisks[n - 1].disk_dev.id();
// Debug_printf("setting slot %d to ID %hx\n", n, swap);
// _drivewire_bus->changeDeviceId(&_fnDisks[n].disk_dev, swap);
// }
// // The first slot gets the device ID of the last slot
// Debug_printf("setting slot %d to ID %hx\n", 0, last_id);
// _drivewire_bus->changeDeviceId(&_fnDisks[0].disk_dev, last_id);
// // Say whatever disk is in D1:
// if (Config.get_general_rotation_sounds())
// {
// for (int i = 0; i <= count; i++)
// {
// if (_fnDisks[i].disk_dev.id() == 0x31)
// {
// say_swap_label();
// say_number(i + 1); // because i starts from 0
// }
// }
// }
// }
}
// This gets called when we're about to shutdown/reboot
void drivewireFuji::shutdown()
{
for (int i = 0; i < MAX_DISK_DEVICES; i++)
_fnDisks[i].disk_dev.unmount();
}
void drivewireFuji::open_directory()
{
Debug_println("Fuji cmd: OPEN DIRECTORY");
uint8_t hostSlot = fnUartBUS.read();
fnUartBUS.readBytes((uint8_t *)&dirpath, 256);
if (_current_open_directory_slot == -1)
{
// See if there's a search pattern after the directory path
const char *pattern = nullptr;
int pathlen = strnlen(dirpath, sizeof(dirpath));
if (pathlen < sizeof(dirpath) - 3) // Allow for two NULLs and a 1-char pattern
{
pattern = dirpath + pathlen + 1;
int patternlen = strnlen(pattern, sizeof(dirpath) - pathlen - 1);
if (patternlen < 1)
pattern = nullptr;
}
// Remove trailing slash
if (pathlen > 1 && dirpath[pathlen - 1] == '/')
dirpath[pathlen - 1] = '\0';
Debug_printf("Opening directory: \"%s\", pattern: \"%s\"\n", dirpath, pattern ? pattern : "");
if (_fnHosts[hostSlot].dir_open(dirpath, pattern, 0))
{
_current_open_directory_slot = hostSlot;
}
}
}
void _set_additional_direntry_details(fsdir_entry_t *f, uint8_t *dest, uint8_t maxlen)
{
// File modified date-time
struct tm *modtime = localtime(&f->modified_time);
modtime->tm_mon++;
modtime->tm_year -= 100;
dest[0] = modtime->tm_year;
dest[1] = modtime->tm_mon;
dest[2] = modtime->tm_mday;
dest[3] = modtime->tm_hour;
dest[4] = modtime->tm_min;
dest[5] = modtime->tm_sec;
// File size
uint32_t fsize = f->size;
dest[6] = (fsize >> 24) & 0xFF;
dest[7] = (fsize >> 16) & 0xFF;
dest[8] = (fsize >> 8) & 0xFF;
dest[9] = fsize & 0xFF;
// File flags
#define FF_DIR 0x01
#define FF_TRUNC 0x02
dest[10] = f->isDir ? FF_DIR : 0;
maxlen -= ADDITIONAL_DETAILS_BYTES; // Adjust the max return value with the number of additional bytes we're copying
if (f->isDir) // Also subtract a byte for a terminating slash on directories
maxlen--;
if (strlen(f->filename) >= maxlen)
dest[11] |= FF_TRUNC;
// File type
dest[12] = MediaType::discover_mediatype(f->filename);
Debug_printf("Addtl: ");
for (int i = 0; i < ADDITIONAL_DETAILS_BYTES; i++)
Debug_printf("%02x ", dest[i]);
Debug_printf("\n");
}
char current_entry[256];
void drivewireFuji::read_directory_entry()
{
uint8_t maxlen = fnUartBUS.read();
uint8_t addtl = fnUartBUS.read();
Debug_printf("Fuji cmd: READ DIRECTORY ENTRY (max=%hu) (addtl=%02x)\n", maxlen, addtl);
memset(current_entry, 0, sizeof(current_entry));
fsdir_entry_t *f = _fnHosts[_current_open_directory_slot].dir_nextfile();
if (f == nullptr)
{
Debug_println("Reached end of of directory");
current_entry[0] = 0x7F;
current_entry[1] = 0x7F;
}
else
{
Debug_printf("::read_direntry \"%s\"\n", f->filename);
int bufsize = sizeof(current_entry);
int fno=0;
// If 0x80 is set on AUX2, send back additional information
if (addtl & 0x80)
{
Debug_printf("Add additional info.\n");
_set_additional_direntry_details(f, (uint8_t *)current_entry, maxlen);
// Adjust remaining size of buffer and file path destination
bufsize = sizeof(dirpath) - ADDITIONAL_DETAILS_BYTES;
fno += ADDITIONAL_DETAILS_BYTES;
}
else
{
bufsize = maxlen;
}
// int filelen = strlcpy(filenamedest, f->filename, bufsize);
int filelen = util_ellipsize(f->filename, ¤t_entry[fno], bufsize);
// Add a slash at the end of directory entries
if (f->isDir && filelen < (bufsize - 2))
{
current_entry[filelen] = '/';
current_entry[filelen + 1] = '\0';
}
}
response.clear();
response.shrink_to_fit();
response = std::string((const char *)current_entry, maxlen);
}
void drivewireFuji::get_directory_position()
{
Debug_println("Fuji cmd: GET DIRECTORY POSITION");
uint16_t pos = _fnHosts[_current_open_directory_slot].dir_tell();
// Return the value we read
fnUartBUS.write(pos << 8);
fnUartBUS.write(pos & 0xFF);
}
void drivewireFuji::set_directory_position()
{
uint8_t h, l;
Debug_println("Fuji cmd: SET DIRECTORY POSITION");
// DAUX1 and DAUX2 hold the position to seek to in low/high order
h = fnUartBUS.read();
l = fnUartBUS.read();
Debug_printf("H: %02x L: %02x", h, l);
uint16_t pos = UINT16_FROM_HILOBYTES(h, l);
bool result = _fnHosts[_current_open_directory_slot].dir_seek(pos);
}
void drivewireFuji::close_directory()
{
Debug_println("Fuji cmd: CLOSE DIRECTORY");
if (_current_open_directory_slot != -1)
_fnHosts[_current_open_directory_slot].dir_close();
_current_open_directory_slot = -1;
}
// Get network adapter configuration
void drivewireFuji::get_adapter_config()
{
Debug_println("Fuji cmd: GET ADAPTER CONFIG");
// Response to FUJICMD_GET_ADAPTERCONFIG
AdapterConfig cfg;
memset(&cfg, 0, sizeof(cfg));
strlcpy(cfg.fn_version, fnSystem.get_fujinet_version(true), sizeof(cfg.fn_version));
if (!fnWiFi.connected())
{
strlcpy(cfg.ssid, "NOT CONNECTED", sizeof(cfg.ssid));
}
else
{
strlcpy(cfg.hostname, fnSystem.Net.get_hostname().c_str(), sizeof(cfg.hostname));
strlcpy(cfg.ssid, fnWiFi.get_current_ssid().c_str(), sizeof(cfg.ssid));
fnWiFi.get_current_bssid(cfg.bssid);
fnSystem.Net.get_ip4_info(cfg.localIP, cfg.netmask, cfg.gateway);
fnSystem.Net.get_ip4_dns_info(cfg.dnsIP);
}
fnWiFi.get_mac(cfg.macAddress);
response.clear();
response.shrink_to_fit();
errorCode = 1;
response = std::string((const char *)&cfg, sizeof(cfg));
}
// Make new disk and shove into device slot
void drivewireFuji::new_disk()
{
Debug_println("Fuji cmd: NEW DISK");
struct
{
unsigned char numDisks;
unsigned char hostSlot;
unsigned char deviceSlot;
char filename[MAX_FILENAME_LEN]; // WIll set this to MAX_FILENAME_LEN, later.