drivers/win32/win32.c
author Edouard Tisserant
Thu, 24 Jan 2019 13:53:01 +0100
changeset 808 de1fc3261f21
parent 801 32d146b64a35
permissions -rwxr-xr-x
Adding -fPIC isn't necessary of xeno-config is set correctly. Backed out changeset b9f1fcda7d30
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     1
/*
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     3
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     5
Copyright (C) Win32 Port Leonid Tochinski
697
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
     6
Modified by: Jaroslav Fojtik
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     7
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     8
See COPYING file for copyrights details.
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
     9
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    10
This library is free software; you can redistribute it and/or
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    11
modify it under the terms of the GNU Lesser General Public
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    12
License as published by the Free Software Foundation; either
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    13
version 2.1 of the License, or (at your option) any later version.
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    14
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    15
This library is distributed in the hope that it will be useful,
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    16
but WITHOUT ANY WARRANTY; without even the implied warranty of
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    18
Lesser General Public License for more details.
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    19
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    20
You should have received a copy of the GNU Lesser General Public
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    21
License along with this library; if not, write to the Free Software
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    22
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    23
*/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    24
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    25
/*
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    26
 CAN driver interface.
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    27
*/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    28
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    29
#include <windows.h>
591
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    30
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    31
#ifdef __cplusplus
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    32
extern "C" {
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    33
#endif
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    34
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    35
#include "canfestival.h"
801
32d146b64a35 Rename timer.h into timers.h to avoid clash with Xenomai includes.
Edouard Tisserant
parents: 777
diff changeset
    36
#include "timers.h"
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    37
#include "timers_driver.h"
591
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    38
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    39
#ifdef __cplusplus
577
0bb82be64630 add extern \"C\" to keep compatibilty with C++
greg
parents: 556
diff changeset
    40
};
591
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    41
#endif
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
    42
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    43
// GetProcAddress doesn't have an UNICODE version for NT
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    44
#ifdef UNDER_CE
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    45
  #define myTEXT(str) TEXT(str)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    46
