]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - drivers/can_socket/can_socket.c
Fix can_socket again.
[CanFestival-3.git] / drivers / can_socket / can_socket.c
1 /*
2 This file is part of CanFestival, a library implementing CanOpen Stack.
3
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
5
6 See COPYING file for copyrights details.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stddef.h> /* for NULL */
27
28 #include "config.h"
29
30 #ifdef RTCAN_SOCKET
31 #include "rtdm/rtcan.h"
32 #define CAN_IFNAME     "rtcan%s"
33 #define CAN_SOCKET     rt_dev_socket
34 #define CAN_CLOSE      rt_dev_close
35 #define CAN_RECV       rt_dev_recv
36 #define CAN_SEND       rt_dev_send
37 #define CAN_BIND       rt_dev_bind
38 #define CAN_IOCTL      rt_dev_ioctl
39 #else
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include "linux/can.h"
43 #include "linux/can/raw.h"
44 #include "net/if.h"
45 #define PF_CAN 29
46 #define AF_CAN PF_CAN
47 //#include "af_can.h"
48 #define CAN_IFNAME     "can%s"
49 #define CAN_SOCKET     socket
50 #define CAN_CLOSE      close
51 #define CAN_RECV       recv
52 #define CAN_SEND       send
53 #define CAN_BIND       bind
54 #define CAN_IOCTL      ioctl
55 #endif
56
57 #include "can_driver.h"
58
59 /*********functions which permit to communicate with the board****************/
60 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
61 {
62        int res;
63        struct can_frame frame;
64
65        res = CAN_RECV(*(int*)fd0, &frame, sizeof(frame), 0);
66        if (res < 0)
67                return 1;
68
69        m->cob_id.w = frame.can_id & CAN_EFF_MASK;
70        m->len      = frame.can_dlc;
71        if (frame.can_id & CAN_RTR_FLAG)
72                m->rtr = 1;
73        else
74                m->rtr = 0;
75        memcpy(m->data, frame.data, 8);
76
77        return 0;
78 }
79
80
81 /***************************************************************************/
82 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
83 {
84        int res;
85        struct can_frame frame;
86
87        frame.can_id = m->cob_id.w;
88        if (frame.can_id >= 0x800)
89                frame.can_id |= CAN_EFF_FLAG;
90        frame.can_dlc = m->len;
91        if (m->rtr)
92                frame.can_id |= CAN_RTR_FLAG;
93        else
94                memcpy(frame.data, m->data, 8);
95
96        res = CAN_SEND(*(int*)fd0, &frame, sizeof(frame), 0);
97        if (res < 0)
98                return 1;
99
100        return 0;
101 }
102
103 /***************************************************************************/
104 CAN_HANDLE canOpen_driver(s_BOARD *board)
105 {
106        struct ifreq ifr;
107        struct sockaddr_can addr;
108        int err;
109        CAN_HANDLE fd0 = malloc(sizeof(int));
110
111        *(int*)fd0 = CAN_SOCKET(PF_CAN, SOCK_RAW, CAN_RAW);
112        if(*(int*)fd0 < 0){
113                fprintf(stderr,"Socket creation failed.\n");
114                goto error_ret;
115        }
116
117        snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname);
118        err = CAN_IOCTL(*(int*)fd0, SIOCGIFINDEX, &ifr);
119        if (err) {
120                fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name);
121                goto error_close;
122        }
123
124        addr.can_family  = AF_CAN;
125        addr.can_ifindex = ifr.ifr_ifindex;
126        err = CAN_BIND(*(int*)fd0, (struct sockaddr *)&addr,
127                              sizeof(addr));
128        if (err) {
129                fprintf(stderr, "Binding failed.\n");
130                goto error_close;
131        }
132
133        return fd0;
134
135  error_close:
136        CAN_CLOSE(*(int*)fd0);
137
138  error_ret:
139        free(fd0);
140        return NULL;
141 }
142
143 /***************************************************************************/
144 int canClose_driver(CAN_HANDLE fd0)
145 {
146        if (fd0) {
147                CAN_CLOSE(*(int*)fd0);
148                free(fd0);
149        }
150        return 0;
151 }