Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
authorEdouard Tisserant
Wed, 06 Mar 2013 16:54:21 +0900
changeset 774 91d708a2cb4e
parent 773 c10def4bfbde
child 775 c8df14b3a388
Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
drivers/can_tcp_win32/can_tcp_win32.cpp
drivers/can_tcp_win32/can_tcp_win32_server.cpp
drivers/win32/win32.c
include/can_driver.h
--- a/drivers/can_tcp_win32/can_tcp_win32.cpp	Wed Mar 06 13:40:54 2013 +0900
+++ b/drivers/can_tcp_win32/can_tcp_win32.cpp	Wed Mar 06 16:54:21 2013 +0900
@@ -28,99 +28,99 @@
 
 extern "C" {
 #include "can_driver.h"
-}
 
 //------------------------------------------------------------------------
-extern "C"
-   UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
-   {
+UNS8 LIBAPI canReceive_driver(CAN_HANDLE fd0, Message *m)
+{
 
-      string l = reinterpret_cast<SocketClient*>(fd0)->ReceiveLine();
+    string l = reinterpret_cast<SocketClient*>(fd0)->ReceiveLine();
 
-      int res = sscanf(l.c_str(),"{0x%3hx,%1hhd,%1hhd,{0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx}}",
-                &m->cob_id,
-                &m->rtr,
-                &m->len,
-                &m->data[0],
-                &m->data[1],
-                &m->data[2],
-                &m->data[3],
-                &m->data[4],
-                &m->data[5],
-                &m->data[6],
-                &m->data[7]
-                );
+    int res = sscanf(l.c_str(),
+            "{0x%3hx,%1hhd,%1hhd,{0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx}}",
+            &m->cob_id,
+            &m->rtr,
+            &m->len,
+            &m->data[0],
+            &m->data[1],
+            &m->data[2],
+            &m->data[3],
+            &m->data[4],
+            &m->data[5],
+            &m->data[6],
+            &m->data[7]
+            );
 
 
 #if defined DEBUG_MSG_CONSOLE_ON
-  printf("in : ");
-  print_message(m);
+    printf("in : ");
+    print_message(m);
 #endif
-   
-      return res==11 ? 0 : 1 ;
-   }
 
-extern "C"
-   UNS8 canSend_driver(CAN_HANDLE fd0, Message const *m)
-   {
-        char s[1024];        
-        sprintf(s,"{0x%3.3x,%1d,%1d,{0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x}}",
-                m->cob_id,
-                m->rtr,
-                m->len,
-                m->data[0],
-                m->data[1],
-                m->data[2],
-                m->data[3],
-                m->data[4],
-                m->data[5],
-                m->data[6],
-                m->data[7]
-                );
-    
-        reinterpret_cast<SocketClient*>(fd0)->SendLine(s);
+  return res==11 ? 0 : 1 ;
+}
+
+UNS8 LIBAPI canSend_driver(CAN_HANDLE fd0, Message const *m)
+{
+    char s[1024];        
+    sprintf(s,"{0x%3.3x,%1d,%1d,{0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x}}",
+            m->cob_id,
+            m->rtr,
+            m->len,
+            m->data[0],
+            m->data[1],
+            m->data[2],
+            m->data[3],
+            m->data[4],
+            m->data[5],
+            m->data[6],
+            m->data[7]
+            );
+
+    reinterpret_cast<SocketClient*>(fd0)->SendLine(s);
 #if defined DEBUG_MSG_CONSOLE_ON
-  printf("out : ");
-  print_message(m);
+    printf("out : ");
+    print_message(m);
 #endif
-        return 0;
-   }
+    return 0;
+}
 
-extern "C"
-   CAN_HANDLE canOpen_driver(s_BOARD *board)
-   {
-      Socket* s;
-      try {
-        char *dst = "127.0.0.1";
-        if(strlen(board->busname)){
-            dst=board->busname;
-        }
-        s = new SocketClient(dst, 11898);
-      } 
-      catch (const char* _s) {
-        cerr << _s << endl;
-        return NULL;
-      } 
-      catch (std::string _s) {
-        cerr << _s << endl;
-        return NULL;
-      } 
-      catch (...) {
-        cerr << "unhandled exception\n";
-        return NULL;
+CAN_HANDLE LIBAPI canOpen_driver(s_BOARD *board)
+{
+    Socket* s;
+    try {
+      char *dst = "127.0.0.1";
+      if(!strlen(board->busname)){
+          dst=board->busname;
       }
-      return (CAN_HANDLE) s;
-   }
+      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;
+    } 
+    catch (...) {
+      cerr << "can_tcp_win32 exception :\n";
+      cerr << "unhandled exception\n";
+      return NULL;
+    }
+    return (CAN_HANDLE) s;
+}
 
-extern "C"
-   int canClose_driver(CAN_HANDLE inst)
-   {
-   delete reinterpret_cast<SocketClient*>(inst);
-   return 1;
-   }
-extern "C"
-	UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
-	{
-	cerr << "canChangeBaudRate not yet supported by this driver\n";
-	return 0;
-	}
+int LIBAPI canClose_driver(CAN_HANDLE inst)
+{
+    delete reinterpret_cast<SocketClient*>(inst);
+    return 1;
+}
+
+UNS8 LIBAPI canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
+{
+    cerr << "canChangeBaudRate not yet supported by this driver\n";
+    return 0;
+}
+}
--- a/drivers/can_tcp_win32/can_tcp_win32_server.cpp	Wed Mar 06 13:40:54 2013 +0900
+++ b/drivers/can_tcp_win32/can_tcp_win32_server.cpp	Wed Mar 06 16:54:21 2013 +0900
@@ -69,9 +69,13 @@
 		 "and repeat '\\n' terminated lines \n"
 		 "to each connected client\n"
 		 "\n"
