Harmonized unix and win32 canClose_driver, fixed crash on close under win32
authorEdouard Tisserant
Thu, 07 Mar 2013 11:40:28 +0900
changeset 777 bbbfd27c1bd1
parent 776 30ced15f996f
child 779 5bbf6e4521bb
Harmonized unix and win32 canClose_driver, fixed crash on close under win32
drivers/can_tcp_win32/can_tcp_win32.cpp
drivers/unix/unix.c
drivers/win32/win32.c
--- a/drivers/can_tcp_win32/can_tcp_win32.cpp	Wed Mar 06 23:50:35 2013 +0100
+++ b/drivers/can_tcp_win32/can_tcp_win32.cpp	Thu Mar 07 11:40:28 2013 +0900
@@ -86,30 +86,18 @@
 
 CAN_HANDLE LIBAPI canOpen_driver(s_BOARD *board)
 {
-    Socket* s;
+    char *dst = "127.0.0.1";
+    if(!strlen(board->busname)){
+      dst=board->busname;
+    }
     try {
-      char *dst = "127.0.0.1";
-      if(!strlen(board->busname)){
-          dst=board->busname;
-      }
-      s = new SocketClient(dst, 11898);
-    } 
-    catch (const char* _s) {
-      cerr << "can_tcp_win32 exception :\n";
-      cerr << _s << endl;
-      return NULL;
-    } 
-    catch (std::string _s) {
-      cerr << "can_tcp_win32 exception :\n";
-      cerr << _s << endl;
-      return NULL;
+      CAN_HANDLE res = (CAN_HANDLE) new SocketClient(dst, 11898);
+      return res;
     } 
     catch (...) {
-      cerr << "can_tcp_win32 exception :\n";
-      cerr << "unhandled exception\n";
+      cerr << "can_tcp_win32: couldn't connect to" << dst << endl;
       return NULL;
     }
-    return (CAN_HANDLE) s;
 }
 
 int LIBAPI canClose_driver(CAN_HANDLE inst)
--- a/drivers/unix/unix.c	Wed Mar 06 23:50:35 2013 +0100
+++ b/drivers/unix/unix.c	Thu Mar 07 11:40:28 2013 +0900
@@ -63,12 +63,6 @@
 
 #include "can_driver.h"
 
-/*Declares the funtion pointers for dll binding or simple protos*/
-/*UNS8 DLL_CALL(canReceive)(CAN_HANDLE, Message *);
-UNS8 DLL_CALL(canSend)(CAN_HANDLE, Message *);
-CAN_HANDLE DLL_CALL(canOpen)(s_BOARD *);
-int DLL_CALL(canClose)(CAN_HANDLE);
-*/
 CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}};
 
 #ifndef NOT_USE_DYNAMIC_LOADING
@@ -120,15 +114,6 @@
 
 #endif
 
-
-
-/*Not needed -- canReceiveLoop calls _canReceive directly *//*
-UNS8 canReceive(CAN_PORT port, Message *m)
-{
-	return DLL_CALL(canReceive)(port->fd, Message *m);
-}
-*/
-
 /**
  * CAN send routine
  * @param port CAN port
@@ -162,13 +147,6 @@
                EnterMutex();
                canDispatch(((CANPort*)port)->d, &m);
                LeaveMutex();
-
-#ifdef __KERNEL__
-#ifdef USE_XENO
-               /* periodic task for Xenomai kernel realtime */
-               rt_task_wait_period(NULL);
-#endif
-#endif
        }
 }
 
@@ -214,18 +192,18 @@
  */
 int canClose(CO_Data * d)
 {
-	UNS8 res;
-
-	((CANPort*)d->canHandle)->used = 0;
-	CANPort* tmp = (CANPort*)d->canHandle;
-
-	// kill receiver task before port is closed and handle set to NULL
-	WaitReceiveTaskEnd(&tmp->receiveTask);
-
-	// close CAN port
-	res = DLL_CALL(canClose)(tmp->fd);
-
-	d->canHandle = NULL;
+	int res = 0;
+
+	CANPort* port = (CANPort*)d->canHandle;
+    if(port){
+        ((CANPort*)d->canHandle)->used = 0;
+
+        res = DLL_CALL(canClose)(port->fd);
+
+        WaitReceiveTaskEnd(&port->receiveTask);
+
+        d->canHandle = NULL;
+    }
 
 	return res;
 }
@@ -241,7 +219,7 @@
 {
    if(port){
 		UNS8 res;
-	        //LeaveMutex();
+	    //LeaveMutex();
 		res = DLL_CALL(canChangeBaudRate)(((CANPort*)port)->fd, baud);
 		//EnterMutex();
 		return res; // OK
--- a/drivers/win32/win32.c	Wed Mar 06 23:50:35 2013 +0100
+++ b/drivers/win32/win32.c	Thu Mar 07 11:40:28 2013 +0900
@@ -159,7 +159,8 @@
 	Message m;
 	while(((CANPort*)port)->used)
 	{
-		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) continue;
+		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) 
+            break;
 		EnterMutex();
 		canDispatch(((CANPort*)port)->d, &m);
 		LeaveMutex();
@@ -214,27 +215,20 @@
 /***************************************************************************/
 int canClose(CO_Data * d)
 {
-	UNS8 res;
-	CANPort* tmp;
-
-	tmp = (CANPort*)d->canHandle;
-
-	if(tmp)
-	{
-	  d->canHandle = NULL;
-
-		// close CAN port
-	  res = m_canClose(tmp->fd);
-
-		// kill receiver task
-	  WaitReceiveTaskEnd(&tmp->receiveTask);
-
-		// release used flag as a last step.
-	  tmp->used = 0;
-	}
-  else res = 255;
-
-return res;
+    int res = 0;
+
+    CANPort* port = (CANPort*)d->canHandle;
+    if(port){
+        ((CANPort*)d->canHandle)->used = 0;
+
+        res = m_canClose(port->fd);
+
+        WaitReceiveTaskEnd(&port->receiveTask);
+
+        d->canHandle = NULL;
+    }
+
+    return res;
 }
 
 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)