#else
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    47
  #define myTEXT(str) str
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    48
#endif
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    49
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    50
#define MAX_NB_CAN_PORTS 16
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    51
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    52
typedef UNS8 (CALLBACK* CANRECEIVE_DRIVER_PROC)(void* inst, Message *m);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    53
typedef UNS8 (CALLBACK* CANSEND_DRIVER_PROC)(void* inst, const Message *m);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    54
typedef void* (CALLBACK* CANOPEN_DRIVER_PROC)(s_BOARD *board);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    55
typedef int (CALLBACK* CANCLOSE_DRIVER_PROC)(void* inst);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    56
typedef UNS8 (CALLBACK* CANCHANGEBAUDRATE_DRIVER_PROC)(void* fd, char* baud);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    57
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    58
CANRECEIVE_DRIVER_PROC m_canReceive;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    59
CANSEND_DRIVER_PROC m_canSend;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    60
CANOPEN_DRIVER_PROC m_canOpen;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    61
CANCLOSE_DRIVER_PROC m_canClose;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    62
CANCHANGEBAUDRATE_DRIVER_PROC m_canChangeBaudRate;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    63
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    64
/* CAN port structure */
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    65
typedef struct
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    66
{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    67
  char used;  /**< flag indicating CAN port usage, will be used to abort Receiver task*/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    68
  CAN_HANDLE fd; /**< CAN port file descriptor*/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    69
  TASK_HANDLE receiveTask; /**< CAN Receiver task*/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    70
  CO_Data* d; /**< CAN object data*/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    71
}CANPort;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    72
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    73
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}};
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    74
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    75
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    76
/***************************************************************************/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    77
UNS8 UnLoadCanDriver(LIB_HANDLE handle)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    78
{
638
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
    79
#ifndef NOT_USE_DYNAMIC_LOADING
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    80
	if(handle != NULL)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    81
	{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    82
		FreeLibrary(handle);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    83
		handle=NULL;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    84
		return 0;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    85
	}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    86
	return -1;
638
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
    87
#else
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
    88
  handle = NULL;
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
    89
  return 0;
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
    90
#endif //NOT_USE_DYNAMIC_LOADING
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    91
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    92
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    93
/***************************************************************************/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    94
/**
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    95
 * Loads the dll and get funcs ptr
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    96
 *
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    97
 * @param driver_name String containing driver's dynamic library name
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    98
 * @return Library handle
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
    99
 */
723
Christian Taedcke <hacking@taedcke.com>
parents: 697
diff changeset
   100
LIB_HANDLE LoadCanDriver(LPCSTR driver_name)
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   101
{
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   102
    // driver module handle
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   103
    LIB_HANDLE handle = NULL;
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   104
638
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
   105
#ifndef NOT_USE_DYNAMIC_LOADING
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   106
    handle = LoadLibrary(driver_name);
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   107
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   108
    if (!handle)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   109
    {
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   110
        fprintf (stderr, "LoadLibrary error : %d\n", GetLastError());
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   111
        return NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   112
    }
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   113
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   114
    m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver"));
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   115
    m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver"));
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   116
    m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   117
    m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   118
    m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   119
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   120
    if(m_canReceive==NULL || m_canSend==NULL || m_canOpen==NULL || m_canClose==NULL || m_canChangeBaudRate==NULL)
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   121
    {
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   122
        m_canReceive = NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   123
        m_canSend = NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   124
        m_canOpen = NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   125
        m_canClose = NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   126
        m_canChangeBaudRate = NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   127
        FreeLibrary(handle);
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   128
        fprintf (stderr, "GetProc error : %d\n", GetLastError());
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   129
        return NULL;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   130
    }
638
00bc03cb5606 CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents: 629
diff changeset
   131
#else
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   132
    //compiled in...
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   133
    handle = 1; //TODO: remove this hack
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   134
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   135
    m_canReceive = canReceive_driver;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   136
    m_canSend = canSend_driver;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   137
    m_canOpen = canOpen_driver;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   138
    m_canClose = canClose_driver;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   139
    m_canChangeBaudRate = canChangeBaudRate_driver;
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   140
#endif
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   141
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   142
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 773
diff changeset
   143
    return handle;
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   144
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   145
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   146
/***************************************************************************/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   147
UNS8 canSend(CAN_PORT port, Message *m)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   148
{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   149
	if (port && (m_canSend != NULL))
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   150
	{
691
17d9c0736935 [ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents: 687
diff changeset
   151
		return m_canSend(((CANPort*)port)->fd, m);
17d9c0736935 [ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents: 687
diff changeset
   152
	}
696
381af6bb6155 Windows CAN DLL driver sanity check.
JaFojtik
parents: 691
diff changeset
   153
	return 1; /* NOT OK */	
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   154
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   155
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   156
/***************************************************************************/
697
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   157
DWORD canReceiveLoop(CAN_PORT port)
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   158
{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   159
	Message m;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   160
	while(((CANPort*)port)->used)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   161
	{
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   162
		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) 
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   163
            break;
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   164
		EnterMutex();
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   165
		canDispatch(((CANPort*)port)->d, &m);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   166
		LeaveMutex();
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   167
	}
697
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   168
	return 0;
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   169
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   170
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   171
/***************************************************************************/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   172
CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   173
{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   174
	int i;
697
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   175
	CAN_HANDLE fd0;
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   176
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   177
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   178
	  /* Fix of multiple opening one data set, added by J.Fojtik. */
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   179
	if(d->canHandle)
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   180
	{
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   181
	  canClose(d);
24a2aec61731 ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents: 696
diff changeset
   182
	}
591
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
   183
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   184
	for(i=0; i < MAX_NB_CAN_PORTS; i++)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   185
	{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   186
		if(!canports[i].used)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   187
		break;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   188
	}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   189
577
0bb82be64630 add extern \"C\" to keep compatibilty with C++
greg
parents: 556
diff changeset
   190
	#ifndef NOT_USE_DYNAMIC_LOADING
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   191
	if (m_canOpen == NULL)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   192
	{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   193
	   	fprintf(stderr,"CanOpen : Can Driver dll not loaded\n");
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   194
	   	return NULL;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   195
	}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   196
	#endif
577
0bb82be64630 add extern \"C\" to keep compatibilty with C++
greg
parents: 556
diff changeset
   197
591
513f5ec8b628 add extern C to maintain compatibility with C++
greg
parents: 577
diff changeset
   198
	fd0 = m_canOpen(board);
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   199
	if(fd0)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   200
	{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   201
		canports[i].used = 1;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   202
		canports[i].fd = fd0;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   203
		canports[i].d = d;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   204
		d->canHandle = (CAN_PORT)&canports[i];
577
0bb82be64630 add extern \"C\" to keep compatibilty with C++
greg
parents: 556
diff changeset
   205
		CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop);
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   206
		return (CAN_PORT)&canports[i];
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   207
	}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   208
	else
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   209
	{
687
Christian Taedcke <hacking@taedcke.com>
parents: 652
diff changeset
   210
		MSG("CanOpen : Cannot open board {busname='%S',baudrate='%S'}\n",board->busname, board->baudrate);
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   211
		return NULL;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   212
	}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   213
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   214
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   215
/***************************************************************************/
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   216
int canClose(CO_Data * d)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   217
{
777
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   218
    int res = 0;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   219
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   220
    CANPort* port = (CANPort*)d->canHandle;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   221
    if(port){
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   222
        ((CANPort*)d->canHandle)->used = 0;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   223
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   224
        res = m_canClose(port->fd);
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   225
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   226
        WaitReceiveTaskEnd(&port->receiveTask);
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   227
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   228
        d->canHandle = NULL;
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   229
    }
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   230
bbbfd27c1bd1 Harmonized unix and win32 canClose_driver, fixed crash on close under win32
Edouard Tisserant
parents: 774
diff changeset
   231
    return res;
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   232
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   233
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   234
UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   235
{
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   236
   if(port){
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   237
		UNS8 res;
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   238
	    //LeaveMutex();
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   239
		res = m_canChangeBaudRate(((CANPort*)port)->fd, baud);
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   240
		//EnterMutex();
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   241
		return res; // OK
577
0bb82be64630 add extern \"C\" to keep compatibilty with C++
greg
parents: 556
diff changeset
   242
	}
556
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   243
	return 1; // NOT OK
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   244
}
8296acd119a9 Rewrite win32 code (C only)
greg
parents:
diff changeset
   245