00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "SocketCANDevice.h"
00022 #include <errno.h>
00023
00024 struct can_frame frame;
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 int SocketCANDevice::getDeviceError(int iErrorState) {
00040 int error = ERRID_DEV_WRITEERROR;
00041
00042 if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_QRCVEMPTY) {
00043 warning("receive queue is empty");
00044 error = ERRID_DEV_WRITEERROR;
00045 }
00046 if (iErrorState & CAN_ERR_OVERRUN) {
00047 warning("receive buffer overrun");
00048 error = ERRID_DEV_READERROR;
00049 }
00050 if (iErrorState & CAN_ERR_XMTFULL) {
00051 warning("transmit buffer full");
00052 error = ERRID_DEV_WRITEERROR;
00053 }
00054 if (iErrorState & CAN_ERR_BUSOFF) {
00055 warning("CAN_ERR_OFF_BUS");
00056 error = ERRID_DEV_READERROR;
00057 }
00058 if (iErrorState & CAN_ERR_ILLPARAMTYPE) {
00059 warning("CAN_ERR_ILLPARAMTYPE");
00060 error = ERRID_DEV_READERROR;
00061 }
00062 if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_QXMTFULL) {
00063 warning("transmit queue full");
00064 error = ERRID_DEV_WRITEERROR;
00065 }
00066 if (iErrorState & CAN_ERR_BUSLIGHT) {
00067 warning("bus error");
00068 error = ERRID_DEV_WRITEERROR;
00069 }
00070 if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_BUSHEAVY) {
00071 warning("bus error");
00072 error = ERRID_DEV_WRITEERROR;
00073 }
00074 if (((unsigned int) iErrorState & 0xFFFF) & CAN_ERR_RESOURCE) {
00075 warning("can't create resource");
00076 error = ERRID_DEV_WRITEERROR;
00077 }
00078
00079 return error;
00080 }
00081
00082 int SocketCANDevice::setBaudRate(unsigned char iBaudRate) {
00083 return setBaudRate();
00084 }
00085
00086 int SocketCANDevice::setBaudRate() {
00087 return m_iErrorState;
00088 }
00089
00090 int SocketCANDevice::setMessageId(unsigned long uiMessageId) {
00091 return m_iErrorState;
00092 }
00093
00094 int SocketCANDevice::clearReadQueue() {
00095
00096 int iRetVal = 0;
00097 can_frame frame;
00098
00099 debug(1, "entering SocketCANDevice::clearReadQueue()...\n");
00100 m_iErrorState = 0;
00101 do {
00102 debug(1, "Trying to read messages ...");
00103 iRetVal = read(m_iDeviceId, &frame, sizeof(frame));
00104 debug(0, " 0x%04x\n", iRetVal);
00105
00106 } while (iRetVal != CAN_ERR_QRCVEMPTY);
00107 return iRetVal;
00108 }
00109
00110 int SocketCANDevice::reinit(unsigned char ucBaudRateId) {
00111
00112 return m_iErrorState;
00113 }
00114
00115 int SocketCANDevice::readDevice(CProtocolMessage& rclProtocolMessage) {
00116
00117 frame.data[0] = 0;
00118 frame.data[1] = 0;
00119 frame.data[2] = 0;
00120 frame.data[3] = 0;
00121 frame.data[4] = 0;
00122 frame.data[5] = 0;
00123 frame.data[6] = 0;
00124 frame.data[7] = 0;
00125 frame.can_dlc = 8;
00126 frame.can_id = 0;
00127 int iCount = 0;
00128 int bRet = 1, iRet = 0;
00129 int nrRead = 0;
00130
00131 debug(1, "Trying to read Device");
00132
00133 iRet = read(m_iDeviceId, &frame, sizeof(frame));
00134
00135 if (iRet >= 0)
00136 {
00137
00138 debug(1, "sizeof(frame)", sizeof(frame));
00139 debug(1, "bytes read: %d", iRet);
00140 debug(1, "id: %d", frame.can_id);
00141 debug(1, "data: %c ", frame.data);
00142 debug(1, "length: %d ", frame.can_dlc);
00143
00144
00145 rclProtocolMessage.m_uiMessageId = frame.can_id;
00146 rclProtocolMessage.m_ucMessageLength = frame.can_dlc;
00147 memcpy(rclProtocolMessage.m_aucMessageData, frame.data,
00148 rclProtocolMessage.m_ucMessageLength);
00149 bRet = 0;
00150 } else
00151 {
00152
00153 if (iCount > 26)
00154 printf("error in SocketCANDevice::readDevice()");
00155 iCount++;
00156 bRet = 1;
00157 }
00158
00159 debug(1, "bRet before return from SocketCANDevice::readDevice: %d ", bRet);
00160 return bRet;
00161
00162 }
00163
00164 int SocketCANDevice::writeDevice(CProtocolMessage& rclProtocolMessage) {
00165
00166 debug(1, "SocketCANDevice::writeDevice ");
00167
00168 int bytes_sent = -1;
00169
00170 debug(1, "m_iDeviceId %d", m_iDeviceId);
00171
00172 if (m_bInitialized == false)
00173 return false;
00174
00175 frame.can_dlc = (int) rclProtocolMessage.m_ucMessageLength;
00176
00177 debug(1, "frame can_dlc: %d", frame.can_dlc);
00178 debug(1, "clProtocolMessage.m_ucMessageLength: %d",
00179 (unsigned int) rclProtocolMessage.m_ucMessageLength);
00180
00181 frame.can_id = rclProtocolMessage.m_uiMessageId;
00182
00183 debug(1, "frame can_id_%d ", frame.can_id);
00184 debug(1, "rclProtocolMessage.m_uiMessageId: %d ",
00185 rclProtocolMessage.m_uiMessageId);
00186
00187
00188 for (int i = 0; i < (unsigned int) rclProtocolMessage.m_ucMessageLength;
00189 i++) {
00190
00191 frame.data[i] = rclProtocolMessage.m_aucMessageData[i];
00192 debug(1, "rclProtocolMessage.m_aucMessageData: %c ",
00193 rclProtocolMessage.m_aucMessageData);
00194 debug(1, "frame data[%d]: %c", i, frame.data);
00195 }
00196 debug(1, "sizeof frame : %d", sizeof(frame));
00197
00198 int bRet = 1;
00199
00200
00201 bytes_sent = write(m_iDeviceId, &frame, sizeof(frame));
00202 usleep(10000);
00203 if (bytes_sent < 0) {
00204 debug(1, "error in SocketCANDevice::writeDevice: ");
00205 bRet = 1;
00206 }
00207
00208 if (bytes_sent > 0) {
00209 bRet = 0;
00210 debug(1, "bytes sent in SocketCANDevice::writeDevice: %d ", bytes_sent);
00211 }
00212
00213 return bRet;
00214
00215 }
00216
00217
00218
00219
00220
00221
00222
00223 SocketCANDevice::SocketCANDevice() {
00224 m_bInitialized = false;
00225 }
00226
00227 SocketCANDevice::SocketCANDevice(const SocketCANDevice& rclSocketCANDevice) {
00228 error(-1, "Sorry constructor is not implemented");
00229 }
00230
00231 SocketCANDevice::~SocketCANDevice() {
00232 if (m_bInitialized) {
00233 this->exit();
00234 }
00235 }
00236
00237
00238
00239
00240
00241
00242 SocketCANDevice& SocketCANDevice::operator=(const SocketCANDevice& rclSocketCANDevice) {
00243 error(-1, "Sorry operator= is not implemented");
00244 return *this;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 void SocketCANDevice::setQueueSize(unsigned short uiQueueSize) {
00260 m_uiQueueSize = uiQueueSize;
00261 }
00262
00263 void SocketCANDevice::setTimeOut(unsigned long uiTimeOut) {
00264 m_uiTimeOut = uiTimeOut;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 int SocketCANDevice::init() {
00280 return init(0x014);
00281 }
00282
00283 int SocketCANDevice::init(unsigned long baudRate) {
00284 return m_iErrorState;
00285 }
00286
00287 int SocketCANDevice::init(const char* acInitString) {
00288
00289 printf("Trying to open CAN on can0 ...");
00290 m_iErrorState = 0;
00291 setTimeOut(100000);
00292 m_iDeviceId = socket(PF_CAN, SOCK_RAW, CAN_RAW);
00293
00294 struct timeval timeout;
00295 timeout.tv_sec = 0;
00296 timeout.tv_usec = m_uiTimeOut;
00297
00298 if (setsockopt(m_iDeviceId, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout,
00299 sizeof(struct timeval)) < 0)
00300 error("setsockopt failed\n");
00301
00302 struct ifreq ifr;
00303 char* pcToken;
00304 char acString[128];
00305 strncpy(m_acInitString, acInitString, 128);
00306 strncpy(acString, acInitString, 128);
00307 pcToken = strtok(acString, ",");
00308 std::string pcTokenStrInit(pcToken);
00309 pcToken = strtok(pcToken, ":");
00310 if (!pcToken) {
00311 m_iErrorState = ERRID_DEV_BADINITSTRING;
00312 return m_iErrorState;
00313 }
00314 if (strcmp(pcToken, "SOCKETCAN") != 0) {
00315 m_iErrorState = ERRID_DEV_BADINITSTRING;
00316 return m_iErrorState;
00317 }
00318 std::string pcTokenStr(pcToken);
00319 std::string devName = pcTokenStrInit.substr(pcTokenStr.length() + 1, 4);
00320 strcpy(ifr.ifr_name, devName.c_str());
00321 m_DeviceName = ifr.ifr_name;
00322 debug(1,"name: %x",*m_DeviceName);
00323 int ret = ioctl(m_iDeviceId, SIOCGIFINDEX, &ifr);
00324 struct sockaddr_can addr;
00325 addr.can_family = AF_CAN;
00326 addr.can_ifindex = ifr.ifr_ifindex;
00327 int ret1 = bind(m_iDeviceId, (struct sockaddr*) &addr, sizeof(addr));
00328
00329 m_bInitFlag = true;
00330
00331 if (!m_iDeviceId) {
00332 printf("Cannot open CAN on USB:\n");
00333 } else {
00334 printf("Open CAN on USB suceeded!\n");
00335 m_bInitialized = true;
00336 }
00337
00338 updateModuleIdMap();
00339
00340 debug(1, "finished updateModuleIdMap");
00341 debug(1,
00342 "m_iErrorState before returning of SocketCANDevice::init(const char* acInitString): %d",
00343 m_iErrorState);
00344
00345 return m_iErrorState;
00346 }
00347
00348 int SocketCANDevice::exit() {
00349 int iRetVal = 0;
00350 if (!m_bInitialized) {
00351 warning("exit:device not initialized");
00352 m_iErrorState = ERRID_DEV_NOTINITIALIZED;
00353 return m_iErrorState;
00354 }
00355 EnterCriticalSection(&m_csDevice);
00356 iRetVal = CAN_ERR_OK;
00357 if (iRetVal != CAN_ERR_OK) {
00358 warning("can close failed Errorcode: %d", iRetVal);
00359 getDeviceError(iRetVal);
00360 m_iErrorState = ERRID_DEV_EXITERROR;
00361 }
00362 m_bInitFlag = false;
00363 LeaveCriticalSection(&m_csDevice);
00364 DeleteCriticalSection(&m_csDevice);
00365 return m_iErrorState;
00366 }
00367
00368 int SocketCANDevice::waitForStartMotionAll() {
00369 int iRetVal = 0;
00370 TPCANRdMsg TPCMsg;
00371 TPCMsg.Msg.LEN = 8;
00372 TPCMsg.Msg.MSGTYPE = 0;
00373 TPCMsg.Msg.ID = 0;
00374 bool bRecieved = false;
00375 can_frame frame;
00376 m_iErrorState = 0;
00377 iRetVal = read(m_iDeviceId, &frame, sizeof(frame));
00378 m_iErrorState = 0;
00379
00380 do {
00381 iRetVal = read(m_iDeviceId, &frame, sizeof(frame));
00382 if (iRetVal != CAN_ERR_OK) {
00383 warning("can read failed Errorcode: 0x%04x", iRetVal);
00384 m_iErrorState = getDeviceError(iRetVal);
00385 return m_iErrorState;
00386 }
00387 bRecieved = true;
00388 if (frame.can_id != MSGID_ALL) {
00389 debug(1, "received CAN-ID %x, expected %x", TPCMsg.Msg.ID,
00390 MSGID_ALL);
00391 bRecieved = false;
00392 }
00393 if (frame.data[0] != CMDID_STARTMOVE) {
00394 debug(1, "wrong command ID");
00395 bRecieved = false;
00396 }
00397 } while (!bRecieved);
00398 return m_iErrorState;
00399 }
00400