drivers/unix/unix.c
author edouard
Mon, 05 Oct 2009 14:31:55 +0200
changeset 599 b2d2c3fab094
parent 480 d0d76932ad74
child 629 b9274b595650
permissions -rw-r--r--
Added timeout for waiting timer thread end on windows.
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     1
/*
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     3
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     5
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     7
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    12
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    17
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    21
*/
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    22
#ifndef __KERNEL__
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    23
#include <unistd.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    24
#include <stdio.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    25
#include <stdlib.h>
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    26
#else
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    27
#include <linux/module.h>
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    28
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    29
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    30
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    31
#define DLL_CALL(funcname) (* funcname##_driver)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    32
#define FCT_PTR_INIT =NULL
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    33
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    34
#define DLSYM(name)\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    35
	*(void **) (&name##_driver) = dlsym(handle, #name"_driver");\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    36
	if ((error = dlerror()) != NULL)  {\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    37
		fprintf (stderr, "%s\n", error);\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    38
		UnLoadCanDriver(handle);\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    39
		return NULL;\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    40
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    41
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    42
#else /*NOT_USE_DYNAMIC_LOADING*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    43
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    44
/*Function call is direct*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    45
#define DLL_CALL(funcname) funcname##_driver
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    46
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    47
#endif /*NOT_USE_DYNAMIC_LOADING*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    48
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    49
#include "data.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    50
#include "canfestival.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    51
#include "timers_driver.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    52
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    53
#define MAX_NB_CAN_PORTS 16
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    54
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    55
/** CAN port structure */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    56
typedef struct {
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    57
  char used;  /**< flag indicating CAN port usage, will be used to abort Receiver task*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    58
  CAN_HANDLE fd; /**< CAN port file descriptor*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    59
  TASK_HANDLE receiveTask; /**< CAN Receiver task*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    60
  CO_Data* d; /**< CAN object data*/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    61
} CANPort;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    62
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    63
#include "can_driver.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    64
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    65
/*Declares the funtion pointers for dll binding or simple protos*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    66
/*UNS8 DLL_CALL(canReceive)(CAN_HANDLE, Message *);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    67
UNS8 DLL_CALL(canSend)(CAN_HANDLE, Message *);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    68
CAN_HANDLE DLL_CALL(canOpen)(s_BOARD *);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    69
int DLL_CALL(canClose)(CAN_HANDLE);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    70
*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    71
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}};
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    72
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    73
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    74
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    75
/*UnLoads the dll*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    76
UNS8 UnLoadCanDriver(LIB_HANDLE handle)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    77
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    78
	if(handle!=NULL)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    79
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    80
		dlclose(handle);
151
ebf4bd44f282 Updated doc. Cleaned some code.
etisserant
parents: 149
diff changeset
    81
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    82
		handle=NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    83
		return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    84
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    85
	return -1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    86
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    87
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    88
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    89
 * Loads the dll and get funcs ptr
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    90
 *
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    91
 * @param driver_name String containing driver's dynamic library name
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    92
 * @return Library handle
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    93
 */
480
d0d76932ad74 Added missing const for dll names strings
etisserant
parents: 467
diff changeset
    94
LIB_HANDLE LoadCanDriver(const char* driver_name)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    95
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    96
	LIB_HANDLE handle = NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    97
	char *error;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    98
	
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    99
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   100
	if(handle==NULL)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   101
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   102
		handle = dlopen(driver_name, RTLD_LAZY);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   103
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   104
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   105
	if (!handle) {
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   106
		fprintf (stderr, "%s\n", dlerror());
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   107
        	return NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   108
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   109
 
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   110
	/*Get function ptr*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   111
	DLSYM(canReceive)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   112
	DLSYM(canSend)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   113
	DLSYM(canOpen)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   114
	DLSYM(canChangeBaudRate)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   115
	DLSYM(canClose)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   116
197
182008694a32 Fixed "function LoadCanDriver() should return library handle instead of 0 if succeeded" bug
etisserant
parents: 162
diff changeset
   117
	return handle;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   118
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   119
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   120
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   121
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   122
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   123
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   124
/*Not needed -- canReceiveLoop calls _canReceive directly *//*
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   125
UNS8 canReceive(CAN_PORT port, Message *m)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   126
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   127
	return DLL_CALL(canReceive)(port->fd, Message *m);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   128
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   129
*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   130
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   131
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   132
 * CAN send routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   133
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   134
 * @param m CAN message
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   135
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   136
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   137
UNS8 canSend(CAN_PORT port, Message *m)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   138
{
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   139
	if(port){
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   140
		UNS8 res;
155
746b49869cbc Removed Mutex liberation on CanSend. Too much race condition to avoid. Cannot be deeply tested.
etisserant
parents: 151
diff changeset
   141
	        //LeaveMutex();
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   142
		res = DLL_CALL(canSend)(((CANPort*)port)->fd, m);
155
746b49869cbc Removed Mutex liberation on CanSend. Too much race condition to avoid. Cannot be deeply tested.
etisserant
parents: 151
diff changeset
   143
		//EnterMutex();
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 197
diff changeset
   144
		return res; // OK
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   145
	}               
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 197
diff changeset
   146
	return 1; // NOT OK
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   147
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   148
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   149
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   150
 * CAN Receiver Task
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   151
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   152
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   153
void canReceiveLoop(CAN_PORT port)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   154
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   155
       Message m;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   156
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   157
       while (((CANPort*)port)->used) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   158
               if (DLL_CALL(canReceive)(((CANPort*)port)->fd, &m) != 0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   159
                       break;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   160
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   161
               EnterMutex();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   162
               canDispatch(((CANPort*)port)->d, &m);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   163
               LeaveMutex();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   164
       }
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   165
}
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   166
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   167
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   168
 * CAN open routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   169
 * @param board device name and baudrate
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   170
 * @param d CAN object data
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   171
 * @return valid CAN_PORT pointer or NULL
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   172
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   173
CAN_PORT canOpen(s_BOARD *board, CO_Data * d)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   174
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   175
	int i;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   176
	for(i=0; i < MAX_NB_CAN_PORTS; i++)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   177
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   178
		if(!canports[i].used)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   179
		break;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   180
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   181
	
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   182
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   183
	if (&DLL_CALL(canOpen)==NULL) {
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   184
        	fprintf(stderr,"CanOpen : Can Driver dll not loaded\n");
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   185
        	return NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   186
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   187
#endif	
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   188
	CAN_HANDLE fd0 = DLL_CALL(canOpen)(board);
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   189
	if(fd0){
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   190
		canports[i].used = 1;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   191
		canports[i].fd = fd0;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   192
		canports[i].d = d;
454
bc000083297a - add RTAI support
greg
parents: 442
diff changeset
   193
		d->canHandle = (CAN_PORT)&canports[i];		
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   194
		CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, &canReceiveLoop);
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   195
		return (CAN_PORT)&canports[i];
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   196
	}else{
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   197
        	MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate);
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   198
		return NULL;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   199
	}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   200
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   201
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   202
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   203
 * CAN close routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   204
 * @param d CAN object data
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   205
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   206
 */
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   207
int canClose(CO_Data * d)
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   208
{
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   209
	UNS8 res;
454
bc000083297a - add RTAI support
greg
parents: 442
diff changeset
   210
	
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   211
	((CANPort*)d->canHandle)->used = 0;
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   212
	CANPort* tmp = (CANPort*)d->canHandle;
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   213
	d->canHandle = NULL;
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   214
	
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   215
	// close CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   216
	res = DLL_CALL(canClose)(tmp->fd);
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   217
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   218
	// kill receiver task
401
2c90876b9751 Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents: 391
diff changeset
   219
	WaitReceiveTaskEnd(&tmp->receiveTask);
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   220
	
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   221
	return res;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   222
}
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   223
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   224
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   225
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   226
 * CAN change baudrate routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   227
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   228
 * @param baud baudrate
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   229
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   230
 */
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   231
UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   232
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   233
   if(port){
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   234
		UNS8 res;
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   235
	        //LeaveMutex();
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   236
		res = DLL_CALL(canChangeBaudRate)(((CANPort*)port)->fd, baud);
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   237
		//EnterMutex();
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   238
		return res; // OK
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   239
	}               
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   240
	return 1; // NOT OK
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   241
}
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   242
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   243
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   244
#ifdef __KERNEL__
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   245
EXPORT_SYMBOL (canOpen);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   246
EXPORT_SYMBOL (canClose);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   247
EXPORT_SYMBOL (canSend);
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 454
diff changeset
   248
EXPORT_SYMBOL (canChangeBaudRate);
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   249
#endif