]> rtime.felk.cvut.cz Git - socketcan-simulink.git/commitdiff
The first partially working version - objects assignment is incorrect still.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Mon, 6 Oct 2014 18:30:30 +0000 (20:30 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Mon, 6 Oct 2014 18:30:30 +0000 (20:30 +0200)
The build requires _BSD_SOURCE define because it is necessary
for enabling "struct ifreq" declaration in "net/if.h" header
file.

Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
blocks/tlc_c/rpp_can_common.tlc
blocks/tlc_c/sfunction_canreceive.tlc
blocks/tlc_c/sfunction_cansetup.tlc
blocks/tlc_c/sfunction_cantransmit.tlc

index e38b474d3810c4731bc75e2b5c84607507b24963..4c038c0d23e3f49d55ddbf31787701ec650793aa 100644 (file)
@@ -64,6 +64,7 @@
        %<LibAddToCommonIncludes("<sys/ioctl.h>")>
        %<LibAddToCommonIncludes("<linux/can.h>")>
        %<LibAddToCommonIncludes("<linux/can/raw.h>")>
+       %<LibAddToCommonIncludes("<stdlib.h>")>
        %<LibAddToCommonIncludes("<fcntl.h>")>
        %<LibAddToCommonIncludes("<errno.h>")>
   %endif
index e8b27a829da85be7b930ef1d7a5c25eee2c9ab96..29d25dc395966b2b5972a43eaaf530e29989b353 100644 (file)
          int ret;
          int dlc;
 
-         ret = recv(%<RppRxInfo.HwObj>, &sc_frame, sizeof(sc_frame), 0);
+         ret = recv(can_rx_handles[%<RppRxInfo.HwObj>], &sc_frame, sizeof(sc_frame), 0);
          if (ret == -1) {
             if (errno!=EAGAIN) {
               printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
index f2eec3498b0e35c97327e485af301b982452bdff..f228c6f182bfd319f0e82756ec4d4b38a3d4ca09 100644 (file)
                struct ert_can_tx_config *tx_config;
                struct ert_can_rx_config *rx_config;
                struct ert_can_channel_config *channel_config;
+               const char **channel_ifname;
        };
 
        #define CAN_TX_COUNT %<Rpp.Can.Tx.NumBlocks>
          %endforeach
        };
 
+       const char *ert_can_channel_ifname[] = {
+               "can0", /*FIXME - for now skip this */
+               "can0",
+               "can1",
+               "can2",
+               "can3"
+       };
+
        const struct ert_can_config can_config = {
                .num_tx_obj = CAN_TX_COUNT,
                .num_rx_obj = CAN_RX_COUNT,
                .tx_config = tx_config,
                .rx_config = rx_config,
-               .channel_config = can_channel_config
+               .channel_config = can_channel_config,
+               .channel_ifname = ert_can_channel_ifname
        };
 
-        int can_rx_handles[CAN_RX_COUNT];
+        int can_tx_handles[CAN_TX_COUNT];
 
-        int can_tx_handles[CAN_RX_COUNT];
+        int can_rx_handles[CAN_RX_COUNT];
 
        %closefile buffer
        %<LibSetSourceFileSection(LibGetModelDotCFile(), "Declarations", buffer)>
-       int returnstatus;
+       {
+               int idx;
+
+               for (idx = 0; idx < CAN_TX_COUNT; idx++) {
+                       struct ert_can_tx_config *cfg = &can_config.tx_config[idx];
+                       const char *ifname = can_config.channel_ifname[cfg->channel];
+                       int fd;
+                       struct sockaddr_can addr;
+                       struct ifreq ifr;
+
+                       fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+                       if (fd == -1) {
+                               printf("CAN socket create error.\n");
+                               exit(1);
+                       }
+                       if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
+                               printf("CAN socket set O_NONBLOCK error.\n");
+                               exit(1);
+                       }
+                       memset(&addr, 0, sizeof(addr));
+                       addr.can_family = AF_CAN;
+                       strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+                       if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
+                               printf("CAN socket ioctl SIOCGIFINDEX error.\n");
+                               exit(1);
+                       }
+                       addr.can_ifindex = ifr.ifr_ifindex;
+
+                       /* disable default receive filter on this RAW socket */
+                       setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
+
+                       if (bind(fd, (struct sockaddr *)(void*)&addr, sizeof(addr)) < 0) {
+                               printf("CAN socket bind error.\n");
+                               exit(1);
+                       }
+                       printf("rx opening %s\n", ifname);
+                       can_tx_handles[idx] = fd;
+               }
+
+               for (idx = 0; idx < CAN_RX_COUNT; idx++) {
+                       struct ert_can_rx_config *cfg = &can_config.rx_config[idx];
+                       const char *ifname = can_config.channel_ifname[cfg->channel];
+                       int fd;
+                       struct sockaddr_can addr;
+                       struct ifreq ifr;
+                       struct can_filter sc_filter;
+
+                       fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+                       if (fd == -1) {
+                               printf("CAN socket create error.\n");
+                               exit(1);
+                       }
+                       if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
+                               printf("CAN socket set O_NONBLOCK error.\n");
+                               exit(1);
+                       }
+                       memset(&addr, 0, sizeof(addr));
+                       addr.can_family = AF_CAN;
+                       strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+                       if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
+                               printf("CAN socket ioctl SIOCGIFINDEX error.\n");
+                               exit(1);
+                       }
+                       addr.can_ifindex = ifr.ifr_ifindex;
+
+                       /* disable default receive filter on this RAW socket */
+                       setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
 
