2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
6 See COPYING file for copyrights details.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifndef NOT_USE_DYNAMIC_LOADING
27 #define DLL_CALL(funcname) (* funcname##_driver)
28 #define FCT_PTR_INIT =NULL
32 *(void **) (&_##name) = GetProcAddress(handle, TEXT(#name"_driver"));\
33 if (name##_driver == NULL) {\
34 fprintf (stderr, "Error loading symbol %s\n",#name"_driver");\
35 UnLoadCanDriver(handle);\
41 *(void **) (&name##_driver) = dlsym(handle, #name"_driver");\
42 if ((error = dlerror()) != NULL) {\
43 fprintf (stderr, "%s\n", error);\
44 UnLoadCanDriver(handle);\
49 #else /*NOT_USE_DYNAMIC_LOADING*/
51 /*Function call is direct*/
52 #define DLL_CALL(funcname) funcname##_driver
54 #endif /*NOT_USE_DYNAMIC_LOADING*/
57 #include "canfestival.h"
58 #include "timers_driver.h"
60 #define MAX_NB_CAN_PORTS 16
65 TASK_HANDLE receiveTask;
69 #include "can_driver.h"
71 /*Declares the funtion pointers for dll binding or simple protos*/
72 /*UNS8 DLL_CALL(canReceive)(CAN_HANDLE, Message *);
73 UNS8 DLL_CALL(canSend)(CAN_HANDLE, Message *);
74 CAN_HANDLE DLL_CALL(canOpen)(s_BOARD *);
75 int DLL_CALL(canClose)(CAN_HANDLE);
77 CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}};
79 #ifndef NOT_USE_DYNAMIC_LOADING
82 UNS8 UnLoadCanDriver(LIB_HANDLE handle)
97 /*Loads the dll and get funcs ptr*/
98 LIB_HANDLE LoadCanDriver(char* driver_name)
100 LIB_HANDLE handle = NULL;
107 handle = LoadLibrary(driver_name);
110 if (handle == NULL) {
111 fprintf(stderr,"Error loading Can Driver dll \n");
118 handle = dlopen(driver_name, RTLD_LAZY);
122 fprintf (stderr, "%s\n", dlerror());
140 /*Not needed -- canReceiveLoop calls _canReceive directly *//*
141 UNS8 canReceive(CAN_PORT port, Message *m)
143 return DLL_CALL(canReceive)(port->fd, Message *m);
147 UNS8 canSend(CAN_PORT port, Message *m)
152 res = DLL_CALL(canSend)(((CANPort*)port)->fd, m);
159 void canReceiveLoop(CAN_PORT port)
164 if (DLL_CALL(canReceive)(((CANPort*)port)->fd, &m) != 0)
168 canDispatch(((CANPort*)port)->d, &m);
172 CAN_PORT canOpen(s_BOARD *board, CO_Data * d)
175 for(i=0; i < MAX_NB_CAN_PORTS; i++)
177 if(!canports[i].used)
181 #ifndef NOT_USE_DYNAMIC_LOADING
182 if (&DLL_CALL(canOpen)==NULL) {
183 fprintf(stderr,"CanOpen : Can Driver dll not loaded\n");
187 CAN_HANDLE fd0 = DLL_CALL(canOpen)(board);
189 canports[i].used = 1;
190 canports[i].fd = fd0;
193 CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, &canReceiveLoop);
196 d->canHandle = (CAN_PORT)&canports[i];
198 return (CAN_PORT)&canports[i];
201 int canClose(CO_Data * d)
204 ((CANPort*)d->canHandle)->used = 0;
205 CANPort* tmp = (CANPort*)d->canHandle;
209 int res = DLL_CALL(canClose)(tmp->fd);
211 WaitReceiveTaskEnd(tmp->receiveTask);