drivers/can_socket/can_socket.c
author etisserant
Sat, 12 May 2007 23:06:43 +0200
changeset 193 0487270d441c
parent 192 9dd6f17ef7e5
child 196 65aa7a664f6f
permissions -rw-r--r--
Fixed problems in can_virtual, loop when closing pipes.
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     1
/*
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     3
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     5
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     7
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    12
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    17
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    21
*/
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    22
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    23
#include <stdio.h>
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    24
#include <string.h>
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    25
#include <stdlib.h>
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    26
#include <stddef.h> /* for NULL */
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
    27
#include <errno.h>
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    28
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    29
#include "config.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    30
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    31
#ifdef RTCAN_SOCKET
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    32
#include "rtdm/rtcan.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    33
#define CAN_IFNAME     "rtcan%s"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    34
#define CAN_SOCKET     rt_dev_socket
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    35
#define CAN_CLOSE      rt_dev_close
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    36
#define CAN_RECV       rt_dev_recv
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    37
#define CAN_SEND       rt_dev_send
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    38
#define CAN_BIND       rt_dev_bind
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    39
#define CAN_IOCTL      rt_dev_ioctl
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    40
#else
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    41
#include <sys/socket.h>
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    42
#include <sys/ioctl.h>
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    43
#include "linux/can.h"
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    44
#include "linux/can/raw.h"
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    45
#include "net/if.h"
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    46
#define PF_CAN 29
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    47
#define AF_CAN PF_CAN
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    48
//#include "af_can.h"
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    49
#define CAN_IFNAME     "can%s"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    50
#define CAN_SOCKET     socket
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    51
#define CAN_CLOSE      close
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    52
#define CAN_RECV       recv
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    53
#define CAN_SEND       send
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    54
#define CAN_BIND       bind
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    55
#define CAN_IOCTL      ioctl
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    56
#endif
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    57
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    58
#include "can_driver.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    59
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    60
/*********functions which permit to communicate with the board****************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    61
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    62
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    63
       int res;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    64
       struct can_frame frame;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    65
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    66
       res = CAN_RECV(*(int*)fd0, &frame, sizeof(frame), 0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    67
       if (res < 0)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    68
               return 1;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    69
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    70
       m->cob_id.w = frame.can_id & CAN_EFF_MASK;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    71
       m->len      = frame.can_dlc;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    72
       if (frame.can_id & CAN_RTR_FLAG)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    73
               m->rtr = 1;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    74
       else
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    75
               m->rtr = 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    76
       memcpy(m->data, frame.data, 8);
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    77
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    78
       return 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    79
}
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    80
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    81
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    82
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    83
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    84
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    85
       int res;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    86
       struct can_frame frame;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    87
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    88
       frame.can_id = m->cob_id.w;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    89
       if (frame.can_id >= 0x800)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    90
               frame.can_id |= CAN_EFF_FLAG;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    91
       frame.can_dlc = m->len;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    92
       if (m->rtr)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    93
               frame.can_id |= CAN_RTR_FLAG;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    94
       else
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    95
               memcpy(frame.data, m->data, 8);
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    96
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
    97
       res = CAN_SEND(*(int*)fd0, &frame, sizeof(frame), 0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    98
       if (res < 0)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    99
               return 1;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   100
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   101
       return 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   102
}
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   103
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   104
/***************************************************************************/
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   105
#ifdef RTCAN_SOCKET
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   106
int TranslateBaudRate(const char* optarg) {
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   107
       int baudrate;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   108
       int val, len;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   109
       char *pos = NULL;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   110
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   111
       len = strlen(optarg);
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   112
       if (!len)
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   113
               return 0;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   114
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   115
       switch ((int)optarg[len - 1]) {
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   116
       case 'M':
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   117
               baudrate =  1000000;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   118
               break;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   119
       case 'K':
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   120
               baudrate =  1000;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   121
               break;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   122
       default:
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   123
               baudrate = 1;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   124
               break;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   125
       }
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   126
       if ((sscanf(optarg, "%i", &val)) == 1)
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   127
               baudrate *= val;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   128
       else
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   129
               baudrate = 0;;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   130
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   131
       printf("baudrate=%d (%s)\n", baudrate, optarg);
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   132
       return baudrate;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   133
}
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   134
#endif
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   135
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   136
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   137
CAN_HANDLE canOpen_driver(s_BOARD *board)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   138
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   139
       struct ifreq ifr;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   140
       struct sockaddr_can addr;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   141
       int err;
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
   142
       CAN_HANDLE fd0 = malloc(sizeof(int));
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   143
#ifdef RTCAN_SOCKET
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   144
       can_baudrate_t *baudrate;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   145
       can_mode_t *mode;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   146
