drivers/can_socket/can_socket.c
changeset 290 43c3b2bf3e32
parent 196 65aa7a664f6f
child 301 f4b64aa848e1
equal deleted inserted replaced
289:a22ce0314063 290:43c3b2bf3e32
    57 #define CAN_ERRNO(err) errno
    57 #define CAN_ERRNO(err) errno
    58 #endif
    58 #endif
    59 
    59 
    60 #include "can_driver.h"
    60 #include "can_driver.h"
    61 
    61 
       
    62 #if defined DEBUG_WAR_CONSOLE_ON || defined DEBUG_ERR_CONSOLE_ON
       
    63 #include "def.h"
       
    64 
       
    65 #define MyCase(fc) case fc: printf(#fc);break;
       
    66 void print_message(Message *m)
       
    67 {
       
    68     int i;
       
    69     switch(m->cob_id.w >> 7)
       
    70     {
       
    71         MyCase(SYNC)
       
    72         MyCase(TIME_STAMP)
       
    73         MyCase(PDO1tx)
       
    74         MyCase(PDO1rx)
       
    75         MyCase(PDO2tx)
       
    76         MyCase(PDO2rx)
       
    77         MyCase(PDO3tx)
       
    78         MyCase(PDO3rx)
       
    79         MyCase(PDO4tx)
       
    80         MyCase(PDO4rx)
       
    81         MyCase(SDOtx)
       
    82         MyCase(SDOrx)
       
    83         MyCase(NODE_GUARD)
       
    84         MyCase(NMT)
       
    85     }
       
    86     printf(" rtr:%d", m->rtr);
       
    87     printf(" len:%d", m->len);
       
    88     for (i = 0 ; i < m->len ; i++)
       
    89         printf(" %02x", m->data[i]);
       
    90     printf("\n");
       
    91 }
       
    92 
       
    93 #endif
    62 /*********functions which permit to communicate with the board****************/
    94 /*********functions which permit to communicate with the board****************/
    63 UNS8
    95 UNS8
    64 canReceive_driver (CAN_HANDLE fd0, Message * m)
    96 canReceive_driver (CAN_HANDLE fd0, Message * m)
    65 {
    97 {
    66   int res;
    98   int res;
    79     m->rtr = 1;
   111     m->rtr = 1;
    80   else
   112   else
    81     m->rtr = 0;
   113     m->rtr = 0;
    82   memcpy (m->data, frame.data, 8);
   114   memcpy (m->data, frame.data, 8);
    83 
   115 
       
   116 #if defined DEBUG_WAR_CONSOLE_ON || defined DEBUG_ERR_CONSOLE_ON
       
   117   printf("in : ");
       
   118   print_message(m);
       
   119 #endif
    84   return 0;
   120   return 0;
    85 }
   121 }
    86 
   122 
    87 
   123 
    88 /***************************************************************************/
   124 /***************************************************************************/
    99   if (m->rtr)
   135   if (m->rtr)
   100     frame.can_id |= CAN_RTR_FLAG;
   136     frame.can_id |= CAN_RTR_FLAG;
   101   else
   137   else
   102     memcpy (frame.data, m->data, 8);
   138     memcpy (frame.data, m->data, 8);
   103 
   139 
       
   140 #if defined DEBUG_WAR_CONSOLE_ON || defined DEBUG_ERR_CONSOLE_ON
       
   141   printf("out : ");
       
   142   print_message(m);
       
   143 #endif
   104   res = CAN_SEND (*(int *) fd0, &frame, sizeof (frame), 0);
   144   res = CAN_SEND (*(int *) fd0, &frame, sizeof (frame), 0);
   105   if (res < 0)
   145   if (res < 0)
   106     {
   146     {
   107       fprintf (stderr, "Send failed: %s\n", strerror (CAN_ERRNO (res)));
   147       fprintf (stderr, "Send failed: %s\n", strerror (CAN_ERRNO (res)));
   108       return 1;
   148       return 1;
   175     {
   215     {
   176       fprintf (stderr, "Getting IF index for %s failed: %s\n",
   216       fprintf (stderr, "Getting IF index for %s failed: %s\n",
   177 	       ifr.ifr_name, strerror (CAN_ERRNO (err)));
   217 	       ifr.ifr_name, strerror (CAN_ERRNO (err)));
   178       goto error_close;
   218       goto error_close;
   179     }
   219     }
   180 
   220   
       
   221   {
       
   222     int loopback = 1;
       
   223     setsockopt(*(int *)fd0, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
       
   224                &loopback, sizeof(loopback));
       
   225   }
       
   226   
       
   227   {
       
   228     int recv_own_msgs = 0; /* 0 = disabled (default), 1 = enabled */
       
   229 
       
   230     setsockopt(*(int *)fd0, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
       
   231                &recv_own_msgs, sizeof(recv_own_msgs));
       
   232   }
       
   233   
   181   addr.can_family = AF_CAN;
   234   addr.can_family = AF_CAN;
   182   addr.can_ifindex = ifr.ifr_ifindex;
   235   addr.can_ifindex = ifr.ifr_ifindex;
   183   err = CAN_BIND (*(int *) fd0, (struct sockaddr *) &addr, sizeof (addr));
   236   err = CAN_BIND (*(int *) fd0, (struct sockaddr *) &addr, sizeof (addr));
   184   if (err)
   237   if (err)
   185     {
   238     {