-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathxcplite.c
More file actions
3944 lines (3431 loc) · 160 KB
/
Copy pathxcplite.c
File metadata and controls
3944 lines (3431 loc) · 160 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
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
/*****************************************************************************
| File:
| xcplite.c
|
| Description:
| Implementation of the ASAM XCP Protocol Layer V1.4
| Version V2.1.x
| - Optimized for 64 bit POSIX based platforms (Linux, QNX or MacOS)
| - Compatible with 32 bit platforms
| - Tested on x86 strong and ARM weak memory model
| - Runs on Windows for demonstration purposes with some limitations
|
| Limitations:
| - 8 bit and 16 bit CPUs are not supported
| - No Motorola byte sex
| - No misra compliance
| - Overall number of ODTs limited to 64K
| - Overall number of ODT entries is limited to 64K
| - Fixed ODT-BYTE,res-BYTE, DAQ-WORD DTO header
| - Fixed 32 bit time stamp
| - Only dynamic DAQ list allocation supported
| - Resume is not supported
| - Overload indication by event is not supported
| - ODT optimization not supported
| - Seed & key is not supported
| - Flash programming is not supported
|
| For micro-controllers, more features and more transport layers (CAN, FlexRay) are provided
| by the free XCP basic version available from Vector Informatik GmbH at www.vector.com
|
| Limitations of the XCP basic version:
| - Stimulation (Bypassing) is not available|
| - Bit stimulation is not available
| - SHORT_DOWNLOAD is not implemented
| - MODIFY_BITS is not available|
| - FLASH and EEPROM Programming is not available|
| - Block mode for UPLOAD, DOWNLOAD and PROGRAM is not available
| - Resume mode is not available|
| - Memory write and read protection is not supported
| - Checksum calculation supports only CRC CCITT16 or ADD44
|
| Copyright (c) Vector Informatik GmbH. All rights reserved.
| Licensed under the MIT license. See LICENSE file in the project root for details.
|
| No limitations and full compliance are available with the commercial version
| from Vector Informatik GmbH, please contact Vector
|***************************************************************************/
#include "xcplib_cfg.h" // for OPTION_xxx
#include "xcp_cfg.h" // XCP protocol layer configuration parameters (XCP_xxx)
#include "xcptl_cfg.h" // XCP transport layer configuration parameters (XCPTL_xxx)
#include "xcplite.h" // XCP protocol layer interface functions
#include <assert.h> // for assert
#include <inttypes.h> // for PRIx32, PRIu64
#include <stdarg.h> // for va_list, va_start, va_arg, va_end
#include <stdbool.h> // for bool
#include <stdint.h> // for uint8_t, uint16_t,...
#include <stdlib.h> // for size_t, NULL, abort
#include <string.h> // for memcpy, memset, strlen, strncpy
#ifdef DBG_LEVEL
#include <stdio.h> // for printf
#endif
#ifdef OPTION_SHM_MODE
#include <unistd.h> // for getpid()
#endif
#include "dbg_print.h" // for DBG_LEVEL, DBG_PRINT3, DBG_PRINTF4, DBG...
#ifdef OPTION_ENABLE_PERSISTENCE
#include "persistence.h" // for XcpBinFreezeCalSeg
#endif
#include "platform.h" // for atomics
#include "queue.h" // for QueueXxx transport queue layer interface
#ifdef OPTION_SHM_MODE
#include "shm.h" // for shared memory management, declares nothing outside SHM mode
#endif
#include "xcp.h" // XCP protocol definitions
#include "xcptl.h" // for transport layer abstraction XcpTlWaitForTransmitQueueEmpty and XcpTlSendCrm
#ifdef OPTION_CAL_SEGMENTS
#include "cal.h" // for XcpCalSegXxx
#endif
#if defined(XCPTL_ENABLE_MULTICAST)
#include "xcpethtl.h" // for ethernet specific transport layer functions XcpEthTl
#endif
/****************************************************************************/
/* Defaults and checks */
/****************************************************************************/
/* Check limits of the XCP imnplementation */
#if defined(XCPTL_MAX_CTO_SIZE)
#if (XCPTL_MAX_CTO_SIZE > 255)
#error "XCPTL_MAX_CTO_SIZE must be <= 255"
#endif
#if (XCPTL_MAX_CTO_SIZE < 8)
#error "XCPTL_MAX_CTO_SIZE must be >= 8"
#endif
#else
#error "Please define XCPTL_CTO_SIZE"
#endif
#if defined(XCPTL_MAX_DTO_SIZE)
#if (XCPTL_MAX_DTO_SIZE > (XCPTL_MAX_SEGMENT_SIZE - XCPTL_HEADER_SIZE))
#error "XCPTL_MAX_DTO_SIZE too large"
#endif
#if (XCPTL_MAX_DTO_SIZE < 8)
#error "XCPTL_MAX_DTO_SIZE must be >= 8"
#endif
#if (XCPTL_MAX_DTO_SIZE < XCPTL_MAX_CTO_SIZE)
#error "XCPTL_MAX_DTO_SIZE must be >= XCPTL_MAX_CTO_SIZE"
#endif
#else
#error "Please define XCPTL_MAX_DTO_SIZE"
#endif
/* Max. size of an object referenced by an ODT entry XCP_MAX_ODT_ENTRY_SIZE may be limited */
/* Default 248 */
#if defined(XCP_MAX_ODT_ENTRY_SIZE)
#if (XCP_MAX_DTO_ENTRY_SIZE > 255)
#error "XCP_MAX_ODT_ENTRY_SIZE too large"
#endif
#else
#define XCP_MAX_ODT_ENTRY_SIZE 248 // mod 4 = 0 to optimize DAQ copy granularity
#endif
/* Check XCP_DAQ_MEM_SIZE */
#if defined(XCP_DAQ_MEM_SIZE)
#if (XCP_DAQ_MEM_SIZE > 0xFFFFFFFF)
#error "XCP_DAQ_MEM_SIZE must be <= 0xFFFFFFFF"
#endif
#else
#error "Please define XCP_DAQ_MEM_SIZE"
#endif
#ifdef OPTION_CAL_SEGMENTS
/* Check XCP_CAL_MEM_SIZE */
#if defined(XCP_CAL_MEM_SIZE)
#if (XCP_CAL_MEM_SIZE > 0xFFFFFFFF)
#error "XCP_CAL_MEM_SIZE must be <= 0xFFFFFFFF"
#endif
#else
#error "Please define XCP_CAL_MEM_SIZE"
#endif
#endif
/* Check length of of names with null termination must be even*/
#if XCP_EPK_MAX_LENGTH & 1 == 0 || XCP_EPK_MAX_LENGTH >= 128
#error "XCP_EPK_MAX_LENGTH must be <128 and odd for null termination"
#endif
#if XCP_MAX_EVENT_NAME & 1 == 0 || XCP_MAX_EVENT_NAME >= 128
#error "XCP_MAX_EVENT_NAME must be <128 and odd for null termination"
#endif
/****************************************************************************/
/* XCPlite memory signature (XCPLITE__XXXXX) */
/****************************************************************************/
// Current supported addressing schemes are:
// For A2L-Toolset compatibility: Absolute segment addressing mode - XCP_ADDRESS_MODE_XCPLITE__ACSDD
// For SHM mode: Relative segment addressing mode - XCP_ADDRESS_MODE_XCPLITE__CXSDD
// No calibration segment management: Absolute addressing mode - XCP_ADDRESS_MODE_XCPLITE__AXSDD
// Default: Segment relative addressing mode - XCP_ADDRESS_MODE_XCPLITE__CASDD
#ifndef _WIN
__attribute__((used))
#endif
#if defined(XCP_ADDRESS_MODE_XCPLITE__CXSDD)
const uint16_t XCPLITE__CXSDD = XCP_DRIVER_VERSION;
#elif defined(XCP_ADDRESS_MODE_XCPLITE__ACSDD)
const uint16_t XCPLITE__ACSDD = XCP_DRIVER_VERSION;
#elif defined(XCP_ADDRESS_MODE_XCPLITE__CASDD)
const uint16_t XCPLITE__CASDD = XCP_DRIVER_VERSION;
#elif defined(XCP_ADDRESS_MODE_XCPLITE__AXSDD)
const uint16_t XCPLITE__AXSDD = XCP_DRIVER_VERSION;
#else
#error "Please define one of XCP_ADDRESS_MODE_XCPLITE__ACSDD, XCP_ADDRESS_MODE_XCPLITE__CASDD, XCP_ADDRESS_MODE_XCPLITE__CXSDD"
#endif
/****************************************************************************/
/* Protocol layer state data */
/****************************************************************************/
// XCP singleton state
#ifdef OPTION_SHM_MODE // define gXcpData as a pointer
tXcpData *gXcpData = NULL;
#else
tXcpData gXcpData = {0};
#endif
tXcpLocalData gXcpLocalData = {0}; // XCP_MODE_DEACTIVATE by default
// Debug
// Test the thread safety concept
// - Assert mutable access to the XCP singleton is either safe or allowed to the owner thread
// - Assert read access by other threads is only allowed during DAQ running
#ifdef TEST_MUTABLE_ACCESS_OWNERSHIP
#include <pthread.h>
static pthread_t gXcpOwnerThread;
static bool gXcpOwnerThreadValid = false;
static void XcpBindOwnerThread(void) {
pthread_t self = pthread_self();
gXcpOwnerThread = self;
gXcpOwnerThreadValid = true;
}
static inline tXcpData *XcpMut_(const char *file, int line) {
if (!pthread_equal(gXcpOwnerThread, pthread_self())) {
DBG_PRINTF_ERROR("Mutable access to XCP singleton data from non-owner thread in file %s, line %d!\n", file, line);
}
return gXcpData;
}
#define shared (*(const tXcpData *)gXcpData) // Shortcut for read only access to the XCP singleton data
#define shared_mut (*XcpMut_(__FILE__, __LINE__)) // Shortcut for mutable access to the XCP singleton data (checked ownership)
#define shared_mut_safe (*gXcpData) // Shortcut for mutable access to the XCP singleton data (not checked)
#define local (*(const tXcpLocalData *)&gXcpLocalData) // Read-only access to process-local state
#define local_mut gXcpLocalData // Mutable access to process-local state
#elif defined(OPTION_SHM_MODE) // aliases for gXcpData which is a pointer to the shared memory in SHM mode
#define shared (*(const tXcpData *)gXcpData) // Shortcut for read only access to the XCP singleton data
#define shared_mut (*gXcpData) // Shortcut for mutable access to the XCP singleton data
#define shared_mut_safe (*gXcpData) // Shortcut for mutable access to the XCP singleton data
#else
#define shared (*(const tXcpData *)&gXcpData) // Shortcut for read only access to the XCP singleton data
#define shared_mut gXcpData // Shortcut for mutable access to the XCP singleton data
#define shared_mut_safe gXcpData // Shortcut for mutable access to the XCP singleton data
#endif
#define local (*(const tXcpLocalData *)&gXcpLocalData) // Read-only access to process-local state
#define local_mut gXcpLocalData // Mutable access to process-local state
// Global state checks
#ifdef OPTION_SHM_MODE // gXcpData is a pointer to the shared state in SHM mode
#define isActivated() (gXcpData != NULL && 0 != (gXcpData->session_status & SS_ACTIVATED))
#define isStarted() (gXcpData != NULL && 0 != (gXcpData->session_status & SS_STARTED))
#define isConnected() (gXcpData != NULL && 0 != (gXcpData->session_status & SS_CONNECTED))
#define isLegacyMode() (gXcpData != NULL && 0 != (gXcpData->session_status & SS_LEGACY_MODE))
#define isDaqRunning() (gXcpData != NULL && atomic_load_explicit(&gXcpData->daq_running, memory_order_relaxed))
#else
#define isActivated() (0 != (gXcpData.session_status & SS_ACTIVATED))
#define isStarted() (0 != (gXcpData.session_status & SS_STARTED))
#define isConnected() (0 != (gXcpData.session_status & SS_CONNECTED))
#define isLegacyMode() (0 != (gXcpData.session_status & SS_LEGACY_MODE))
#define isDaqRunning() (0 != (gXcpData.session_status & SS_STARTED) && atomic_load_explicit(&gXcpData.daq_running, memory_order_relaxed))
#endif
// Thread safe state checks
/****************************************************************************/
/* Forward declarations of static functions */
/****************************************************************************/
static uint8_t XcpAsyncCommand(bool async, const uint32_t *cmdBuf, uint8_t cmdLen);
/****************************************************************************/
/* Macros */
/****************************************************************************/
// DAQ list access shortcuts
// j is absolute odt number
// i is daq number
#define DaqListOdtTable ((const tXcpOdt *)&shared.daq_lists.u.daq_list[shared.daq_lists.daq_count])
#define DaqListOdtEntryAddrTable ((const uint32_t *)&DaqListOdtTable[shared.daq_lists.odt_count])
#define DaqListOdtEntrySizeTable ((const uint8_t *)&DaqListOdtEntryAddrTable[shared.daq_lists.odt_entry_count])
#ifdef XCP_ENABLE_DAQ_ADDREXT
#define DaqListOdtEntryAddrExtTable ((const uint8_t *)&DaqListOdtEntrySizeTable[shared.daq_lists.odt_entry_count])
#endif
#define DaqListOdtTableMut ((tXcpOdt *)&shared_mut.daq_lists.u.daq_list[shared.daq_lists.daq_count])
#define DaqListOdtEntryAddrTableMut ((uint32_t *)&DaqListOdtTableMut[shared.daq_lists.odt_count])
#define DaqListOdtEntrySizeTableMut ((uint8_t *)&DaqListOdtEntryAddrTableMut[shared.daq_lists.odt_entry_count])
#ifdef XCP_ENABLE_DAQ_ADDREXT
#define DaqListOdtEntryAddrExtTableMut ((uint8_t *)&DaqListOdtEntrySizeTableMut[shared.daq_lists.odt_entry_count])
#endif
#define DaqListOdtEntryCount(j) ((DaqListOdtTable[j].last_odt_entry - DaqListOdtTable[j].first_odt_entry) + 1)
#define DaqListOdtCount(i) ((shared.daq_lists.u.daq_list[i].last_odt - shared.daq_lists.u.daq_list[i].first_odt) + 1)
#define DaqListLastOdt(i) shared.daq_lists.u.daq_list[i].last_odt
#define DaqListFirstOdt(i) shared.daq_lists.u.daq_list[i].first_odt
#define DaqListMode(i) shared.daq_lists.u.daq_list[i].mode
#define DaqListModeMut(i) shared_mut.daq_lists.u.daq_list[i].mode
#define DaqListState(i) shared.daq_lists.u.daq_list[i].state
#define DaqListStateMut(i) shared_mut.daq_lists.u.daq_list[i].state
#define DaqListEventChannel(i) shared.daq_lists.u.daq_list[i].event_id
#define DaqListEventChannelMut(i) shared_mut.daq_lists.u.daq_list[i].event_id
#define DaqListAddrExt(i) shared.daq_lists.u.daq_list[i].addr_ext
#define DaqListAddrExtMut(i) shared_mut.daq_lists.u.daq_list[i].addr_ext
#define DaqListPriority(i) shared.daq_lists.u.daq_list[i].priority
#define DaqListPriorityMut(i) shared_mut.daq_lists.u.daq_list[i].priority
#ifdef XCP_MAX_EVENT_COUNT
#ifdef XCP_ENABLE_DAQ_EVENT_LIST
#define DaqListFirst(event_id) shared.event_list.event[event_id].daq_first
#define DaqListFirstMut(event_id) shared_mut.event_list.event[event_id].daq_first
#else
#define DaqListFirst(event_id) shared.daq_lists.daq_first[event_id]
#define DaqListFirstMut(event_id) shared_mut.daq_lists.daq_first[event_id]
#endif
#define DaqListNext(daq) shared.daq_lists.u.daq_list[daq].next
#define DaqListNextMut(daq) shared_mut.daq_lists.u.daq_list[daq].next
#endif
// Command response buffer access shortcuts
#define CRM_LEN shared_mut.crm_len
#define CRM shared_mut.crm
#define CRM_BYTE(x) (shared_mut.crm.b[x])
#define CRM_WORD(x) (shared_mut.crm.w[x])
#define CRM_DWORD(x) (shared_mut.crm.dw[x])
// Error handling
#define error(e) \
{ \
err = (e); \
goto negative_response; \
}
#define check_error(e) \
{ \
err = (e); \
if (err != 0) \
goto negative_response; \
}
/****************************************************************************/
// Metrics
/****************************************************************************/
#ifdef TEST_ENABLE_DBG_METRICS
uint32_t gXcpWritePendingCount = 0;
uint32_t gXcpCalSegPublishAllCount = 0;
uint32_t gXcpDaqEventCount = 0;
uint32_t gXcpTxPacketCount = 0;
uint32_t gXcpTxMessageCount = 0;
uint32_t gXcpTxIoVectorCount = 0;
uint32_t gXcpRxPacketCount = 0;
#endif
/****************************************************************************/
// Logging
/****************************************************************************/
#if defined(OPTION_ENABLE_DBG_PRINTS) && !defined(OPTION_FIXED_DBG_LEVEL) && defined(OPTION_DEFAULT_DBG_LEVEL)
uint8_t gXcpLogLevel = OPTION_DEFAULT_DBG_LEVEL;
// Set the log level
void XcpSetLogLevel(uint8_t level) {
#ifdef OPTION_MAX_DBG_LEVEL
if (level > OPTION_MAX_DBG_LEVEL) {
DBG_PRINTF_ERROR("Set log level %u > OPTION_MAX_DBG_LEVEL %u\n", level, OPTION_MAX_DBG_LEVEL);
} else
#endif
if (level > 3) {
DBG_PRINTF_WARNING("Set log level %u -> %u\n", gXcpLogLevel, level);
}
gXcpLogLevel = level;
}
#else
// Set the log level dummy, log level is a constant or logging is off
void XcpSetLogLevel(uint8_t level) {
(void)level;
DBG_PRINTF_ERROR("XcpSetLogLevel ignored, fixed log level = %u\n", OPTION_FIXED_DBG_LEVEL);
}
#endif
/****************************************************************************/
// Test instrumentation
/****************************************************************************/
#ifdef XCP_ENABLE_TEST_CHECKS
#define check_len(n) \
if (CRO_LEN < (n)) { \
err = CRC_CMD_SYNTAX; \
goto negative_response; \
}
#else
#define check_len(n)
#endif
#ifdef DBG_LEVEL
static void XcpPrintCmd(const tXcpCto *cro);
static void XcpPrintRes(const tXcpCto *crm);
static void XcpPrintDaqList(uint16_t daq);
#endif
/****************************************************************************/
/* Status */
/****************************************************************************/
bool XcpIsActivated(void) { return isActivated(); }
uint8_t XcpGetInitMode(void) { return local.init_mode; }
uint16_t XcpGetSessionStatus(void) { return shared.session_status; }
bool XcpIsStarted(void) { return isStarted(); }
bool XcpIsConnected(void) { return isConnected(); }
bool XcpIsDaqRunning(void) { return isDaqRunning(); }
bool XcpIsDaqEventRunning(uint16_t event) {
if (!isDaqRunning())
return false; // DAQ not running
for (uint16_t daq = 0; daq < shared.daq_lists.daq_count; daq++) {
if ((DaqListState(daq) & DAQ_STATE_RUNNING) == 0)
continue; // DAQ list not active
if (DaqListEventChannel(daq) == event)
return true; // Event is associated to this DAQ list
}
return false;
}
#ifdef XCP_ENABLE_DAQ_CLOCK_MULTICAST
uint16_t XcpGetClusterId(void) { return local.cluster_id; }
#endif
uint64_t XcpGetDaqStartTime(void) { return local.daq_start_clock; }
uint32_t XcpGetDaqOverflowCount(void) { return shared.daq_overflow_count; }
/**************************************************************************/
/* Project/ECU name */
/**************************************************************************/
// Set the project name
static void XcpSetProjectName(const char *name) {
assert(name != NULL);
strncpy(local_mut.project_name, name, XCP_PROJECT_NAME_MAX_LENGTH);
local_mut.project_name[XCP_PROJECT_NAME_MAX_LENGTH] = 0;
DBG_PRINTF3("Project Name = '%s'\n", local.project_name);
}
// Get the project name
const char *XcpGetProjectName(void) {
if (STRNLEN(local.project_name, XCP_PROJECT_NAME_MAX_LENGTH) == 0) {
assert(0 && "Project name not set");
return "";
}
return local.project_name;
}
/**************************************************************************/
/* EPK version string */
/**************************************************************************/
// Set the EPK, used by XcpInit()
// Copy the EPK string to a static buffer in the local state and remove unwanted characters (space, tab, colon)
static void XcpSetEpk(const char *epk) {
assert(epk != NULL);
strncpy(local_mut.epk, epk, XCP_EPK_MAX_LENGTH);
local_mut.epk[XCP_EPK_MAX_LENGTH] = 0;
// Remove unwanted characters from the EPK string
for (char *p = local_mut.epk; *p; p++) {
if (*p == ' ' || *p == '\t' || *p == ':') {
*p = '_'; // Replace with underscores
}
}
DBG_PRINTF3("EPK = '%s'\n", local.epk);
}
// Get a reference to the EPK in a static lifetime buffer
// local.epk
const char *XcpGetLocalEpk(void) {
if (STRNLEN(local.epk, XCP_EPK_MAX_LENGTH) == 0) {
assert(0 && "EPK not set");
return "";
}
return local.epk;
}
// Get a reference to the EPK in a static lifetime buffer
// local.epk or shared.shm_header.ecu_epk depending on the build configuration
// Only in SHM mode there is a difference to XcpGetLocalEpk(), the ecu EPK is for the complete multi application system, while XcpGetLocalEpk() is for the application
const char *XcpGetEcuEpk(void) {
#ifdef OPTION_SHM_MODE // get ecu epk which is a hash for all applications
assert(XcpShmGetAppCount() > 0);
return XcpShmGetEcuEpk();
#else // ecu epk is the same as application epk
return XcpGetLocalEpk();
#endif
}
/****************************************************************************/
/* Calibration memory access */
/****************************************************************************/
/*
XcpWriteMta is not performance critical, but critical for data consistency.
It is used to modify calibration variables.
For size 1, 2, 4, 8 it uses single "atomic" writes assuming valid aligned target memory locations.
Its responsibility is only to copy memory. Any considerations regarding thread safety must be explicitly managed.
This is also a requirement to the tool, which must ensure that the data is consistent by choosing the right granularity for DOWNLOAD and SHORT_DOWNLOAD operations.
*/
// Copy of size bytes from data to local.mta_ptr or local.mta_addr depending on the addressing mode
uint8_t XcpWriteMta(uint8_t size, const uint8_t *data) {
#ifdef XCP_ENABLE_SEG_ADDRESSING
// EXT == XCP_ADDR_EXT_SEG calibration segment memory access
if (XcpAddrIsSeg(local.mta_ext)) {
uint8_t res = XcpCalSegWriteMemory(local.mta_addr, size, data);
local_mut.mta_addr += size;
return res;
}
#endif
#ifdef XCP_ENABLE_APP_ADDRESSING
// EXT == XCP_ADDR_EXT_APP Application specific memory access
if (XcpAddrIsApp(local.mta_ext)) {
uint8_t res = ApplXcpWriteMemory(local.mta_addr, size, data);
local_mut.mta_addr += size;
return res;
}
#endif
// Standard memory access by pointer local.mta_ptr
if (local.mta_ext == XCP_ADDR_EXT_PTR) {
if (local.mta_ptr == NULL)
return CRC_ACCESS_DENIED;
// TEST
// Test data consistency: slow bytewise write to increase probability for multithreading data consistency problems
// while (size-->0) {
// *local_mut.mta_ptr++ = *data++;
// sleepUs(1);
// }
// Fast write with "atomic" copies of basic types, assuming correctly aligned target memory locations
switch (size) {
case 1:
*local_mut.mta_ptr = *data;
break;
case 2:
*(uint16_t *)local_mut.mta_ptr = *(uint16_t *)data;
break;
case 4:
*(uint32_t *)local_mut.mta_ptr = *(uint32_t *)data;
break;
case 8:
*(uint64_t *)local_mut.mta_ptr = *(uint64_t *)data;
break;
default:
memcpy(local_mut.mta_ptr, data, size);
break;
}
local_mut.mta_ptr += size;
return 0; // Ok
}
return CRC_ACCESS_DENIED; // Access violation, illegal address or extension
}
// Copying of size bytes from data to local.mta_ptr or local.mta_addr, depending on the addressing mode
uint8_t XcpReadMta(uint8_t size, uint8_t *data) {
#ifdef XCP_ENABLE_SEG_ADDRESSING
// EXT == XCP_ADDR_EXT_SEG calibration segment memory access
if (XcpAddrIsSeg(local.mta_ext)) {
uint8_t res = XcpCalSegReadMemory(local.mta_addr, size, data);
local_mut.mta_addr += size;
return res;
}
#endif
#ifdef XCP_ENABLE_APP_ADDRESSING
// EXT == XCP_ADDR_EXT_APP Application specific memory access
if (XcpAddrIsApp(local.mta_ext)) {
uint8_t res = ApplXcpReadMemory(local.mta_addr, size, data);
local_mut.mta_addr += size;
return res;
}
#endif
// Ext == XCP_ADDR_EXT_PTR - Standard memory access by pointer
if (local.mta_ext == XCP_ADDR_EXT_PTR) {
if (local.mta_ptr == NULL)
return CRC_ACCESS_DENIED;
memcpy(data, local.mta_ptr, size);
local_mut.mta_ptr += size;
return 0; // Ok
}
#if defined(XCP_ENABLE_IDT_A2L_UPLOAD) || defined(XCP_ENABLE_IDT_ELF_UPLOAD)
// Ext == XCP_ADDR_EXT_FILE - A2L or ELF file upload address space
if (local.mta_ext == XCP_ADDR_EXT_FILE) {
if (!ApplXcpReadFile(size, local.mta_addr, data))
return CRC_ACCESS_DENIED; // Access violation
local_mut.mta_addr += size;
return 0; // Ok
}
#endif
return CRC_ACCESS_DENIED; // Access violation, illegal address or addressing mode
}
// Set MTA
// Sets the memory transfer address in local.mta_addr/local.mta_ext
// Absolute addressing mode:
// Converted to pointer addressing mode local.mta_ptr=ApplXcpGetBaseAddr()+XcpAddrDecodeAbsOffset(local.mta_addr) and local.mta_ext=XCP_ADDR_EXT_PTR
// EPK access is local.mta_ptr=XcpGetEcuEpk() and local.mta_ext=XCP_ADDR_EXT_PTR
// Absolute access to calibration segments is converted to segment relative addressing mode local.mta_ext=XCP_ADDR_EXT_SEG
// Other addressing mode are left unchanged
// Called by XCP commands SET_MTA, SHORT_DOWNLOAD and SHORT_UPLOAD
uint8_t XcpSetMta(uint8_t ext_, uint32_t addr_) {
local_mut.mta_ext = ext_;
local_mut.mta_addr = addr_;
local_mut.mta_ptr = NULL; // MtaPtr not defined
// If not EPK calibration segment or addressing mode 0 is absolute
#if !defined(XCP_ENABLE_EPK_CALSEG) || (defined(XCP_ENABLE_ABS_ADDRESSING) && (XCP_ADDR_EXT_ABS == 0))
// Direct EPK access
if (local.mta_ext == XCP_ADDR_EXT_EPK && local.mta_addr == XCP_ADDR_EPK) {
local_mut.mta_ptr = (uint8_t *)XcpGetEcuEpk();
local_mut.mta_ext = XCP_ADDR_EXT_PTR;
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_PTR p=%p\n", local_mut.mta_ptr);
return CRC_CMD_OK;
}
#endif
#ifdef XCP_ENABLE_DYN_ADDRESSING
// Event relative addressing mode
if (XcpAddrIsDyn(local.mta_ext)) {
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_DYN:%08X\n", local_mut.mta_addr);
return CRC_CMD_OK;
}
#endif
#ifdef XCP_ENABLE_REL_ADDRESSING
// Relative addressing mode
if (XcpAddrIsRel(local.mta_ext)) {
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_REL:%08X\n", local_mut.mta_addr);
return CRC_CMD_OK;
}
#endif
#ifdef XCP_ENABLE_SEG_ADDRESSING
// Segment relative addressing mode
if (XcpAddrIsSeg(local.mta_ext)) {
if ((local.mta_addr & 0x80000000) == 0) {
#ifdef XCP_ENABLE_ABS_ADDRESSING
// @@@@ TODO: Workaround CANape bug, address extension != 0 for calibration variables sometimes ignored
DBG_PRINTF_WARNING("XcpSetMta: Address extension SEG < 0x80000000, converting to ABS addressing mode, addr=0x%08" PRIx32 "\n", local.mta_addr);
local_mut.mta_ext = XCP_ADDR_EXT_ABS;
#else
return CRC_ACCESS_DENIED; // Access violation,
#endif
}
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_SEG:%08X\n", local_mut.mta_addr);
return CRC_CMD_OK;
}
#endif
#ifdef XCP_ENABLE_APP_ADDRESSING
// Application specific addressing mode
if (XcpAddrIsApp(local.mta_ext)) {
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_APP:%08X\n", local_mut.mta_addr);
return CRC_CMD_OK;
}
#endif
#ifdef XCP_ENABLE_ABS_ADDRESSING
// Absolute addressing mode
if (XcpAddrIsAbs(local.mta_ext)) {
#ifdef OPTION_SHM_MODE // decode app_id from address extension and check it matches the current process application id
// In SHM mode, check application id matches the address extension
uint8_t app_id = XcpAddrExtDecodeAppId(local.mta_ext);
if (app_id != XcpShmGetAppId()) {
DBG_PRINTF_ERROR("XcpSetMta: Absolute address extension must have the application id of the current process, ext=%u, app_id=%u, current_app_id=%u\n", local.mta_ext,
app_id, XcpShmGetAppId());
return CRC_ACCESS_DENIED; // Error invalid application id
}
#endif // SHM_MODE
local_mut.mta_ptr = (uint8_t *)ApplXcpGetBaseAddr() + XcpAddrDecodeAbsOffset(local.mta_addr);
local_mut.mta_ext = XCP_ADDR_EXT_PTR;
#if defined(XCP_ENABLE_CALSEG_LIST) && defined(XCP_ENABLE_ABS_ADDRESSING) && (XCP_ADDR_EXT_ABS == 0x00)
// Check for calibration segment absolute address (XcpSetMta is not performance critical)
tXcpCalSegIndex calseg_index = XcpFindCalSegByAddr(local.mta_ptr);
if (calseg_index != XCP_UNDEFINED_CALSEG) {
const tXcpCalSeg *c = CalSegPtr(calseg_index);
local_mut.mta_ext = XCP_ADDR_EXT_SEG;
local_mut.mta_addr = XcpAddrEncodeSegIndex(calseg_index, local.mta_ptr - c->h.default_page_ptr); // Convert to segment relative address
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_ABS -> XCP_ADDR_EXT_SEG, addr=%08X\n", local_mut.mta_addr);
} else {
DBG_PRINTF6("XcpSetMta: XCP_ADDR_EXT_ABS, a=%08X, p=%p\n", local_mut.mta_addr, local_mut.mta_ptr);
}
#endif
return ApplXcpCheckMemory(local.mta_ext, local.mta_addr, 0 /* size not known here */);
}
#endif
return CRC_OUT_OF_RANGE; // Unsupported addressing mode
}
/**************************************************************************/
/* Checksum calculation */
/**************************************************************************/
/* Table for CCITT checksum calculation */
#ifdef XCP_ENABLE_CHECKSUM
#if (XCP_CHECKSUM_TYPE == XCP_CHECKSUM_TYPE_CRC16CCITT)
static const uint16_t gXcpCRC16CCITTtab[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7u, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1efu, 0x1231, 0x0210, 0x3273, 0x2252,
0x52b5, 0x4294, 0x72f7, 0x62d6u, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3deu, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485u,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58du, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4u, 0xb75b, 0xa77a, 0x9719, 0x8738,
0xf7df, 0xe7fe, 0xd79d, 0xc7bcu, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823u, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92bu,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12u, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1au, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5,
0x2c22, 0x3c03, 0x0c60, 0x1c41u, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49u, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70u,
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78u, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16fu, 0x1080, 0x00a1, 0x30c2, 0x20e3,
0x5004, 0x4025, 0x7046, 0x6067u, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35eu, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256u,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50du, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405u, 0xa7db, 0xb7fa, 0x8799, 0x97b8,
0xe75f, 0xf77e, 0xc71d, 0xd73cu, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634u, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9abu,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3u, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9au, 0x4a75, 0x5a54, 0x6a37, 0x7a16,
0x0af1, 0x1ad0, 0x2ab3, 0x3a92u, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9u, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1u,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8u, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0u};
#endif
static uint8_t calcChecksum(uint32_t checksum_size, uint32_t *checksum_result) {
assert(checksum_size > 0);
assert(checksum_result != NULL);
#if (XCP_CHECKSUM_TYPE == XCP_CHECKSUM_TYPE_CRC16CCITT)
// CRC16 CCITT
uint16_t sum = 0xFFFF;
uint8_t value = 0;
for (uint32_t n = checksum_size; n > 0; n--) {
uint8_t res = XcpReadMta(1, &value);
if (res != CRC_CMD_OK)
return res;
sum = gXcpCRC16CCITTtab[((uint8_t)(sum >> 8)) ^ value] ^ (uint16_t)(sum << 8);
}
*checksum_result = (uint32_t)sum;
#else
// ADD44
uint32_t sum = 0;
uint32_t value = 0;
for (uint32_t n = checksum_size; n >= sizeof(value); n -= sizeof(value)) {
uint8_t res = XcpReadMta(sizeof(value), (uint8_t *)&value);
if (res != CRC_CMD_OK)
return res;
sum += value;
}
*checksum_result = (uint32_t)sum;
#endif
return CRC_CMD_OK;
}
#endif // XCP_ENABLE_CHECKSUM
/**************************************************************************/
/* Eventlist */
/**************************************************************************/
#ifdef XCP_ENABLE_DAQ_EVENT_LIST
// Dynamic event list
//--------------------
// Using the less expensive getEventCount() is a deliberate choice for performance critical code paths like XcpEvent(),
// where the visibility of new events created by other threads is not critical, while acquireEventCount() is used for thread safe access to the event count with guaranteed
// visibility of new events created by other threads.
#define getEventCount() (uint16_t)atomic_load_explicit(&shared.event_list.count, memory_order_relaxed)
#define acquireEventCount() (uint16_t)atomic_load_explicit(&shared.event_list.count, memory_order_acquire)
#define releaseEventCount(n) atomic_store_explicit(&shared_mut_safe.event_list.count, n, memory_order_release)
// Initialize the XCP event list
void XcpInitEventList(void) {
// Reset event list count
releaseEventCount(0);
mutexInit(&local_mut.event_list_mutex, false, 1000);
}
// Get a pointer to the XCP event struct
const tXcpEvent *XcpGetEvent(tXcpEventId event) {
if (!isActivated() || event >= acquireEventCount())
return NULL;
return &shared.event_list.event[event];
}
// Get the current event count, thread safe but visibility of new events created by other threads is not guaranteed
uint16_t XcpGetEventCount(void) { return getEventCount(); }
// Get the cycle time of an event, return 0 if not found
uint32_t XcpGetEventCycleTime(tXcpEventId event) {
const tXcpEvent *e = XcpGetEvent(event);
if (e == NULL)
return 0;
return e->cycle_time_ns;
}
// Get the priority of an event, return 0 if not found
uint8_t XcpGetEventPriority(tXcpEventId event) {
const tXcpEvent *e = XcpGetEvent(event);
if (e == NULL)
return 0;
return (e->flags & XCP_DAQ_EVENT_FLAG_PRIORITY) ? 1 : 0;
}
// Get the events application id
#ifdef OPTION_SHM_MODE // get event application id
uint8_t XcpGetEventAppId(tXcpEventId event) {
if (!isActivated() || event >= getEventCount())
return 0;
return shared.event_list.event[event].app_id;
}
#endif // SHM_MODE
// Get the full event name, including instance index postfix if applicable
// Result has lifetime until next call of XcpGetEventName()
const char *XcpGetEventName(tXcpEventId event) {
if (!isActivated() || event >= getEventCount())
return NULL;
const tXcpEvent *e = &shared.event_list.event[event];
if (e->index > 0) {
// Event instance, append instance index to the name
SNPRINTF(local_mut.event_name_buf, sizeof(local.event_name_buf), "%s_%u", e->name, e->index);
return local.event_name_buf;
}
return (const char *)&shared.event_list.event[event].name;
}
// Get the event index (1..), return 0 if not found
uint16_t XcpGetEventIndex(tXcpEventId event) {
if (!isActivated() || event >= getEventCount())
return 0;
return shared.event_list.event[event].index;
}
// @@@@ TODO: Not process-safe
static tXcpEventId XcpFindEventInstances(const char *name, uint16_t *pcount) {
uint16_t id = XCP_UNDEFINED_EVENT_ID;
if (pcount != NULL)
*pcount = 0;
if (isActivated()) {
uint16_t count = acquireEventCount();
for (uint16_t i = 0; i < count; i++) {
#ifdef OPTION_SHM_MODE // find only events owned by this application process
if (shared.event_list.event[i].app_id == XcpShmGetAppId() && strcmp(shared.event_list.event[i].name, name) == 0) {
#else
if (strcmp(shared.event_list.event[i].name, name) == 0) {
#endif
if (pcount != NULL)
*pcount += 1;
if (id == XCP_UNDEFINED_EVENT_ID)
id = i; // Remember the first found event
}
}
}
return id;
}
// Find an event by name, return XCP_UNDEFINED_EVENT_ID if not found
// Thread safe
tXcpEventId XcpFindEvent(const char *name) { return XcpFindEventInstances(name, NULL); }
// Create an XCP event
// Not thread safe
// Returns the new XCP event id or XCP_UNDEFINED_EVENT_ID when out of memory
tXcpEventId XcpCreateIndexedEvent(const char *name, uint16_t index, uint32_t cycle_time_ns, uint8_t priority) {
if (!isActivated()) {
return XCP_UNDEFINED_EVENT_ID; // Uninitialized
}
assert(name != NULL);
// Check name length
size_t nameLen = STRNLEN(name, XCP_MAX_EVENT_NAME + 1);
if (nameLen > XCP_MAX_EVENT_NAME) {
DBG_PRINTF_ERROR("event name '%.*s' too long (%zu > %d chars)\n", XCP_MAX_EVENT_NAME, name, nameLen, XCP_MAX_EVENT_NAME);
return XCP_UNDEFINED_EVENT_ID;
}
// Check event count
uint16_t e = acquireEventCount();
if (e >= XCP_MAX_EVENT_COUNT) {
DBG_PRINT_ERROR("too many events\n");
return XCP_UNDEFINED_EVENT_ID; // Out of memory
}
// Caller is responsible for thread safety
shared_mut_safe.event_list.event[e].index = index; // Index of the event instance
strncpy(shared_mut_safe.event_list.event[e].name, name, XCP_MAX_EVENT_NAME);
shared_mut_safe.event_list.event[e].name[XCP_MAX_EVENT_NAME] = 0;
shared_mut_safe.event_list.event[e].flags = (priority > 0) ? XCP_DAQ_EVENT_FLAG_PRIORITY : 0;
#ifdef XCP_ENABLE_DAQ_PRESCALER
shared_mut_safe.event_list.event[e].daq_prescaler = 0;
shared_mut_safe.event_list.event[e].daq_prescaler_cnt = 0;
#endif
shared_mut_safe.event_list.event[e].daq_first = XCP_UNDEFINED_DAQ_LIST;
shared_mut_safe.event_list.event[e].cycle_time_ns = cycle_time_ns;
// In SHM mode, assign event to the application, different apps have different namespace
#ifdef OPTION_SHM_MODE // store event application id in event
shared_mut_safe.event_list.event[e].app_id = XcpShmGetAppId();
#endif // SHM_MODE
releaseEventCount(e + 1); // Publish new event, visibility assured, when others acquire the event count, but overall function not thread safe, must be called with locked mutex
DBG_PRINTF3("Create Event %u: %s index=%u, cycle=%uns, prio=%u\n", e, shared.event_list.event[e].name, index, cycle_time_ns, priority);
return e;
}
// Add a measurement event to event list, return event id (0..MAX_EVENT-1),
// If name exists, an event instance index is generated (and only in A2L appended to the event name)
// Thread safe by mutex
// @@@@ TODO: Find a process safe solution for SHM mode
tXcpEventId XcpCreateEventInstance(const char *name, uint32_t cycle_time_ns, uint8_t priority) {
if (!isActivated()) {
return XCP_UNDEFINED_EVENT_ID; // Uninitialized
}
uint16_t count = 0;
mutexLock(&local_mut.event_list_mutex);
XcpFindEventInstances(name, &count);
// @@@@ TODO: Use preloaded event instances instead of creating a new instance
// Event instances have no identity, could use any unused preload event instance with this name
tXcpEventId id = XcpCreateIndexedEvent(name, count + 1, cycle_time_ns, priority);
mutexUnlock(&local_mut.event_list_mutex);
return id;
}
// Add a measurement event to the event list, return event id (0..MAX_EVENT-1)
// If name already exists, just return the existing id
// Thread safe by mutex
// @@@@ TODO: Find a process safe solution for SHM mode
tXcpEventId XcpCreateEvent(const char *name, uint32_t cycle_time_ns, uint8_t priority) {
if (!isActivated()) {
return XCP_UNDEFINED_EVENT_ID; // Uninitialized
}
uint16_t count = 0;
mutexLock(&local_mut.event_list_mutex);
tXcpEventId id = XcpFindEventInstances(name, &count);
if (id != XCP_UNDEFINED_EVENT_ID) {
mutexUnlock(&local_mut.event_list_mutex);
DBG_PRINTF5("Event '%s' already defined, id=%u\n", name, id);
assert(count == 1); // Creating additional event instances is not supported, use XcpCreateEventInstance
return id; // Event already exists, return the existing event id, event could be preloaded from binary freeze file for A2L stability
}
id = XcpCreateIndexedEvent(name, 0, cycle_time_ns, priority);
mutexUnlock(&local_mut.event_list_mutex);
return id;
}
// Pre-register all tXcpEventDescriptor variables placed in the xcp_evts section by DaqCreateEvent().
// Must be called after SS_ACTIVATED is set (XcpCreateEvent requires isActivated()).
// If a persistence file was loaded before this call, events are matched by name and keep their saved id.
static uint16_t XcpRegisterSectionEvents(void) {
uint16_t count = 0;
#ifndef _WIN
const tXcpEventDescriptor *begin = __start_xcp_evts;
const tXcpEventDescriptor *end = __stop_xcp_evts;
if (begin != NULL && end != NULL && begin < end) {
for (const tXcpEventDescriptor *e = begin; e < end; e++) {
tXcpEventId id = XcpFindEvent(e->name);
if (id == XCP_UNDEFINED_EVENT_ID) {
id = XcpCreateEvent(e->name, e->cycle_time_ns, e->priority);
assert(id != XCP_UNDEFINED_EVENT_ID);
count++;
}
}
} else {
DBG_PRINT_WARNING("No xcp_evts section found\n");
}
#endif
if (count > 0)
DBG_PRINTF3(ANSI_COLOR_GREEN "Preregistered %u events from event descriptor section\n" ANSI_COLOR_RESET, count);