-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANOpenShell.c
executable file
·798 lines (672 loc) · 21.5 KB
/
CANOpenShell.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
/*
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
*/
#if defined(WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define CLEARSCREEN "cls"
#define SLEEP(time) Sleep(time * 1000)
#else
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CLEARSCREEN "clear"
#define SLEEP(time) sleep(time)
#include <readline/readline.h>
#include <readline/history.h>
#include <semaphore.h>
#include <time.h>
#include <errno.h>
#endif
//****************************************************************************
// INCLUDES
#include "canfestival.h"
#include "CANOpenShell.h"
#include "CANOpenShellMasterOD.h"
#include "CANOpenShellSlaveOD.h"
//****************************************************************************
// DEFINES
#define MAX_NODES 127
#define cst_str4(c1, c2, c3, c4) ((((unsigned int)0 | \
(char)c4 << 8) | \
(char)c3) << 8 | \
(char)c2) << 8 | \
(char)c1
#define WAIT_NS 500000000l
#define INIT_ERR 2
#define QUIT 1
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
//****************************************************************************
// GLOBALS
char BoardBusName[31];
char BoardBaudRate[5];
s_BOARD Board = {BoardBusName, BoardBaudRate};
CO_Data* CANOpenShellOD_Data;
char LibraryPath[512];
sem_t Write_sem;
struct timespec Write_sem_ts;
sem_t Read_sem;
struct timespec Read_sem_ts;
int CurrentNode=0;
/* Sleep for n seconds */
void SleepFunction(int second)
{
#ifdef USE_RTAI
sleep(second);
#else
SLEEP(second);
#endif
}
/* Ask a slave node to go in operational mode */
void StartNode(UNS8 nodeid)
{
masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Start_Node);
}
/* Ask a slave node to go in pre-operational mode */
void StopNode(UNS8 nodeid)
{
masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Stop_Node);
}
/* Ask a slave node to reset */
void ResetNode(UNS8 nodeid)
{
masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Reset_Node);
}
/* Reset all nodes on the network and print message when boot-up*/
void DiscoverNodes()
{
printf("Wait for Slave nodes bootup...\n\n");
ResetNode(0x00);
}
int get_info_step = 0;
/* Callback function that check the read SDO demand */
void CheckReadInfoSDO(CO_Data* d, UNS8 nodeid)
{
UNS32 abortCode;
UNS32 data=0;
UNS32 size=64;
if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
printf("Master : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
else
{
/* Display data received */
switch(get_info_step)
{
case 1:
printf("Device type : %x\n", data);
break;
case 2:
printf("Vendor ID : %x\n", data);
break;
case 3:
printf("Product Code : %x\n", data);
break;
case 4:
printf("Revision Number : %x\n", data);
break;
}
}
/* Finalize last SDO transfer with this node */
closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
GetSlaveNodeInfo(nodeid);
}
/* Retrieve node informations located at index 0x1000 (Device Type) and 0x1018 (Identity) */
void GetSlaveNodeInfo(UNS8 nodeid)
{
switch(++get_info_step)
{
case 1: /* Get device type */
printf("##################################\n");
printf("#### Informations for node %x ####\n", nodeid);
printf("##################################\n");
readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1000, 0x00, 0, CheckReadInfoSDO, 0);
break;
case 2: /* Get Vendor ID */
readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x01, 0, CheckReadInfoSDO, 0);
break;
case 3: /* Get Product Code */
readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x02, 0, CheckReadInfoSDO, 0);
break;
case 4: /* Get Revision Number */
readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x03, 0, CheckReadInfoSDO, 0);
break;
case 5: /* Print node info */
get_info_step = 0;
}
}
/* Callback function that check the read SDO demand */
void CheckReadSDO(CO_Data* d, UNS8 nodeid)
{
UNS32 abortCode;
UNS32 data=0;
UNS32 size=64;
if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
else
printf("\n= 0x%x (%d)\n", data,data);
/* Finalize last SDO transfer with this node */
closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
}
/* Read a slave node object dictionary entry */
void ReadDeviceEntry(char* sdo)
{
int ret=0;
int nodeid;
int index;
int subindex;
int datatype = 0;
ret = sscanf(sdo, "rsdo#%2x,%4x,%2x", &nodeid, &index, &subindex);
if (ret == 3)
{
printf("##################################\n");
printf("#### Read SDO ####\n");
printf("##################################\n");
printf("NodeId : %2.2x\n", nodeid);
printf("Index : %4.4x\n", index);
printf("SubIndex : %2.2x\n", subindex);
readNetworkDictCallback(CANOpenShellOD_Data, (UNS8)nodeid, (UNS16)index, (UNS8)subindex, (UNS8)datatype, CheckReadSDO, 0);
}
else
printf("Wrong command : %s\n", sdo);
}
/* Read a slave node object dictionary entry */
void ReadSDOEntry(int nodeid, int index, int subindex)
{
int ret=0;
int datatype = 0;
readNetworkDictCallback(CANOpenShellOD_Data, (UNS8)nodeid, (UNS16)index, (UNS8)subindex, (UNS8)datatype, CheckReadSDO, 0);
}
UNS8 SDO_read_callback_result;
UNS32 SDO_read_callback_abortCode;
char SDO_read_data[256];
UNS32 SDO_read_data_size;
/* Callback function that check the read SDO demand */
void SDO_read_callback(CO_Data* d, UNS8 nodeid)
{
SDO_read_data_size = 255;
SDO_read_callback_result = getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &SDO_read_data, &SDO_read_data_size, &SDO_read_callback_abortCode);
SDO_read_data[SDO_read_data_size] = 0;
/* Finalize last SDO transfer with this node */
closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
sem_post(&Read_sem);
}
/* Read a slave node object dictionary entry */
UNS8 SDO_read(CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, UNS8 useBlockMode)
{
int ret=0;
UNS8 readResult;
int s;
SDO_read_callback_result = 44; //set to a random number
readResult = readNetworkDictCallback(CANOpenShellOD_Data, (UNS8)nodeId, (UNS16)index, (UNS8)subIndex, (UNS8)dataType, SDO_read_callback, useBlockMode);
LeaveMutex();
if (clock_gettime(CLOCK_REALTIME, &Read_sem_ts) == -1)
handle_error("clock_gettime");
Read_sem_ts.tv_nsec += WAIT_NS; // 100ms
if (Read_sem_ts.tv_nsec >= 1000000000l){
Read_sem_ts.tv_sec++;
Read_sem_ts.tv_nsec -= 1000000000l;
}
while ((s = sem_timedwait(&Read_sem, &Read_sem_ts)) == -1 && errno == EINTR)
continue; /* Restart if interrupted by handler */
/* Check what happened */
if (s == -1) {
if (errno == ETIMEDOUT)
printf("sem_timedwait() timed out\n");
else
perror("sem_timedwait");
} else
if(SDO_read_callback_result != SDO_FINISHED)
printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeId, SDO_read_callback_abortCode);
/*else
printf("Result: %s\n",SDO_read_data); */
return readResult;
}
UNS8 SDO_write_callback_result;
UNS32 SDO_write_callback_abortCode;
/* Callback function that check the write SDO demand */
void SDO_write_callback(CO_Data* d, UNS8 nodeid)
{
UNS32 abortCode;
SDO_write_callback_result = getWriteResultNetworkDict(CANOpenShellOD_Data, nodeid, &SDO_write_callback_abortCode);
/* Finalize last SDO transfer with this node */
closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
sem_post(&Write_sem);
}
/* Write a slave node object dictionnary entry */
UNS8 SDO_write(CO_Data* d, UNS8 nodeId, UNS16 index,
UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, UNS8 useBlockMode)
{
int ret=0;
UNS8 writeResult;
int s;
SDO_write_callback_result = 44; //set to a random number
//-- Write command --//
writeResult = writeNetworkDictCallBack(d, nodeId,index, subIndex, count, dataType, data, SDO_write_callback, useBlockMode);
LeaveMutex();
if (clock_gettime(CLOCK_REALTIME, &Write_sem_ts) == -1)
handle_error("clock_gettime");
Write_sem_ts.tv_nsec += WAIT_NS; // 100ms
if (Write_sem_ts.tv_nsec >= 1000000000l){
Write_sem_ts.tv_sec++;
Write_sem_ts.tv_nsec -= 1000000000l;
}
while ((s = sem_timedwait(&Write_sem, &Write_sem_ts)) == -1 && errno == EINTR)
continue; /* Restart if interrupted by handler */
/* Check what happened */
if (s == -1) {
if (errno == ETIMEDOUT)
printf("sem_timedwait() timed out\n");
else
perror("sem_timedwait");
} else
if(SDO_write_callback_result != SDO_FINISHED)
printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeId, SDO_write_callback_abortCode);
return writeResult;
}
/* Callback function that check the write SDO demand */
void CheckWriteSDO(CO_Data* d, UNS8 nodeid)
{
UNS32 abortCode;
if(getWriteResultNetworkDict(CANOpenShellOD_Data, nodeid, &abortCode) != SDO_FINISHED)
printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
else
printf("\nSend data OK\n");
/* Finalize last SDO transfer with this node */
closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
}
/* Write a slave node object dictionnary entry */
void WriteDeviceEntry(char* sdo)
{
int ret=0;
int nodeid;
int index;
int subindex;
int size;
int data;
ret = sscanf(sdo, "wsdo#%2x,%4x,%2x,%2x,%x", &nodeid , &index, &subindex, &size, &data);
if (ret == 5)
{
printf("##################################\n");
printf("#### Write SDO ####\n");
printf("##################################\n");
printf("NodeId : %2.2x\n", nodeid);
printf("Index : %4.4x\n", index);
printf("SubIndex : %2.2x\n", subindex);
printf("Size : %2.2x\n", size);
printf("Data : %x\n", data);
writeNetworkDictCallBack(CANOpenShellOD_Data, nodeid, index, subindex, size, 0, &data, CheckWriteSDO, 0);
}
else
printf("Wrong command : %s\n", sdo);
}
/* Write a slave node object dictionnary entry */
void WriteSDOEntry(int nodeid, int index, int subindex, int size, UNS32 data)
{
int ret=0;
writeNetworkDictCallBack(CANOpenShellOD_Data, nodeid, index, subindex, size, 0, &data, CheckWriteSDO, 0);
}
void CANOpenShellOD_post_SlaveBootup(CO_Data* d, UNS8 nodeid)
{
printf("Slave %x boot up\n", nodeid);
}
/*************************** CALLBACK FUNCTIONS *****************************************/
void CANOpenShellOD_initialisation(CO_Data* d)
{
printf("Node_initialisation\n");
}
void CANOpenShellOD_preOperational(CO_Data* d)
{
printf("Node_preOperational\n");
}
void CANOpenShellOD_operational(CO_Data* d)
{
printf("Node_operational\n");
}
void CANOpenShellOD_stopped(CO_Data* d)
{
printf("Node_stopped\n");
}
void CANOpenShellOD_post_sync(CO_Data* d)
{
//printf("Master_post_sync\n");
}
void CANOpenShellOD_post_TPDO(CO_Data* d)
{
//printf("Master_post_TPDO\n");
}
/*************************** INITIALISATION **********************************/
UNS32 OnStatus3Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
{
printf("Status3: %x\n",Status3);
return 0;
}
void Init(CO_Data* d, UNS32 id)
{
if(Board.baudrate)
{
/* Init node state*/
setState(CANOpenShellOD_Data, Initialisation);
}
}
/*************************** CLEANUP *****************************************/
void Exit(CO_Data* d, UNS32 nodeid)
{
if(strcmp(Board.baudrate, "none"))
{
/* Reset all nodes on the network */
masterSendNMTstateChange(CANOpenShellOD_Data, 0 , NMT_Reset_Node);
/* Stop master */
setState(CANOpenShellOD_Data, Stopped);
}
}
int NodeInit(int NodeID, int NodeType)
{
if(NodeType)
CANOpenShellOD_Data = &CANOpenShellMasterOD_Data;
else
CANOpenShellOD_Data = &CANOpenShellSlaveOD_Data;
/* Load can library */
LoadCanDriver(LibraryPath);
/* Define callback functions */
CANOpenShellOD_Data->initialisation = CANOpenShellOD_initialisation;
CANOpenShellOD_Data->preOperational = CANOpenShellOD_preOperational;
CANOpenShellOD_Data->operational = CANOpenShellOD_operational;
CANOpenShellOD_Data->stopped = CANOpenShellOD_stopped;
CANOpenShellOD_Data->post_sync = CANOpenShellOD_post_sync;
CANOpenShellOD_Data->post_TPDO = CANOpenShellOD_post_TPDO;
CANOpenShellOD_Data->post_SlaveBootup=CANOpenShellOD_post_SlaveBootup;
/* Open the Peak CANOpen device */
if(!canOpen(&Board,CANOpenShellOD_Data)) return INIT_ERR;
/* Defining the node Id */
setNodeId(CANOpenShellOD_Data, NodeID);
/* Start Timer thread */
StartTimerLoop(&Init);
return 0;
}
void help_menu(void)
{
printf("Non-prefixed commands are passed via SDO OS interface on the bus.\n");
printf("\n");
printf(".node <nodeid> : Set the node to which unprefixed commands are sent.\n");
printf(" Setup COMMAND (must be on the process invocation):\n");
printf(" load#CanLibraryPath,channel,baudrate,nodeid,type (0:slave, 1:master)\n");
printf("\n");
printf(" NETWORK: (if nodeid=0x00 : broadcast)\n");
printf(" .ssta#nodeid : Start a node\n");
printf(" .ssto#nodeid : Stop a node\n");
printf(" .srst#nodeid : Reset a node\n");
printf(" .scan : Reset all nodes and print message when bootup\n");
printf(" .wait#seconds : Sleep for n seconds\n");
printf("\n");
printf(" SDO: (size in bytes)\n");
printf(" .info#nodeid\n");
printf(" .rsdo#nodeid,index,subindex : read sdo\n");
printf(" ex : .rsdo#42,1018,01\n");
printf(" .wsdo#nodeid,index,subindex,size,data : write sdo\n");
printf(" ex : .wsdo#42,6200,01,01,FF\n");
printf("\n");
printf(" Note: All numbers are hex\n");
printf("\n");
printf(" .clear: Clear the display\n");
printf(" .help : Display this menu\n");
printf(" .quit : Quit application\n");
printf("\n");
printf("\n");
}
int ExtractNodeId(char *command) {
int nodeid;
sscanf(command, "%2x", &nodeid);
return nodeid;
}
int ProcessCommand(char* command)
{
int ret = 0;
int sec = 0;
int NodeID;
int NodeType;
UNS32 data = 0;
char buf[50];
EnterMutex();
switch(cst_str4(command[0], command[1], command[2], command[3]))
{
case cst_str4('h', 'e', 'l', 'p') : /* Display Help*/
help_menu();
break;
case cst_str4('c', 'l', 'e', 'a') : /* Display Help*/
system(CLEARSCREEN);
break;
case cst_str4('s', 's', 't', 'a') : /* Slave Start*/
StartNode(ExtractNodeId(command + 5));
break;
case cst_str4('s', 's', 't', 'o') : /* Slave Stop */
StopNode(ExtractNodeId(command + 5));
break;
case cst_str4('s', 'r', 's', 't') : /* Slave Reset */
ResetNode(ExtractNodeId(command + 5));
break;
case cst_str4('i', 'n', 'f', 'o') : /* Retrieve node informations */
GetSlaveNodeInfo(ExtractNodeId(command + 5));
break;
case cst_str4('r', 's', 'd', 'o') : /* Read device entry */
ReadDeviceEntry(command);
break;
case cst_str4('w', 's', 'd', 'o') : /* Write device entry */
WriteDeviceEntry(command);
break;
case cst_str4('n', 'o', 'd', 'e') : /* Write device entry */
ret = sscanf(command, "node %2x", &NodeID);
data = 0;
SDO_write(CANOpenShellOD_Data,NodeID,0x1024,0x00,1, 0, &data, 0);
CurrentNode = NodeID;
break;
case cst_str4('c', 'm', 'd', ' ') : /* Write device entry */
ret = sscanf(command, "cmd %2x,%s", &NodeID, buf );
SDO_write(CANOpenShellOD_Data,NodeID,0x1023,0x01,strlen(buf),visible_string, buf, 0);
SDO_read(CANOpenShellOD_Data,NodeID,0x1023,0x03,visible_string,0);
return 0;
break;
case cst_str4('s', 'y', 'n', '0') : /* Display master node state */
stopSYNC(CANOpenShellOD_Data);
break;
case cst_str4('s', 'y', 'n', '1') : /* Display master node state */
startSYNC(CANOpenShellOD_Data);
break;
case cst_str4('s', 't', 'a', 't') : /* Display master node state */
printf("Status3: %x\n",Status3);
Status3 = 0;
break;
case cst_str4('s', 'c', 'a', 'n') : /* Display master node state */
DiscoverNodes();
break;
case cst_str4('w', 'a', 'i', 't') : /* Display master node state */
ret = sscanf(command, "wait#%d", &sec);
if(ret == 1) {
LeaveMutex();
SleepFunction(sec);
return 0;
}
break;
case cst_str4('g', 'o', 'o', 'o') : /* Quit application */
setState(CANOpenShellOD_Data, Operational);
break;
case cst_str4('q', 'u', 'i', 't') : /* Quit application */
LeaveMutex();
return QUIT;
case cst_str4('l', 'o', 'a', 'd') : /* Library Interface*/
ret = sscanf(command, "load#%100[^,],%30[^,],%4[^,],%d,%d",
LibraryPath,
BoardBusName,
BoardBaudRate,
&NodeID,
&NodeType);
if(ret == 5)
{
LeaveMutex();
ret = NodeInit(NodeID, NodeType);
return ret;
}
else
{
printf("Invalid load parameters\n");
}
break;
default :
help_menu();
}
LeaveMutex();
return 0;
}
int ProcessFocusedCommand(char* command)
{
int ret = 0;
int sec = 0;
int NodeID;
int NodeType;
UNS32 data = 0;
char buf[50];
int size;
int index;
int subindex = 0;
int datatype = 0;
EnterMutex();
switch(command[0])
{
case 's' : /* Slave Start*/
StartNode(CurrentNode);
break;
case 't' : /* slave stop */
StopNode(CurrentNode);
break;
case 'x' : /* slave reset */
ResetNode(CurrentNode);
break;
case 'r' : /* Read device entry */
ret = sscanf(command, "r%4x,%2x", &index, &subindex);
if (ret)
ReadSDOEntry(CurrentNode,index,subindex);
break;
case 'w' : /* Write device entry */
ret = sscanf(command, "w%4x,%2x,%2x,%x", &index, &subindex, &size, &data);
if (ret)
WriteSDOEntry(CurrentNode,index,subindex,size,data);
break;
case '?' : /* Read device entry */
ReadSDOEntry(CurrentNode,0x6041,0);
break;
case 'c' : /* Write device entry */
ret = sscanf(command, "w%x", &data);
if (ret)
WriteSDOEntry(CurrentNode,0x6040,0,0x2,data);
break;
default :
help_menu();
}
LeaveMutex();
return 0;
}
/* A static variable for holding the line. */
static char *line_read = (char *)NULL;
/* Read a string, and return a pointer to it.
Returns NULL on EOF. */
char *
rl_gets ()
{
/* If the buffer has already been allocated,
return the memory to the free pool. */
if (line_read)
{
free (line_read);
line_read = (char *)NULL;
}
/* Get a line from the user. */
line_read = readline (">");
/* If the line has any text in it,
save it on the history. */
if (line_read && *line_read)
add_history (line_read);
return (line_read);
}
/****************************************************************************/
/*************************** MAIN *****************************************/
/****************************************************************************/
int main(int argc, char** argv)
{
extern char *optarg;
char command[200];
char* res;
int ret=0;
int sysret=0;
int i=0;
if (sem_init(&Write_sem, 0, 0) == -1)
handle_error("Writesem_init");
if (sem_init(&Read_sem, 0, 0) == -1)
handle_error("Readsem_init");
/* Defaults */
strcpy(LibraryPath,"/usr/lib/libcanfestival_can_peak_linux.so");
strcpy(BoardBusName,"0");
strcpy(BoardBaudRate,"1M");
/* Init stack timer */
TimerInit();
if (argc > 1){
printf("ok\n");
/* Strip command-line*/
for(i=1 ; i<argc ; i++)
{
if(ProcessCommand(argv[i]) == INIT_ERR) goto init_fail;
}
}
NodeInit(0,1);
RegisterSetODentryCallBack(CANOpenShellOD_Data, 0x2003, 0, &OnStatus3Update);
help_menu();
CurrentNode = 3;
sleep(1);
//setState(CANOpenShellOD_Data, Operational); // Put the master in operational mode
stopSYNC(CANOpenShellOD_Data);
/* Enter in a loop to read stdin command until "quit" is called */
while(ret != QUIT)
{
// wait on stdin for string command
rl_on_new_line ();
res = rl_gets();
//sysret = system(CLEARSCREEN);
if(res[0]=='.'){
ret = ProcessCommand(res+1);
}
else if(res[0]==','){
ret = ProcessFocusedCommand(res+1);
}
else if (res[0]=='\n'){
}
else {
EnterMutex();
SDO_write(CANOpenShellOD_Data,CurrentNode,0x1023,0x01,strlen(res),visible_string, res, 0);
EnterMutex();
SDO_read(CANOpenShellOD_Data,CurrentNode,0x1023,0x03,visible_string,0);
printf("%s\n",SDO_read_data);
}
fflush(stdout);
usleep(500000);
}
printf("Finishing.\n");
// Stop timer thread
StopTimerLoop(&Exit);
/* Close CAN board */
canClose(CANOpenShellOD_Data);
init_fail:
TimerCleanup();
return 0;
}