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@47: #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: struct CANPort; etisserant@47: #define CAN_HANDLE struct CANPort * etisserant@47: etisserant@47: #include etisserant@47: etisserant@47: #include "timer.h" etisserant@47: #include "can_driver.h" etisserant@47: #include "timers_driver.h" etisserant@47: etisserant@47: typedef struct CANPort { etisserant@47: int fd; etisserant@47: TASK_HANDLE receiveTask; etisserant@47: CO_Data* d; etisserant@47: } CANPort; etisserant@47: etisserant@47: /*********functions which permit to communicate with the board****************/ etisserant@47: UNS8 canReceive(CAN_HANDLE fd0, Message *m) etisserant@47: { etisserant@47: int res; etisserant@47: struct can_frame frame; etisserant@47: etisserant@47: res = CAN_RECV(fd0->fd, &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: void canReceiveLoop(CAN_HANDLE fd0) etisserant@47: { etisserant@47: CO_Data* d = fd0->d; etisserant@47: Message m; etisserant@47: etisserant@47: while (1) { etisserant@47: if (canReceive(fd0, &m) != 0) etisserant@47: break; etisserant@47: etisserant@47: EnterMutex(); etisserant@47: canDispatch(d, &m); etisserant@47: LeaveMutex(); etisserant@47: } etisserant@47: } etisserant@47: etisserant@47: /***************************************************************************/ etisserant@47: UNS8 canSend(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@47: res = CAN_SEND(fd0->fd, &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@47: CAN_HANDLE canOpen(s_BOARD *board) etisserant@47: { etisserant@47: CAN_HANDLE fd0; etisserant@47: struct ifreq ifr; etisserant@47: struct sockaddr_can addr; etisserant@47: int err; etisserant@47: etisserant@47: fd0 = malloc(sizeof(*fd0)); etisserant@47: if (!fd0) etisserant@47: return NULL; etisserant@47: etisserant@47: fd0->fd = CAN_SOCKET(PF_CAN, SOCK_RAW, 0); etisserant@47: if(fd0->fd < 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@47: err = CAN_IOCTL(fd0->fd, 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@47: err = CAN_BIND(fd0->fd, (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: fd0->d = board->d; etisserant@47: CreateReceiveTask(fd0, &fd0->receiveTask); etisserant@47: return fd0; etisserant@47: etisserant@47: error_close: etisserant@47: CAN_CLOSE(fd0->fd); etisserant@47: etisserant@47: error_ret: etisserant@47: free(fd0); etisserant@47: return NULL; etisserant@47: } etisserant@47: etisserant@47: /***************************************************************************/ etisserant@47: int canClose(CAN_HANDLE fd0) etisserant@47: { etisserant@47: if (fd0) { etisserant@47: WaitReceiveTaskEnd(&fd0->receiveTask); etisserant@47: CAN_CLOSE(fd0->fd); etisserant@47: free(fd0); etisserant@47: } etisserant@47: return 0; etisserant@47: }