etisserant@47: /* etisserant@47: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@47: etisserant@47: Copyright (C): Edouard TISSERANT and Francis DUPIN etisserant@47: etisserant@47: See COPYING file for copyrights details. etisserant@47: etisserant@47: This library is free software; you can redistribute it and/or etisserant@47: modify it under the terms of the GNU Lesser General Public etisserant@47: License as published by the Free Software Foundation; either etisserant@47: version 2.1 of the License, or (at your option) any later version. etisserant@47: etisserant@47: This library is distributed in the hope that it will be useful, etisserant@47: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@47: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@47: Lesser General Public License for more details. etisserant@47: etisserant@47: You should have received a copy of the GNU Lesser General Public etisserant@47: License along with this library; if not, write to the Free Software etisserant@47: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@47: */ etisserant@47: etisserant@47: #include etisserant@47: #include etisserant@47: #include etisserant@47: #include /* for NULL */ etisserant@47: etisserant@47: #include "config.h" etisserant@47: etisserant@47: #ifdef RTCAN_SOCKET etisserant@47: #include "rtdm/rtcan.h" etisserant@47: #define CAN_IFNAME "rtcan%s" etisserant@47: #define CAN_SOCKET rt_dev_socket etisserant@47: #define CAN_CLOSE rt_dev_close etisserant@47: #define CAN_RECV rt_dev_recv etisserant@47: #define CAN_SEND rt_dev_send etisserant@47: #define CAN_BIND rt_dev_bind etisserant@47: #define CAN_IOCTL rt_dev_ioctl etisserant@47: #else etisserant@187: #include etisserant@187: #include etisserant@187: #include "linux/can.h" etisserant@187: #include "linux/can/raw.h" etisserant@187: #include "net/if.h" etisserant@187: #define PF_CAN 29 etisserant@187: #define AF_CAN PF_CAN etisserant@187: //#include "af_can.h" etisserant@47: #define CAN_IFNAME "can%s" etisserant@47: #define CAN_SOCKET socket etisserant@47: #define CAN_CLOSE close etisserant@47: #define CAN_RECV recv etisserant@47: #define CAN_SEND send etisserant@47: #define CAN_BIND bind etisserant@47: #define CAN_IOCTL ioctl etisserant@47: #endif etisserant@47: etisserant@47: #include "can_driver.h" etisserant@47: etisserant@47: /*********functions which permit to communicate with the board****************/ etisserant@145: UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) etisserant@47: { etisserant@47: int res; etisserant@47: struct can_frame frame; etisserant@47: etisserant@187: res = CAN_RECV(*(int*)fd0, &frame, sizeof(frame), 0); etisserant@47: if (res < 0) etisserant@47: return 1; etisserant@47: etisserant@47: m->cob_id.w = frame.can_id & CAN_EFF_MASK; etisserant@47: m->len = frame.can_dlc; etisserant@47: if (frame.can_id & CAN_RTR_FLAG) etisserant@47: m->rtr = 1; etisserant@47: else etisserant@47: m->rtr = 0; etisserant@47: memcpy(m->data, frame.data, 8); etisserant@47: etisserant@47: return 0; etisserant@47: } etisserant@47: etisserant@47: etisserant@47: /***************************************************************************/ etisserant@145: UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) etisserant@47: { etisserant@47: int res; etisserant@47: struct can_frame frame; etisserant@47: etisserant@47: frame.can_id = m->cob_id.w; etisserant@47: if (frame.can_id >= 0x800) etisserant@47: frame.can_id |= CAN_EFF_FLAG; etisserant@47: frame.can_dlc = m->len; etisserant@47: if (m->rtr) etisserant@47: frame.can_id |= CAN_RTR_FLAG; etisserant@47: else etisserant@47: memcpy(frame.data, m->data, 8); etisserant@47: etisserant@187: res = CAN_SEND(*(int*)fd0, &frame, sizeof(frame), 0); etisserant@47: if (res < 0) etisserant@47: return 1; etisserant@47: etisserant@47: return 0; etisserant@47: } etisserant@47: etisserant@47: /***************************************************************************/ etisserant@145: CAN_HANDLE canOpen_driver(s_BOARD *board) etisserant@47: { etisserant@47: struct ifreq ifr; etisserant@47: struct sockaddr_can addr; etisserant@47: int err; etisserant@187: CAN_HANDLE fd0 = malloc(sizeof(int)); etisserant@47: etisserant@189: *(int*)fd0 = CAN_SOCKET(PF_CAN, SOCK_RAW, CAN_RAW); etisserant@187: if(*(int*)fd0 < 0){ etisserant@47: fprintf(stderr,"Socket creation failed.\n"); etisserant@47: goto error_ret; etisserant@47: } etisserant@47: etisserant@47: snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname); etisserant@187: err = CAN_IOCTL(*(int*)fd0, SIOCGIFINDEX, &ifr); etisserant@47: if (err) { etisserant@47: fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name); etisserant@47: goto error_close; etisserant@47: } etisserant@47: etisserant@47: addr.can_family = AF_CAN; etisserant@47: addr.can_ifindex = ifr.ifr_ifindex; etisserant@187: err = CAN_BIND(*(int*)fd0, (struct sockaddr *)&addr, etisserant@47: sizeof(addr)); etisserant@47: if (err) { etisserant@47: fprintf(stderr, "Binding failed.\n"); etisserant@47: goto error_close; etisserant@47: } etisserant@47: etisserant@47: return fd0; etisserant@47: etisserant@47: error_close: etisserant@189: CAN_CLOSE(*(int*)fd0); etisserant@47: etisserant@47: error_ret: etisserant@187: free(fd0); etisserant@47: return NULL; etisserant@47: } etisserant@47: etisserant@47: /***************************************************************************/ etisserant@145: int canClose_driver(CAN_HANDLE fd0) etisserant@47: { etisserant@47: if (fd0) { etisserant@187: CAN_CLOSE(*(int*)fd0); etisserant@187: free(fd0); etisserant@47: } etisserant@47: return 0; etisserant@47: }