drivers/unix/unix.c
author Edouard Tisserant
Wed, 27 Mar 2013 09:32:05 +0900
changeset 779 5bbf6e4521bb
parent 777 bbbfd27c1bd1
permissions -rw-r--r--
Merge
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     1
/*
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
145
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>
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
    28
#include <linux/delay.h>
644
11b21e9a92f6 FIXED: - added missing endif
Christian Taedcke <hacking@taedcke.com>
parents: 629
diff changeset
    29
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    30
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    31
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    32
#define DLL_CALL(funcname) (* funcname##_driver)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    33
#define FCT_PTR_INIT =NULL
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    34
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    35
#define DLSYM(name)\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    36
	*(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
    37
	if ((error = dlerror()) != NULL)  {\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    38
		fprintf (stderr, "%s\n", error);\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    39
		UnLoadCanDriver(handle);\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    40
		return NULL;\
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
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    43
#else /*NOT_USE_DYNAMIC_LOADING*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    44
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    45
/*Function call is direct*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    46
#define DLL_CALL(funcname) funcname##_driver
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    47
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    48
#endif /*NOT_USE_DYNAMIC_LOADING*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    49
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    50
#include "data.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    51
#include "canfestival.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    52
#include "timers_driver.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    53
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    54
#define MAX_NB_CAN_PORTS 16
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    55
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    56
/** CAN port structure */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    57
typedef struct {
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    58
  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
    59
  CAN_HANDLE fd; /**< CAN port file descriptor*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    60
  TASK_HANDLE receiveTask; /**< CAN Receiver task*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    61
  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
    62
} CANPort;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    63
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    64
#include "can_driver.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    65
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    66
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
    67
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    68
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    69
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    70
/*UnLoads the dll*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    71
UNS8 UnLoadCanDriver(LIB_HANDLE handle)
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
	if(handle!=NULL)
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
		dlclose(handle);
151
ebf4bd44f282 Updated doc. Cleaned some code.
etisserant
parents: 149
diff changeset
    76
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    77
		handle=NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    78
		return 0;
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
	return -1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    81
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    82
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    83
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    84
 * Loads the dll and get funcs ptr
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    85
 *
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    86
 * @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
    87
 * @return Library handle
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    88
 */
480
d0d76932ad74 Added missing const for dll names strings
etisserant
parents: 467
diff changeset
    89
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
    90
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    91
	LIB_HANDLE handle = NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    92
	char *error;
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
    93
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    94
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    95
	if(handle==NULL)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    96
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    97
		handle = dlopen(driver_name, RTLD_LAZY);
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) {
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   101
		fprintf (stderr, "%s\n", dlerror());
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   102
        	return NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   103
	}
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   104
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   105
	/*Get function ptr*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   106
	DLSYM(canReceive)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   107
	DLSYM(canSend)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   108
	DLSYM(canOpen)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   109
	DLSYM(canChangeBaudRate)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   110
	DLSYM(canClose)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   111
197
182008694a32 Fixed "function LoadCanDriver() should return library handle instead of 0 if succeeded" bug
etisserant
parents: 162
diff changeset
   112
	return handle;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   113
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   114
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   115
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   116
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   117
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   118
 * CAN send routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   119
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   120
 * @param m CAN message
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   121
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   122
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   123
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
   124
{
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   125
	if(port){
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   126
		UNS8 res;
155
746b49869cbc Removed Mutex liberation on CanSend. Too much race condition to avoid. Cannot be deeply tested.
etisserant
parents: 151
diff changeset
   127
	        //LeaveMutex();
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   128
		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
   129
		//EnterMutex();
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 197
diff changeset
   130
		return res; // OK
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   131
	}
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 197
diff changeset
   132
	return 1; // NOT OK
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   133
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   134
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   135
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   136
 * CAN Receiver Task
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   137
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   138
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   139
void canReceiveLoop(CAN_PORT port)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   140
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   141
       Message m;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   142
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   143
       while (((CANPort*)port)->used) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   144
               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
   145
                       break;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   146
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   147
               EnterMutex();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   148
               canDispatch(((CANPort*)port)->d, &m);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   149
               LeaveMutex();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   150
       }
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   151
}
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   152
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   153
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   154
 * CAN open routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   155
 * @param board device name and baudrate
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   156
 * @param d CAN object data
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   157
 * @return valid CAN_PORT pointer or NULL
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   158
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   159
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
   160
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   161
	int i;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   162
	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
   163
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   164
		if(!canports[i].used)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   165
		break;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   166
	}
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   167
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   168
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   169
	if (&DLL_CALL(canOpen)==NULL) {
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   170
        	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
   171
        	return NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   172
	}
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   173
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   174
	CAN_HANDLE fd0 = DLL_CALL(canOpen)(board);
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   175
	if(fd0){
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   176
		canports[i].used = 1;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   177
		canports[i].fd = fd0;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   178
		canports[i].d = d;
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   179
		d->canHandle = (CAN_PORT)&canports[i];
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   180
		CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, &canReceiveLoop);
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   181
		return (CAN_PORT)&canports[i];
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   182
	}else{
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   183
        	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
   184
		return NULL;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   185
	}
145
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
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   188
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   189
 * CAN close routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   190
 * @param d CAN object data
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   191
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   192
 */
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   193
int canClose(CO_Data * d)
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   194
{
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   195
	int res = 0;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   196
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   197
	CANPort* port = (CANPort*)d->canHandle;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   198
    if(port){
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   199
        ((CANPort*)d->canHandle)->used = 0;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   200
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   201
        res = DLL_CALL(canClose)(port->fd);
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   202
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   203
        WaitReceiveTaskEnd(&port->receiveTask);
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   204
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   205
        d->canHandle = NULL;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   206
    }
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   207
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   208
	return res;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   209
}
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   210
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   211
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   212
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   213
 * CAN change baudrate routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   214
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   215
 * @param baud baudrate
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   216
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   217
 */
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   218
UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   219
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   220
   if(port){
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   221
		UNS8 res;
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 644
diff changeset
   222
	    //LeaveMutex();
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   223
		res = DLL_CALL(canChangeBaudRate)(((CANPort*)port)->fd, baud);
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   224
		//EnterMutex();
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   225
		return res; // OK
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   226
	}
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   227
	return 1; // NOT OK
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   228
}
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   229
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   230
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   231
#ifdef __KERNEL__
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   232
EXPORT_SYMBOL (canOpen);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   233
EXPORT_SYMBOL (canClose);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   234
EXPORT_SYMBOL (canSend);
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 454
diff changeset
   235
EXPORT_SYMBOL (canChangeBaudRate);
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   236
#endif