forked from FujiNetWIFI/fujinet-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrs232Fuji.cpp
More file actions
418 lines (378 loc) · 13 KB
/
rs232Fuji.cpp
File metadata and controls
418 lines (378 loc) · 13 KB
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
#ifdef BUILD_RS232
#include "rs232Fuji.h"
#include "network.h"
#include "fnSystem.h"
#include "fnConfig.h"
#include "fsFlash.h"
#include "fnWiFi.h"
#include "utils.h"
#include "compat_string.h"
#include "fuji_endian.h"
#define IMAGE_EXTENSION ".img"
#define LOBBY_URL "tnfs://tnfs.fujinet.online/MSDOS/lobby.img"
#ifndef ESP_PLATFORM // why ESP does not like it? it throws a linker error undefined reference to 'basename'
#include <libgen.h>
#endif /* ESP_PLATFORM */
#ifndef ESP_PLATFORM // why ESP does not like it? it throws a linker error undefined reference to 'basename'
#include <libgen.h>
#endif /* ESP_PLATFORM */
rs232Fuji platformFuji;
fujiDevice *theFuji = &platformFuji;
rs232Network rs232NetDevs[MAX_NETWORK_DEVICES];
rs232Fuji::rs232Fuji() : fujiDevice(MAX_DISK_DEVICES, IMAGE_EXTENSION, LOBBY_URL)
{}
// Initializes base settings and adds our devices to the RS232 bus
void rs232Fuji::setup()
{
// set up Fuji device
populate_slots_from_config();
insert_boot_device(Config.get_general_boot_mode(), MEDIATYPE_UNKNOWN, &bootdisk);
// Disable booting from CONFIG if our settings say to turn it off
boot_config = Config.get_general_config_enabled();
// Add our devices to the RS232 bus
for (int i = 0; i < MAX_DISK_DEVICES; i++)
SYSTEM_BUS.addDevice(&_fnDisks[i].disk_dev,
static_cast<fujiDeviceID_t>(FUJI_DEVICEID_DISK + i));
for (int i = 0; i < MAX_NETWORK_DEVICES; i++)
SYSTEM_BUS.addDevice(&rs232NetDevs[i],
static_cast<fujiDeviceID_t>(FUJI_DEVICEID_NETWORK + i));
}
// Status
void rs232Fuji::rs232_status(FujiStatusReq reqType)
{
transaction_continue(TRANS_STATE::NO_GET);
Debug_println("Fuji cmd: STATUS");
if (reqType == STATUS_MOUNT_TIME)
{
// Return drive slot mount status: 0 if unmounted, otherwise time when mounted
time_t mount_status[MAX_DISK_DEVICES];
int idx;
for (idx = 0; idx < MAX_DISK_DEVICES; idx++)
mount_status[idx] = _fnDisks[idx].disk_dev.mount_time();
transaction_put((uint8_t *) mount_status, sizeof(mount_status), false);
}
else
{
char ret[4] = {0};
Debug_printf("Status for what? %08x\n", reqType);
transaction_put((uint8_t *)ret, sizeof(ret), false);
}
return;
}
// Set SSID
void rs232Fuji::rs232_net_set_ssid(bool save) // was aux1
{
SSIDConfig cfg;
transaction_continue(TRANS_STATE::WILL_GET);
if (!transaction_get((uint8_t *)&cfg, sizeof(cfg)) ||
!fujicore_net_set_ssid_success(cfg.ssid, cfg.password, save))
{
transaction_error();
return;
}
transaction_complete();
}
// Make new disk and shove into device slot
void rs232Fuji::rs232_new_disk()
{
transaction_continue(TRANS_STATE::WILL_GET);
Debug_println("Fuji cmd: NEW DISK");
struct
{
unsigned short numSectors;
unsigned short sectorSize;
unsigned char hostSlot;
unsigned char deviceSlot;
char filename[MAX_FILENAME_LEN]; // WIll set this to MAX_FILENAME_LEN, later.
} newDisk;
// Ask for details on the new disk to create
if (!transaction_get((uint8_t *)&newDisk, sizeof(newDisk)))
{
Debug_print("rs232_new_disk Bad checksum\n");
transaction_error();
return;
}
if (newDisk.deviceSlot >= MAX_DISK_DEVICES || newDisk.hostSlot >= MAX_HOSTS)
{
Debug_print("rs232_new_disk Bad disk or host slot parameter\n");
transaction_error();
return;
}
// A couple of reference variables to make things much easier to read...
fujiDisk &disk = _fnDisks[newDisk.deviceSlot];
fujiHost &host = _fnHosts[newDisk.hostSlot];
disk.host_slot = newDisk.hostSlot;
disk.access_mode = DISK_ACCESS_MODE_WRITE;
strlcpy(disk.filename, newDisk.filename, sizeof(disk.filename));
if (host.file_exists(disk.filename))
{
Debug_printf("rs232_new_disk File exists: \"%s\"\n", disk.filename);
transaction_error();
return;
}
disk.fileh = host.fnfile_open(disk.filename, disk.filename, sizeof(disk.filename), "w");
if (disk.fileh == nullptr)
{
Debug_printf("rs232_new_disk Couldn't open file for writing: \"%s\"\n", disk.filename);
transaction_error();
return;
}
bool ok = disk.disk_dev.write_blank(disk.fileh, newDisk.sectorSize, newDisk.numSectors);
fnio::fclose(disk.fileh);
if (ok == false)
{
Debug_print("rs232_new_disk Data write failed\n");
transaction_error();
return;
}
Debug_print("rs232_new_disk succeeded\n");
transaction_complete();
}
void rs232Fuji::rs232_test()
{
uint8_t buf[512];
transaction_continue(TRANS_STATE::NO_GET);
Debug_printf("rs232_test()\n");
memset(buf, 'A', 512);
transaction_put(buf, 512, false);
}
size_t rs232Fuji::set_additional_direntry_details(fsdir_entry_t *f, uint8_t *dest, uint8_t maxlen)
{
struct {
dirEntryTimestamp modified;
uint32_t size;
uint8_t flags;
uint8_t mediatype;
} __attribute__((packed)) custom_details;
dirEntryDetails details;
details = _additional_direntry_details(f);
custom_details.modified = details.modified;
custom_details.modified.year -= 70;
custom_details.size = htole32(details.size);
custom_details.flags = details.flags;
custom_details.mediatype = details.mediatype;
maxlen -= sizeof(custom_details);
// Subtract a byte for a terminating slash on directories
if (custom_details.flags & DET_FF_DIR)
maxlen--;
if (strlen(f->filename) >= maxlen)
custom_details.flags |= DET_FF_TRUNC;
memcpy(dest, &custom_details, sizeof(custom_details));
return sizeof(custom_details);
}
void rs232Fuji::rs232_process(FujiBusPacket &packet)
{
Debug_println("rs232Fuji::rs232_process() called");
switch (packet.command())
{
case FUJICMD_STATUS:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient status paramaters: %d", packet.paramCount());
transaction_error();
}
else
rs232_status(static_cast<FujiStatusReq>(packet.param(0)));
break;
case FUJICMD_RESET:
fujicmd_reset();
break;
case FUJICMD_SCAN_NETWORKS:
fujicmd_net_scan_networks();
break;
case FUJICMD_GET_SCAN_RESULT:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient scan paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_net_scan_result(packet.param(0));
break;
case FUJICMD_SET_SSID:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient SSID paramaters: %d", packet.paramCount());
transaction_error();
}
else
rs232_net_set_ssid(packet.param(0));
break;
case FUJICMD_GET_SSID:
fujicmd_net_get_ssid();
break;
case FUJICMD_GET_WIFISTATUS:
fujicmd_net_get_wifi_status();
break;
case FUJICMD_MOUNT_HOST:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient mount host paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_mount_host_success(packet.param(0));
break;
case FUJICMD_MOUNT_IMAGE:
if (packet.paramCount() < 2) {
Debug_printv("Insufficient mount image paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_mount_disk_image_success(packet.param(0), (disk_access_flags_t) packet.param(1));
break;
case FUJICMD_OPEN_DIRECTORY:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient open dir paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_open_directory_success(packet.param(0));
break;
case FUJICMD_READ_DIR_ENTRY:
if (packet.paramCount() < 2) {
Debug_printv("Insufficient read dir paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_read_directory_entry(packet.param(0), packet.param(1));
break;
case FUJICMD_CLOSE_DIRECTORY:
fujicmd_close_directory();
break;
case FUJICMD_GET_DIRECTORY_POSITION:
fujicmd_get_directory_position();
break;
case FUJICMD_SET_DIRECTORY_POSITION:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient set dir position paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_set_directory_position(packet.param(0));
break;
case FUJICMD_READ_HOST_SLOTS:
fujicmd_read_host_slots();
break;
case FUJICMD_WRITE_HOST_SLOTS:
fujicmd_write_host_slots();
break;
case FUJICMD_READ_DEVICE_SLOTS:
fujicmd_read_device_slots();
break;
case FUJICMD_WRITE_DEVICE_SLOTS:
fujicmd_write_device_slots();
break;
case FUJICMD_GET_WIFI_ENABLED:
fujicmd_net_get_wifi_enabled();
break;
case FUJICMD_UNMOUNT_IMAGE:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient unmount image paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_unmount_disk_image_success(packet.param(0));
break;
case FUJICMD_GET_ADAPTERCONFIG:
fujicmd_get_adapter_config();
break;
case FUJICMD_GET_ADAPTERCONFIG_EXTENDED:
fujicmd_get_adapter_config_extended();
break;
case FUJICMD_NEW_DISK:
rs232_new_disk();
break;
case FUJICMD_SET_DEVICE_FULLPATH:
if (packet.paramCount() < 3) {
Debug_printv("Insufficient set device fullpath paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_set_device_filename_success(packet.param(0), packet.param(1),
(disk_access_flags_t) packet.param(2));
break;
case FUJICMD_SET_HOST_PREFIX:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient set host prefix paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_set_host_prefix(packet.param(0));
break;
case FUJICMD_GET_HOST_PREFIX:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient get host prefix paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_get_host_prefix(packet.param(0));
break;
case FUJICMD_WRITE_APPKEY:
fujicmd_write_app_key(packet.data() ? packet.data()->size() : 0);
break;
case FUJICMD_READ_APPKEY:
fujicmd_read_app_key();
break;
case FUJICMD_OPEN_APPKEY:
fujicmd_open_app_key();
break;
case FUJICMD_CLOSE_APPKEY:
fujicmd_close_app_key();
break;
case FUJICMD_GET_DEVICE_FULLPATH:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient get device fullpath paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_get_device_filename(packet.param(0));
break;
case FUJICMD_CONFIG_BOOT:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient config boot paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_set_boot_config(packet.param(0));
break;
case FUJICMD_COPY_FILE:
if (packet.paramCount() < 2 || !packet.data().has_value()) {
Debug_printv("Insufficient copy files paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_copy_file_success(packet.param(0), packet.param(1),
packet.dataAsString().value_or(""));
break;
case FUJICMD_MOUNT_ALL:
fujicmd_mount_all_success();
break;
case FUJICMD_SET_BOOT_MODE:
if (packet.paramCount() < 1) {
Debug_printv("Insufficient set boot mode paramaters: %d", packet.paramCount());
transaction_error();
}
else
fujicmd_set_boot_mode(packet.param(0), MEDIATYPE_UNKNOWN, &bootdisk);
break;
case FUJICMD_DEVICE_READY:
Debug_printf("FUJICMD DEVICE TEST\n");
rs232_test();
break;
case FUJICMD_GENERATE_GUID:
fujicmd_generate_guid();
break;
default:
transaction_error();
}
}
std::optional<std::vector<uint8_t>> rs232Fuji::fujicore_read_app_key()
{
auto result = fujiDevice::fujicore_read_app_key();
if (result)
{
uint16_t len = htole16(result->size());
result->resize(result->size() + sizeof(len), 0);
const uint8_t *len_bytes = reinterpret_cast<const uint8_t*>(&len);
result->insert(result->begin(), len_bytes, len_bytes + sizeof(len));
}
return result;
}
#endif /* BUILD_RS232 */