-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlib_iso14229.c
1612 lines (1347 loc) · 51.4 KB
/
lib_iso14229.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
/*!
@file iso14229_1.c
@brief Source file of the ISO14229-1 library
@t.odo -
---------------------------------------------------------------------------
MIT License
Copyright (c) 2020 Ioannis D. (devcoons)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/
/******************************************************************************
* Includes
******************************************************************************/
#include "lib_iso14229.h"
#ifdef LIB_ISO14229_1_ENABLED
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
static iso14299_1_sid_t sid_list[] =
{
{.sid = UDS_SRVC_DiagnosticSessionControl, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_ECUReset, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_SecurityAccess, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_CommunicationControl, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_TesterPresent, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_Authentication, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_SecuredDataTransmission, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_ControlDTCSetting, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_ResponseOnEvent, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_LinkControl, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_ReadDataByIdentifier, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_ReadMemoryByAddress, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_ReadScalingDataByIdentifier, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_ReadDataByPeriodicIdentifier, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_DynamicallyDefineDataIdentifier, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_WriteDataByIdentifier, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_WriteMemoryByAddress, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_ClearDiagnosticInformation, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_ReadDTCInformation, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_InputOutputControlByIdentifier, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_RoutineControl, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_RequestDownload, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_RequestUpload, .is_supported = iso14229_1_NO },
{.sid = UDS_SRVC_TransferData, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_RequestTransferExit, .is_supported = iso14229_1_YES },
{.sid = UDS_SRVC_RequestFileTransfer, .is_supported = iso14229_1_NO }
};
static n_req_t out_frame =
{
.n_ai.n_pr = 0x06,
.n_ai.n_sa = ISO14229_1_DEVICE_ADDRESS,
.n_ai.n_ta = 0x00,
.n_ai.n_ae = 0x00,
.n_ai.n_tt = N_TA_T_PHY,
.msg = {0},
.msg_sz = 0,
};
__attribute__ ((section(".buffers"))) iso14229_t uds_server =
{
.n_pr = 0x06,
.n_sa = ISO14229_1_DEVICE_ADDRESS,
.nl.addr_md = N_ADM_FIXED,
.nl.fr_id_type = CBUS_ID_T_EXTENDED,
.nl.clbs.get_ms = iso14229_getms,
.nl.config.stmin = 0x03,
.nl.config.bs = 0x0F,
.nl.config.n_bs = 100,
.nl.config.n_cr = 3,
.last_updated = 0
};
static __attribute__ ((section(".buffers")))
n_indn_t iso14229_1_received_indn = {0};
static __attribute__ ((section(".buffers")))
uint8_t transfer_data_collection[UDS_TDC_SZ] = {0};
static __attribute__ ((section(".buffers")))
uint8_t temporary_flash_64bytes[256] = {0};
static __attribute__ ((section(".buffers")))
uint8_t iso14229_1_temporary_buffer[514] = {0};
static __attribute__ ((section(".buffers")))
uint32_t transfer_data_collection_pos = 0;
static __attribute__ ((section(".buffers")))
uint32_t iso14229_1_timeout_extra_time = 0;
/******************************************************************************
* Declaration | Static Functions
******************************************************************************/
static void indn(n_indn_t* info);
static void on_error(n_rslt err_type);
/******************************************************************************
* Definition | Static Functions
******************************************************************************/
static void on_error(n_rslt err_type)
{
uds_server.errn = 1;
UNUSED(err_type);
}
static void indn(n_indn_t* info)
{
if(info->rslt != N_OK)
return;
memmove(&iso14229_1_received_indn, info,sizeof(n_indn_t));
uds_server.p_msg = 1;
}
static void cfm(n_cfm_t* info)
{
uds_server.s_msg = 1;
UNUSED(info);
}
/******************************************************************************
* Definition | Public Functions
******************************************************************************/
void iso14229_init()
{
memset(&iso14229_1_received_indn,0,sizeof(n_indn_t));
memset(transfer_data_collection,0,UDS_TDC_SZ);
memset(temporary_flash_64bytes,0,256);
memset(iso14229_1_temporary_buffer,0,514);
transfer_data_collection_pos = 0;
iso14229_1_timeout_extra_time = 0;
uds_server.n_pr = 0x06;
uds_server.n_sa = ISO14229_1_DEVICE_ADDRESS;
uds_server.p_msg = 0;
memset(&uds_server.nl,0,sizeof(iso15765_t));
uds_server.nl.addr_md = N_ADM_FIXED;
uds_server.nl.fr_id_type = CBUS_ID_T_EXTENDED,
uds_server.nl.clbs.send_frame = send_frame;
uds_server.nl.clbs.on_error = on_error;
uds_server.nl.clbs.get_ms = iso14229_getms;
uds_server.nl.clbs.indn = indn;
uds_server.nl.clbs.cfm = cfm;
uds_server.nl.config.stmin = 0x2;
uds_server.nl.config.bs = 0x00;
uds_server.nl.config.n_bs = 0x96;
uds_server.nl.config.n_cr = 0x96;
uds_server.last_updated = iso14229_getms();
iso15765_init(&uds_server.nl);
iso14229_postinit();
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
uint8_t iso14229_inactive()
{
if(uds_sessions[0].sts == A_ACTIVE)
{
if((uds_server.last_updated + 128 + iso14229_1_timeout_extra_time) < iso14229_getms())
return 0;
}
return 1;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
uint8_t iso14229_process()
{
iso14229_1_srvc_timeouts();
if((iso15765_process(&uds_server.nl) & N_IDLE) == 0)
{
uds_server.last_updated = iso14229_getms();
}
if(iso14229_1_srvc_routines_process() == RTN_ACTIVE)
{
uds_server.last_updated = iso14229_getms();
}
iso14229_1_srvc_input_output_control_process();
if(uds_server.p_msg != 1)
return 0;
uds_server.last_updated = iso14229_getms();
uds_server.p_msg = 0;
if(sid_supported(iso14229_1_received_indn.msg[0]) != iso14229_1_YES)
goto gt_iso14229_process_nack;
iso14229_1_srvc_diagnostic_session_refresh_timeout();
uint8_t is_fnr = iso14229_1_received_indn.n_ai.n_tt == N_TA_T_FUNC ? 1 : 0;
switch(iso14229_1_received_indn.msg[0])
{
case UDS_SRVC_DiagnosticSessionControl:
if(is_fnr == 0)
iso14229_1_srvc_diagnostic_session_control();
break;
case UDS_SRVC_ECUReset:
iso14229_1_uds_srvc_ecu_reset();
break;
case UDS_SRVC_SecurityAccess:
if(is_fnr == 0)
iso14229_1_srvc_security_access();
break;
case UDS_SRVC_CommunicationControl:
break;
case UDS_SRVC_TesterPresent:
iso14229_1_srvc_tester_present();
break;
case UDS_SRVC_Authentication:
break;
case UDS_SRVC_SecuredDataTransmission:
break;
case UDS_SRVC_ControlDTCSetting:
break;
case UDS_SRVC_ResponseOnEvent:
break;
case UDS_SRVC_LinkControl:
break;
case UDS_SRVC_ReadDataByIdentifier:
iso14229_srvc_read_data_by_localid();
iso14229_1_timeout_extra_time = 5000;
break;
case UDS_SRVC_ReadMemoryByAddress:
iso14229_1_srvc_read_memory_by_address();
break;
case UDS_SRVC_ReadScalingDataByIdentifier:
break;
case UDS_SRVC_ReadDataByPeriodicIdentifier:
break;
case UDS_SRVC_DynamicallyDefineDataIdentifier:
break;
case UDS_SRVC_WriteDataByIdentifier:
iso14229_srvc_write_data_by_localid();
iso14229_1_timeout_extra_time = 5000;
break;
case UDS_SRVC_WriteMemoryByAddress:
break;
case UDS_SRVC_ClearDiagnosticInformation:
iso14229_1_srvc_ClearDiagnosticInformation();
break;
case UDS_SRVC_ReadDTCInformation:
iso14229_1_srvc_readDTCinformation();
break;
case UDS_SRVC_InputOutputControlByIdentifier:
iso14229_1_srvc_input_output_control_by_identifier();
break;
case UDS_SRVC_RoutineControl:
iso14229_1_srvc_routine_control();
break;
case UDS_SRVC_RequestDownload:
if(is_fnr == 0)
iso14229_1_uds_srvc_request_download();
break;
case UDS_SRVC_RequestUpload:
break;
case UDS_SRVC_TransferData:
if(is_fnr == 0)
iso14229_1_srvc_tranfer_data();
break;
case UDS_SRVC_RequestTransferExit:
if(is_fnr == 0)
iso14229_1_srvc_request_transfer_exit();
break;
case UDS_SRVC_RequestFileTransfer:
break;
default:
break;
}
return 1;
gt_iso14229_process_nack:
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,
iso14229_1_received_indn.msg[0],UDS_NRC_SNS);
return 1;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
iso14229_1_status sid_supported(uint8_t sid)
{
uint32_t list_sz = sizeof(sid_list)/sizeof(iso14299_1_sid_t);
for(register uint32_t i = 0;i<list_sz;i++)
{
if(sid_list[i].sid == sid)
return sid_list[i].is_supported == iso14229_1_YES
? iso14229_1_YES
: iso14229_1_NO;
}
return iso14229_1_NO;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
iso14229_1_status sub_sid_supported(uint8_t sid,uint8_t sub)
{
uint32_t list_sz = sizeof(sid_list)/sizeof(iso14299_1_sid_t);
UNUSED(sub);
for(register uint32_t i = 0;i<list_sz;i++)
{
if(sid_list[i].sid == sid)
return sid_list[i].is_supported == iso14229_1_YES
? iso14229_1_YES
: iso14229_1_NO;
}
return iso14229_1_NO;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_send(n_ai_t *ai, uint8_t* data, uint16_t sz)
{
out_frame.n_ai.n_ae = ai->n_ae;
out_frame.n_ai.n_sa = ISO14229_1_DEVICE_ADDRESS;
out_frame.n_ai.n_ta = ai->n_sa;
out_frame.n_ai.n_pr = ai->n_pr;
out_frame.n_ai.n_tt = N_TA_T_PHY;
out_frame.fr_fmt = iso14229_1_received_indn.fr_fmt;
out_frame.msg_sz = sz;
memmove(out_frame.msg,data,sz);
iso15765_send(&uds_server.nl,&out_frame);
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_send_NRC(n_ai_t *ai,uint8_t sid, uint8_t code)
{
static uint8_t data[3];
data[0] = 0x7F;
data[1] = sid;
data[2] = code;
iso14229_send(ai, data, code == 0 ? 2 : 3);
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_1_srvc_timeouts()
{
uint32_t list_sz = sizeof(uds_sessions)/sizeof(uds_session_t);
for(register uint32_t i = 0;i<list_sz;i++)
{
if((uds_sessions[i].sts & 0x0F) != 0 )
{
if((iso14229_getms() - uds_sessions[i].timeout.last_update) > uds_sessions[i].timeout.time_limit )
{
uint32_t sa_list_sz = sizeof(uds_security_accesses)/sizeof(uds_security_access_t);
for(register uint32_t j = 0;j<sa_list_sz;j++)
uds_security_accesses[j].sts = uds_security_accesses[j].default_sts;
if(uds_sessions[i].on_closing != NULL)
uds_sessions[i].on_closing();
for(register uint32_t j = 0;j<list_sz;j++)
{
uds_sessions[j].sts = uds_sessions[j].default_sts;
}
iso14229_1_srvc_diagnostic_session_refresh_timeout();
}
}
}
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_1_srvc_request_transfer_exit()
{
if( (iso14229_1_received_indn.msg_sz != 7))
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
if(uds_tranfer_data.sts != TD_ACTIVE || uds_tranfer_data.remaining_data_len!=0 || transfer_data_collection_pos !=0)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_RSE);
return;
}
uint16_t dtr_crc = (iso14229_1_received_indn.msg[1] << 8) | iso14229_1_received_indn.msg[2];
uint32_t dtr_len = (iso14229_1_received_indn.msg[3] << 24) | (iso14229_1_received_indn.msg[4]<<16) | (iso14229_1_received_indn.msg[5] << 8) | iso14229_1_received_indn.msg[6];
if(dtr_crc == uds_tranfer_data.calculated_crc && dtr_len == uds_tranfer_data.expected_data_len)
{
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[1] = iso14229_1_received_indn.msg[1];
uds_tranfer_data.sts = uds_tranfer_data.default_sts;
uds_download_request.sts = uds_download_request.default_sts;
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,2);
}
else
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_GPF);
return;
}
return;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
intptr_t iso14229_srvc_ioc_get(uds_io_control_by_id_t* h)
{
if(h->ptr_iocontrol != h->ptr_inactive && h->ptr_iocontrol != (uint32_t)&h->out_val)
{
return h->ptr_inactive;
}
return h->ptr_iocontrol;
}
void iso14229_1_srvc_input_output_control_process()
{
uint32_t sessions_list_sz = sizeof(uds_sessions) / sizeof(uds_session_t);
uint32_t session_valid = -1;
for(register uint32_t i = 0; i < sessions_list_sz; i++)
{
if(uds_sessions[i].sts == A_ACTIVE)
session_valid = i;
}
if(session_valid == -1)
return;
uint32_t list_sz = sizeof(uds_io_control_by_id)/sizeof(uds_io_control_by_id_t*);
iocontrol_status sts = RTN_INACTIVE;
for(register uint32_t i = 0;i<list_sz;i++)
{
if(uds_io_control_by_id[i]->sts == IOC_ACTIVE && uds_io_control_by_id[i]->session != uds_sessions[session_valid].id)
{
uds_io_control_by_id[i]->ptr_iocontrol = uds_io_control_by_id[i]->ptr_inactive;
///RESET CONTROL TO MATLAB
uds_io_control_by_id[i]->sts = IOC_INACTIVE;
}
}
}
/* --- ClearDiagnosticInformation (ref:iso14229-1) Cap 12.2 p223----*/
void iso14229_1_srvc_ClearDiagnosticInformation()
{
//Minimum lenght check
if(iso14229_1_received_indn.msg_sz >= 4) //pag 226
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
uint32_t groupOfDtc = 0x00 <<24 | iso14229_1_received_indn.msg[1]<<16 | iso14229_1_received_indn.msg[2]<<8|iso14229_1_received_indn.msg[3];
if(groupOfDtc != 0x00FFFFFF) //GODTC_supported ?
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_ROOR);
return;
}
}
/* --- readDTCinformation (ref:iso14229-1) Cap 12.3 p22----------- */
void iso14229_1_srvc_readDTCinformation()
{
//Minimum lenght check
if(iso14229_1_received_indn.msg_sz < 3) //pag 301
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
uint8_t req_type = __uds_get_subfunction(iso14229_1_received_indn.msg);
uint8_t size=0;
switch(req_type)
{
case UDS_RDTC_RNODTCBSM:/* rep.Num.OfDTCByStatusMask */
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg); //SID +0x40
iso14229_1_temporary_buffer[1] = UDS_RDTC_RNODTCBSM;
iso14229_1_temporary_buffer[2] = 0x01;//ISO_14229-1_DTCFormat
iso14229_1_temporary_buffer[3] = iso14229_1_received_indn.msg[3]; //id
iso14229_1_temporary_buffer[4] = reportNumberOfDTCByStatusMask(0xff)>>8;
iso14229_1_temporary_buffer[5] = reportNumberOfDTCByStatusMask(0xff);
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,6);
break;
case UDS_RDTC_RDTCBSM: /* rep.DTCByStatusMask */
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg); //SID +0x40
iso14229_1_temporary_buffer[1] = UDS_RDTC_RDTCBSM;
iso14229_1_temporary_buffer[2] = iso14229_1_received_indn.msg[3];
size = 3;
reportDTCByStatusMask(0xFF,iso14229_1_temporary_buffer,&size); //id
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,size);
break;
default://Not Supported
break;
}
}
/* --- InputOutput control functional unit (ref:iso14229-1(2020) Cap 13 p.297) ------------ */
void iso14229_1_srvc_input_output_control_by_identifier()
{
//Minimum lenght check
if(iso14229_1_received_indn.msg_sz < 4) //pag 301
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
uds_io_control_by_id_t* current_iocontrol = NULL;
//DID supports service 0x2F in active session AND InputOutput is support
uint32_t list_sz = sizeof(uds_io_control_by_id)/sizeof(uds_io_control_by_id_t*);
uint16_t data_id = iso14229_1_received_indn.msg[1]<<8 | iso14229_1_received_indn.msg[2];
uint8_t session_valid = 0;
uint8_t security_check = 0;
for(register uint32_t i = 0;i<list_sz;i++)
{
if(uds_io_control_by_id[i]->id == data_id && uds_io_control_by_id[i]->id != 0)
{
current_iocontrol = uds_io_control_by_id[i];
break;
}
}
if(current_iocontrol == NULL) // check if IO is present
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,
__uds_get_function(iso14229_1_received_indn.msg), UDS_NRC_ROOR);
return;
}
list_sz = sizeof(uds_sessions) / sizeof(uds_session_t);
for(register uint32_t i = 0; i < list_sz; i++)
{
if(uds_sessions[i].id == current_iocontrol->session && uds_sessions[i].sts == A_ACTIVE)
session_valid = 1;
}
if(session_valid == 0)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,
__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_ROOR);
return;
}
//Total length check
//controlState is supported (if applicable) AND control mask is supported (if applicable)
//authentication check ok? [Not used]
//Security check ok for requested DID?
uint32_t sa_list_sz = sizeof(uds_security_accesses)/sizeof(uds_security_access_t);
for(register uint32_t j = 0;j<sa_list_sz;j++)
{
if(uds_security_accesses[j].access_lvl == current_iocontrol->security_level && uds_security_accesses[j].sts == SA_ACTIVE)
security_check = 1;
}
if(security_check == 0 && current_iocontrol->security_level != 0xFF)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_SAD);
return;
}
//se arrivo qui attivo lo status ioc_active
uint8_t ioc_param = iso14229_1_received_indn.msg[3];
if (ioc_param >= 0x04)// ISOSAERESRVD
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_ROOR);
return;
}
current_iocontrol->sts = IOC_ACTIVE;
//
if(current_iocontrol->var_type == VAR_TYPE_U8 || current_iocontrol->var_type == VAR_TYPE_I8)
current_iocontrol->out_val = iso14229_1_received_indn.msg[4];
else if(current_iocontrol->var_type == VAR_TYPE_U16 || current_iocontrol->var_type == VAR_TYPE_I16)
current_iocontrol->out_val = iso14229_1_received_indn.msg[4]<<8 | iso14229_1_received_indn.msg[5];
else if(current_iocontrol->var_type == VAR_TYPE_U32 || current_iocontrol->var_type == VAR_TYPE_I32)
current_iocontrol->out_val = iso14229_1_received_indn.msg[4]<<24 | iso14229_1_received_indn.msg[5]<<16 | iso14229_1_received_indn.msg[6]<<8 | iso14229_1_received_indn.msg[7];
switch(ioc_param)
{
case 00://RCTECU - returnControlToECU
current_iocontrol->ptr_iocontrol = current_iocontrol->ptr_inactive;
current_iocontrol->sts = IOC_INACTIVE;
break;
case 01: //RTD - resetToDefault
break;
case 02://FCS - freezeCurrentState
break;
case 03://STA - shortTermAdjustment
current_iocontrol->ptr_iocontrol = (intptr_t)¤t_iocontrol->out_val;
current_iocontrol->sts = IOC_ACTIVE;
break;
}
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg); //SID +0x40
iso14229_1_temporary_buffer[1] = __uds_get_subfunction(iso14229_1_received_indn.msg); //0x2F
iso14229_1_temporary_buffer[2] = iso14229_1_received_indn.msg[2]; //id
iso14229_1_temporary_buffer[3] = iso14229_1_received_indn.msg[3]; //id
iso14229_1_temporary_buffer[4] = iso14229_1_received_indn.msg[4]; //val
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,5);
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_1_srvc_routine_control()
{
if(iso14229_1_received_indn.msg_sz < 4)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
volatile uds_routine_local_id_t* current_routine = NULL;
uint16_t routine_cmd = iso14229_1_received_indn.msg[1];
uint16_t routine_id = iso14229_1_received_indn.msg[2]<< 8 | iso14229_1_received_indn.msg[3];
uint8_t session_valid = 0;
uint8_t security_check = 0;
uint8_t* routine_args = NULL;
uint16_t routing_args_sz = 0;
uint32_t list_sz = sizeof(uds_routines)/sizeof(uds_routine_local_id_t);
for(register uint32_t i = 0;i<list_sz;i++)
{
if(uds_routines[i].id == routine_id && uds_routines[i].rountine != NULL)
{
current_routine = &uds_routines[i];
}
}
if(current_routine == NULL)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai, __uds_get_function(iso14229_1_received_indn.msg), UDS_NRC_ROOR);
return;
}
uint8_t is_fnr = iso14229_1_received_indn.n_ai.n_tt == N_TA_T_FUNC ? 1 : 0;
if(current_routine->fnr_enabled != 1 && is_fnr == 1)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai, __uds_get_function(iso14229_1_received_indn.msg), UDS_NRC_ROOR);
return;
}
list_sz = sizeof(uds_sessions) / sizeof(uds_session_t);
for(register uint32_t i = 0; i < list_sz; i++)
{
if(uds_sessions[i].id == current_routine->session && uds_sessions[i].sts == A_ACTIVE)
session_valid = 1;
}
if(session_valid == 0)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_ROOR);
return;
}
uint32_t sa_list_sz = sizeof(uds_security_accesses)/sizeof(uds_security_access_t);
for(register uint32_t j = 0;j<sa_list_sz;j++)
{
if(uds_security_accesses[j].access_lvl >= current_routine->security_level && uds_security_accesses[j].sts == SA_ACTIVE)
security_check = 1;
}
if(security_check == 0 && current_routine->security_level != 0xFF)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_SAD);
return;
}
routing_args_sz = iso14229_1_received_indn.msg_sz - 4;
if(routing_args_sz != 0)
routine_args = &iso14229_1_received_indn.msg[4];
current_routine->rst = NULL;
current_routine->rst_sz = 0;
uint8_t rslt = current_routine->rountine((void*)current_routine,routine_cmd,routine_args,routing_args_sz);
if(rslt == 0)
{
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[1] = __uds_get_subfunction(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[2] = iso14229_1_received_indn.msg[2];
iso14229_1_temporary_buffer[3] = iso14229_1_received_indn.msg[3];
iso14229_1_temporary_buffer[4] = rslt;
if(current_routine->rst != NULL && current_routine->rst_sz !=0)
memmove(&iso14229_1_temporary_buffer[5],current_routine->rst,current_routine->rst_sz);
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,5+current_routine->rst_sz);
}
else
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),rslt);
}
return;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
routine_status iso14229_1_srvc_routines_process()
{
uint32_t list_sz = sizeof(uds_routines)/sizeof(uds_routine_local_id_t);
routine_status sts = RTN_INACTIVE;
for(register uint32_t i = 0;i<list_sz;i++)
{
if(uds_routines[i].sts == RTN_ACTIVE && uds_routines[i].rountine != NULL)
{
sts = RTN_ACTIVE;
uds_routines[i].rountine(&uds_routines[i],RTN_CONTINUE,NULL,0);
}
}
if(sts == RTN_ACTIVE)
iso14229_1_srvc_diagnostic_session_refresh_timeout();
return sts;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
static uint32_t inc_delay = 0;
static uint32_t last_trial_time = 0;
void iso14229_1_srvc_security_access()
{
uint32_t key;
uint32_t resp_key;
uds_security_access_t *current_sa = NULL;
uint8_t req_type = (__uds_get_subfunction(iso14229_1_received_indn.msg) & 0x01);
uint8_t req_sa_lvl = req_type == 1
? (__uds_get_subfunction(iso14229_1_received_indn.msg) ) : (__uds_get_subfunction(iso14229_1_received_indn.msg) - 0x01);
uint32_t list_sz = sizeof(uds_security_accesses)/sizeof(uds_security_access_t);
for(register uint32_t i = 0;i<list_sz;i++)
{
if(uds_security_accesses[i].access_lvl == req_sa_lvl)
{
current_sa = &uds_security_accesses[i];
}
}
if(current_sa == NULL)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_SFNS);
return;
}
if(current_sa->sts == SA_NOT_EXISTS)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_SFNS);
return;
}
if(req_type == 0x00 && current_sa->sts != SA_IN_PROGRESS)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_RSE);
return;
}
if(iso14229_1_received_indn.msg_sz != 2 && req_type == 0x01)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
if(iso14229_1_received_indn.msg_sz != 6 && req_type == 0x00)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
if(last_trial_time !=0 && inc_delay != 0)
{
if(inc_delay > 5)
{
if( (last_trial_time + (60*60*1000)) > xTaskGetTickCount() )
{
last_trial_time = xTaskGetTickCount();
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_RTDNE);
return;
}
}
else
{
if((last_trial_time + inc_delay*2000) > xTaskGetTickCount())
{
last_trial_time = xTaskGetTickCount();
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_RTDNE);
return;
}
}
}
if(current_sa->sts == SA_ACTIVE && req_type == 0x01)
{
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[1] = __uds_get_subfunction(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[2] = 0;
iso14229_1_temporary_buffer[3] = 0;
iso14229_1_temporary_buffer[4] = 0;
iso14229_1_temporary_buffer[5] = 0;
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,6);
return;
}
switch(req_type)
{
case 0x01:
for(register uint32_t i = 0;i<list_sz;i++)
{
if(uds_security_accesses[i].sts == SA_IN_PROGRESS)
uds_security_accesses[i].sts = uds_security_accesses[i].default_sts;
}
current_sa->sts = SA_IN_PROGRESS;
current_sa->current_seed = random32();
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[1] = __uds_get_subfunction(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[2] = (current_sa->current_seed & 0xFF000000) >> 24;
iso14229_1_temporary_buffer[3] = (current_sa->current_seed & 0x00FF0000) >> 16;
iso14229_1_temporary_buffer[4] = (current_sa->current_seed & 0x0000FF00) >> 8;
iso14229_1_temporary_buffer[5] = (current_sa->current_seed & 0x000000FF) >> 0;
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,6);
break;
case 0x00:
key = current_sa->key_validation != NULL ? current_sa->key_validation(current_sa->current_seed) : current_sa->current_seed;
resp_key = ((uint32_t)iso14229_1_received_indn.msg[2] << 24);
resp_key |= ((uint32_t)iso14229_1_received_indn.msg[3] << 16);
resp_key |= ((uint32_t)iso14229_1_received_indn.msg[4] << 8);
resp_key += iso14229_1_received_indn.msg[5];
if(key == resp_key)
{
for(register uint32_t i = 0;i<list_sz;i++)
{
uds_security_accesses[i].sts = uds_security_accesses[i].default_sts;
}
last_trial_time = 0;
inc_delay=0;
current_sa->sts = SA_ACTIVE;
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[1] = __uds_get_subfunction(iso14229_1_received_indn.msg);
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,2);
return;
}
else
{
last_trial_time = xTaskGetTickCount();
inc_delay+=1;
current_sa->sts = current_sa->default_sts;
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IK);
return;
}
break;
default:
break;
}
return;
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_1_srvc_tester_present()
{
if(iso14229_1_received_indn.msg_sz != 2)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_IMLOIF);
return;
}
if(__uds_get_subfunction(iso14229_1_received_indn.msg) != 0)
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_SFNS);
return;
}
iso14229_1_temporary_buffer[0] = __uds_get_function_positive_response(iso14229_1_received_indn.msg);
iso14229_1_temporary_buffer[1] = __uds_get_subfunction(iso14229_1_received_indn.msg);
iso14229_send(&iso14229_1_received_indn.n_ai,iso14229_1_temporary_buffer,2);
iso14229_1_srvc_diagnostic_session_refresh_timeout();
}
/* --- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (ref: xxxxxxxxxx p.xx) ------------ */
void iso14229_1_srvc_tranfer_data()
{
if((uds_tranfer_data.sts != TD_INACTIVE && uds_tranfer_data.sts!=TD_ACTIVE)
|| ((uds_tranfer_data.block_counter != (uint32_t)(iso14229_1_received_indn.msg[1]+1)
&& (uds_tranfer_data.block_counter == 0xff && iso14229_1_received_indn.msg[1] != 0x01)
&& (uds_tranfer_data.sts == TD_INACTIVE && iso14229_1_received_indn.msg[1] != 0x01)) ))
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_RSE);
return;
}
if( (iso14229_1_received_indn.msg_sz < 3) || ((iso14229_1_received_indn.msg_sz - 2) > 0x200) )
{
iso14229_send_NRC(&iso14229_1_received_indn.n_ai,__uds_get_function(iso14229_1_received_indn.msg),UDS_NRC_RSE);
return;
}
uds_tranfer_data.block_counter = iso14229_1_received_indn.msg[1];
memmove(&transfer_data_collection[transfer_data_collection_pos], &iso14229_1_received_indn.msg[2],iso14229_1_received_indn.msg_sz - 2);
transfer_data_collection_pos += iso14229_1_received_indn.msg_sz - 2;
uds_tranfer_data.calculated_crc = crc16_ccitt(uds_tranfer_data.calculated_crc, &iso14229_1_received_indn.msg[2], iso14229_1_received_indn.msg_sz - 2);
uint32_t t_transfer_data_collection_pos = 0;
if(uds_tranfer_data.current_address % 0x20 != 0)
{
uint8_t diff = uds_tranfer_data.current_address % 0x20;
uds_tranfer_data.current_address -= diff;
memmove(&transfer_data_collection[diff],transfer_data_collection,transfer_data_collection_pos);
memmove(transfer_data_collection,(uint32_t*)uds_tranfer_data.current_address,diff);
uds_tranfer_data.remaining_data_len+=diff;
transfer_data_collection_pos += diff;
}
for(uint32_t i=0;i<(transfer_data_collection_pos-(transfer_data_collection_pos%64));i+=64)
{
iso14229_ecu_flash_write(uds_tranfer_data.current_address, &transfer_data_collection[i], 64);
t_transfer_data_collection_pos+=64;
uds_tranfer_data.current_address+=64;
uds_tranfer_data.remaining_data_len -= 64;
}
memmove(transfer_data_collection,&transfer_data_collection[t_transfer_data_collection_pos],transfer_data_collection_pos-t_transfer_data_collection_pos);
transfer_data_collection_pos=transfer_data_collection_pos-t_transfer_data_collection_pos;
if( (uds_tranfer_data.remaining_data_len == 0 && transfer_data_collection_pos !=0 )
|| ( uds_tranfer_data.remaining_data_len == transfer_data_collection_pos && uds_tranfer_data.remaining_data_len!=0))
{
memmove(temporary_flash_64bytes,(uint32_t*)uds_tranfer_data.current_address,64);
memmove(temporary_flash_64bytes,transfer_data_collection,transfer_data_collection_pos);