00001 /* 00002 * Copyright 2016 Intelligent Industrial Robotics (IIROB) Group, 00003 * Institute for Anthropomatics and Robotics (IAR) - 00004 * Intelligent Process Control and Robotics (IPR), 00005 * Karlsruhe Institute of Technology (KIT) 00006 * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA) 00007 * 00008 * Licensed under the Apache License, Version 2.0 (the "License"); 00009 * you may not use this file except in compliance with the License. 00010 * You may obtain a copy of the License at 00011 * 00012 * http://www.apache.org/licenses/LICENSE-2.0 00013 00014 * Unless required by applicable law or agreed to in writing, software 00015 * distributed under the License is distributed on an "AS IS" BASIS, 00016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 * See the License for the specific language governing permissions and 00018 * limitations under the License. 00019 */ 00020 00021 #ifndef SocketCANDevice_INCLUDEDEF_H 00022 #define SocketCANDevice_INCLUDEDEF_H 00023 //----------------------------------------------- 00024 00025 #include <sys/types.h> 00026 #include <sys/stat.h> 00027 #include <sys/socket.h> 00028 #include <sys/ioctl.h> 00029 00030 #include <fcntl.h> 00031 #include <cerrno> 00032 #include <cstring> 00033 00034 #include <linux/can.h> 00035 #include <linux/can/bcm.h> 00036 #include <linux/can/raw.h> 00037 00038 #include <net/if.h> 00039 #include <string.h> 00040 00041 #ifndef PF_CAN 00042 #define PF_CAN 29 00043 #endif 00044 00045 #ifndef AF_CAN 00046 #define AF_CAN PF_CAN 00047 #endif 00048 00049 00050 //----------------------------------------------- 00051 00052 #include <iostream> 00053 #include <cstdio> 00054 #include <libpcan/libpcan.h> 00055 #include "ProtocolDevice.h" 00056 00057 //----------------------------------------------- 00058 00059 class SocketCANDevice: public CProtocolDevice { 00060 00061 private: 00062 00063 // ---- private data ---------------------------------------------------- ; 00064 00065 // ---- private auxiliary functions ------------------------------------- ; 00066 00067 protected: 00068 // ---- protected data ---------------------------------------------------- ; 00069 bool m_bInitialized; 00070 int m_iDeviceId; 00071 int m_iNoOfRetries; 00072 unsigned short m_uiQueueSize; 00073 unsigned long m_uiTimeOut; 00074 char * m_DeviceName; 00075 00076 00077 // ---- protected auxiliary functions ------------------------------------- ; 00078 int getDeviceError(int iErrorState); 00079 int setBaudRate(); 00080 int setBaudRate(unsigned char iBaudRate); 00081 int setMessageId(unsigned long uiMessageId); 00082 int clearReadQueue(); 00083 int reinit(unsigned char ucBaudRateId); 00084 int readDevice(CProtocolMessage& rclProtocolMessage); 00085 int writeDevice(CProtocolMessage& rclProtocolMessage); 00086 00087 public: 00088 00089 // ---- public data ----------------------------------------------------- ; 00090 00091 // ---- constructors / destructor --------------------------------------- ; 00092 00093 // default constructor 00094 SocketCANDevice(); 00095 SocketCANDevice(const SocketCANDevice& rclSocketCANDevice); 00096 ~SocketCANDevice(); 00097 00098 // ---- operators ------------------------------------------------------- ; 00099 // assignment operator 00100 SocketCANDevice& operator=(const SocketCANDevice& rclSocketCANDevice); 00101 00102 // ---- query functions ------------------------------------------------- ; 00103 00104 // ---- modify functions ------------------------------------------------ ; 00105 00106 void setQueueSize(unsigned short uiQueueSize); 00107 void setTimeOut(unsigned long uiTimeOut); 00108 00109 // ---- I/O functions --------------------------------------------------- ; 00110 00111 // ---- exec functions -------------------------------------------------- ; 00112 00113 int init(); 00114 int init(unsigned long baudRate); 00115 int init(const char* acInitString); 00116 int exit(); 00117 int waitForStartMotionAll(); 00118 00119 00120 }; 00121 #endif