-       returnstatus = rpp_can_init(&can_config);
-       if (returnstatus < 0) {
-           printf("CAN driver initialization error.\n");
+                       if (bind(fd, (struct sockaddr *)(void*)&addr, sizeof(addr)) < 0) {
+                               printf("CAN socket bind error.\n");
+                               exit(1);
+                       }
+
+                       memset(&sc_filter, 0, sizeof(sc_filter));
+                       sc_filter.can_id = cfg->id;
+                       sc_filter.can_mask = CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG;
+                       if (cfg->id_type == ERT_CAN_EXTENDED)
+                               sc_filter.can_id |= CAN_EFF_FLAG;
+                       else if(cfg->id_type == ERT_CAN_MIXED)
+                               sc_filter.can_mask &= ~CAN_EFF_FLAG;
+
+                       if (setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &sc_filter, sizeof(sc_filter)) < 0) {
+                               printf("CAN socket setsockopt CAN_RAW_FILTER error.\n");
+                               exit(1);
+                       }
+
+                       printf("tx opening %s\n", ifname);
+                       can_rx_handles[idx] = fd;
+               }
        }
        %%else {
        %%      printf("CAN communication initialized.\n");
index cd6d1d919fe1622e8dd11862b30ed627456e84c6..7ef9b689ea55939c18edabc2e5539f5bf39727be 100644 (file)
        %endforeach
       %endif
       sc_frame.can_id=%<RppTxInfo.Id>;
-
-
+      if (%<RppTxInfo.Type> == 2)
+        sc_frame.can_id|=CAN_EFF_FLAG;
       %%printf("CAN%d (hw_obj: %d): Sending message id 0x%X from mailbox %d. Data: ", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>);
       %%for (i = 0; i < pdu.dlc; i++ ) {
        %%      printf("%X ", pdu.data[i]);
        %%}
        %%printf("\n");
-       ret = send(%<RppTxInfo.HwObj>, &sc_frame, sizeof(sc_frame), 0);
+       ret = send(can_tx_handles[%<RppTxInfo.HwObj>], &sc_frame, sizeof(sc_frame), 0);
        if (ret < sizeof(sc_frame)) {
          /% printf("CAN%d (hw_obj: %d): Sending a message id 0x%X from mailbox %d failed.\n", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>); %/
        }