Refs #316. Re-integrated CAN error detection for the SocketCAN interface module.

git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@327 5dc33758-31d5-4daf-9ae8-b24bf3d40d73
This commit is contained in:
Frank Voorburg 2017-08-02 17:07:57 +00:00
parent 4e11b1401f
commit b36420fdd1
1 changed files with 60 additions and 11 deletions

View File

@ -43,6 +43,7 @@
#include <net/if.h> /* network interfaces */ #include <net/if.h> /* network interfaces */
#include <linux/can.h> /* CAN kernel definitions */ #include <linux/can.h> /* CAN kernel definitions */
#include <linux/can/raw.h> /* CAN raw sockets */ #include <linux/can/raw.h> /* CAN raw sockets */
#include <linux/can/error.h> /* CAN errors */
#include "util.h" /* Utility module */ #include "util.h" /* Utility module */
#include "candriver.h" /* Generic CAN driver module */ #include "candriver.h" /* Generic CAN driver module */
#include "socketcan.h" /* SocketCAN interface */ #include "socketcan.h" /* SocketCAN interface */
@ -104,6 +105,11 @@ static volatile tCanEvents * socketCanEventsList;
/** \brief Total number of event entries into the \ref socketCanEventsList list. */ /** \brief Total number of event entries into the \ref socketCanEventsList list. */
static volatile uint32_t socketCanEventsEntries; static volatile uint32_t socketCanEventsEntries;
/** \brief Flag to set in the event thread when either a bus off or bus heavy situation
*
*/
static volatile bool socketCanErrorDetected;
/** \brief Event thread control. */ /** \brief Event thread control. */
static volatile tSocketCanThreadCtrl eventThreadCtrl; static volatile tSocketCanThreadCtrl eventThreadCtrl;
@ -140,6 +146,7 @@ static void SocketCanInit(tCanSettings const * settings)
/* Initialize locals. */ /* Initialize locals. */
socketCanEventsList = NULL; socketCanEventsList = NULL;
socketCanEventsEntries = 0; socketCanEventsEntries = 0;
socketCanErrorDetected = false;
/* Reset CAN interface settings. */ /* Reset CAN interface settings. */
socketCanSettings.devicename = ""; socketCanSettings.devicename = "";
socketCanSettings.channel = 0; socketCanSettings.channel = 0;
@ -218,6 +225,10 @@ static bool SocketCanConnect(void)
struct ifreq ifr; struct ifreq ifr;
int32_t flags; int32_t flags;
struct can_filter rxFilter; struct can_filter rxFilter;
can_err_mask_t errMask;
/* Reset the error flag. */
socketCanErrorDetected = false;
/* Check settings. */ /* Check settings. */
assert(socketCanSettings.devicename != NULL); assert(socketCanSettings.devicename != NULL);
@ -304,6 +315,17 @@ static bool SocketCanConnect(void)
} }
} }
/* Configure reception of bus off and other CAN controller status related events. */
if (result)
{
errMask = (CAN_ERR_BUSOFF | CAN_ERR_CRTL);
if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &errMask, sizeof(errMask)) != 0)
{
close(canSocket);
result = false;
}
}
if (result) if (result)
{ {
/* Start the event thread. */ /* Start the event thread. */
@ -329,6 +351,8 @@ static void SocketCanDisconnect(void)
SocketCanStopEventThread(); SocketCanStopEventThread();
/* Close the socket. */ /* Close the socket. */
close(canSocket); close(canSocket);
/* Reset the error flag. */
socketCanErrorDetected = false;
} /*** end of SocketCanDisconnect ***/ } /*** end of SocketCanDisconnect ***/
@ -398,15 +422,16 @@ static bool SocketCanIsBusError(void)
{ {
bool result= false; bool result= false;
/* The purpose of this function is that when an error is detected, SocketCanConnect() /* Read flag to detect bus off or bus heavy. */
* can be called to reset the error. With SocketCAN a bus off error is reset on UtilCriticalSectionEnter();
* network link level and not by this interface module. It is therefore not needed to if (socketCanErrorDetected)
* check for the errors here. It is better to configure the automatic bus off recovery {
* on network link level. For example: /* Update result. */
* ip link set can0 type can bitrate 500000 restart-ms 100 result = true;
* where "restart-ms <value>" specifies the number of milliseconds after which that /* Reset the error flag. */
* automatic recovery is started. socketCanErrorDetected = false;
*/ }
UtilCriticalSectionExit();
/* Give the result back to the caller. */ /* Give the result back to the caller. */
return result; return result;
@ -531,11 +556,35 @@ static void *SocketCanEventThread(void *param)
/* Check if CAN frames were received. */ /* Check if CAN frames were received. */
while (read(canSocket, &canRxFrame, sizeof(struct can_frame)) == (ssize_t)sizeof(struct can_frame)) while (read(canSocket, &canRxFrame, sizeof(struct can_frame)) == (ssize_t)sizeof(struct can_frame))
{ {
/* Ignore remote frames and error information. */ /* Ignore remote frames */
if ( (canRxFrame.can_id & CAN_RTR_FLAG) || (canRxFrame.can_id & CAN_ERR_FLAG) ) if (canRxFrame.can_id & CAN_RTR_FLAG)
{ {
continue; continue;
} }
/* Does the message contain error information? */
else if (canRxFrame.can_id & CAN_ERR_FLAG)
{
/* Was it a bus off event? */
if ((canRxFrame.can_id & CAN_ERR_BUSOFF) != 0)
{
/* Set the error flag. */
UtilCriticalSectionEnter();
socketCanErrorDetected = true;
UtilCriticalSectionExit();
}
/* Was it a CAN controller event? */
else if ((canRxFrame.can_id & CAN_ERR_CRTL) != 0)
{
/* Is the controller in error passive mode (bus heavy)? */
if ((canRxFrame.data[1] & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE)) != 0)
{
/* Set the error flag. */
UtilCriticalSectionEnter();
socketCanErrorDetected = true;
UtilCriticalSectionExit();
}
}
}
/* It was a regular CAN message with either 11- or 29-bit identifier. */ /* It was a regular CAN message with either 11- or 29-bit identifier. */
else else
{ {