forked from Larswad/uno2iec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
506 lines (438 loc) · 16 KB
/
interface.cpp
File metadata and controls
506 lines (438 loc) · 16 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
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
#include "interface.hpp"
#include "d64driver.hpp"
#include "t64driver.hpp"
#include "m2idriver.hpp"
#include <QStringList>
#include <logger.hpp>
#include <QDir>
using namespace Logging;
#define CBM_BACK_ARROW 95
#define CBM_EXCLAMATION '!'
#define MAX_BYTES_PER_REQUEST 160
namespace {
const QString FAC_IFACE("IFACE");
// The previous cmd is copied to this string:
//char oldCmdStr[IEC::ATN_CMD_MAX_LENGTH];
const QStringList s_IOErrorMessages = QStringList()
<< "00,OK"
<< "21,READ ERROR"
<< "26,WRITE PROTECT ON"
<< "33,SYNTAX ERROR"
<< "62,FILE NOT FOUND"
<< "63,FILE EXISTS"
<< "73,UNO2IEC DOS V0.1"
<< "74,DRIVE NOT READY"
<< "75,RPI SERIAL ERR.";
const QString s_unknownMessage = "99, UNKNOWN ERROR";
const QString s_errorEnding = ",00,00";
}
Interface::Interface(QextSerialPort& port)
: m_currFileDriver(0), m_port(port), m_queuedError(ErrOK), m_openState(O_NOTHING), m_pListener(0)
{
m_fsList.append(&m_native);
m_fsList.append(&m_d64);
m_fsList.append(&m_t64);
// m_fsList.append(&m_m2i);
m_currFileDriver = &m_native;
} // ctor
void Interface::setImageFilters(const QString& filters, bool showDirs)
{
m_native.setListingFilters(filters, showDirs);
} // setImageFilters
void Interface::reset()
{
m_currFileDriver = &m_native;
m_queuedError = ErrOK;
m_openState = O_NOTHING;
m_dirListing.empty();
m_lastCmdString.clear();
foreach(FileDriverBase* fs, m_fsList)
fs->closeHostFile(); // TODO: Better with a reset or init method on all file systems.
} // reset
void Interface::setMountNotifyListener(IFileOpsNotify* pListener)
{
m_pListener = pListener;
} // setMountNotifyListener
// This function crops cmd string of initial @ or until :
// Returns true if @ was detected
bool Interface::removeFilePrefix(QString& cmd)
{
bool ret = false;
// crop all from ':' and afterwards.
int cropPos = cmd.indexOf(QChar(':'));
if(cmd.at(0) == QChar('@')) {
ret = true;
if(-1 == cropPos)
cropPos = 0;
}
if(-1 not_eq cropPos)
cmd = cmd.mid(cropPos + 1);
return ret;
} // removeFilePrefix
// Parse LOAD command, open either file/directory/d64/t64
//
void Interface::openFile(const QString& cmdString, bool localImageSelection)
{
QString cmd(cmdString);
if(localImageSelection) {// for this we have to fall back to nativeFS driver first.
m_currFileDriver->closeHostFile();
m_currFileDriver = &m_native;
}
// fall back result
m_openState = O_NOTHING;
// Check double back arrow first
if((CBM_BACK_ARROW == cmd.at(0).toLatin1()) and (CBM_BACK_ARROW == cmd.at(1))) {
// reload sdcard and send info
m_currFileDriver = &m_native;
m_openState = m_currFileDriver->supportsMediaInfo() ? O_INFO : O_NOTHING;
if(0 not_eq m_pListener)
m_pListener->imageUnmounted();
Log(FAC_IFACE, "Going back to NativeFS and sending media info.", info);
}
else if((CBM_EXCLAMATION == cmd.at(0).toLatin1()) and (CBM_EXCLAMATION == cmd.at(1))) {
// to get the media info for any OTHER media, the '!!' should be used on the CBM side.
// whatever file system we have active, check if it supports media info.
m_openState = m_currFileDriver->supportsMediaInfo() ? O_INFO : O_NOTHING;
}
// Commented out here: Not applicable on pi really. However, some kind of sane check of fs might be done anyway...hmmm.
// else if(!sdCardOK or !(fatGetStatus() & FAT_STATUS_OK)) {
// // User tries to open stuff and there is a problem. Status is fail
// m_queuedError = ErrDriveNotReady;
// m_currFileDriver = 0;
// }
else if(cmd.at(0) == QChar('$')) {
// Send directory listing of current dir
m_openState = O_DIR;
}
else if(CBM_BACK_ARROW == cmd.at(0)) {
// Exit current file format or cd..
if(m_currFileDriver == &m_native) {
m_currFileDriver->setCurrentDirectory("..");
if(0 not_eq m_pListener) // notify UI listener of change.
m_pListener->directoryChanged(QDir::currentPath());
m_openState = O_DIR;
}
else if(0 not_eq m_currFileDriver) {
// We are in some other state, exit to FAT and show current dir
m_currFileDriver = &m_native;
m_openState = O_DIR;
if(0 not_eq m_pListener)
m_pListener->imageUnmounted();
}
}
else {
// It was not any special command, remove eventual CBM dos prefix
if(removeFilePrefix(cmd)) {
// @ detected, this means save with replace:
m_openState = O_SAVE_REPLACE;
}
else {
// open file depending on interface state
if(m_currFileDriver == &m_native) {
// Exchange 0xFF with tilde to allow shortened long filenames
// HMM: This is DOS mumble jumble, we're on a linux FS...will not work.
cmd.replace(QChar(0xFF), "~");
// Try if cd works, then try open as file and if none of these ok...then give up
if(m_native.setCurrentDirectory(cmd)) {
Log(FAC_IFACE, QString("Changed to native FS directory: %1").arg(cmd), success);
if(0 not_eq m_pListener) // notify UI listener of change.
m_pListener->directoryChanged(QDir::currentPath());
m_openState = O_DIR;
}
else if(m_native.openHostFile(cmd)) {
// File opened, investigate filetype
QList<FileDriverBase*>::iterator i;
for(i = m_fsList.begin(); i < m_fsList.end(); ++i) {
// if extension matches last three characters in any file system, then we set that filesystem into use.
if(!(*i)->extension().isEmpty() and cmd.endsWith((*i)->extension(), Qt::CaseInsensitive)) {
m_currFileDriver = *i;
break;
}
}
if(i not_eq m_fsList.end()) {
m_native.closeHostFile();
Log(FAC_IFACE, QString("Trying image mount using driver: %1").arg(m_currFileDriver->extension()), info);
// file extension matches, change interface state
// call new format's reset
if(m_currFileDriver->openHostFile(cmd)) {
// see if this format supports listing, if not we're just opening as a file.
if(!m_currFileDriver->supportsListing())
m_openState = O_FILE;
else {
// otherwise we're in directory mode now on this selected file system image.
m_openState = O_DIR;
// Notify UI listener if attached.
if(0 not_eq m_pListener)
m_pListener->imageMounted(cmd, m_currFileDriver);
}
Log(FAC_IFACE, QString("Mount OK of image: %1").arg(m_currFileDriver->openedFileName()), success);
}
else {
// Error initializing driver, back to native file system.
Log(FAC_IFACE, QString("Error initializing driver for FS.ext: %1. Going back to native.").arg(m_currFileDriver->extension()), error);
m_currFileDriver = &m_native;
m_openState = O_FILE_ERR;
}
}
else { // No specific file format for this file, stay in FAT and send as straight .PRG
Log(FAC_IFACE, QString("No specific file format for the names extension: %1, assuming .PRG in native file mode.").arg(cmd), info);
m_openState = O_FILE;
}
}
else { // File not found, giveup.
Log(FAC_IFACE, QString("File %1 not found. Returning FNF to CBM.").arg(cmd), warning);
m_openState = O_NOTHING;
}
}
else if(0 not_eq m_currFileDriver) {
// Call file format's own open command
Log(FAC_IFACE, QString("Trying open FS file: %1 on FS: %2").arg(cmd).arg(m_currFileDriver->extension()), info);
if(m_currFileDriver->fopen(cmd))
m_openState = O_FILE;
else // File not found
m_openState = O_NOTHING;
}
}
}
if(!localImageSelection) {
// Remember last cmd string.
m_lastCmdString = cmd;
if(O_INFO == m_openState or O_DIR == m_openState)
buildDirectoryOrMediaList();
}
} // openFile
void Interface::processOpenCommand(const QString& cmd, bool localImageSelectionMode)
{
// Request: <channel>|<command string>
Log(FAC_IFACE, QString("Open Request, cmd: %1").arg(cmd), info);
QStringList params(cmd.split('|'));
if(params.count() < 2) // enough params?
m_queuedError = ErrSerialComm;
else {
uchar chan = params.at(0).toInt();
// Are we addressing the command channel?
if(CMD_CHANNEL == chan) {
// command channel command
// (note: if any previous openfile command has given us an error, the 'current' file system to use is not defined and
// therefore the command will fail, we don't even have the native fs to use for the open command).
if(0 not_eq m_currFileDriver) {
m_queuedError = m_currFileDriver->cmdChannel(params.at(1));
}
else
m_queuedError = ErrDriveNotReady;
if(!localImageSelectionMode) {
// Response: ><code><CR>
// The code return is according to the values of the IOErrorMessage enum.
// send back m_queuedError to uno.
QByteArray data;
data.append('>');
data.append((char)m_queuedError);
data.append('\r');
m_port.write(data);
m_port.flush();
Log(FAC_IFACE, QString("CmdChannel_Response code: %1").arg(QString::number(m_queuedError)), m_queuedError == ErrOK ? success : error);
}
}
else {
// ...otherwise it was a open file command.
m_openState = O_NOTHING;
openFile(params.at(1), localImageSelectionMode);
// if it was not only a local "UI" operation, we need to return some response to client.
if(!localImageSelectionMode) {
// Response: ><code><CR>
// The code return is according to the values of the IOErrorMessage enum.
QByteArray data;
data.append('>');
data.append((char)m_openState);
data.append('\r');
m_port.write(data);
m_port.flush();
bool fail = m_openState == O_NOTHING or m_openState == O_FILE_ERR;
Log(FAC_IFACE, QString("Open_Response code: %1").arg(QString::number(m_openState)), fail ? error : success);
// notify UI of opened file name and size.
if(O_FILE == m_openState and 0 not_eq m_pListener)
m_pListener->fileLoading(m_currFileDriver->openedFileName(), m_currFileDriver->openedFileSize());
}
}
}
} // processOpenCommand
void Interface::processCloseCommand()
{
QString name = m_currFileDriver->openedFileName();
QByteArray data;
data.append('N').append((char)name.length()).append(name);
m_port.write(data);
m_port.flush();
if(0 not_eq m_pListener) // notify UI listener of change.
m_pListener->fileClosed(name);
Log(FAC_IFACE, QString("Returning last opened file name: %1").arg(name), info);
} // processCloseCommand
void Interface::processGetOpenFileSize()
{
ushort size = m_currFileDriver->openedFileSize();
QByteArray data;
data.append('S').append(size >> 8).append(size bitand 0xff);
m_port.write(data.data(), data.size());
m_port.flush();
Log(FAC_IFACE, QString("Returning file size: %1").arg(QString::number(size)), info);
} // processGetOpenedFileSize
void Interface::processLineRequest()
{
if(O_INFO == m_openState or O_DIR == m_openState) {
if(m_dirListing.isEmpty()) {
// last line was produced. Send back the ending char.
m_port.write("l");
m_port.flush();
Log(FAC_IFACE, "Last directory line written to arduino.", success);
}
else {
m_port.write(m_dirListing.first().data(), m_dirListing.first().size());
m_port.flush();
m_dirListing.removeFirst();
}
}
else {
// TODO: This is a strange error state. Maybe we should return something to CBM here.
Log(FAC_IFACE, "Strange state.", error);
}
} // processOpenCommand
void Interface::processReadFileRequest(void)
{
QByteArray data;
uchar count;
bool atEOF = false;
for(count = 0; count < MAX_BYTES_PER_REQUEST and not atEOF; ++count) {
data.append(m_currFileDriver->getc());
atEOF = m_currFileDriver->isEOF();
}
// prepend whatever count we got.
data.prepend(count);
// If we reached end of file, head byte in answer indicates with 'E' instead of 'B'.
data.prepend(atEOF ? 'E' : 'B');
m_port.write(data.data(), data.size());
m_port.flush();
} // processReadFileRequest
void Interface::processWriteFileRequest(uchar theByte)
{
m_currFileDriver->putc(theByte);
} // processReadFileRequest
void Interface::processErrorStringRequest(IOErrorMessage code)
{
// the return message begins with ':' for sync.
QByteArray retStr(1, ':');
retStr.append(code >= s_IOErrorMessages.count() ? s_unknownMessage : s_IOErrorMessages.at(code));
// append the common ending.
retStr.append(s_errorEnding);
// terminate with CR.
retStr.append('\r');
m_port.write(retStr);
m_port.flush();
} // processErrorStringRequest
void Interface::send(short lineNo, const QString& text)
{
QByteArray line(text.toLocal8Bit());
// the line number is included with the line itself. It goes in with lobyte,hibyte.
line.prepend(uchar((lineNo bitand 0xFF00) >> 8));
line.prepend(uchar(lineNo bitand 0xFF));
// length of it all is the first byte.
line.prepend((uchar)text.size() + 2);
// add the response byte.
line.prepend('L');
// add it to the total dirlisting array.
m_dirListing.append(line);
} // send
void Interface::buildDirectoryOrMediaList()
{
m_dirListing.clear();
if(O_DIR == m_openState) {
Log(FAC_IFACE, QString("Producing directory listing for FS: \"%1\"...").arg(m_currFileDriver->extension()), info);
if(!m_currFileDriver->sendListing(*this))
Log(FAC_IFACE, QString("Directory listing indicated error. Still sending: %1 chars").arg(QString::number(m_dirListing.length())), warning);
else
Log(FAC_IFACE, QString("Directory listing ok (%1 lines). Ready waiting for line requests from arduino.").arg(m_dirListing.count()), success);
}
else if(O_INFO == m_openState) {
Log(FAC_IFACE, QString("Producing media info for FS: \"%1\"...").arg(m_currFileDriver->extension()), info);
if(!m_currFileDriver->sendMediaInfo(*this))
Log(FAC_IFACE, QString("Media info listing indicated error. Still sending: %1 chars").arg(QString::number(m_dirListing.length())), warning);
else
Log(FAC_IFACE, QString("Media info listing ok (%1 lines). Ready waiting for line requests from arduino.").arg(m_dirListing.count()), success);
}
} // buildDirectoryOrMediaList
bool Interface::changeNativeFSDirectory(const QString& newDir)
{
return m_native.setCurrentDirectory(newDir);
} // changeNativeFSDirectory
////////////////////////////////////////////////////////////////////////////////
//
// File formats definition
//
/*
typedef void(*PFUNC_SEND_LINE)(short line_no, unsigned char len, char *text);
typedef void(*PFUNC_SEND_LISTING)(PFUNC_SEND_LINE send_line);
typedef void(*PFUNC_VOID_VOID)(void);
typedef unsigned char(*PFUNC_UCHAR_VOID)(void);
typedef char(*PFUNC_CHAR_VOID)(void);
typedef unsigned char(*PFUNC_UCHAR_CHAR)(char);
typedef void(*PFUNC_VOID_CSTR)(char *s);
typedef unsigned char(*PFUNC_UCHAR_CSTR)(char *s);
#define FILE_FORMATS 5
struct file_format_struct {
char extension[3]; // DOS extension of file format
PFUNC_UCHAR_CSTR init; // intitialize driver, cmd_str is parameter,
// must return true if OK
PFUNC_SEND_LISTING send_listing; // Send listing function.
PFUNC_UCHAR_CSTR cmd; // command channel command, must return ERR NUMBER!
PFUNC_UCHAR_CSTR open; // open file
PFUNC_UCHAR_CSTR newfile;// create new empty file
PFUNC_CHAR_VOID getc; // get char from open file
PFUNC_UCHAR_VOID eof; // returns true if EOF
PFUNC_UCHAR_CHAR putc; // write char to open file, return false if failure
PFUNC_UCHAR_VOID close; // close file
};
*/
/*
const struct file_format_struct file_format[FILE_FORMATS] = {
{{0,0,0}, &dummy_2, &fat_send_listing, &fat_cmd,
&fatFopen, &fat_newfile, &fatFgetc, &fatFeof, &fatFputc, &fatFclose},
{{'D','6','4'}, &D64_reset, &D64_send_listing, &dummy_cmd,
&D64_fopen, &dummy_no_newfile, &D64_fgetc, &D64_feof, &dummy_1, &D64_fclose},
{{'M','2','I'}, &M2I_init, &M2I_send_listing, &M2I_cmd,
&M2I_open, &M2I_newfile, &M2I_getc, &M2I_eof, &M2I_putc, &M2I_close},
{{'T','6','4'}, &T64_reset, &T64_send_listing, &dummy_cmd,
&T64_fopen, &dummy_no_newfile, &T64_fgetc, &T64_feof, &dummy_1, &T64_fclose},
{{'P','0','0'}, &P00_reset, 0, &dummy_cmd,
&dummy_2, &dummy_no_newfile, &fatFgetc, &fatFeof, &dummy_1, &close_to_fat}
};
*/
/*
const char pstr_file_err[] = "ERROR: FILE FORMAT";
void send_file_err(void (*send_line)(word lineNo, byte len, char* text))
{
char buffer[19];
memcpy(buffer, pstr_file_err, 18);
(send_line)(0, 18, buffer);
} // send_file_err
*/
////////////////////////////////////////////////////////////////////////////////
//
// Dummy functions for unused function pointers
//
/*
byte dummy_1(char c)
{
return true;
}
byte dummy_2(char *s)
{
return true;
}
byte dummy_cmd(char *s)
{
return ErrWriteProtectOn;
}
byte dummy_no_newfile(char *s)
{
return false;
}
*/