drivers/can_socket/can_socket.c
author etisserant
Fri, 06 Apr 2007 16:49:28 +0200
changeset 149 fe50ada8020b
parent 145 e747d2e26af0
child 187 d3930d3f1323
permissions -rw-r--r--
Changes in the API:
- No more mandatory callbacks declared for each node anymore
heartbeatError
SDOtimeoutError
initialisation
preOperational
operational
stopped
post_sync
post_TPDO
canSend

- Fill function ptr to declare callbacks
CO_Data.heartbeatError
CO_Data.SDOtimeoutError
CO_Data.initialisation
CO_Data.preOperational
CO_Data.operational
CO_Data.stopped
CO_Data.post_sync
CO_Data.post_TPDO

- CanClose now takes CO_Data* as parameter
- canSend is provided to the stack directly by OS interface, no specific callback.
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 */
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    27
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    28
#include "config.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    29
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    30
#ifdef RTCAN_SOCKET
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    31
#include "rtdm/rtcan.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    32
#define CAN_IFNAME     "rtcan%s"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    33
#define CAN_SOCKET     rt_dev_socket
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    34
#define CAN_CLOSE      rt_dev_close
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    35
#define CAN_RECV       rt_dev_recv
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    36
#define CAN_SEND       rt_dev_send
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    37
#define CAN_BIND       rt_dev_bind
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    38
#define CAN_IOCTL      rt_dev_ioctl
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    39
#else
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    40
#include "af_can.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    41
#define CAN_IFNAME     "can%s"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    42
#define CAN_SOCKET     socket
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    43
#define CAN_CLOSE      close
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    44
#define CAN_RECV       recv
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    45
#define CAN_SEND       send
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    46
#define CAN_BIND       bind
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    47
#define CAN_IOCTL      ioctl
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    48
#endif
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    49
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    50
#include "can_driver.h"
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    51
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    52
/*********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
    53
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    54
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    55
       int res;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    56
       struct can_frame frame;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    57
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    58
       res = CAN_RECV(fd0, &frame, sizeof(frame), 0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    59
       if (res < 0)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    60
               return 1;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    61
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    62
       m->cob_id.w = frame.can_id & CAN_EFF_MASK;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    63
       m->len      = frame.can_dlc;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    64
       if (frame.can_id & CAN_RTR_FLAG)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    65
               m->rtr = 1;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    66
       else
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    67
               m->rtr = 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    68
       memcpy(m->data, frame.data, 8);
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
       return 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    71
}
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    72
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    73
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    74
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    75
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    76
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    77
       int res;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    78
       struct can_frame frame;
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
       frame.can_id = m->cob_id.w;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    81
       if (frame.can_id >= 0x800)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    82
               frame.can_id |= CAN_EFF_FLAG;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    83
       frame.can_dlc = m->len;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    84
       if (m->rtr)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    85
               frame.can_id |= CAN_RTR_FLAG;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    86
       else
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    87
               memcpy(frame.data, m->data, 8);
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    88
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    89
       res = CAN_SEND(fd0, &frame, sizeof(frame), 0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    90
       if (res < 0)
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    91
               return 1;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    92
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    93
       return 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    94
}
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    95
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    96
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
    97
CAN_HANDLE canOpen_driver(s_BOARD *board)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    98
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
    99
       CAN_HANDLE fd0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   100
       struct ifreq ifr;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   101
       struct sockaddr_can addr;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   102
       int err;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   103
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   104
       fd0 = CAN_SOCKET(PF_CAN, SOCK_RAW, 0);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   105
       if(fd0 < 0){
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   106
               fprintf(stderr,"Socket creation failed.\n");
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   107
               goto error_ret;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   108
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   109
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   110
       snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname);
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   111
       err = CAN_IOCTL(fd0, SIOCGIFINDEX, &ifr);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   112
       if (err) {
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   113
               fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name);
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   114
               goto error_close;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   115
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   116
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   117
       addr.can_family  = AF_CAN;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   118
       addr.can_ifindex = ifr.ifr_ifindex;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   119
       err = CAN_BIND(fd0, (struct sockaddr *)&addr,
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   120
                             sizeof(addr));
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   121
       if (err) {
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   122
               fprintf(stderr, "Binding failed.\n");
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   123
               goto error_close;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   124
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   125
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   126
       return fd0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   127
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   128
 error_close:
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   129
       CAN_CLOSE(fd0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   130
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   131
 error_ret:
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   132
       return NULL;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   133
}
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   134
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   135
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   136
int canClose_driver(CAN_HANDLE fd0)
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   137
{
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   138
       if (fd0) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 47
diff changeset
   139
               CAN_CLOSE(fd0);
47
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   140
       }
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   141
       return 0;
8a1047ab51f4 SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff changeset
   142
}