-		 "Use netcat to monitor trafic\n"
+		 "Use netcat to monitor/log trafic\n"
 		 " nc 127.0.0.1 11898\n"
+		 "\n"
+         "CAN message format:\n"
+         "{COB_ID,RTR,data_len,{data}}\n"
 		 "\n");
+
   fflush(stdout);
   SocketServer in(11898,5);
   
--- a/drivers/win32/win32.c	Wed Mar 06 13:40:54 2013 +0900
+++ b/drivers/win32/win32.c	Wed Mar 06 16:54:21 2013 +0900
@@ -99,50 +99,48 @@
  */
 LIB_HANDLE LoadCanDriver(LPCSTR driver_name)
 {
-	// driver module handle
-	LIB_HANDLE handle = NULL;
+    // driver module handle
+    LIB_HANDLE handle = NULL;
 
 #ifndef NOT_USE_DYNAMIC_LOADING
-	if(handle == NULL)
-	{
-		handle = LoadLibrary(driver_name);
-	}
-
-	if (!handle)
-	{
-		fprintf (stderr, "%d\n", GetLastError());
-    	return NULL;
-	}
-
-	m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver"));
-	m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver"));
-	m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
-	m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
-	m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
-
-	if(m_canReceive==NULL || m_canSend==NULL || m_canOpen==NULL || m_canClose==NULL || m_canChangeBaudRate==NULL)
-	{
-	  m_canReceive = NULL;
-	  m_canSend = NULL;
-	  m_canOpen = NULL;
-	  m_canClose = NULL;
-	  m_canChangeBaudRate = NULL;
-	  FreeLibrary(handle);
-	  handle = NULL;
-	}
+    handle = LoadLibrary(driver_name);
+
+    if (!handle)
+    {
+        fprintf (stderr, "LoadLibrary error : %d\n", GetLastError());
+        return NULL;
+    }
+
+    m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver"));
+    m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver"));
+    m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
+    m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
+    m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
+
+    if(m_canReceive==NULL || m_canSend==NULL || m_canOpen==NULL || m_canClose==NULL || m_canChangeBaudRate==NULL)
+    {
+        m_canReceive = NULL;
+        m_canSend = NULL;
+        m_canOpen = NULL;
+        m_canClose = NULL;
+        m_canChangeBaudRate = NULL;
+        FreeLibrary(handle);
+        fprintf (stderr, "GetProc error : %d\n", GetLastError());
+        return NULL;
+    }
 #else
-  //compiled in...
-  handle = 1; //TODO: remove this hack
-
-  m_canReceive = canReceive_driver;
-	m_canSend = canSend_driver;
-	m_canOpen = canOpen_driver;
-	m_canClose = canClose_driver;
-	m_canChangeBaudRate = canChangeBaudRate_driver;
-#endif
-
-
-	return handle;
+    //compiled in...
+    handle = 1; //TODO: remove this hack
+
+    m_canReceive = canReceive_driver;
+    m_canSend = canSend_driver;
+    m_canOpen = canOpen_driver;
+    m_canClose = canClose_driver;
+    m_canChangeBaudRate = canChangeBaudRate_driver;
+#endif
+
+
+    return handle;
 }
 
 /***************************************************************************/
--- a/include/can_driver.h	Wed Mar 06 13:40:54 2013 +0900
+++ b/include/can_driver.h	Wed Mar 06 16:54:21 2013 +0900
@@ -46,7 +46,8 @@
 };
 
 #ifndef DLL_CALL
-#if !defined(WIN32) || defined(__CYGWIN__)
+#if !defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
+#define LIBAPI
 #define DLL_CALL(funcname) funcname##_driver
 #else
 #define LIBAPI __stdcall