]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blobdiff - blocks/tlc_c/sfunction_canreceive.tlc
The first round of changes to port RPP code to Linux SocketCAN.
[socketcan-simulink.git] / blocks / tlc_c / sfunction_canreceive.tlc
index 602b23a9320fab5ca83690612208548423f3d7ec..e8b27a829da85be7b930ef1d7a5c25eee2c9ab96 100644 (file)
        %assign message = LibBlockOutputSignal(1, "", "", 1)
 
        {
-         struct rpp_can_pdu pdu;
+         struct can_frame sc_frame;
          int ret;
+         int dlc;
 
-         ret = rpp_can_read(%<RppRxInfo.HwObj>, &pdu);
-         switch (ret) {
-           case -RPP_EINVAL:
-               rpp_sci_printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
-               break;
-           case -RPP_ENODATA:
-               break;
-           case SUCCESS: {
-               %if %<data_type_par>==4
-                 // CAN_MESSAGE
-                 %<message>.Length = pdu.dlc;
-                 %<message>.ID = pdu.id;
-                 int i;
-                 for (i = 0; i < pdu.dlc; i++ ) {
-                       %<message>.Data[i] = pdu.data[i];
-                       %%rpp_sci_printf("%X ", pdu.data[i]);
-                 }
-               %elseif %<data_type_par>==3
-                 // uint32
-                 int msg =     (pdu.data[0]) |
-                 (pdu.data[1]<<8) |
-                 (pdu.data[2]<<16) |
-                 (pdu.data[3]<<24);
-                 %<message> = msg;
-                 %%rpp_sci_printf("32b: %X ", msg);
-               %elseif %<data_type_par>==2
-                 // uint16
-                 int msg =     (pdu.data[0]) |
-                 (pdu.data[1]<<8);
-                 %<message> = msg;
-                 %%rpp_sci_printf("16b: %X ", msg);
-               %else
-                 // uint8
-                 int msg = pdu.data[0];
-                 %<message> = msg;
-                 %%rpp_sci_printf("8b: %X ", msg);
-               %endif
-               %%rpp_sci_printf("\n");
-
-               %% Call a function to process the received message via function-call subsystem
-               %foreach callIdx = NumSFcnSysOutputCalls
-                 %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
-                       %continue
-                 %endif
-                 %% call the downstream system
-                 %<LibBlockExecuteFcnCall(block, callIdx)>\
-               %endforeach
-               break;
+         ret = recv(%<RppRxInfo.HwObj>, &sc_frame, sizeof(sc_frame), 0);
+         if (ret == -1) {
+            if (errno!=EAGAIN) {
+              printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
            }
+         } else if (ret < sizeof(sc_frame)) {
+            printf("Receiving CAN message (%s) returns truncated length %d.\n", "%<RppRxInfo.Name>", ret);
+         } else {
+           dlc = sc_frame.can_dlc;
+           if (dlc > 8)
+             dlc = 8;
+           %if %<data_type_par>==4
+               // CAN_MESSAGE
+               %<message>.Length = dlc;
+               %<message>.ID = sc_frame.can_id & CAN_EFF_MASK;
+               int i;
+               for (i = 0; i < dlc; i++ ) {
+                 %<message>.Data[i] = sc_frame.data[i];
+                 printf("%X ", sc_frame.data[i]);
+               }
+           %elseif %<data_type_par>==3
+               // uint32
+               unsigned int msg = (sc_frame.data[0]) |
+                 (sc_frame.data[1]<<8) |
+                 (sc_frame.data[2]<<16) |
+                 (sc_frame.data[3]<<24);
+               if (dlc < 4)
+                 msg &= 0xffffffff << (8 * dlc);
+               %<message> = msg;
+               printf("32b: %X ", msg);
+           %elseif %<data_type_par>==2
+               // uint16
+               unsigned int msg = (sc_frame.data[0]) |
+                   (sc_frame.data[1]<<8);
+               if (dlc < 2)
+                 msg &= 0xffffffff << (8 * dlc);
+               %<message> = msg;
+               printf("16b: %X ", msg);
+           %else
+               // uint8
+               unsigned int msg = sc_frame.data[0];
+               %<message> = msg;
+               printf("8b: %X ", msg);
+           %endif
+           printf("\n");
+
+           %% Call a function to process the received message via function-call subsystem
+           %foreach callIdx = NumSFcnSysOutputCalls
+               %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
+                 %continue
+               %endif
+               %% call the downstream system
+               %<LibBlockExecuteFcnCall(block, callIdx)>\
+           %endforeach
          }
        }
   %endif