From 2b3f4b7f3144de1133dcc86257fead0cd680556f Mon Sep 17 00:00:00 2001 From: Frank Voorburg Date: Mon, 31 Jul 2017 17:45:47 +0000 Subject: [PATCH] Refs #316. Refactoring to perform CAN interface linking inside the CAN driver module's CanInit() itself. git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@321 5dc33758-31d5-4daf-9ae8-b24bf3d40d73 --- Host/Source/LibOpenBLT/candriver.c | 48 +++++++++++++++++++++++++----- Host/Source/LibOpenBLT/candriver.h | 2 +- Host/Source/LibOpenBLT/xcptpcan.c | 28 +---------------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/Host/Source/LibOpenBLT/candriver.c b/Host/Source/LibOpenBLT/candriver.c index b1857c06..c949cff2 100644 --- a/Host/Source/LibOpenBLT/candriver.c +++ b/Host/Source/LibOpenBLT/candriver.c @@ -33,7 +33,14 @@ #include /* for standard integer types */ #include /* for NULL declaration */ #include /* for boolean type */ +#include /* for string library */ #include "candriver.h" /* Generic CAN driver module */ +#if defined(PLATFORM_WIN32) +#include "pcanusb.h" /* Peak PCAN-USB interface */ +#endif +#if defined(PLATFORM_LINUX) +#include "socketcan.h" /* SocketCAN interface */ +#endif /**************************************************************************************** @@ -49,10 +56,9 @@ static bool canConnected; /************************************************************************************//** ** \brief Initializes the CAN module. Typically called once at program startup. ** \param settings Pointer to the CAN module settings. -** \param interface Pointer to the CAN interface to link. ** ****************************************************************************************/ -void CanInit(tCanSettings const * settings, tCanInterface const * const interface) +void CanInit(tCanSettings const * settings) { /* Initialize locals. */ canIfPtr = NULL; @@ -60,15 +66,41 @@ void CanInit(tCanSettings const * settings, tCanInterface const * const interfac /* Check parameters. */ assert(settings != NULL); - assert(interface != NULL); /* Only continue with valid parameters. */ - if ( (settings != NULL) && (interface != NULL) ) /*lint !e774 */ + if (settings != NULL) /*lint !e774 */ { - /* Link the CAN interface. */ - canIfPtr = interface; - /* Initialize the CAN interface. */ - canIfPtr->Init(settings); + /* Check device name. */ + assert(settings->devicename != NULL); + + /* Only continue with a valid device name. */ + if (settings->devicename != NULL) /*lint !e774 */ + { + /* Determine the pointer to the correct CAN interface, based on the specified + * device name. + */ +#if defined(PLATFORM_WIN32) + if (strcmp(settings->devicename, "peak_pcanusb") == 0) + { + canIfPtr = PCanUsbGetInterface(); + } +#endif +#if defined(PLATFORM_LINUX) + /* On Linux, the device name is the name of the SocketCAN link, so always link + * the SocketCAN interface to the CAN driver. + */ + canIfPtr = SocketCanGetInterface(); +#endif + /* Check validity of the interface. */ + assert(canIfPtr != NULL); + + /* Only continue with a valid interface. */ + if (canIfPtr != NULL) /*lint !e774 */ + { + /* Initialize the CAN interface. */ + canIfPtr->Init(settings); + } + } } } /*** end of CanInit ***/ diff --git a/Host/Source/LibOpenBLT/candriver.h b/Host/Source/LibOpenBLT/candriver.h index 88b527c2..13721187 100644 --- a/Host/Source/LibOpenBLT/candriver.h +++ b/Host/Source/LibOpenBLT/candriver.h @@ -139,7 +139,7 @@ typedef struct t_can_interface /*************************************************************************************** * Function prototypes ****************************************************************************************/ -void CanInit(tCanSettings const * settings, tCanInterface const * const interface); +void CanInit(tCanSettings const * settings); void CanTerminate(void); bool CanConnect(void); void CanDisconnect(void); diff --git a/Host/Source/LibOpenBLT/xcptpcan.c b/Host/Source/LibOpenBLT/xcptpcan.c index f97c8f43..9a5fc2d0 100644 --- a/Host/Source/LibOpenBLT/xcptpcan.c +++ b/Host/Source/LibOpenBLT/xcptpcan.c @@ -40,12 +40,6 @@ #include "xcptpcan.h" /* XCP CAN transport layer */ #include "util.h" /* Utility module */ #include "candriver.h" /* Generic CAN driver module */ -#if defined(PLATFORM_WIN32) -#include "pcanusb.h" /* Peak PCAN-USB interface */ -#endif -#if defined(PLATFORM_LINUX) -#include "socketcan.h" /* SocketCAN interface */ -#endif /**************************************************************************************** @@ -124,7 +118,6 @@ tXcpTransport const * XcpTpCanGetTransport(void) static void XcpTpCanInit(void const * settings) { char * canDeviceName; - tCanInterface const * canInterface = NULL; tCanSettings canSettings; /* Reset transport layer settings. */ @@ -158,25 +151,6 @@ static void XcpTpCanInit(void const * settings) { strcpy(canDeviceName, ((tXcpTpCanSettings *)settings)->device); tpCanSettings.device = canDeviceName; - - /* ##Vg TODO Refactor such that the CAN driver does this interface linking - * automatically. - */ - /* Determine the pointer to the correct CAN interface, based on the specified - * device name. - */ -#if defined(PLATFORM_WIN32) - if (strcmp(tpCanSettings.device, "peak_pcanusb") == 0) - { - canInterface = PCanUsbGetInterface(); - } -#endif -#if defined(PLATFORM_LINUX) - /* On Linux, the device name is the name of the SocketCAN link, so always link - * the SocketCAN interface to the CAN driver. - */ - canInterface = SocketCanGetInterface(); -#endif } } } @@ -226,7 +200,7 @@ static void XcpTpCanInit(void const * settings) } canSettings.mask = 0x9fffffff; /* Initialize the CAN driver. */ - CanInit(&canSettings, canInterface); + CanInit(&canSettings); /* Register CAN event functions. */ CanRegisterEvents(&canEvents); } /*** end of XcpTpCanInit ***/