-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
3219 lines (2993 loc) · 106 KB
/
main.c
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
/*********************************************************************
* *
* Copyright (C) 2022 SWS Inc. *
* *
* All rights reserved *
* *
**********************************************************************
* *
* SEGGER RTT * Real Time Transfer for LAUTERBACH TRACE32 *
* *
**********************************************************************
*/
/*********************************************************************
*
* Include Section
*
**********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>
#ifdef __linux__
#include <getopt.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <time.h>
#include <winsock2.h>
#include "getopt.h"
#endif
#include "t32.h"
/*********************************************************************
*
* Defines, fixed
*
**********************************************************************
*/
#define BEL 0x07
#define BS 0x08
#define TAB 0x09
#define LF 0x0A
#define VT 0x0B
#define FF 0x0C
#define CR 0x0D
#define SO 0x0E
#define SI 0x0F
#define CAN 0x18
#define SUB 0x1A
#define ESC 0x1B
#define SS2 0x8E
#define SS3 0x8F
#define DCS 0x90
#define CSI 0x9B
#define ST 0x9C
#define OSC 0x9D
#define PM 0x9E
#define APC 0x9F
//
// RTT CB offset calc
//
#define RTTCB_SIZEOF_BASE (0x00)
#define RTTCB_SIZEOF_ACID (0x10)
#define RTTCB_SIZEOF_MAXNUMUPBUFFERS (0x04)
#define RTTCB_SIZEOF_MAXNUMDOWNBUFFERS (0x04)
#define RTTCB_SIZEOF_AUP (0x18)
#define RTTCB_SIZEOF_ADOWN (0x18)
#define RTTCB_SIZEOF_SNAME (0x04)
#define RTTCB_SIZEOF_PBUFFER (0x04)
#define RTTCB_SIZEOF_SIZEOFBUFFER (0x04)
#define RTTCB_SIZEOF_WROFF (0x04)
#define RTTCB_SIZEOF_RDOFF (0x04)
#define RTTCB_SIZEOF_FLAGS (0x04)
#define RTTCB_SIZEOF_AUP_INDEX( bufindex) (bufindex * RTTCB_SIZEOF_AUP)
#define RTTCB_SIZEOF_AUP_MAXNUM( maxupnum) (maxupnum * RTTCB_SIZEOF_AUP)
#define RTTCB_SIZEOF_ADOWN_INDEX( bufindex) (bufindex * RTTCB_SIZEOF_ADOWN)
#define RTTCB_OFFSET_ACID( address) (address + RTTCB_SIZEOF_BASE)
#define RTTCB_OFFSET_MAXNUMUPBUFFERS( address) (RTTCB_OFFSET_ACID( address) + RTTCB_SIZEOF_ACID)
#define RTTCB_OFFSET_MAXNUMDOWNBUFFERS( address) (RTTCB_OFFSET_MAXNUMUPBUFFERS( address) + RTTCB_SIZEOF_MAXNUMUPBUFFERS)
#define RTTCB_OFFSET_AUP( address) (RTTCB_OFFSET_MAXNUMDOWNBUFFERS( address) + RTTCB_SIZEOF_MAXNUMDOWNBUFFERS)
#define RTTCB_OFFSET_ADOWN( address, maxupnum) (RTTCB_OFFSET_AUP( address) + RTTCB_SIZEOF_AUP_MAXNUM(maxupnum))
#define RTTCB_OFFSET_AUP_INDEX( address , bufindex) (RTTCB_OFFSET_AUP( address) + (RTTCB_SIZEOF_AUP_INDEX( bufindex)))
#define RTTCB_OFFSET_ADOWN_INDEX( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN( address, maxupnum) + (RTTCB_SIZEOF_ADOWN_INDEX( bufindex)))
#define RTTCB_OFFSET_AUP_SNAME( address , bufindex) (RTTCB_OFFSET_AUP_INDEX( address, bufindex) + RTTCB_SIZEOF_BASE )
#define RTTCB_OFFSET_AUP_PBUFFER( address , bufindex) (RTTCB_OFFSET_AUP_SNAME( address, bufindex) + RTTCB_SIZEOF_SNAME)
#define RTTCB_OFFSET_AUP_SIZEOFBUFFER( address , bufindex) (RTTCB_OFFSET_AUP_PBUFFER( address, bufindex) + RTTCB_SIZEOF_PBUFFER)
#define RTTCB_OFFSET_AUP_WROFF( address , bufindex) (RTTCB_OFFSET_AUP_SIZEOFBUFFER( address, bufindex) + RTTCB_SIZEOF_SIZEOFBUFFER)
#define RTTCB_OFFSET_AUP_RDOFF( address , bufindex) (RTTCB_OFFSET_AUP_WROFF( address, bufindex) + RTTCB_SIZEOF_WROFF)
#define RTTCB_OFFSET_AUP_FLAGS( address , bufindex) (RTTCB_OFFSET_AUP_RDOFF( address, bufindex) + RTTCB_SIZEOF_RDOFF)
#define RTTCB_OFFSET_ADOWN_SNAME( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN_INDEX( address, maxupnum, bufindex) + RTTCB_SIZEOF_BASE)
#define RTTCB_OFFSET_ADOWN_PBUFFER( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN_SNAME( address, maxupnum, bufindex) + RTTCB_SIZEOF_SNAME)
#define RTTCB_OFFSET_ADOWN_SIZEOFBUFFER( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN_PBUFFER( address, maxupnum, bufindex) + RTTCB_SIZEOF_PBUFFER)
#define RTTCB_OFFSET_ADOWN_WROFF( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN_SIZEOFBUFFER( address, maxupnum, bufindex) + RTTCB_SIZEOF_SIZEOFBUFFER)
#define RTTCB_OFFSET_ADOWN_RDOFF( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN_WROFF( address, maxupnum, bufindex) + RTTCB_SIZEOF_WROFF)
#define RTTCB_OFFSET_ADOWN_FLAGS( address, maxupnum, bufindex) (RTTCB_OFFSET_ADOWN_RDOFF( address, maxupnum, bufindex) + RTTCB_SIZEOF_RDOFF)
//
// Ring Buffer offset calc
//
#define RTTBUFFER_SIZEOF_BASE (0x00)
#define RTTBUFFER_SIZEOF_SNAME (0x04)
#define RTTBUFFER_SIZEOF_PBUFFER (0x04)
#define RTTBUFFER_SIZEOF_SIZEOFBUFFER (0x04)
#define RTTBUFFER_SIZEOF_WROFF (0x04)
#define RTTBUFFER_SIZEOF_RDOFF (0x04)
#define RTTBUFFER_SIZEOF_FLAGS (0x04)
#define RTTBUFFER_OFFSET_SNAME( address) ( address + RTTBUFFER_SIZEOF_BASE)
#define RTTBUFFER_OFFSET_PBUFFER( address) (RTTBUFFER_OFFSET_SNAME( address) + RTTBUFFER_SIZEOF_SNAME)
#define RTTBUFFER_OFFSET_SIZEOFBUFFER( address) (RTTBUFFER_OFFSET_PBUFFER( address) + RTTBUFFER_SIZEOF_PBUFFER)
#define RTTBUFFER_OFFSET_WROFF( address) (RTTBUFFER_OFFSET_SIZEOFBUFFER( address) + RTTBUFFER_SIZEOF_SIZEOFBUFFER)
#define RTTBUFFER_OFFSET_RDOFF( address) (RTTBUFFER_OFFSET_WROFF( address) + RTTBUFFER_SIZEOF_WROFF)
#define RTTBUFFER_OFFSET_FLAGS( address) (RTTBUFFER_OFFSET_RDOFF( address) + RTTBUFFER_SIZEOF_RDOFF)
#define RTTBUFFER_SIZEOF_AUP (0x18)
#define RTTBUFFER_SIZEOF_AUP_INDEX( bufindex) (bufindex * RTTBUFFER_SIZEOF_AUP)
#define RTTBUFFER_OFFSET_AUP_INDEX( address, bufindex) (address + (RTTBUFFER_SIZEOF_AUP_INDEX(bufindex)))
#define RTTBUFFER_OFFSET_AUP_SNAME( address, bufindex) (RTTBUFFER_OFFSET_AUP_INDEX( address, bufindex) + RTTBUFFER_SIZEOF_BASE)
#define RTTBUFFER_OFFSET_AUP_PBUFFER( address, bufindex) (RTTBUFFER_OFFSET_AUP_SNAME( address, bufindex) + RTTBUFFER_SIZEOF_SNAME)
#define RTTBUFFER_OFFSET_AUP_SIZEOFBUFFER( address, bufindex) (RTTBUFFER_OFFSET_AUP_PBUFFER( address, bufindex) + RTTBUFFER_SIZEOF_PBUFFER)
#define RTTBUFFER_OFFSET_AUP_WROFF( address, bufindex) (RTTBUFFER_OFFSET_AUP_SIZEOFBUFFER( address, bufindex) + RTTBUFFER_SIZEOF_SIZEOFBUFFER)
#define RTTBUFFER_OFFSET_AUP_RDOFF( address, bufindex) (RTTBUFFER_OFFSET_AUP_WROFF( address, bufindex) + RTTBUFFER_SIZEOF_WROFF)
#define RTTBUFFER_OFFSET_AUP_FLAGS( address, bufindex) (RTTBUFFER_OFFSET_AUP_RDOFF( address, bufindex) + RTTBUFFER_SIZEOF_RDOFF)
#define RTTBUFFER_SIZEOF_ADOWN (0x18)
#define RTTBUFFER_SIZEOF_ADOWN_INDEX( bufindex) (bufindex * RTTBUFFER_SIZEOF_ADOWN)
#define RTTBUFFER_OFFSET_ADOWN_INDEX( address, bufindex) (address + (RTTBUFFER_SIZEOF_ADOWN_INDEX(bufindex)))
#define RTTBUFFER_OFFSET_ADOWN_SNAME( address, bufindex) (RTTBUFFER_OFFSET_ADOWN_INDEX( address, bufindex) + RTTBUFFER_SIZEOF_BASE)
#define RTTBUFFER_OFFSET_ADOWN_PBUFFER( address, bufindex) (RTTBUFFER_OFFSET_ADOWN_SNAME( address, bufindex) + RTTBUFFER_SIZEOF_SNAME)
#define RTTBUFFER_OFFSET_ADOWN_SIZEOFBUFFER( address, bufindex) (RTTBUFFER_OFFSET_ADOWN_PBUFFER( address, bufindex) + RTTBUFFER_SIZEOF_PBUFFER)
#define RTTBUFFER_OFFSET_ADOWN_WROFF( address, bufindex) (RTTBUFFER_OFFSET_ADOWN_SIZEOFBUFFER( address, bufindex) + RTTBUFFER_SIZEOF_SIZEOFBUFFER)
#define RTTBUFFER_OFFSET_ADOWN_RDOFF( address, bufindex) (RTTBUFFER_OFFSET_ADOWN_WROFF( address, bufindex) + RTTBUFFER_SIZEOF_WROFF)
#define RTTBUFFER_OFFSET_ADOWN_FLAGS( address, bufindex) (RTTBUFFER_OFFSET_ADOWN_RDOFF( address, bufindex) + RTTBUFFER_SIZEOF_RDOFF)
//
// Operating modes. Define behavior if buffer is full (not enough space for entire message)
//
#define SEGGER_RTT_MODE_NO_BLOCK_SKIP (0) // Skip. Do not block, output nothing. (Default)
#define SEGGER_RTT_MODE_NO_BLOCK_TRIM (1) // Trim: Do not block, output as much as fits.
#define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL (2) // Block: Wait until there is space in the buffer.
#define SEGGER_RTT_MODE_MASK (3)
#define _SYS_SOCKET_INVALID_HANDLE (-1)
#define _SYS_SOCKET_IP_ADDR_ANY (0)
#define _SYS_SOCKET_IP_ADDR_LOCALHOST (0x7F000001) // 127.0.0.1 (localhost)
#define _SYS_SOCKET_PORT_ANY (0)
#define _SYS_SOCKET_ERR_UNSPECIFIED (-1)
#define _SYS_SOCKET_ERR_WOULDBLOCK (-2)
#define _SYS_SOCKET_ERR_TIMEDOUT (-3)
#define _SYS_SOCKET_ERR_CONNRESET (-4)
#define SYS_SOCKET_ERR_INTERRUPT (-5)
#define _SYS_SOCKET_SHUT_RD (0)
#define _SYS_SOCKET_SHUT_WR (1)
#define _SYS_SOCKET_SHUT_RDWR (2)
#define SOCKET_ERROR (-1) // General socket error returned from send(), sendto(), ...
#ifdef __linux__
#define INVALID_SOCKET (-1)
#endif
#define HEX_BYTES_PER_LINE 16
/*********************************************************************
*
* defines, configurable
*
**********************************************************************
*/
//#define _TELNET_RTT_DEBUG
/*********************************************************************
*
* RTT_IDLE_DELAY
* Maximum delay after which available data from the SystemView RTT
* Buffer is sent to SystemView App.
*
*/
#ifndef RTT_IDLE_DELAY
#define RTT_IDLE_DELAY 20
#endif
/*********************************************************************
*
* RTT_COMM_SEND_THRESHOLD
* Threshold for the SystemView RTT Buffer level,
* after which the data is sent to SystemView App.
*
*/
#ifndef RTT_SEND_THRESHOLD
#define RTT_SEND_THRESHOLD 512
#endif
/*********************************************************************
*
* RTT_COMM_POLL_INTERVAL
* Polling interval to check for send threshold or idle delay timeout.
*
*/
#ifndef RTT_COMM_POLL_INTERVAL
#define RTT_COMM_POLL_INTERVAL 2
#endif
/*********************************************************************
*
* Function-like macros
*
**********************************************************************
*/
#define COUNTOF(a) (sizeof((a))/sizeof((a)[0]))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define ERR_TO_STR(e) (x==(e)) ? #e //lint !e9024 !e773
#define USE_PARA(Para) (void)Para // Some compiler complain about unused parameters. This works for most compilers.
#define ADDR2PTR(Type, Addr) (((Type*)((unsigned int)(Addr)))) // Allow cast from address to pointer.
#define PTR2ADDR(p) (((unsigned int)(p))) // Allow cast from pointer to address.
#define PTR2PTR(Type, p) (((Type*)(p))) // Allow cast from one pointer type to another (ignore different size).
#define PTR_DISTANCE(p0, p1) (PTR2ADDR(p0) - PTR2ADDR(p1))
#ifdef _TELNET_RTT_DEBUG
#define Log_Print(...) SYS_Log("%s:%d, %s(): ", __FILE__, __LINE__, __FUNCTION__); SYS_Log(__VA_ARGS__)
#else
#define Log_Print(...)
#endif
/*********************************************************************
*
* Types
*
**********************************************************************
*/
typedef int _SYS_SOCKET_HANDLE;
typedef enum _VT_STATE_T {
Normal,
Esc,
Csi,
Dcs,
DcsString,
DropOne
} VT_STATE_T;
/*********************************************************************
*
* static data
*
**********************************************************************
*/
static int _int1 = 1;
static const char hexchar[] = "0123456789ABCDEF";
static char telnetCmd[] = {0xff, 0xfb, 0x01, 0xff, 0xfb, 0x03, 0xff, 0xfc, 0x1f};
/*********************************************************************
*
* Static const data
*
**********************************************************************
*/
/*********************************************************************
*
* Global data
*
**********************************************************************
*/
/*********************************************************************
*
* Static functions
*
**********************************************************************
*/
static void T32_DefaultState(int exit);
/*********************************************************************
*
* util log functions
*
**********************************************************************
*/
/*********************************************************************
*
* SYS_Log()
*
* Function description
* Outputs a formatted log message.
*
* Parameters
* sFormat : String to output that might contain placeholders.
*/
void SYS_Log(const char* sFormat, ...) {
va_list ParamList;
char ac[256];
char acTime[40];
char acFileName[64];
static FILE *pFile = NULL;
struct tm *tm;
struct timeval tv;
//
// Replace placeholders (%d, %x, etc.) by values and call output routine.
//
va_start(ParamList, sFormat);
(void)vsnprintf(ac, (int)sizeof(ac), sFormat, ParamList);
va_end(ParamList);
if (pFile == NULL) {
gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
strftime(acTime, sizeof(acTime), "%Y-%m-%dT%H:%M:%S", tm);
sprintf(acFileName, "main_%s.log", acTime);
//
// call output routine.
//
pFile = fopen(acFileName, "a+");
}
if (pFile != NULL) {
fprintf(pFile, "%s", ac);
fflush(pFile);
}
fprintf(stderr, "%s", ac);
fflush(stderr);
}
/*********************************************************************
*
* system task functions
*
**********************************************************************
*/
/*********************************************************************
*
* SYS_Sleep()
*
* Function description
* Yield the execution of current thread for msec miliseconds.
*/
#ifdef __linux__
int32_t SYS_Sleep(uint32_t msec) {
if (msec) {
struct timeval timeout;
timeout.tv_sec = msec / 1000;
timeout.tv_usec = (msec % 1000) * 1000;
select(0, NULL, NULL, NULL, &timeout); // won't get interrupted by signals if no FD_SETs are given (says libc's manual...)
} else {
sched_yield();
}
return 0;
}
#endif
#ifdef _WIN32
int32_t SYS_Sleep(uint32_t msec) {
Sleep(msec);
return 0;
}
#endif
/*********************************************************************
*
* system signal functions
*
**********************************************************************
*/
/*********************************************************************
*
* T32_Err2Str()
*
* Function description
* Converts trace2 error code to a readable string by simply using
* the defines name.
*
* Parameters
* x: Error code returned by API of the stack.
*
* Return value
* Pointer to string of the define name.
*/
const char* T32_Err2Str(int x) {
return (
ERR_TO_STR(T32_OK) :
ERR_TO_STR(T32_ERR_COM_RECEIVE_FAIL) :
ERR_TO_STR(T32_ERR_COM_TRANSMIT_FAIL) :
ERR_TO_STR(T32_ERR_COM_PARA_FAIL) :
ERR_TO_STR(T32_ERR_COM_SEQ_FAIL) :
ERR_TO_STR(T32_ERR_NOTIFY_MAX_EVENT) :
ERR_TO_STR(T32_ERR_MALLOC_FAIL) :
ERR_TO_STR(T32_ERR_STD_RUNNING) :
ERR_TO_STR(T32_ERR_STD_NOTRUNNING) :
ERR_TO_STR(T32_ERR_STD_RESET) :
ERR_TO_STR(T32_ERR_STD_ACCESSTIMEOUT) :
ERR_TO_STR(T32_ERR_STD_INVALID) :
ERR_TO_STR(T32_ERR_STD_REGUNDEF) :
ERR_TO_STR(T32_ERR_STD_VERIFY) :
ERR_TO_STR(T32_ERR_STD_BUSERROR) :
ERR_TO_STR(T32_ERR_STD_NOMEM) :
ERR_TO_STR(T32_ERR_STD_RESETDETECTED) :
ERR_TO_STR(T32_ERR_STD_FDXBUFFER) :
ERR_TO_STR(T32_ERR_STD_RTCKTIMEOUT) :
ERR_TO_STR(T32_ERR_STD_INVALIDLICENSE) :
ERR_TO_STR(T32_ERR_STD_CORENOTACTIVE) :
ERR_TO_STR(T32_ERR_STD_USERSIGNAL) :
ERR_TO_STR(T32_ERR_STD_NORAPI) :
ERR_TO_STR(T32_ERR_STD_FAILED) :
ERR_TO_STR(T32_ERR_STD_LOCKED) :
ERR_TO_STR(T32_ERR_STD_POWERFAIL) :
ERR_TO_STR(T32_ERR_STD_DEBUGPORTFAIL) :
ERR_TO_STR(T32_ERR_STD_DEBUGPORTTIMEOUT) :
ERR_TO_STR(T32_ERR_STD_NODEVICE) :
ERR_TO_STR(T32_ERR_STD_RESETFAIL) :
ERR_TO_STR(T32_ERR_STD_EMUTIMEOUT) :
ERR_TO_STR(T32_ERR_STD_NORTCK) :
ERR_TO_STR(T32_ERR_STD_ATTACH) :
ERR_TO_STR(T32_ERR_STD_FATAL) :
ERR_TO_STR(T32_ERR_GETRAM_INTERNAL) :
ERR_TO_STR(T32_ERR_READREGBYNAME_NOTFOUND) :
ERR_TO_STR(T32_ERR_READREGBYNAME_FAILED) :
ERR_TO_STR(T32_ERR_WRITEREGBYNAME_NOTFOUND) :
ERR_TO_STR(T32_ERR_WRITEREGBYNAME_FAILED) :
ERR_TO_STR(T32_ERR_READREGOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_READREGOBJ_MAXCORE) :
ERR_TO_STR(T32_ERR_READREGOBJ_NOTFOUND) :
ERR_TO_STR(T32_ERR_READREGSETOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_READREGSETOBJ_NUMREGS) :
ERR_TO_STR(T32_ERR_WRITEREGOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_WRITEREGOBJ_MAXCORE) :
ERR_TO_STR(T32_ERR_WRITEREGOBJ_NOTFOUND) :
ERR_TO_STR(T32_ERR_WRITEREGOBJ_FAILED) :
ERR_TO_STR(T32_ERR_SETBP_FAILED) :
ERR_TO_STR(T32_ERR_READMEMOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_WRITEMEMOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_TRANSFERMEMOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_TRANSFERMEMOBJ_TRANSFERFAIL) :
ERR_TO_STR(T32_ERR_READVAR_ALLOC) :
ERR_TO_STR(T32_ERR_READVAR_ACCESS) :
ERR_TO_STR(T32_ERR_READBPOBJ_PARAFAIL) :
ERR_TO_STR(T32_ERR_READBPOBJ_NOTFOUND) :
ERR_TO_STR(T32_ERR_WRITEBPOBJ_FAILED) :
ERR_TO_STR(T32_ERR_WRITEBPOBJ_ADDRESS) :
ERR_TO_STR(T32_ERR_WRITEBPOBJ_ACTION) :
ERR_TO_STR(T32_ERR_MMUTRANSLATION_FAIL) :
ERR_TO_STR(T32_ERR_EXECUTECOMMAND_FAIL) :
ERR_TO_STR(T32_ERR_EXECUTEFUNCTION_FAIL) :
"unknown error code"
);
}
/*********************************************************************
*
* SYS_Hexdump()
*
*/
void SYS_Hexdump(void *inbuf, unsigned inlen, bool ascii, bool addr) {
unsigned char *cp = (unsigned char *)inbuf;
unsigned char *ap = (unsigned char *)inbuf;
int len = inlen;
int clen, alen;
char outbuf[96];
char *outp = &outbuf[0];
int line = 0;
Log_Print("====================================HEX DUMP START\n");
while (len > 0) {
if (addr)
outp += sprintf(outp, "[0x%08X] ", (int)cp);
clen = alen = MIN(HEX_BYTES_PER_LINE, len);
// display data in hex
for (int i = 0; i < HEX_BYTES_PER_LINE; i++) {
unsigned char uc = *cp++;
if (--clen >= 0) {
*outp++ = hexchar[(uc >> 4) & 0x0f];
*outp++ = hexchar[(uc) & 0x0f];
*outp++ = ' ';
}
else if (line != 0) {
*outp++ = ' ';
*outp++ = ' ';
*outp++ = ' ';
}
}
if (ascii) {
*outp++ = ' ';
*outp++ = ' ';
// display data in ascii
while (--alen >= 0) {
unsigned char uc = *ap++;
*outp++ = ((uc >= 0x20) && (uc < 0x7f)) ? uc : '.';
}
}
// output the line
*outp++ = '\n';
*outp++ = '\0';
Log_Print("%s", outbuf);
outp = &outbuf[0];
len -= HEX_BYTES_PER_LINE;
line++;
}
Log_Print("====================================HEX DUMP END\n\n");
}
/*********************************************************************
*
* LRealPath()
*
*/
char * LRealPath (const char *filename) {
// Method 1: The system has a compile time upper bound on a filename
// path. Use that and realpath() to canonicalize the name. This is
// the most common case. Note that, if there isn't a compile time
// upper bound, you want to avoid realpath() at all costs.
#if defined(__linux__)
{
char buf[PATH_MAX];
const char *rp = realpath (filename, buf);
if (rp == NULL)
rp = filename;
return strdup (rp);
}
#endif
// The MS Windows method. If we don't have realpath, we assume we
// don't have symlinks and just canonicalize to a Windows absolute
// path. GetFullPath converts ../ and ./ in relative paths to
// absolute paths, filling in current drive if one is not given
// or using the current directory of a specified drive (eg, "E:foo").
// It also converts all forward slashes to back slashes.
#if defined (_WIN32)
{
char buf[MAX_PATH];
char* basename;
DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
if (len == 0 || len > MAX_PATH - 1)
return _strdup (filename);
else
{
// The file system is case-preserving but case-insensitive,
// Canonicalize to lowercase, using the codepage associated
// with the process locale.
CharLowerBuff (buf, len);
return _strdup (buf);
}
}
#endif
}
#ifdef _WIN32
//
// epoch
//
static const unsigned __int64 epoch = 116444736000000000LL;
/*********************************************************************
*
* gettimeofday()
*
*/
static int gettimeofday(struct timeval *tp, struct timezone *tzp) {
FILETIME file_time;
SYSTEMTIME system_time;
ULARGE_INTEGER ularge;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
ularge.LowPart = file_time.dwLowDateTime;
ularge.HighPart = file_time.dwHighDateTime;
tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
return 0;
}
#endif
/*********************************************************************
*
* RTT_TelnetLogS()
*
* Function description
* Execute the current command line.
*
*/
static void RTT_TelnetLogS(char *logFile, char *outBuf, uint32_t outLen) {
static FILE *pFile = NULL;
static VT_STATE_T vt_state = Normal;
char *sPath = NULL;
int chr = 0;
if (pFile == NULL) {
// Convert the script to an absolute path
sPath = LRealPath(logFile);
//
// call output routine.
//
pFile = fopen(sPath, "a+");
free(sPath);
}
if (pFile != NULL) {
while (outLen--) {
chr = *outBuf++ & 0xFF;
if (vt_state == DropOne) {
vt_state = Normal;
continue;
}
// Handle normal ANSI escape mechanism
// (Note that this terminates DCS strings!)
if (vt_state == Esc && chr >= 0x40 && chr <= 0x5F) {
vt_state = Normal;
chr += 0x40;
}
switch (chr) {
case CAN:
case SUB:
vt_state = Normal;
break;
case ESC:
vt_state = Esc;
break;
case CSI:
vt_state = Csi;
break;
case DCS:
case OSC: // VT320 commands
case PM:
case APC:
vt_state = Dcs;
break;
default:
if ((chr & 0x6F) < 0x20) { // Check controls
switch (chr) {
// VT oddity -- controls go through regardless of state.
case BEL : break; // Pass these through
case BS : break;
case TAB : break;
case LF : fprintf(pFile, "%c", chr); break;
case VT : break;
case FF : break;
case CR : break;
}
break;
}
switch (vt_state) {
case Normal:
fprintf(pFile, "%c", chr);
break;
case Esc:
vt_state = Normal;
switch (chr) {
case 'c': case '7': case '8':
case '=': case '>': case '~':
case 'n': case '\123': case 'o':
case '|':
break;
case '#': case ' ': case '(':
case ')': case '*': case '+':
vt_state = DropOne;
break;
}
break;
case Csi:
case Dcs:
if (chr >= 0x40 && chr <= 0x7E)
if (vt_state == Csi)
vt_state = Normal;
else
vt_state = DcsString;
break;
case DcsString:
// Just drop everything here
break;
} //switch (vt_state) {
} // switch (chr) {
} // while (outLen--) {
fflush(pFile);
} // if (pFile != NULL) {
}
/*********************************************************************
*
* SEGGER_atoi()
*
* Function description
* Converts a string to an integer.
*
* Parameters
* s: String to parse.
*
* Return value
* Converted integer value.
*/
int SEGGER_atoi(const char* s) {
int Value;
int Digit;
char c;
Value = 0;
c = *s++;
do {
if ((c >= '0') && (c <= '9')) {
Digit = (int)(c - '0');
} else {
break; // Will break on '\0' as well.
}
Value = (Value * 10) + Digit;
c = *s++;
} while (c != '\0');
return Value;
}
/*********************************************************************
*
* SYS_ExitHandler
*
* Function description
* This is signal Handler
*/
static void SYS_ExitHandler(int signum) {
T32_DefaultState(0);
//T32_Terminate(T32_OK);
T32_Exit();
switch (signum) {
// interrupt
case SIGINT : Log_Print("Interrupt.\n"); break;
// Software termination signal from kill
case SIGTERM : Log_Print("Terminated.\n"); break;
// illegal instruction - invalid function image
case SIGILL : Log_Print("Terminated.\n"); break;
// floating point exception
case SIGFPE : Log_Print("Terminated.\n"); break;
// segment violation
case SIGSEGV : Log_Print("Terminated.\n"); break;
//Ctrl-Break sequence
#ifdef _WIN32
case SIGBREAK : Log_Print("Terminated.\n"); break;
#endif
// abnormal termination triggered by abort call
case SIGABRT : Log_Print("Terminated.\n"); break;
default : Log_Print("unknow.\n"); break;
}
exit(signum);
}
/*********************************************************************
*
* trace32 rtt functions
*
**********************************************************************
*/
/*********************************************************************
*
* T32_GetRTTCBAddr()
*
*/
unsigned int T32_GetRTTCBAddr(const char * symname) {
unsigned int address, size, reserved;
int Result;
Result = T32_GetSymbol( symname, &address, &size, &reserved );
if (Result != T32_OK) {
Log_Print("T32_GetRTTCBAddr error, Result = %s.\n", T32_Err2Str(Result));
SYS_ExitHandler(Result);
}
return address;
}
/*********************************************************************
*
* T32_GetRTTCBSize()
*
*/
unsigned int T32_GetRTTCBSize(const char * symname) {
unsigned int address, size, reserved;
int Result;
Result = T32_GetSymbol( symname, &address, &size, &reserved );
if (Result != T32_OK) {
Log_Print("T32_GetRTTCBSize error, Result = %s.\n", T32_Err2Str(Result));
SYS_ExitHandler(Result);
}
return size;
}
/*********************************************************************
*
* T32_GetBytes
*
*/
void T32_GetBytes(unsigned int address, unsigned int cnt, void *dest) {
int Result;
Result = T32_ReadMemory(address, 0x40 /* E:*/, (unsigned char*)(dest), cnt);
if (Result != T32_OK) {
Log_Print("T32_GetBytes error, Result = %s.\n", T32_Err2Str(Result));
SYS_ExitHandler(Result);
}
}
/*********************************************************************
*
* T32_GetByte
*
*/
unsigned char T32_GetByte(unsigned int address) {
char byte;
T32_GetBytes(address, sizeof(char), &byte);
return byte;
}
/*********************************************************************
*
* T32_GetWord
*
*/
unsigned int T32_GetWord(unsigned int address) {
int word;
T32_GetBytes(address, sizeof(int), &word);
return word;
}
/*********************************************************************
*
* T32_SetBytes
*
*/
void T32_SetBytes(unsigned int address, unsigned int cnt, void const *src) {
int Result;
Result = T32_WriteMemory(address, 0x40 /* E:*/, (unsigned char*)(src), cnt);
if (Result != T32_OK) {
Log_Print("T32_SetBytes error, Result = %s.\n", T32_Err2Str(Result));
SYS_ExitHandler(Result);
}
}
/*********************************************************************
*
* T32_SetByte
*
*/
void T32_SetByte(unsigned int address, unsigned char data) {
T32_SetBytes(address, sizeof(char), &data);
}
/*********************************************************************
*
* T32_SetWord
*
*/
void T32_SetWord(unsigned int address, unsigned int data) {
T32_SetBytes(address, sizeof(int), &data);
}
/*********************************************************************
*
* T32_strlen
*
*/
unsigned T32_strlen(const char * s) {
unsigned Len;
char c = 0;
int addr = 0;
addr = (unsigned int)s;
Len = 0;
c = T32_GetByte(addr);
while (c != '\0') {
c = T32_GetByte(addr);
addr = addr + 1;
Len++;
}
return Len;
}
/*********************************************************************
*
* T32_strcpy
*
*/
char * T32_strcpy(char * dst, char * src) {
char * p = NULL;
char c = 0;
int addr = 0;
addr = (unsigned int)src;
if (dst != NULL) {
p = dst;
c = T32_GetByte(addr);
while(c != '\0') {
c = T32_GetByte(addr);
*dst++ = c;
addr = addr + 1;
}
*dst = '\0';
}
return p;
}
/*********************************************************************
*
* T32_memcpy2P()
*
*/
void T32_memcpy2P(void* pDest, void* pSrc, unsigned NumBytes) {
int Result;
Result = T32_ReadMemory((unsigned int)pSrc, 0x40 /* E:*/, (unsigned char *)pDest, NumBytes);
if (Result != T32_OK) {
Log_Print("T32 memcpy to pc error, Result = %s.\n", T32_Err2Str(Result));
SYS_ExitHandler(Result);
}
}
/*********************************************************************
*
* T32_memcpy2C()
*
*/
void T32_memcpy2C(void* pDest, void* pSrc, unsigned NumBytes) {
int Result;
Result = T32_WriteMemory((unsigned int)pDest, 0x40 /* E:*/, (unsigned char *)pSrc, NumBytes);
if (Result != T32_OK) {
Log_Print("T32 memcpy to chip error, Result = %s.\n", T32_Err2Str(Result));
SYS_ExitHandler(Result);
}
}
/*********************************************************************
*
* T32_RetryGetState()
*
*/
static int T32_RetryGetState(int (*pGetState)(int *pState), int *pState, int nRetry) {
int Result;
while (nRetry > 0) {
Result = pGetState(pState);
if (Result == T32_ERR_COM_RECEIVE_FAIL || Result == T32_ERR_COM_TRANSMIT_FAIL) {
nRetry--;
SYS_Sleep(5);
}
else {
return Result;
}
}
return Result;
}
/*********************************************************************
*