forked from JonathanDotCel/NOTPSXSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgParser.cs
More file actions
881 lines (684 loc) · 25.6 KB
/
Copy pathArgParser.cs
File metadata and controls
881 lines (684 loc) · 25.6 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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using static GlobalState;
public static class GlobalState
{
// Shared args
public static string destDeviceOrIP = ""; // E.g. COM8, /dev/tty.SLAB_USBtoUART, 127.0.0.1, etc
public static UInt32 destTCPPort = 0; // the port we'll connect to the device/emu on
public static Command mainCommand = Command.NOT_SET;
// Generic reusable args.
public static UInt32 argAddr;
public static UInt32 argSize;
public static UInt32 argValue;
public static UInt32 argMemcardNum;
public static string argString = "";
public static string argRegister = "";
public static string argIP; // argIP and argPort used for both local and remote params
public static UInt32 argListenPort = 0; // the listen port we'll use for bridge mode
public static string argFileName; // input or output file
public static byte[] argFileRawBytes; // auto-read file contents for input files
// Older /fast /medi /slow do not need an okay from the PSX
public static SIOBaud pendingBuiltinSpeed = SIOBaud.DEFAULT;
// Newer/custom speeds:
public static bool useCustomBauds = false;
public static UInt32 pendingPSXSpeed = 0;
// If the PSX is already at 518400 for example, and the user
// wants to drop down to 115200
// - set the PC speed to match 518400 so it can communicate with the PSX
// - tell the PSX to drop to 115200
// - switch the PC over to 115200
public static UInt32 pendingPCPreSpeed = 0;
public static UInt32 pendingPCPostSpeed = 0;
// Using Dan's FTDI mod
public static bool enterFTDIMode = false;
// Force gapped datel V2 model via SIO
// (or force it off in the case of homebrew carts triggering auto detect)
public static bool forceFlatMode = false;
public static bool forceGappedMode = false;
// Switch to 2-way monitor after e.g. .exe updload
public static bool monitorComms = false;
// user did like `nops /fast /exe something.exe /slow /m`
// i.e. to upload quickly, then drop back to default comms speed to monitor
public static bool switchBackToSlowModeWhenFinished = false;
// Loaded the serial device or IP from comport.txt
public static bool usingCachedComportOrIP = false;
}
public static class ArgParser
{
// Lil' queue to simplify parsing different arg types
// Pop from index 0, or rase an exception on unknown switches/commands
public class ParserState
{
public List<string> remainingArgs = new List<string>();
// store the '/exe' part of '/exe mything.exe'
// to allow clearer debug logs
public string lastArg = "";
public ParserState(List<string> inArgs)
{
remainingArgs = inArgs;
}
public string Peek(bool storeLastArg = false)
{
if (Empty) return null;
return remainingArgs[0];
}
public string Pop(bool storeLastArg = false)
{
if (Empty) return null;
string ret = Peek();
remainingArgs.RemoveAt(0);
return ret;
}
public string StoredArg => lastArg;
public bool Empty => remainingArgs.Count == 0;
public int ArgsLeft => remainingArgs.Count;
}
/// <summary>
/// Ensure the input args are vaguely sane
/// - Make a list of args
/// - Cross them off one by one as they're valid
/// - Determine COM port
/// - Extra args such as /monitor comms
/// - Grab address values, filename etc
/// </summary>
/// <param name="inArgs">Command line args</param>
/// <returns>True on success</returns>
public static bool VerifyArgs(string[] inArgs)
{
if (inArgs.Length == 0)
{
Header.PrintUsage(false);
return false;
}
ParserState ps = new ParserState(inArgs.ToList());
string upgradeWarning = "\nPlease note that NOPS now expects '/dest' or '/d' before the target serial/TCP device.\n\n"
+ "E.g.:\n"
+ " /dest COM3 instead of: COM3\n"
+ " /d COM3 instead of: COM3\n"
+ " /dest /dev/ttyUSB1234 instead of: /dev/ttyUSB1234\n\n"
+ "Once this is cached in comport.txt you can ignore it.\n";
while (!ps.Empty)
{
string cmdStr = ps.Pop(true);
// check if it's a known arg/switch
Command newCommand = CommandExtensions.CommandFromString(cmdStr);
if (newCommand == Command.NOT_SET)
{
return Utils.Error($"Unknown command: {cmdStr}\n{upgradeWarning}");
}
// We can only have one main command like /exe, /rom, /mcdn, etc
// but we can have multiple switches like /fast /returntoslow /m, etc
if (!newCommand.IsSecondary())
{
// we parsed a new command, but
// do we already have a command?
if (mainCommand != Command.NOT_SET)
{
return Utils.Error($"Please specify only one main command, got {mainCommand.Text()} and {newCommand.Text()}");
}
mainCommand = newCommand;
}
switch (newCommand)
{
default:
break;
//
// Primary Args (main command like /rom, /exe, etc)
//
case Command.SEND_EXE:
case Command.SEND_ROM:
if (!GetInputFileArgAndReadData(ps, ref argFileName, ref argFileRawBytes))
{
return false;
}
continue;
case Command.SEND_BIN:
if (!GetInputFileArgAndReadData(ps, ref argFileName, ref argFileRawBytes))
{
return false;
}
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
continue;
case Command.DUMP:
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
if (!GetHexSizeArg(ps, ref argSize))
{
return false;
}
if (!GetOutputFileArg(ps, ref argFileName))
{
return false;
}
continue;
case Command.JUMP_JMP:
case Command.JUMP_CALL:
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
continue;
case Command.POKE8:
case Command.POKE16:
case Command.POKE32:
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
if (!GetHexValueArg(ps, ref argValue))
{
return false;
}
continue;
case Command.WATCH:
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
if (!GetHexSizeArg(ps, ref argSize))
{
return false;
}
continue;
case Command.RESET:
case Command.PING:
case Command.DEBUG:
// no args.
continue;
case Command.BRIDGE:
case Command.GDB:
if (!GetIPAndPortArg(ps, ref argIP, ref argListenPort))
{
return false;
}
continue;
case Command.HALT:
case Command.CONT:
case Command.REGS:
// no args;
continue;
case Command.SETREG:
if (!GetRegisterArg(ps, ref argRegister))
{
return false;
}
if (!GetHexValueArg(ps, ref argValue))
{
return false;
}
continue;
case Command.HOOKREAD:
case Command.HOOKWRITE:
case Command.HOOKEXEC:
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
continue;
case Command.UNHOOK:
// no args;
continue;
case Command.MCUP:
if (!GetCardNumberArg(ps, ref argMemcardNum))
{
return false;
}
if (!GetInputFileArgAndReadData(ps, ref argFileName, ref argFileRawBytes))
{
return false;
}
continue;
case Command.MCDOWN:
if (!GetCardNumberArg(ps, ref argMemcardNum))
{
return false;
}
if (!GetOutputFileArg(ps, ref argFileName))
{
return false;
}
continue;
case Command.WIPEMEM:
if (!GetHexValueArg(ps, ref argValue))
{
return false;
}
if (!GetHexAddrArg(ps, ref argAddr))
{
return false;
}
continue;
case Command.STDIN:
case Command.TYPESTRING:
if (!GetStringArg(ps, ref argString))
{
return false;
}
continue;
//
// Secondary Args ( /fast /pcbaud /m, etc)
//
case Command.MONITOR:
monitorComms = true;
continue;
case Command.DEST:
if (!GetTargetDeviceArg(ps, ref destDeviceOrIP, ref destTCPPort))
{
return false;
}
continue;
case Command.FAST:
pendingBuiltinSpeed = SIOBaud.FAST;
continue;
case Command.MEDI:
pendingBuiltinSpeed = SIOBaud.MEDI;
continue;
case Command.SLOW:
pendingBuiltinSpeed = SIOBaud.DEFAULT;
continue;
case Command.FTDI:
enterFTDIMode = true;
pendingBuiltinSpeed = SIOBaud.FTDI;
continue;
case Command.RETURNTOSLOW:
switchBackToSlowModeWhenFinished = true;
continue;
case Command.PSXBAUD:
if (!GetPSXBaudArg(ps, ref pendingPSXSpeed))
{
return false;
}
useCustomBauds = true;
continue;
case Command.PCBAUD:
if (!GetPCBaudArg(ps, ref pendingPCPostSpeed))
{
return false;
}
useCustomBauds = true;
continue;
case Command.PCPREBAUD:
if (!GetPCBaudArg(ps, ref pendingPCPreSpeed))
{
return false;
}
useCustomBauds = true;
continue;
case Command.FLAT:
forceFlatMode = true;
continue;
case Command.GAPS:
forceGappedMode = true;
continue;
case Command.VERBOSE:
Log.SetLevel(LogType.Info | LogType.Warning | LogType.Error | LogType.Debug);
continue;
case Command.NOMANIFEST:
GDBServer.DisableManifest();
continue;
} // switch
} // foreach arg
#if DebugArgs
Log.WriteLine("__DEBUG__Argsleft: " + ps.remainingArgs.Count, LogType.Debug);
Log.WriteLine("__DEBUG__TARGDEVICE: " + destDeviceOrIP, LogType.Debug);
Log.WriteLine("__DEBUG__TARGPORT: " + destPortOrNone, LogType.Debug);
Log.WriteLine("__DEBUG__IP:TARGETPORT: " + argIP + ":" + destPortOrNone, LogType.Debug);
Log.WriteLine("__DEBUG__IP:LISTENPORT: " + argIP + ":" + argListenPort, LogType.Debug);
Log.WriteLine("__DEBUG__PENDING_BUILTIN_SPEED: " + pendingBuiltinSpeed, LogType.Debug);
Log.WriteLine("__DEBUG__NEGOTIATE_PSX_SPEED: " + negotiatePSXSpeed, LogType.Debug);
Log.WriteLine("__DEBUG__PENDING_PSX_SPEED: " + pendingPSXSpeed, LogType.Debug);
Log.WriteLine("__DEBUG__PENDING_PC_PRE_SPEED: " + pendingPCPreSpeed, LogType.Debug);
Log.WriteLine("__DEBUG__PENDING_PC_POST_SPEED: " + pendingPCPostSpeed, LogType.Debug);
Log.WriteLine("__DEBUG__FTDI: " + enterFTDIMode, LogType.Debug);
Log.WriteLine("__DEBUG__COMMAND: " + mainCommand, LogType.Debug);
Log.WriteLine("__DEBUG__ADDR: " + argAddr.ToString("X8"), LogType.Debug);
Log.WriteLine("__DEBUG__VALUE: " + argValue.ToString("X8"), LogType.Debug);
Log.WriteLine("__DEBUG__FILENAME: " + argFileName, LogType.Debug);
Log.WriteLine("__DEBUG__INPUTFILE: " + inFile, LogType.Debug);
Log.WriteLine("__DEBUG__stringArg: " + argString, LogType.Debug);
#endif
// there shouldn't be any arguments left!
// (noise on the console, or old style COMx)
if (ps.remainingArgs.Count > 0)
{
for (int i = ps.remainingArgs.Count - 1; i >= 0; i--)
{
Utils.Error("Unknown arg! " + ps.remainingArgs[i]);
}
Log.WriteLine(upgradeWarning);
return false;
}
// Quick warning:
if (useCustomBauds && pendingBuiltinSpeed != SIOBaud.DEFAULT)
{
Utils.Error("Don't mix /fast /medi and /slow with /pcbaud /psxbaud");
return false;
}
// Cache the com port, or load the cached value
if (string.IsNullOrEmpty(destDeviceOrIP))
{
// no dest device, see if we have one cached in comport.txt
string readVal = "";
try
{
if (System.IO.File.Exists("comport.txt"))
{
readVal = File.ReadAllText("comport.txt");
}
}
catch (System.Exception e)
{
Log.WriteLine("Error checking for cached com port...\n" + e, LogType.Error);
}
if (ValidIPAndPort(readVal, ref destDeviceOrIP, ref destTCPPort))
{
Log.WriteLine($"Using address and port: {destDeviceOrIP}:{destTCPPort}");
}
else if (ValidTargetDevice(readVal, ref destDeviceOrIP))
{
Log.WriteLine($"Using device {destDeviceOrIP}");
}
else
{
return Utils.Error($"\nPlease specify a device or COM port in the format:\n /dest {Utils.SampleCOMPortForEnvironment}\nor\n /dest 127.0.0.1:1234\nor\n /dest /dev/ttysomething\netc\n");
}
usingCachedComportOrIP = true;
}
else
{
// port was specified, cache it for later in comport.txt
try
{
string savedTarget = destTCPPort != 0 ? (destDeviceOrIP + ":" + destTCPPort) : destDeviceOrIP;
File.WriteAllText("comport.txt", savedTarget);
}
catch (System.Exception e)
{
Log.WriteLine("Error writing cached com port!\n" + e, LogType.Error);
}
}
// Allow no command only if we're doing /monitor
if (mainCommand == Command.NOT_SET && !monitorComms)
{
return Utils.Error("Please specify a command - e.g. /exe, /rom, /m etc!\n\n");
}
// All done
return true;
} //VerifyArgs
static bool GetInputFileArgAndReadData(ParserState ps, ref string outFileName, ref byte[] outFileBytes)
{
if (ps.Empty)
{
return Utils.Error($"Out of args to read INPUT FILENAME for command {ps.StoredArg}");
}
outFileName = ps.Pop();
try
{
outFileBytes = File.ReadAllBytes(outFileName);
}
catch (System.Exception e)
{
return Utils.Error($"Couldn't open INPUT FILENAME '{outFileName}' for command {ps.StoredArg}. Exception: {e}");
}
return true;
}
static bool GetOutputFileArg(ParserState ps, ref string outFileName)
{
if (ps.Empty)
{
return Utils.Error($"Out of args to read OUTPUT FILENAME for command {ps.StoredArg}");
}
outFileName = ps.Pop();
return true;
}
static bool GetStringArg(ParserState ps, ref string outString)
{
if (ps.Empty)
{
return Utils.Error($"Out of args to read STRING ARG for command {ps.StoredArg}");
}
outString = ps.Pop();
return true;
}
static bool GetRegisterArg(ParserState ps, ref string outReg)
{
if (ps.Empty)
{
return Utils.Error($"Specify a CPU REGISTER for command {ps.StoredArg}.");
}
// TODO: verify register validity?
// TODO: settle on a format: R4 vs V0 vs $V0, etc
outReg = ps.Pop();
return true;
}
static bool GetHexAddrArg(ParserState ps, ref UInt32 outAddr)
{
string errorString = $"Need an ADDRESS param in the format 0x123456 for command {ps.lastArg}";
if (ps.Empty)
{
return Utils.Error(errorString);
}
string val = ps.Pop();
if (!ParseHexa(val, ref outAddr))
{
return Utils.Error(errorString);
}
return true;
}
static bool GetHexSizeArg(ParserState ps, ref UInt32 outSize)
{
string errorString = $"Need a SIZE param in the format 0x123456 for command {ps.lastArg}";
if (ps.Empty)
{
return Utils.Error(errorString);
}
string val = ps.Pop();
if (!ParseHexa(val, ref outSize))
{
return Utils.Error(errorString);
}
return true;
}
static bool GetHexValueArg(ParserState ps, ref UInt32 outSize)
{
string errorString = $"Need a HEX VALUE param in the format 0x123456 for command {ps.lastArg}";
if (ps.Empty)
{
return Utils.Error(errorString);
}
string val = ps.Pop();
if (!ParseHexa(val, ref outSize))
{
return Utils.Error(errorString);
}
return true;
}
static bool GetIPAndPortArg(ParserState ps, ref string outIP, ref UInt32 outPort)
{
if (ps.Empty)
{
return Utils.Error($"Out of args to read IP:PORT for command {ps.StoredArg}. E.g. e.g. 192.168.0.7:1234");
}
string ipPortStr = ps.Pop();
if (!ValidIPAndPort(ipPortStr, ref outIP, ref outPort))
{
return Utils.Error($"Specify an IP:PORT combo for command {ps.StoredArg}");
}
return true;
}
static bool GetTargetDeviceArg(ParserState ps, ref string outDeviceOrIP, ref UInt32 outPortOrNone)
{
if (ps.Empty)
{
return Utils.Error($"Need to specify a target device for /dest like COM3, COM5, IP:PORT, /dev/ttysomething, etc");
}
string dev = ps.Pop();
if (ValidIPAndPort(dev, ref outDeviceOrIP, ref outPortOrNone))
{
// 127.0.0.1:1234 etc
return true;
}
else if (ValidTargetDevice(dev, ref destDeviceOrIP))
{
// COM5 or /dev/uart_thing/
return true;
}
return Utils.Error($"Need to specify a target device for /dest like COM3, COM5, IP:PORT, /dev/ttysomething, etc");
}
static bool GetCardNumberArg(ParserState ps, ref UInt32 outCardNumber)
{
if (ps.Empty)
{
return Utils.Error($"Out of args to read MEMORY CARD NUMBER for command {ps.StoredArg}. E.g. 0 or 1");
}
string cardNum = ps.Pop();
if (cardNum != "0" && cardNum != "1")
{
return Utils.Error($"Need a MEMORY CARD NUMBER for command {ps.StoredArg}. E.g. 0 or 1");
}
outCardNumber = UInt32.Parse(cardNum);
return true;
}
static bool GetPSXBaudArg(ParserState ps, ref UInt32 outBaud)
{
string errorString = $"Need a PSX BAUD RATE for command {ps.StoredArg}. E.g. 115200";
if (ps.Empty)
{
return Utils.Error(errorString);
}
string baudStr = ps.Pop();
UInt32 parsedSpeed = 0;
if (!UInt32.TryParse(baudStr, out parsedSpeed))
{
return Utils.Error(errorString);
}
bool isValid = PSXBauds.IsValidPSXBaud(parsedSpeed);
if (!isValid)
{
string baudsError = PSXBauds.GetBaudsErrorString();
return Utils.Error(baudsError);
}
outBaud = parsedSpeed;
return true;
}
static bool GetPCBaudArg(ParserState ps, ref UInt32 outBaud)
{
string errorString = $"Need a PC BAUD RATE for command {ps.StoredArg}. E.g. 115200";
if (ps.Empty)
{
return Utils.Error(errorString);
}
string baudStr = ps.Pop();
UInt32 parsedSpeed = 0;
if (!UInt32.TryParse(baudStr, out parsedSpeed))
{
return Utils.Error(errorString);
}
outBaud = parsedSpeed;
return true;
}
/// <summary>
/// Grabs a 32 bit hex value from a string
/// Annoyingly required to bridge UInt32.TryParse
/// freaking out at "0x"
/// </summary>
public static bool ParseHexa(string inString, ref UInt32 outVal)
{
try
{
// Whatever's left should be the address
outVal = (UInt32)Convert.ToInt32(inString, 16);
}
catch (Exception)
{
return false;
}
Log.Write(" - Parsed hexa: 0x" + outVal.ToString("X8") + "\n", LogType.Debug);
return true;
}
/// <summary>
/// Check if we have something in the format addr:port or ip:port
/// Hint: just lookin' for that ":"
/// </summary>
/// <param name="inAddress"></param>
/// <returns>yay or nay</returns>
/// TODO: this will not play nice with ipv6
public static bool ValidIPAndPort(string inAddress, ref string outAddr, ref UInt32 outPort)
{
// no separator, move on
if (inAddress.IndexOf(":") == -1) return false;
string[] parts = inAddress.Split(':');
// too many bits
if (parts.Length != 2) return false;
if (parts[0].Length == 0) return false;
if (parts[1].Length == 0) return false;
// what about the start bit
// is it 4 numbers separated by 3 dots?
string[] bytes = parts[0].Split('.');
if (bytes.Length != 4) return false;
for (int i = 0; i < 4; i++)
{
if (!UInt32.TryParse(bytes[i], out _))
{
return false;
}
}
// First bit looks like an IP
// Does the last bit look like a uint?
try
{
outPort = UInt32.Parse(parts[1]);
}
catch (System.Exception)
{
return false;
}
outAddr = parts[0];
// Guess it's valid
return true;
}
/// <summary>
/// Verify that the port argument is (potentially) valid
/// </summary>
/// <param name="inDevice">COM5, /dev/ttyUSB0, etc</param>
/// <returns>Does it look potentially valid?</returns>
public static bool ValidTargetDevice(string inDevice, ref string outTargetDevice)
{
switch (Utils.CurrentOS)
{
// A windowsy sort of affair
default:
if (inDevice.ToUpperInvariant().StartsWith("COM"))
goto yay;
break;
// Mac
case PlatformID.MacOSX:
if (inDevice.ToLowerInvariant().StartsWith("/dev/cu") | inDevice.ToLowerInvariant().StartsWith("/dev/tty"))
goto yay;
break;
// Some kinda POSIXy environment
// Allow any tty device as *nix users could be using all sorts of pipes n redirects
case (PlatformID)128:
case PlatformID.Unix:
if (inDevice.ToLowerInvariant().StartsWith("/dev/tty"))
goto yay;
break;
}
// nay
return false;
yay:;
outTargetDevice = inDevice;
return true;
}
}