[PATCH 1/2] testsuites/libtests/can01: Serialize CAN write and read messages among the tasks

Prashanth S fishesprashanth at gmail.com
Sat Dec 3 09:02:08 UTC 2022


The CAN framework has minimal Rx implementation, so to test loopback
driver the test application is modified to write and read atomically
---
 testsuites/libtests/can01/init.c | 71 ++++++++++++++------------------
 1 file changed, 31 insertions(+), 40 deletions(-)

diff --git a/testsuites/libtests/can01/init.c b/testsuites/libtests/can01/init.c
index 0675fe606f..f70e68fb51 100644
--- a/testsuites/libtests/can01/init.c
+++ b/testsuites/libtests/can01/init.c
@@ -48,7 +48,7 @@
                    if (c3 == '9') {     \
                      if (c2 == 'z') {   \
                        if (c1 == 'z') { \
-                         printf("not enough task letters for names !!!\n"); \
+                         CAN_ERR("not enough task letters for names !!!\n"); \
                          exit( 1 );     \
                        } else           \
                          c1++;          \
@@ -63,6 +63,10 @@
                  else                   \
                    c4++                 \
 
+struct test_thread {
+  rtems_mutex mutex;
+};
+
 static void test_task(rtems_task_argument);
 int can_loopback_init(const char *);
 int create_task(int);
@@ -70,60 +74,54 @@ int create_task(int);
 static rtems_id task_id[TASKS];
 static rtems_id task_test_status[TASKS] = {[0 ... (TASKS - 1)] = false};
 
+static struct test_thread thread;
+
 const char rtems_test_name[] = "CAN test TX, RX with CAN loopback driver";
 
-/*FIXME: Should Implement one more test application for the 
- * RTR support
- *
- * For testing, the number of successful read and write 
- * count is verified.
- */
 static void test_task(rtems_task_argument data)
 {
-  //sleep so that other tasks will be created.
   sleep(1);
 
   int fd, task_num = (uint32_t)data;
   uint32_t count = 0, msg_size;
 
-  struct can_msg msg;
+  struct can_msg send_msg, recv_msg;
 
   printf("CAN tx and rx for %s\n", CAN_DEV_FILE);
 
   fd = open(CAN_DEV_FILE, O_RDWR);
   if (fd < 0) {
-    printf("open error: task = %u %s: %s\n", task_num, CAN_DEV_FILE, strerror(errno));
+    CAN_ERR("open error: task = %u %s: %s\n", task_num, CAN_DEV_FILE, strerror(errno));
   }
 
   rtems_test_assert(fd >= 0);
 
   for (int i = 0; i < NUM_TEST_MSGS; i++) {
-    printf("test_task %u\n", task_num);
-
-    msg.id = task_num;
-    //FIXME: Implement Test cases for other flags also.
-    msg.flags = 0;
-    msg.len = (i + 1) % 9;
+    send_msg.id = task_num;
+    send_msg.flags = 0;
+    send_msg.len = (i + 1) % 9;
 
-    for (int j = 0; j < msg.len; j++) {
-      msg.data[j] = 'a' + j;
+    for (int j = 0; j < send_msg.len; j++) {
+      send_msg.data[j] = 'a' + j;
     }
   
-    msg_size = ((char *)&msg.data[msg.len] - (char *)&msg);
+    msg_size = ((char *)&send_msg.data[send_msg.len] - (char *)&send_msg);
 
-    printf("calling write task = %u\n", task_num);
+    rtems_mutex_lock(&thread.mutex);
 
-    count = write(fd, &msg, sizeof(msg));
+    count = write(fd, &send_msg, sizeof(send_msg));
     rtems_test_assert(count == msg_size);
-    printf("task = %u write count = %u\n", task_num, count);
 
-    printf("calling read task = %u\n", task_num);
-    count = read(fd, &msg, sizeof(msg));
-    rtems_test_assert(count > 0);
-    printf("task = %u read count = %u\n", task_num, count);
+    count = read(fd, &recv_msg, sizeof(recv_msg));
+    rtems_test_assert(send_msg.len == recv_msg.len);
+
+    for (int i = 0; i < recv_msg.len; i++) {                                          
+      rtems_test_assert(send_msg.data[i] == recv_msg.data[i]);
+    }                                                                             
 
-    printf("received message\n");
-    can_print_msg(&msg);
+    rtems_mutex_unlock(&thread.mutex);
+
+    can_print_msg(&recv_msg);
 
     sleep(1);
   }
@@ -131,14 +129,11 @@ static void test_task(rtems_task_argument data)
 
   task_test_status[task_num] = true;
 
-  printf("task exited = %u\n", task_num);
   rtems_task_exit();
 }
 
 int create_task(int i)
 {
-  printf("Creating task %d\n", i);
-
   rtems_status_code result;
   rtems_name name;    
 
@@ -156,21 +151,18 @@ int create_task(int i)
                              RTEMS_FIFO | RTEMS_FLOATING_POINT,
                              &task_id[i]);
   if (result != RTEMS_SUCCESSFUL) {
-    printf("rtems_task_create error: %s\n", rtems_status_text(result));
+    CAN_ERR("rtems_task_create error: %s\n", rtems_status_text(result));
     rtems_test_assert(result == RTEMS_SUCCESSFUL);
   }
 
-  printf("number = %3" PRIi32 ", id = %08" PRIxrtems_id ", starting, ", i, task_id[i]);
-
   fflush(stdout);
 
-  printf("starting task\n");
   result = rtems_task_start(task_id[i],
                             test_task,
                             (rtems_task_argument)i);
 
   if (result != RTEMS_SUCCESSFUL) {
-    printf("rtems_task_start failed %s\n", rtems_status_text(result));
+    CAN_ERR("rtems_task_start failed %s\n", rtems_status_text(result));
     rtems_test_assert(result == RTEMS_SUCCESSFUL);
   }
 
@@ -183,8 +175,6 @@ static rtems_task Init(
   rtems_task_argument ignored
 )
 {
-  printf("Init\n");
-
   int ret;
 
   rtems_print_printer_fprintf_putc(&rtems_test_printer);
@@ -199,10 +189,12 @@ static rtems_task Init(
 
   ret = can_loopback_init(CAN_DEV_FILE);
   if (ret != RTEMS_SUCCESSFUL) {
-    printf("%s failed\n", rtems_test_name);
+    CAN_ERR("%s failed\n", rtems_test_name);
     rtems_test_assert(ret == RTEMS_SUCCESSFUL);
   }
 
+  rtems_mutex_init(&thread.mutex, "CAN test");
+
   for (int i = 0; i < TASKS; i++) {  
     create_task(i);
   }
@@ -212,7 +204,6 @@ static rtems_task Init(
     int flag = 0;
     for (int i = 0; i < TASKS; i++) {
       if (task_test_status[i] == false) {
-        printf("task not exited = %d\n", i);
         sleep(1);
         flag = 1;
         break;
-- 
2.25.1



More information about the devel mailing list