#endif
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   147
189
1c1d1893f1c9 Fix can_socket again.
etisserant
parents: 187
diff changeset
   148
       *(int*)fd0 = CAN_SOCKET(PF_CAN, SOCK_RAW, CAN_RAW);
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   149
       if (*(int*)fd0 < 0) {
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   150
               perror("Socket creation failed");
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   151
               goto error_ret;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   152
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   153
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   154
       if (*board->busname >= '0' && *board->busname <= '9')
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   155
               snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME,
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   156
                        board->busname);
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   157
       else
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   158
               strncpy(ifr.ifr_name, board->busname, IFNAMSIZ);
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
   159
       err = CAN_IOCTL(*(int*)fd0, SIOCGIFINDEX, &ifr);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   160
       if (err) {
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   161
               fprintf(stderr, "Getting IF index for %s failed: %s\n",
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   162
                       ifr.ifr_name, strerror(errno));
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   163
               goto error_close;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   164
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   165
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   166
       addr.can_family  = AF_CAN;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   167
       addr.can_ifindex = ifr.ifr_ifindex;
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
   168
       err = CAN_BIND(*(int*)fd0, (struct sockaddr *)&addr,
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   169
                             sizeof(addr));
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   170
       if (err) {
192
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   171
               perror("Binding failed");
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   172
               goto error_close;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   173
       }
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   174
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   175
#ifdef RTCAN_SOCKET
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   176
       baudrate = (can_baudrate_t *)&ifr.ifr_ifru;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   177
       *baudrate = TranslateBaudRate(board->baudrate);
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   178
       if (!*baudrate)
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   179
               goto error_close;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   180
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   181
       err = CAN_IOCTL(*(int*)fd0, SIOCSCANBAUDRATE, &ifr);
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   182
       if (err) {
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   183
               fprintf(stderr,
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   184
                       "Setting baudrate %d failed: %s\n",
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   185
                       *baudrate, strerror(errno));
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   186
               goto error_close;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   187
       }
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   188
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   189
       mode = (can_mode_t *)&ifr.ifr_ifru;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   190
       *mode = CAN_MODE_START;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   191
       err = CAN_IOCTL(*(int*)fd0, SIOCSCANMODE, &ifr);
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   192
       if (err) {
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   193
               perror("Starting CAN device failed");
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   194
               goto error_close;
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   195
       }
9dd6f17ef7e5 Wolfgang enhancements for RT-socket-CAN support
etisserant
parents: 189
diff changeset
   196
#endif
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   197
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   198
       return fd0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   199
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   200
 error_close:
189
1c1d1893f1c9 Fix can_socket again.
etisserant
parents: 187
diff changeset
   201
       CAN_CLOSE(*(int*)fd0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   202
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   203
 error_ret:
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
   204
       free(fd0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   205
       return NULL;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   206
}
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   207
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   208
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   209
int canClose_driver(CAN_HANDLE fd0)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   210
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   211
       if (fd0) {
187
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
   212
               CAN_CLOSE(*(int*)fd0);
d3930d3f1323 Fixed can_socket compilation bugs
etisserant
parents: 145
diff changeset
   213
               free(fd0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   214
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   215
       return 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   216
}