39#include <socan_util.h>
51typedef enum { T_CHECK=2, T_REMOTE=4, T_LEAVE=8, T_CONSISTENCE=16,
95static void test_simple_rw(
int object1,
int port1,
int object2,
int port2,
96 int w_timeout,
int r_timeout,
103 char wbuf[8]= {0,0,0,0,0,0,0,0};
104 char rbuf[8]= {0,0,0,0,0,0,0,0};
107 printf(
"opening sci :\n");
109 rc= sci_open(&s_struc,
"", &err);
112 printf(
"sci_open returned %s, aborting\n",
116 printf(
"8 bytes will be written, content of these 8 bytes:\n");
117 printf(
"1 2 3 4 5 6 7 8\n");
118 printf(
"set write-mode for object %d, port %d ...",object1, port1);
119 printf(
"(timeout %d)\n", w_timeout);
120 rc= sci_init_object(s_struc, &obj1, port1, object1, 8, w_timeout,
124 printf(
"sci_init_object returned %s, aborting\n",
130 printf(
"set read-mode for object %d, port %d ...",object2, port2);
131 printf(
"(timeout %d)\n", r_timeout);
132 rc= sci_init_object(s_struc, &obj2, port2, object2, 8, r_timeout,
136 printf(
"sci_init_object returned %s, aborting\n",
144 printf(
"now writing the data...");
145 rc= sci_write(s_struc, obj1, wbuf);
148 printf(
"sci_write returned %s, aborting\n",
158 printf(
"now reading the data...");
159 rc= sci_read(s_struc, obj2, rbuf);
162 printf(
"sci_read returned %s, aborting\n",
168 printf(
"message-content:\n");
169 nice_dump(rbuf, 8, 0);
171 printf(
"closing sci...\n");
175static void test_read(
int object,
int port,
unsigned int timeout,
178 int quietlevel,
bool readnow,
179 int countsize,
int flag)
185 char cbuf[8]= {0, 0, 0, 0, 0, 0, 0, 0};
189 unsigned long c,count= 0;
196 func_name=
"sci_read_now";
198 func_name=
"sci_read";
200 if (countsize>=
sizeof(
int))
203 mask= 0xFFFFFFFF >> ((
sizeof(int)-countsize)*8);
206 printf(
"sleeping %f seconds...\n", sleep);
207 mldelay((
long)(sleep*1000+0.5));
209 printf(
"opening sci :\n");
211 rc= sci_open(&s_struc,
"", &err);
214 printf(
"sci_open returned %s, aborting\n",
219 printf(
"defining read-object...\n");
224 rc= sci_init_object(s_struc, &obj, port,
object, countsize, timeout,
228 printf(
"sci_init_object returned %s, aborting\n",
234 eval_quietlevel(quietlevel, &scale, &ch, &l);
236 for(; loops>0; loops--)
239 rc= sci_read_now(s_struc, obj, cbuf);
241 rc= sci_read(s_struc, obj, cbuf);
245 { l= scale; putchar(ch); fflush(stdout); };
248 { printf(
"received: data:\n");
249 nice_dump(cbuf, countsize, 0);
257 {
if ((quietlevel>0) && (rc ==
SCI_LOST))
259 putchar(
'L'); fflush(stdout);
265 printf(
"%s returned %d (%s)\n",
266 func_name, rc, sci_str_status(rc));
270 sci_get_errcode(s_struc, &err);
271 printf(
"%s returned error %d (%s)\n",
272 func_name, err, sci_str_err(err));
279 if (flag & T_CONSISTENCE)
282 for (i=1; i<countsize; i++)
285 printf(
"INCONSISTENT DATA!\n");
286 printf(
"received: data:\n");
287 nice_dump(cbuf, countsize, 0);
294 memcpy(&c, cbuf, countsize);
297 if ((c>count) || (countsize>1))
300 printf(
"[%lx,%lx]",c,count);
302 printf(
"%ld messages missed, got:%ld expected:%ld\n",
306 printf(
"messages repeated, got:%ld, expected:%ld\n",
317 printf(
"driver (on request) not closed!\n");
318 for(;;) mldelay(60*1000);
321 printf(
"closing sci...\n");
325static void test_queue_read(
int port,
unsigned int timeout,
330 int countsize,
int flag)
337 char cbuf[8]= {0, 0, 0, 0, 0, 0, 0, 0};
344 unsigned long *count= NULL;
350 printf(
"wrong id_no: %d\n", id_no);
353 count= calloc(
COB_NO,
sizeof(
unsigned long));
354 indices= calloc(
COB_NO,
sizeof(
int));
357 if (countsize>=
sizeof(
int))
360 mask= 0xFFFFFFFF >> ((
sizeof(int)-countsize)*8);
364 printf(
"sleeping %f seconds...\n", sleep);
365 mldelay((
long)(sleep*1000+0.5));
367 printf(
"opening sci :\n");
369 rc= sci_open(&s_struc,
"", &err);
372 printf(
"sci_open returned %s, aborting\n",
377 printf(
"defining read-objects...\n");
379 for(i=0; i<id_no; i++)
383 rc= sci_init_object(s_struc, &(objs[i]), port, ids[i],
388 printf(
"sci_init_object returned %s, aborting\n",
393 rc= sci_set_callback(s_struc, objs[i], NULL);
396 printf(
"sci_set_callback returned %s, aborting\n",
403 eval_quietlevel(quietlevel, &scale, &ch, &l);
405 for(; loops>0; loops--)
407 rc= sci_queue_read(s_struc, &obj, cbuf);
409 index= sci_get_user(obj);
413 { l= scale; putchar(ch); fflush(stdout); };
416 { printf(
"received on %d: data:\n", ids[*index]);
417 nice_dump(cbuf, countsize, 0);
425 {
if ((quietlevel>0) && (rc ==
SCI_LOST))
427 putchar(
'L'); fflush(stdout);
433 printf(
"sci_queue_read returned %d (%s)\n",
434 rc, sci_str_status(rc));
438 sci_get_errcode(s_struc, &err);
439 printf(
"sci_queue_read returned error %d (%s)\n",
440 err, sci_str_err(err));
447 if (flag & T_CONSISTENCE)
450 for (i=1; i<countsize; i++)
453 printf(
"INCONSISTENT DATA!\n");
454 printf(
"received: data:\n");
455 nice_dump(cbuf, countsize, 0);
462 memcpy(&c, cbuf, countsize);
463 if (c != (count[*index]))
465 if ((c>(count[*index])) || (countsize>1))
468 printf(
"[%lx,%lx]",c,(count[*index]));
470 printf(
"%ld messages missed, got:%ld expected:%ld\n",
471 c-(count[*index]), c, (count[*index]));
474 printf(
"messages repeated, got:%ld, expected:%ld\n",
479 (count[*index])&= mask;
485 printf(
"driver (on request) not closed!\n");
486 for(;;) mldelay(60*1000);
489 printf(
"closing sci...\n");
494static void test_write(
int object,
int port,
unsigned int timeout,
495 unsigned long inhibit_tm,
500 int countsize,
int countstart,
507 char cbuf[8]= {0, 0, 0, 0, 0, 0, 0, 0};
512 bool timed= (write_cmd==CMD_WRITE_INHIBIT);
513 bool busy_wait= (write_cmd==CMD_WRITE_W);
514 unsigned long counter;
515 unsigned long timeval;
521 case CMD_WRITE_INHIBIT:
522 func_name=
"sci_write_inhibit";
526 func_name=
"sci_write";
529 printf(
"internal error line %d\n", __LINE__);
537 printf(
"sleeping %f seconds...\n", sleep);
538 mldelay((
long)(sleep*1000+0.5));
540 printf(
"opening sci :\n");
542 rc= sci_open(&s_struc,
"", &err);
545 printf(
"sci_open returned %s, aborting\n",
550 printf(
"defining write-object...\n");
555 rc= sci_init_object(s_struc, &obj, port,
object, countsize, timeout,
559 printf(
"sci_init_object returned %s, aborting\n",
567 rc= sci_set_inhibit(s_struc, obj, inhibit_tm);
570 printf(
"scitest.c line %d: sci_set_inhibit returned "
571 "an error: %d (%s)\n", __LINE__, rc, sci_str_status(rc));
572 printf(
"test-abort!\n");
578 eval_quietlevel(quietlevel, &scale, &ch, &l);
580 for(; loops>0; loops--)
581 {
if (flag & T_CONSISTENCE)
583 unsigned char b= (
unsigned char) counter;
584 for(i=0; i<countsize; cbuf[i++]= b);
590 memcpy(cbuf, &counter, countsize);
595 { l= scale; putchar(ch); fflush(stdout); };
598 {
if (flag & T_CONSISTENCE)
599 printf(
"consistency test with byte %lu\n", counter & 0xFF);
601 { printf(
"writing %lu, %d bytes on %d\n",
602 counter, countsize,
object);
608 case CMD_WRITE_INHIBIT:
610 rc= sci_write_inhibit(s_struc, obj, cbuf, &timeval);
614 rc= sci_write(s_struc, obj, cbuf);
617 printf(
"INTERNAL ERROR AT LINE %d\n", __LINE__);
633 sci_get_errcode(s_struc, &err);
634 printf(
"scitest.c line %d: %s returned an error: %d (%s)\n",
635 __LINE__, func_name, err, sci_str_err(err));
638 printf(
"%s return code: %d (%s)\n", func_name, rc, sci_str_status(rc));
646 { printf(
"driver (on request) not closed!\n");
647 for(;;) mldelay(60*1000);
650 printf(
"closing sci...\n");
660 printf(
" **** scitest %s ****\n", VERSION);
661 printf(
"the sci test program\n"
663 "scitest {options}\n"
665 " **************** Configuration*****************\n"
666 " --interface,-i INTERFACE \n"
667 " Use INTERFACE for CAN communication, this option\n"
668 " can be given more than once to use more than one\n"
670 " --realtime READER-PRIORITY WRITER-PRIORITY\n"
671 " Use given realtime priorities for reader and writer\n"
674 " for all functions that define CAN-objects, hold the\n"
675 " program just before close() is performed. All CAN\n"
676 " objects remain defined. End the program with CRTL-C in\n"
678 " --trace,-t LEVEL\n"
679 " set trace level, LEVEL is an integer 0..2 (default 0)\n"
680 " --errprintlevel,-E LEVEL\n"
681 " set errprint level, LEVEL is an integer 0..3 (default 1)\n"
683 printf(
" *************** command options ***************\n"
684 " --loop {no of loops}\n"
685 " run read, write, readnow or readmany in loop-mode\n"
686 " --sleep {seconds}\n"
687 " sleep a number of seconds before starting the test.\n"
688 " seconds may be a floating point number.\n"
689 " --quiet,-q {level}\n"
690 " run read, write, readnow or readmany in quiet-mode\n"
691 " level may be 1 .. 5, meaning that each 10**(level-1)\n"
692 " events print a single character on the screen.\n"
694 " check sequence in --read, (must be used together with\n"
695 " --write in another sotest-task\n"
696 " --size,-s [bytes]\n"
697 " use [bytes] bytes to transfer and receive data\n"
698 " --count [number]\n"
699 " use [number] as initial number to write with -w\n"
701 " send alternating 8 bytes of 0xFF or 0x00 to test data\n"
704 " do remote-read or remote-write, implemented for\n"
705 " --write and --read\n"
707 printf(
" ***************data transmission commands****************\n"
708 " --rwtest [object1] [port1] [object2] [port2] "
709 "[write-timeout] [read-timeout]\n"
710 " make a write-object (object1,port1) and a read-object\n"
711 " (object2,port2) with a timeout set for the 2nd object\n"
712 " then write 8 bytes (values 1 to 8) and read the 2nd\n"
713 " object (read immediately) and return the results\n"
714 " --write,-w [object] [port] [timeout]\n"
715 " write data (a running counter), non blocking\n"
716 " --write-w [object] [port] [timeout]\n"
717 " the same as --write but do silently retry to write\n"
718 " when the driver returns a SOCAN_WAIT status.\n"
719 " --writeinhibit [object] [port] [timeout] [inhibit-time]\n"
720 " similar to writelater, but the CAN-object gets a timestamp\n"
721 " when it is transmitted. Inhibit-time is the minumum\n"
722 " time that has to pass before a new object is sent. This\n"
723 " concerns only writeinhibit combined with the loop-command.\n"
724 " In this case only about every [inhibit-time]\n"
725 " microseconds new data will be sent (note that the unit\n"
726 " for this parameter is microseconds, not milliseconds\n"
727 " --read,-r [object] [port] [timeout]\n"
728 " read data from an object (blocking).\n"
729 " --readnow [object] [port] [timeout]\n"
730 " read data from an object (immediately).\n"
731 " --queue-read [port] [timeout] [object] {[object]}\n"
732 " read more than one object via the signal-function in \n"
733 " conjunction with the new queue-read operation\n"
734 " together with --remote, all objects will be remote-read\n"
739#define MAX_INTERFACES 4
741int main(
int argc,
char *argv[])
748 {
"-i" , INTERFACE },
749 {
"--interface" , INTERFACE },
752 {
"--trace" , TRACE},
753 {
"-E" , ERRPRINTLEVEL},
754 {
"--errprintlevel", ERRPRINTLEVEL},
755 {
"--sleep" , SLEEP},
757 {
"--quiet" , QUIET},
758 {
"--check" , CHECK},
761 {
"--count" , COUNT},
762 {
"--leave" , LEAVE},
763 {
"--realtime" , REALTIME},
764 {
"--remote" , REMOTE},
765 {
"--consistence" , CONSISTENCE},
766 {
"--ignore_old" , IGNORE_OLD},
767 {
"--rwtest" , RWTEST},
770 {
"--readnow" , READ_NOW},
771 {
"--queue-read" , QUEUE_READ},
773 {
"--write" , WRITE},
774 {
"--write-w" , WRITE_W},
775 {
"--writeinhibit" , WRITE_INHIBIT},
778 int opt_no=
sizeof(options) /
sizeof(
option);
779 enum commands operation=CMD_NOTHING;
781 char *interfaces[MAX_INTERFACES];
783 int n[45], i, l, tracelevel= -1, errprintlevel= -1;
788 int countsize =
sizeof(int);
792 int rt_prios[]= {-1, -1};
797 my_exit(
"error, option/command missing\n");
800 while(opt_index<argc)
802 opt= match(argc, argv, &opt_index, options, opt_no);
806 my_exit(
"unknown option: %s\n", argv[opt_index]);
811 if (match_st(argc, argv, &opt_index, &st))
812 my_exit(
"interface name missing!\n");
813 if (interface_no >= MAX_INTERFACES)
814 my_exit(
"too many interfaces given (more than %d)\n",
816 interfaces[interface_no++]= st;
821 rc= match_int(argc, argv, &opt_index, &(rt_prios[i]));
823 my_exit(
"error, argument missing\n");
825 my_exit(
"error, argument is not a number\n");
829 rc= match_float(argc, argv, &opt_index, &sleep);
831 my_exit(
"the sleep parameter is missing!\n");
833 my_exit(
"the sleep parameter is is not a number!\n");
836 rc= match_int(argc, argv, &opt_index, &quietlevel);
838 my_exit(
"the quiet parameter is missing!\n");
840 my_exit(
"the quiet parameter is is not a number!\n");
841 if ((quietlevel<1) || (quietlevel>5))
842 my_exit(
"only quiet-levels 1 to 5 are allowed\n"
843 "(input was %d)\n", quietlevel);
846 rc= match_int(argc, argv, &opt_index, &countsize);
848 my_exit(
"the count parameter is missing!\n");
850 my_exit(
"the count parameter is is not a number!\n");
851 if ((countsize<1) || (countsize>8))
852 my_exit(
"[bytes]-parameter must be between 1 and 8 !!\n");
855 rc= match_int(argc, argv, &opt_index, &countstart);
857 my_exit(
"the countstart parameter is missing!\n");
859 my_exit(
"the countstart parameter is is not a number!\n");
861 my_exit(
"[count]-parameter must be greater than 0 !!\n");
864 rc= match_int(argc, argv, &opt_index, &loops);
866 my_exit(
"the loop parameter is missing!\n");
868 my_exit(
"the loop parameter is is not a number!\n");
870 my_exit(
"loop-parameter should be greater than zero!\n"
871 "(input was %ld)\n", loops);
874 rc= match_int(argc, argv, &opt_index, &tracelevel);
876 my_exit(
"the tracelevel parameter is missing!\n");
878 my_exit(
"the tracelevel parameter is is not a number!\n");
880 my_exit(
"trace level must be positive\n"
881 "(input was %d)\n", tracelevel);
884 rc= match_int(argc, argv, &opt_index, &errprintlevel);
886 my_exit(
"the errprintlevel parameter is missing!\n");
888 my_exit(
"the errprintlevel parameter is is not a number!\n");
890 my_exit(
"errprintlevel must be positive\n"
891 "(input was %d)\n", errprintlevel);
904 flag|= (T_CONSISTENCE | T_CHECK);
912 if (operation!=CMD_NOTHING)
913 my_exit(
"contradicting options\n");
914 operation=CMD_RWTEST;
917 rc= match_int(argc, argv, &opt_index, &(n[i]));
919 my_exit(
"error, argument missing\n");
921 my_exit(
"error, argument is not a number\n");
926 if (operation!=CMD_NOTHING)
927 my_exit(
"contradicting options\n");
931 operation=CMD_READ;
break;
933 operation=CMD_READ_NOW;
break;
937 rc= match_int(argc, argv, &opt_index, &(n[i]));
939 my_exit(
"error, argument missing\n");
941 my_exit(
"error, argument is not a number\n");
945 if (operation!=CMD_NOTHING)
946 my_exit(
"contradicting options\n");
947 operation= CMD_QUEUE_READ;
950 while ((rc==0) && (last_valid_n<21))
953 rc= match_int(argc, argv, &opt_index, &(n[last_valid_n]));
958 my_exit(
"error, port missing\n");
962 my_exit(
"error, timeout missing\n");
966 my_exit(
"error, first object-id missing\n");
978 if (operation!=CMD_NOTHING)
979 my_exit(
"contradicting options\n");
983 operation=CMD_WRITE;
break;
985 operation=CMD_WRITE_W;
break;
987 operation=CMD_WRITE_INHIBIT;
break;
990 if (operation==CMD_WRITE_INHIBIT)
994 rc= match_int(argc, argv, &opt_index, &(n[i]));
996 my_exit(
"error, argument missing\n");
998 my_exit(
"error, argument is not a number\n");
1003 if (interface_no==0)
1005 printf(
"Error, no interfaces defined\n");
1008 if (!init_socan(interfaces, interface_no,
1009 tracelevel, errprintlevel,
1010 rt_prios[0], rt_prios[1]))
1015 printf(
"error, no command given\n");
1018 test_simple_rw(n[0], n[1], n[2], n[3], n[4], n[5],
1023 test_read(n[0], n[1], n[2],
1024 loops, sleep, quietlevel,
1025 (operation==CMD_READ_NOW),
1028 case CMD_QUEUE_READ:
1029 test_queue_read(n[0], n[1], n+2, last_valid_n-1,
1031 quietlevel, countsize, flag);
1035 case CMD_WRITE_INHIBIT:
1036 test_write(n[0], n[1], n[2], n[3],
1037 loops, sleep, quietlevel,
1039 countsize,countstart,
c header file for the simple CAN interface (sci)
enum sci_Errcode_Constants sci_Errcode
c header file for socan object layer library.