-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsdo.cpp
2274 lines (2144 loc) · 93.8 KB
/
sdo.cpp
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
/*
This file is part of CanFestival, a library implementing CanOpen Stack.
Copyright (C): Edouard TISSERANT and Francis DUPIN
See COPYING file for copyrights details.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*!
** @file sdo.c
** @author Edouard TISSERANT and Francis DUPIN
** @date Tue Jun 5 09:32:32 2007
**
** @brief
**
**
*/
/* #define DEBUG_WAR_CONSOLE_ON */
/* #define DEBUG_ERR_CONSOLE_ON */
#include <stdlib.h>
#include "applicfg.h"
#include "data.h"
#include "CO_timer.h"
#include "objacces.h"
#include "sysdep.h"
/* Uncomment if your compiler does not support inline functions */
#define NO_INLINE
#ifdef NO_INLINE
#define INLINE
#else
#define INLINE inline
#endif
/*Internals prototypes*/
UNS8 GetSDOClientFromNodeId(UNS8 nodeId);
/*!
** Called by writeNetworkDict
**
** @param d
** @param nodeId
** @param index
** @param subIndex
** @param count
** @param dataType
** @param data
** @param Callback
** @param endianize
**
** @return
**/
INLINE UNS8 _writeNetworkDict(UNS8 nodeId, UNS16 index,
UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, SDOCallback_t Callback, UNS8 endianize, UNS8 useBlockMode);
/*!
** Called by readNetworkDict
**
** @param d
** @param nodeId
** @param index
** @param subIndex
** @param dataType
** @param Callback
**
** @return
**/
INLINE UNS8 _readNetworkDict(UNS8 nodeId, UNS16 index, UNS8 subIndex,
UNS8 dataType, SDOCallback_t Callback, UNS8 useBlockMode);
/***************************************************************************/
/* SDO (un)packing macros */
/** Returns the command specifier (cs, ccs, scs) from the first byte of the SDO
*/
#define getSDOcs(byte) (byte >> 5)
/** Returns the number of bytes without data from the first byte of the SDO. Coded in 2 bits
*/
#define getSDOn2(byte) ((byte >> 2) & 3)
/** Returns the number of bytes without data from the first byte of the SDO. Coded in 3 bits
*/
#define getSDOn3(byte) ((byte >> 1) & 7)
/** Returns the transfer type from the first byte of the SDO
*/
#define getSDOe(byte) ((byte >> 1) & 1)
/** Returns the size indicator from the first byte of the SDO
*/
#define getSDOs(byte) (byte & 1)
/** Returns the indicator of end transmission from the first byte of the SDO
*/
#define getSDOc(byte) (byte & 1)
/** Returns the toggle from the first byte of the SDO
*/
#define getSDOt(byte) ((byte >> 4) & 1)
/** Returns the index from the bytes 1 and 2 of the SDO
*/
#define getSDOindex(byte1, byte2) (((UNS16)byte2 << 8) | ((UNS16)byte1))
/** Returns the subIndex from the byte 3 of the SDO
*/
#define getSDOsubIndex(byte3) (byte3)
/** Returns the subcommand in SDO block transfer
*/
#define getSDOblockSC(byte) (byte & 3)
/*!
**
**
** @param d
** @param id
**/
void SDOTimeoutAlarm(UNS8 id) {
UNS8 nodeId;
const subindex *si;
UNS8 si_size;
ODCallback_t *callbacks;
/* Get the client->server cobid.*/
if (!(si = ObjDict_scanIndexOD(0x1280 + ObjDict_Data.transfers[id].CliServNbr, &si_size, &callbacks))) {
return;
}
nodeId = (UNS8) *((UNS8*) si[3].pObject);
MSG_ERR(0x1A01, "SDO timeout. SDO response not received.", 0);
MSG_WAR(0x2A02, "server node id : ", nodeId);
MSG_WAR(0x2A02, " index : ", ObjDict_Data.transfers[id].index);
MSG_WAR(0x2A02, " subIndex : ", ObjDict_Data.transfers[id].subIndex);
/* Reset timer handler */
ObjDict_Data.transfers[id].timer = TIMER_NONE;
/*Set aborted state*/
ObjDict_Data.transfers[id].state = SDO_ABORTED_INTERNAL;
/* Sending a SDO abort */
sendSDOabort(ObjDict_Data.transfers[id].whoami, ObjDict_Data.transfers[id].CliServNbr, ObjDict_Data.transfers[id].index, ObjDict_Data.transfers[id].subIndex, SDOABT_TIMED_OUT);
ObjDict_Data.transfers[id].abortCode = SDOABT_TIMED_OUT;
/* Call the user function to inform of the problem.*/
if (ObjDict_Data.transfers[id].Callback)
/*If ther is a callback, it is responsible to close SDO transfer (client)*/
(*(ObjDict_Data.transfers[id].Callback))(nodeId);
/*Reset the line if (whoami == SDO_SERVER) or the callback did not close the line.
Otherwise this sdo transfer would never be closed. */
if (ObjDict_Data.transfers[id].abortCode == SDOABT_TIMED_OUT)
resetSDOline((UNS8) id);
}
#define StopSDO_TIMER(id) \
MSG_WAR(0x3A05, "StopSDO_TIMER for line : ", line);\
ObjDict_Data.transfers[id].timer = DelAlarm(ObjDict_Data.transfers[id].timer);
#define StartSDO_TIMER(id) \
MSG_WAR(0x3A06, "StartSDO_TIMER for line : ", line);\
ObjDict_Data.transfers[id].timer = SetAlarm(id,&SDOTimeoutAlarm,MS_TO_TIMEVAL(SDO_TIMEOUT_MS),0);
#define RestartSDO_TIMER(id) \
MSG_WAR(0x3A07, "restartSDO_TIMER for line : ", line);\
if(ObjDict_Data.transfers[id].timer != TIMER_NONE) { StopSDO_TIMER(id) StartSDO_TIMER(id) }
/*!
** Reset all sdo buffers
**
** @param d
**/
void resetSDO() {
UNS8 j;
/* transfer structure initialization */
for (j = 0; j < SDO_MAX_SIMULTANEOUS_TRANSFERS; j++)
resetSDOline(j);
}
/*!
**
**
** @param d
** @param line
**
** @return
**/
UNS32 SDOlineToObjdict(UNS8 line) {
UNS32 size;
UNS32 errorCode;
MSG_WAR(0x3A08, "Enter in SDOlineToObjdict ", line);
/* if SDO initiated with e=0 and s=0 count is null, offset carry effective size*/
if (ObjDict_Data.transfers[line].count == 0)
ObjDict_Data.transfers[line].count = ObjDict_Data.transfers[line].offset;
size = ObjDict_Data.transfers[line].count;
#ifdef SDO_DYNAMIC_BUFFER_ALLOCATION
if (size > SDO_MAX_LENGTH_TRANSFER)
{
errorCode = setODentry(ObjDict_Data.transfers[line].index, ObjDict_Data.transfers[line].subIndex,
(void *) ObjDict_Data.transfers[line].dynamicData, &size, 1);
}
else
{
errorCode = setODentry(ObjDict_Data.transfers[line].index, ObjDict_Data.transfers[line].subIndex,
(void *) ObjDict_Data.transfers[line].data, &size, 1);
}
#else //SDO_DYNAMIC_BUFFER_ALLOCATION
errorCode =
setODentry(ObjDict_Data.transfers[line].index, ObjDict_Data.transfers[line].subIndex, (void * ) ObjDict_Data.transfers[line].data, &size, 1);
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
if (errorCode != OD_SUCCESSFUL)
return errorCode;
MSG_WAR(0x3A08, "exit of SDOlineToObjdict ", line);
return 0;
}
/*!
**
**
** @param d
** @param line
**
** @return
**/
UNS32 objdictToSDOline(UNS8 line) {
UNS32 size = SDO_MAX_LENGTH_TRANSFER;
UNS8 dataType;
UNS32 errorCode;
MSG_WAR(0x3A05, "objdict->line index : ", ObjDict_Data.transfers[line].index);
MSG_WAR(0x3A06, " subIndex : ", ObjDict_Data.transfers[line].subIndex);
#ifdef SDO_DYNAMIC_BUFFER_ALLOCATION
/* Try to use the static buffer. */
errorCode = getODentry(ObjDict_Data.transfers[line].index,
ObjDict_Data.transfers[line].subIndex,
(void *)ObjDict_Data.transfers[line].data,
&size, &dataType, 1);
if (errorCode == SDOABT_OUT_OF_MEMORY) {
/* The static buffer is too small, try again using a dynamic buffer. *
* 'size' now contains the real size of the requested object. */
if (size <= SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE) {
ObjDict_Data.transfers[line].dynamicData = (UNS8 *) malloc(size * sizeof(UNS8));
if (ObjDict_Data.transfers[line].dynamicData != NULL) {
ObjDict_Data.transfers[line].dynamicDataSize = size;
errorCode = getODentry(ObjDict_Data.transfers[line].index,
ObjDict_Data.transfers[line].subIndex,
(void *) ObjDict_Data.transfers[line].dynamicData,
&ObjDict_Data.transfers[line].dynamicDataSize,
&dataType,
1);
}
}
}
#else //SDO_DYNAMIC_BUFFER_ALLOCATION
errorCode =
getODentry(ObjDict_Data.transfers[line].index, ObjDict_Data.transfers[line].subIndex, (void * )ObjDict_Data.transfers[line].data, &size, &dataType, 1);
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
if (errorCode != OD_SUCCESSFUL)
return errorCode;
ObjDict_Data.transfers[line].count = size;
ObjDict_Data.transfers[line].offset = 0;
return 0;
}
/*!
**
**
** @param d
** @param line
** @param nbBytes
** @param data
**
** @return
**/
UNS8 lineToSDO(UNS8 line, UNS32 nbBytes, UNS8* data) {
UNS8 i;
UNS32 offset;
#ifndef SDO_DYNAMIC_BUFFER_ALLOCATION
if ((ObjDict_Data.transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFER) {
MSG_ERR(0x1A10, "SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFER", nbBytes);
return 0xFF;
}
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
if ((ObjDict_Data.transfers[line].offset + nbBytes) > ObjDict_Data.transfers[line].count) {
MSG_ERR(0x1A11, "SDO Size of data too large. Exceed count", nbBytes);
return 0xFF;
}
offset = ObjDict_Data.transfers[line].offset;
#ifdef SDO_DYNAMIC_BUFFER_ALLOCATION
if (ObjDict_Data.transfers[line].count <= SDO_MAX_LENGTH_TRANSFER)
{
for (i = 0; i < nbBytes; i++)
* (data + i) = ObjDict_Data.transfers[line].data[offset + i];
}
else
{
if (ObjDict_Data.transfers[line].dynamicData == NULL)
{
MSG_ERR(0x1A11,"SDO's dynamic buffer not allocated. Line", line);
return 0xFF;
}
for (i = 0; i < nbBytes; i++)
* (data + i) = ObjDict_Data.transfers[line].dynamicData[offset + i];
}
#else //SDO_DYNAMIC_BUFFER_ALLOCATION
for (i = 0; i < nbBytes; i++)
*(data + i) = ObjDict_Data.transfers[line].data[offset + i];
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
ObjDict_Data.transfers[line].offset = ObjDict_Data.transfers[line].offset + nbBytes;
return 0;
}
/*!
**
**
** @param d
** @param line
** @param nbBytes
** @param data
**
** @return
**/
UNS8 SDOtoLine(UNS8 line, UNS32 nbBytes, UNS8* data) {
UNS8 i;
UNS32 offset;
#ifndef SDO_DYNAMIC_BUFFER_ALLOCATION
if ((ObjDict_Data.transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFER) {
MSG_ERR(0x1A15, "SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFER", nbBytes);
return 0xFF;
}
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
offset = ObjDict_Data.transfers[line].offset;
#ifdef SDO_DYNAMIC_BUFFER_ALLOCATION
{
UNS8* lineData = ObjDict_Data.transfers[line].data;
if ((ObjDict_Data.transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFER) {
if (ObjDict_Data.transfers[line].dynamicData == NULL) {
ObjDict_Data.transfers[line].dynamicData = (UNS8*) malloc(SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE);
ObjDict_Data.transfers[line].dynamicDataSize = SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE;
if (ObjDict_Data.transfers[line].dynamicData == NULL) {
MSG_ERR(0x1A15,"SDO allocating dynamic buffer failed, size", SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE);
return 0xFF;
}
//Copy present data
memcpy(ObjDict_Data.transfers[line].dynamicData, ObjDict_Data.transfers[line].data, offset);
}
else if ((ObjDict_Data.transfers[line].offset + nbBytes) > ObjDict_Data.transfers[line].dynamicDataSize)
{
UNS8* newDynamicBuffer = (UNS8*) realloc(ObjDict_Data.transfers[line].dynamicData, ObjDict_Data.transfers[line].dynamicDataSize + SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE);
if (newDynamicBuffer == NULL) {
MSG_ERR(0x1A15,"SDO reallocating dynamic buffer failed, size", ObjDict_Data.transfers[line].dynamicDataSize + SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE);
return 0xFF;
}
ObjDict_Data.transfers[line].dynamicData = newDynamicBuffer;
ObjDict_Data.transfers[line].dynamicDataSize += SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE;
}
lineData = ObjDict_Data.transfers[line].dynamicData;
}
for (i = 0; i < nbBytes; i++)
lineData[offset + i] = * (data + i);
}
#else //SDO_DYNAMIC_BUFFER_ALLOCATION
for (i = 0; i < nbBytes; i++)
ObjDict_Data.transfers[line].data[offset + i] = *(data + i);
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
ObjDict_Data.transfers[line].offset = ObjDict_Data.transfers[line].offset + nbBytes;
return 0;
}
/*!
**
**
** @param d
** @param CliServNbr
** @param whoami
** @param index
** @param subIndex
** @param abortCode
**
** @return
**/
UNS8 failedSDO(UNS8 CliServNbr, UNS8 whoami, UNS16 index,
UNS8 subIndex, UNS32 abortCode) {
UNS8 err;
UNS8 line;
err = getSDOlineOnUse(CliServNbr, whoami, &line);
if (!err) /* If a line on use have been found.*/
MSG_WAR(0x3A20, "FailedSDO : line found : ", line);
if ((!err) && (whoami == SDO_SERVER)) {
resetSDOline(line);
MSG_WAR(0x3A21, "FailedSDO : line released : ", line);
}
if ((!err) && (whoami == SDO_CLIENT)) {
StopSDO_TIMER(line);
ObjDict_Data.transfers[line].state = SDO_ABORTED_INTERNAL;
ObjDict_Data.transfers[line].abortCode = abortCode;
}
MSG_WAR(0x3A22, "Sending SDO abort ", 0);
err = sendSDOabort(whoami, CliServNbr, index, subIndex, abortCode);
if (err) {
MSG_WAR(0x3A23, "Unable to send the SDO abort", 0);
return 0xFF;
}
return 0;
}
/*!
**
**
** @param d
** @param line
**/
void resetSDOline(UNS8 line) {
UNS32 i;
MSG_WAR(0x3A25, "reset SDO line nb : ", line);
initSDOline(line, 0, 0, 0, SDO_RESET);
for (i = 0; i < SDO_MAX_LENGTH_TRANSFER; i++)
ObjDict_Data.transfers[line].data[i] = 0;
ObjDict_Data.transfers[line].whoami = 0;
ObjDict_Data.transfers[line].abortCode = 0;
}
/*!
**
**
** @param d
** @param line
** @param CliServNbr
** @param index
** @param subIndex
** @param state
**
** @return
**/
UNS8 initSDOline(UNS8 line, UNS8 CliServNbr, UNS16 index, UNS8 subIndex, UNS8 state) {
MSG_WAR(0x3A25, "init SDO line nb : ", line);
if (state == SDO_DOWNLOAD_IN_PROGRESS || state == SDO_UPLOAD_IN_PROGRESS || state == SDO_BLOCK_DOWNLOAD_IN_PROGRESS
|| state == SDO_BLOCK_UPLOAD_IN_PROGRESS) {
StartSDO_TIMER(line)
} else {
StopSDO_TIMER(line)
}
ObjDict_Data.transfers[line].CliServNbr = CliServNbr;
ObjDict_Data.transfers[line].index = index;
ObjDict_Data.transfers[line].subIndex = subIndex;
ObjDict_Data.transfers[line].state = state;
ObjDict_Data.transfers[line].toggle = 0;
ObjDict_Data.transfers[line].count = 0;
ObjDict_Data.transfers[line].offset = 0;
ObjDict_Data.transfers[line].peerCRCsupport = 0;
ObjDict_Data.transfers[line].blksize = 0;
ObjDict_Data.transfers[line].ackseq = 0;
ObjDict_Data.transfers[line].objsize = 0;
ObjDict_Data.transfers[line].lastblockoffset = 0;
ObjDict_Data.transfers[line].seqno = 0;
ObjDict_Data.transfers[line].endfield = 0;
ObjDict_Data.transfers[line].rxstep = RXSTEP_INIT;
ObjDict_Data.transfers[line].dataType = 0;
ObjDict_Data.transfers[line].Callback = NULL;
#ifdef SDO_DYNAMIC_BUFFER_ALLOCATION
free(ObjDict_Data.transfers[line].dynamicData);
ObjDict_Data.transfers[line].dynamicData = 0;
ObjDict_Data.transfers[line].dynamicDataSize = 0;
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
return 0;
}
/*!
**
**
** @param d
** @param whoami
** @param line
**
** @return
**/
UNS8 getSDOfreeLine(UNS8 whoami, UNS8 *line) {
UNS8 i;
for (i = 0; i < SDO_MAX_SIMULTANEOUS_TRANSFERS; i++) {
if (ObjDict_Data.transfers[i].state == SDO_RESET) {
*line = i;
ObjDict_Data.transfers[i].whoami = whoami;
return 0;
} /* end if */
} /* end for */
MSG_ERR(0x1A25, "Too many SDO in progress. Aborted.", i);
return 0xFF;
}
/*!
**
**
** @param d
** @param CliServNbr
** @param whoami
** @param line
**
** @return
**/
UNS8 getSDOlineOnUse(UNS8 CliServNbr, UNS8 whoami, UNS8 *line) {
UNS8 i;
for (i = 0; i < SDO_MAX_SIMULTANEOUS_TRANSFERS; i++) {
if ((ObjDict_Data.transfers[i].state != SDO_RESET) && (ObjDict_Data.transfers[i].state != SDO_ABORTED_INTERNAL)
&& (ObjDict_Data.transfers[i].CliServNbr == CliServNbr) && (ObjDict_Data.transfers[i].whoami == whoami)) {
if (line)
*line = i;
return 0;
}
}
return 0xFF;
}
/*!
**
**
** @param d
** @param CliServNbr
** @param whoami
** @param line
**
** @return
**/
UNS8 getSDOlineToClose(UNS8 CliServNbr, UNS8 whoami, UNS8 *line) {
UNS8 i;
for (i = 0; i < SDO_MAX_SIMULTANEOUS_TRANSFERS; i++) {
if ((ObjDict_Data.transfers[i].state != SDO_RESET) && (ObjDict_Data.transfers[i].CliServNbr == CliServNbr)
&& (ObjDict_Data.transfers[i].whoami == whoami)) {
if (line)
*line = i;
return 0;
}
}
return 0xFF;
}
/*!
**
**
** @param d
** @param CliServNbr
** @param whoami
**
** @return
**/
UNS8 closeSDOtransfer(UNS8 nodeId, UNS8 whoami) {
UNS8 err;
UNS8 line;
UNS8 CliNbr;
/* First let's find the corresponding SDO client in our OD */
CliNbr = GetSDOClientFromNodeId(nodeId);
if (CliNbr >= 0xFE)
return SDO_ABORTED_INTERNAL;
err = getSDOlineToClose(CliNbr, whoami, &line);
if (err) {
MSG_WAR(0x2A30, "No SDO communication to close", 0);
return 0xFF;
}
resetSDOline(line);
return 0;
}
/*!
**
**
** @param d
** @param line
** @param nbBytes
**
** @return
**/
UNS8 getSDOlineRestBytes(UNS8 line, UNS32 * nbBytes) {
/* SDO initiated with e=0 and s=0 have count set to null */
if (ObjDict_Data.transfers[line].count == 0)
*nbBytes = 0;
else
*nbBytes = ObjDict_Data.transfers[line].count - ObjDict_Data.transfers[line].offset;
return 0;
}
/*!
**
**
** @param d
** @param line
** @param nbBytes
**
** @return
**/
UNS8 setSDOlineRestBytes(UNS8 line, UNS32 nbBytes) {
#ifndef SDO_DYNAMIC_BUFFER_ALLOCATION
if (nbBytes > SDO_MAX_LENGTH_TRANSFER) {
MSG_ERR(0x1A35, "SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFER", nbBytes);
return 0xFF;
}
#endif //SDO_DYNAMIC_BUFFER_ALLOCATION
ObjDict_Data.transfers[line].count = nbBytes;
return 0;
}
/*!
**
**
** @param d
** @param whoami
** @param CliServNbr
** @param pData
**
** @return
**/
UNS8 sendSDO(UNS8 whoami, UNS8 CliServNbr, UNS8 *pData) {
UNS8 i;
Message m;
const subindex *si;
UNS8 si_size;
ODCallback_t *callbacks;
MSG_WAR(0x3A38, "sendSDO", 0);
if (!((ObjDict_Data.nodeState == Operational) || (ObjDict_Data.nodeState == Pre_operational))) {
MSG_WAR(0x2A39, "unable to send the SDO (not in op or pre-op mode", ObjDict_Data.nodeState);
return 0xFF;
}
/*get the server->client cobid*/
/* Get the client->server cobid.*/
if (whoami == SDO_SERVER) {
if (!(si = ObjDict_scanIndexOD(0x1200 + CliServNbr, &si_size, &callbacks))) {
MSG_ERR(0x1A42, "SendSDO : SDO server not found", 0);
return 0xFF;
}
m.cob_id = (UNS16) *((UNS32*) si[2].pObject);
MSG_WAR(0x3A41, "I am server Tx cobId : ", m.cob_id);
} else { /*case client*/
/* Get the client->server cobid.*/
if (!(si = ObjDict_scanIndexOD(0x1280 + CliServNbr, &si_size, &callbacks))) {
MSG_ERR(0x1A42, "SendSDO : SDO client not found", 0);
return 0xFF;
}
m.cob_id = (UNS16) *((UNS32*) si[1].pObject);
MSG_WAR(0x3A41, "I am client Tx cobId : ", m.cob_id);
}
/* message copy for sending */
m.rtr = NOT_A_REQUEST;
/* the length of SDO must be 8 */
m.len = 8;
for (i = 0; i < 8; i++) {
m.data[i] = pData[i];
}
return canSend(&m);
}
/*!
**
**
** @param d
** @param whoami
** @param index
** @param subIndex
** @param abortCode
**
** @return
**/
UNS8 sendSDOabort(UNS8 whoami, UNS8 CliServNbr, UNS16 index, UNS8 subIndex, UNS32 abortCode) {
UNS8 data[8];
UNS8 ret;
MSG_WAR(0x2A50, "Sending SDO abort ", abortCode);
data[0] = 0x80;
/* Index */
data[1] = index & 0xFF; /* LSB */
data[2] = (index >> 8) & 0xFF; /* MSB */
/* Subindex */
data[3] = subIndex;
/* Data */
data[4] = (UNS8) (abortCode & 0xFF);
data[5] = (UNS8) ((abortCode >> 8) & 0xFF);
data[6] = (UNS8) ((abortCode >> 16) & 0xFF);
data[7] = (UNS8) ((abortCode >> 24) & 0xFF);
ret = sendSDO(whoami, CliServNbr, data);
return ret;
}
/*!
**
**
** @param d
** @param m
**
** @return
**/
UNS8 proceedSDO(Message *m) {
UNS8 err;
UNS8 cs;
UNS8 line;
UNS32 nbBytes; /* received or to be transmited. */
// UNS8 nodeId = 0; /* The node Id of the server if client otherwise unused */
UNS8 CliServNbr;
UNS8 whoami = SDO_UNKNOWN; /* SDO_SERVER or SDO_CLIENT.*/
UNS32 errorCode; /* while reading or writing in the local object dictionary.*/
UNS8 data[8]; /* data for SDO to transmit */
UNS16 index;
UNS8 subIndex;
// UNS32 abortCode;
UNS32 i;
UNS8 j;
UNS16 *pCobId = NULL;
UNS8 SubCommand; /* Block transfer only */
UNS8 SeqNo; /* Sequence number in block transfer */
UNS8 AckSeq; /* Sequence number of last segment that was received successfully */
UNS8 NbBytesNoData; /* Number of bytes that do not contain data in last segment of block transfer */
const subindex *si;
UNS8 si_size;
ODCallback_t *callbacks;
MSG_WAR(0x3A60, "proceedSDO ", 0);
whoami = SDO_UNKNOWN;
/* Looking for the cobId in the object dictionary. */
/* Am-I a server ? */
j = 0;
while ((si = ObjDict_scanIndexOD(0x1200 + j, &si_size, &callbacks))) {
if (si_size <= 1) {
MSG_ERR(0x1A61, "Subindex 1 not found at index ", 0x1200 + j);
return 0xFF;
}
/* Looking for the cobid received. */
pCobId = (UNS16*) si[1].pObject;
if (*pCobId == UNS16_LE(m->cob_id)) {
whoami = SDO_SERVER;
MSG_WAR(0x3A62, "proceedSDO. I am server. index : ", 0x1200 + j);
/* Defining Server number = index minus 0x1200 where the cobid received is defined. */
CliServNbr = j;
break;
}
j++;
} /* end while */
if (whoami == SDO_UNKNOWN) {
/* Am-I client ? */
j = 0;
while ((si = ObjDict_scanIndexOD(0x1280 + j, &si_size, &callbacks))) {
if (si_size <= 3) {
MSG_ERR(0x1A63, "Subindex 3 not found at index ", 0x1280 + j);
return 0xFF;
}
/* Looking for the cobid received. */
pCobId = (UNS16*) si[2].pObject;
if (*pCobId == UNS16_LE(m->cob_id)) {
whoami = SDO_CLIENT;
MSG_WAR(0x3A64, "proceedSDO. I am client index : ", 0x1280 + j);
/* Defining Client number = index minus 0x1280 where the cobid received is defined. */
CliServNbr = j;
/* Reading the server node ID, if client it is mandatory in the OD */
// nodeId = *((UNS8*) si[3].pObject);
break;
}
j++;
} /* end while */
}
if (whoami == SDO_UNKNOWN) {
return 0xFF;/* This SDO was not for us ! */
}
/* Test if the size of the SDO is ok */
if ((*m).len != 8) {
MSG_ERR(0x1A67, "Error size SDO", 0);
failedSDO(CliServNbr, whoami, 0, 0, SDOABT_GENERAL_ERROR);
return 0xFF;
}
if (whoami == SDO_CLIENT) {
MSG_WAR(0x3A68, "I am CLIENT number ", CliServNbr);
} else {
MSG_WAR(0x3A69, "I am SERVER number ", CliServNbr);
}
/* Look for an SDO transfer already initiated. */
err = getSDOlineOnUse(CliServNbr, whoami, &line);
/* Let's find cs value, first it is set as "not valid" */
cs = 0xFF;
/* Special cases for block transfer : in frames with segment data cs is not spécified */
if (!err) {
if (((whoami == SDO_SERVER) && (ObjDict_Data.transfers[line].state == SDO_BLOCK_DOWNLOAD_IN_PROGRESS))
|| ((whoami == SDO_CLIENT) && (ObjDict_Data.transfers[line].state == SDO_BLOCK_UPLOAD_IN_PROGRESS))) {
if (m->data[0] == 0x80) /* If first byte is 0x80 it is an abort frame (seqno = 0 not allowed) */
cs = 4;
else
cs = 6;
}
}
/* Other cases : cs is specified */
if (cs == 0xFF)
cs = getSDOcs(m->data[0]);
/* Testing the command specifier */
/* Allowed : cs = 0, 1, 2, 3, 4, 5, 6 */
/* cs = other : Not allowed -> abort. */
switch (cs) {
case 0:
/* I am SERVER */
if (whoami == SDO_SERVER) {
/* Receiving a download segment data : an SDO transfer should have been yet initiated. */
if (!err)
err = ObjDict_Data.transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS;
if (err) {
MSG_ERR(0x1A70, "SDO error : Received download segment for unstarted trans. index 0x1200 + ", CliServNbr);
failedSDO(CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR);
return 0xFF;
}
/* Reset the wathdog */
RestartSDO_TIMER(line)
MSG_WAR(0x3A71, "Received SDO download segment defined at index 0x1200 + ", CliServNbr);
index = ObjDict_Data.transfers[line].index;
subIndex = ObjDict_Data.transfers[line].subIndex;
/* Toggle test. */
if (ObjDict_Data.transfers[line].toggle != getSDOt(m->data[0])) {
MSG_ERR(0x1A72, "SDO error : Toggle error : ", getSDOt(m->data[0]));
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED);
return 0xFF;
}
/* Nb of data to be downloaded */
nbBytes = 7 - getSDOn3(m->data[0]);
/* Store the data in the transfer structure. */
err = SDOtoLine(line, nbBytes, (*m).data + 1);
if (err) {
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR);
return 0xFF;
}
/* Sending the SDO response, CS = 1 */
data[0] = (1 << 5) | (ObjDict_Data.transfers[line].toggle << 4);
for (i = 1; i < 8; i++)
data[i] = 0;
MSG_WAR(0x3A73, "SDO. Send response to download request defined at index 0x1200 + ", CliServNbr);
sendSDO(whoami, CliServNbr, data);
/* Inverting the toggle for the next segment. */
ObjDict_Data.transfers[line].toggle = !ObjDict_Data.transfers[line].toggle & 1;
/* If it was the last segment, */
if (getSDOc(m->data[0])) {
/* Transfering line data to object dictionary. */
/* The code does not use the "d" of initiate frame. So it is safe if e=s=0 */
errorCode = SDOlineToObjdict(line);
if (errorCode) {
MSG_ERR(0x1A54, "SDO error : Unable to copy the data in the object dictionary", 0);
failedSDO(CliServNbr, whoami, index, subIndex, errorCode);
return 0xFF;
}
/* Release of the line */
resetSDOline(line);
MSG_WAR(0x3A74, "SDO. End of download defined at index 0x1200 + ", CliServNbr);
}
} /* end if SERVER */
#ifdef CO_ENABLE_SDO_CLIENT
else { /* if CLIENT */
/* I am CLIENT */
/* It is a request for a previous upload segment. We should find a line opened for this.*/
if (!err)
err = ObjDict_Data.transfers[line].state != SDO_UPLOAD_IN_PROGRESS;
if (err) {
MSG_ERR(0x1A75, "SDO error : Received segment response for unknown trans. from nodeId", nodeId);
failedSDO(CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR);
return 0xFF;
}
/* Reset the wathdog */
RestartSDO_TIMER(line)
index = ObjDict_Data.transfers[line].index;
subIndex = ObjDict_Data.transfers[line].subIndex;
/* test of the toggle; */
if (ObjDict_Data.transfers[line].toggle != getSDOt(m->data[0])) {
MSG_ERR(0x1A76, "SDO error : Received segment response Toggle error. from nodeId", nodeId);
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED);
return 0xFF;
}
/* nb of data to be uploaded */
nbBytes = 7 - getSDOn3(m->data[0]);
/* Storing the data in the line structure. */
err = SDOtoLine(line, nbBytes, (*m).data + 1);
if (err) {
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR);
return 0xFF;
}
/* Inverting the toggle for the next segment. */
ObjDict_Data.transfers[line].toggle = ! ObjDict_Data.transfers[line].toggle & 1;
/* If it was the last segment,*/
if ( getSDOc(m->data[0])) {
/* Put in state finished */
/* The code is safe for the case e=s=0 in initiate frame. */
StopSDO_TIMER(line)
ObjDict_Data.transfers[line].state = SDO_FINISHED;
if(ObjDict_Data.transfers[line].Callback) (*ObjDict_Data.transfers[line].Callback)(nodeId);
MSG_WAR(0x3A77, "SDO. End of upload from node : ", nodeId);
}
else { /* more segments to receive */
/* Sending the request for the next segment. */
data[0] = (3 << 5) | (ObjDict_Data.transfers[line].toggle << 4);
for (i = 1; i < 8; i++)
data[i] = 0;
sendSDO(whoami, CliServNbr, data);
MSG_WAR(0x3A78, "SDO send upload segment request to nodeId", nodeId);
}
} /* End if CLIENT */
#endif
break;
case 1:
/* I am SERVER */
/* Receive of an initiate download */
if (whoami == SDO_SERVER) {
index = getSDOindex(m->data[1], m->data[2]);
subIndex = getSDOsubIndex(m->data[3]);
MSG_WAR(0x3A79, "Received SDO Initiate Download (to store data) defined at index 0x1200 + ", CliServNbr);
MSG_WAR(0x3A80, "Writing at index : ", index);
MSG_WAR(0x3A80, "Writing at subIndex : ", subIndex);
/* Search if a SDO transfer have been yet initiated */
if (!err) {
MSG_ERR(0x1A81, "SDO error : Transmission yet started.", 0);
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR);
return 0xFF;
}
/* No line on use. Great ! */
/* Try to open a new line. */
err = getSDOfreeLine(whoami, &line);
if (err) {
MSG_ERR(0x1A82, "SDO error : No line free, too many SDO in progress. Aborted.", 0);
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR);
return 0xFF;
}
initSDOline(line, CliServNbr, index, subIndex, SDO_DOWNLOAD_IN_PROGRESS);
if (getSDOe(m->data[0])) { /* If SDO expedited */
/* nb of data to be downloaded */
nbBytes = 4 - getSDOn2(m->data[0]);
/* Storing the data in the line structure. */
ObjDict_Data.transfers[line].count = nbBytes;
err = SDOtoLine(line, nbBytes, (*m).data + 4);
if (err) {
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR);
return 0xFF;
}
/* SDO expedited -> transfer finished. Data can be stored in the dictionary. */
/*The line will be reseted when it is downloading in the dictionary. */
MSG_WAR(0x3A83, "SDO Initiate Download is an expedited transfer. Finished. ", 0);
/* Transfering line data to object dictionary. */
errorCode = SDOlineToObjdict(line);
if (errorCode) {
MSG_ERR(0x1A84, "SDO error : Unable to copy the data in the object dictionary", 0);
failedSDO(CliServNbr, whoami, index, subIndex, errorCode);
return 0xFF;
}
/* Release of the line. */
resetSDOline(line);
} else {/* So, if it is not an expedited transfer */
if (getSDOs(m->data[0])) {
nbBytes = (m->data[4]) + ((UNS32) (m->data[5]) << 8) + ((UNS32) (m->data[6]) << 16)
+ ((UNS32) (m->data[7]) << 24);
err = setSDOlineRestBytes(line, nbBytes);
if (err) {
failedSDO(CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR);
return 0xFF;
}
}
}
/*Sending a SDO, cs=3*/