Index: /trunk/MagicSoft/Cosy/candrv/vmodican.cc
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 8830)
+++ /trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 8831)
@@ -16,7 +16,7 @@
 !
 !
-!   Author(s): Thomas Bretz <mailto:tbretz@uni-sw.gwdg.de>, 2001
+!   Author(s): Thomas Bretz 2001 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2001
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -31,7 +31,4 @@
 ///////////////////////////////////////////////////////////////////////
 #include "vmodican.h"
-
-#include <iostream.h>      // cout
-#include <iomanip.h>       // setw, setfill
 
 #include <fcntl.h>         // O_RDONLY
@@ -41,20 +38,11 @@
 #include <sys/ioctl.h>     // ioctl
 
+#include "canopen.h"
+
+#include "MLog.h"
+
 ClassImp(VmodIcan);
 
-// --------------------------------------------------------------------------
-//
-// Prints a CAN Message.
-//
-void VmodIcan::PrintMsg(Message *m)
-{
-    cout << "Cmd=0x" << hex << (int)m->cmd << dec << " " << flush;
-    cout << "len=" << (int)m->len << ":" << flush;
-
-    cout << hex << flush;
-    for (int i=0; i<m->len; i++)
-        cout << " " << (int)m->data[i] << flush;
-    cout << dec << endl;
-}
+using namespace std;
 
 // --------------------------------------------------------------------------
@@ -64,5 +52,5 @@
 int VmodIcan::Ioctl(int msg, void *arg)
 {
-    return fd<0 ? 1 : ioctl(fd, msg, (int)arg) >= 0;
+    return fd<0 ? 1 : ioctl(fd, msg, arg) >= 0;
 }
 
@@ -93,5 +81,5 @@
     while (!Send(&msg));
 
-    lout << "- CAN Bus Termination set to " << (state?"on":"off") << endl;
+    gLog << "- CAN Bus Termination set to " << (state?"on":"off") << endl;
 }
 
@@ -102,10 +90,10 @@
 //  - therefor overload HandleCanMessge
 //
-void *VmodIcan::Thread()
+Int_t VmodIcan::Thread()
 {
     if (fd<0)
-        return NULL;
-
-    lout << "- Starting Receiver Loop." << endl;
+        return 0;
+
+    gLog << "- Starting Receiver Loop." << endl;
 
     while (1)
@@ -114,6 +102,11 @@
         // Sleeps until a message arrives
         //
+        TThread::CancelPoint();
+
         unsigned char c;
         const int n = read(fd, &c, 1);
+
+        TThread::CancelPoint();
+
         if (n<0)
         {
@@ -125,10 +118,4 @@
 
         //
-        // read the time for the message as soon as possible
-        //
-        timeval_t tv;
-        gettimeofday(&tv, NULL);
-
-        //
         // if n==0 something strange happened. Stop receiver(?)
         //
@@ -137,5 +124,5 @@
             cerr << "Vmodican: Panic read '" << strerror(errno) << "' ";
             cerr << "(errno=" << errno << ")" << endl;
-            return (void *)1;
+            return 1;
         }
 
@@ -148,4 +135,5 @@
             // Fast message (not used/working)
             //
+            /*
         case FAST_QUEUE:
             cout << "--> Fast Queue:  " << flush;
@@ -164,5 +152,5 @@
             cout << endl;
             continue;
-
+            */
         //
         // Plain Can Message to be processed
@@ -176,7 +164,7 @@
             //
             if (Receive(&msg) < 0)
-                return (void *)1;
-
-            HandleMessage(&msg, &tv);
+                return 1;
+
+            HandleMessage(msg);
             continue;
         }
@@ -184,90 +172,5 @@
         cout << "Vmodican: read, Message c=" << (int)c << " unknown." << endl;
     }
-    return NULL;
-}
-
-// --------------------------------------------------------------------------
-//
-// Does a basic message processing and hadles the so called command (cmd)
-// stamp of the message. This is some kind of message type which is send
-// by the can interface
-//
-void VmodIcan::HandleMessage(Message *msg, timeval_t *tv)
-{
-    //
-    // Decode message
-    //
-    const WORD_t desc  = msg->data[2]<<8 | msg->data[3];
-    const BYTE_t rtr   = (desc>>4)&1;
-    const BYTE_t len   = desc&0xf;
-    const WORD_t cobid = desc>>5;
-
-    switch (msg->cmd) // FROM mican.h
-    {
-    case M_MSG_LOST:
-        cout << "VmodIcan reports: " << dec << (int)msg->data[0] << " msg(s) lost!" << endl;
-        return;
-
-    case M_BCAN_TX_con:  /* confirm (+/-) transmission */
-        cout << "VmodIcan reports: CTXcon=0x35" << endl;
-        cout << "This normally means, that the transmission of the following CAN frame failed:" << hex << endl;
-        cout << "Descr: 0x" << cobid << dec;
-        cout << "  Rtr: "   << (rtr?"Yes":"No");
-        cout << "  Len: "   << (int)len << endl;
-        return;
-
-    case M_BCAN_EVENT_ind:
-        cout << "VmodIcan reports: CEVTind=0x37: " << hex;
-        switch (msg->data[0]) // error indicator
-        {
-        case 0x01:
-            cout << "Error interrup occured" << endl;
-            cout << "This means noisy network normally. Please check the bus termination." << endl;
-            switch (msg->data[1]) // msg type (board depending)
-            {
-            case 2: // SJA1000
-                cout << dec;
-                cout << "ModeReg=" << (int)msg->data[2] << ", ";
-                cout << "StatReg=" << (int)msg->data[3] << ", ";
-                cout << "RxErrCnt=" << (int)msg->data[4] << ", ";
-                cout << "TxErrCnt=" << (int)msg->data[5] << endl;
-            }
-            //FIXME? TerminateApp();
-            return;
-        case 0x02:
-            cout << "Overrun interrup occured" << endl;
-            return;
-        case 0x04:
-            cout << "Interrupts lost" << endl;
-            return;
-        case 0x08:
-            cout << "Send queue full" << endl;
-            return;
-        case 0x10:
-            cout << "CANbus bus-error" << endl;
-            return;
-        }
-        return;
-    case M_BCAN_RX_ind:
-        //
-        // Message is a message from the Can bus
-        //
-        HandleCanMessage(cobid, &msg->data[4], tv);
-        return;
-    }
-
-    //
-    // Nothing of the above happened
-    //
-    cout << hex;
-    cout << "Cmd=0x"    << (int)msg->cmd << ":  ";
-    cout << "Descr: 0x" << cobid << dec;
-    cout << "  Rtr: "   << (rtr?"Yes":"No");
-    cout << "  Len: "   << (int)len << endl;
-
-    cout << "As Raw Data:" << hex << setfill('0');
-    for (int i=0; i<msg->len; i++)
-        cout << " " << setw(2) << (int)(msg->data[i]) << flush;
-    cout << endl;
+    return 0;
 }
 
@@ -294,5 +197,4 @@
 int VmodIcan::Receive(Message *pm) /* receive buffer */
 {
-
     struct dpm_rw_can_desc arg;
 
@@ -323,5 +225,5 @@
 //   * can be used to poll a module for incoming messages.
 //   */
-//
+/*
 int VmodIcan::ReceiveFast(FastMessage *pm)
 {
@@ -335,4 +237,5 @@
     return arg.rval;
 }
+*/
 
 // --------------------------------------------------------------------------
@@ -410,5 +313,5 @@
     while (!Send(&msg));      /* transmitt to module */
 
-    lout << "- Baudrate set to " << rate << "kbps" << endl;
+    gLog << "- Baudrate set to " << rate << "kbps" << endl;
 }
 
@@ -439,5 +342,5 @@
     while (!Send(&msg));
 
-    lout << "- Controller connected to bus" << endl;
+    gLog << "- Controller connected to bus" << endl;
 }
 
@@ -484,5 +387,5 @@
         return -1;
 
-    lout << "- Fast Host Interface Enabled" << endl;
+    gLog << "- Fast Host Interface Enabled" << endl;
 
     return 0;
@@ -512,5 +415,5 @@
 void VmodIcan::DisableCanBusConnection()
 {
-    lout << "- Disconnect VmodIcan module from Bus!" << endl;
+    gLog << "- Disconnect VmodIcan module from Bus!" << endl;
 
     Message msg;                  /* buffer for module messages */
@@ -521,5 +424,5 @@
     while (!Send(&msg));
 
-    lout << "- VmodIcan disconnected." << endl;
+    gLog << "- VmodIcan disconnected." << endl;
 }
 
@@ -548,5 +451,5 @@
 void VmodIcan::Close()
 {
-    lout << "- Closing device VmodIcan #" << (int)fd << endl;
+    gLog << "- Closing device VmodIcan #" << (int)fd << endl;
 
     Message msg; /* disconnect message */
@@ -559,5 +462,5 @@
     close(fd);
 
-    lout << "- Device closed." << endl;
+    gLog << "- Device closed." << endl;
 }
 
@@ -576,5 +479,5 @@
     while (!Send(&msg));
 
-    lout << "- Fifo enabled" << endl;
+    gLog << "- Fifo enabled" << endl;
 
     return TRUE;
@@ -589,5 +492,5 @@
     const int rc = Ioctl(DPM_RESET, 0);
 
-    lout << "- Reset done." << endl;
+    gLog << "- Reset done." << endl;
 
     return rc;
@@ -613,10 +516,10 @@
     if (fd < 0)
     {
-        lout << "Error: Opening device '" << devname << "' (rc=" << fd << ")" << endl;
-        lout << strerror(errno) << endl;
+        gLog << "Error: Opening device '" << devname << "' (rc=" << fd << ")" << endl;
+        gLog << strerror(errno) << endl;
         return FALSE;
     }
 
-    lout << "- Device " << devname << " #" << fd << " open." << endl;
+    gLog << "- Device " << devname << " #" << fd << " open." << endl;
 
     return TRUE;
@@ -679,5 +582,5 @@
     Ioctl(DPM_INIT_NEW_HOSTIF_PRIO, &hdp);
 
-    lout << "- New style host interface enabled" << endl;
+    gLog << "- New style host interface enabled" << endl;
 
     return 0;
@@ -707,4 +610,5 @@
 //   */
 //
+/*
 int VmodIcan::SendHi(Message *pm)
 {
@@ -718,4 +622,5 @@
     return arg.rval;
 }
+*/
 
 // --------------------------------------------------------------------------
@@ -742,4 +647,5 @@
 //   *
 //   */
+/*
 int VmodIcan::SendLo(Message *pm)
 {
@@ -753,4 +659,5 @@
     return arg.rval;
 }
+*/
 
 // --------------------------------------------------------------------------
@@ -807,5 +714,6 @@
 //   */
 //
-int VmodIcan::Send(FastMessage *pm) /* file descriptor, message to send */
+/*
+int VmodIcan::Send(FastMessage *pm)
 {
     struct dpm_write_fast_can_desc arg;
@@ -816,8 +724,9 @@
         return FALSE;
 
-    lout << "done." << endl;
+    gLog << "done." << endl;
 
     return arg.rval;
 }
+*/
 
 // --------------------------------------------------------------------------
@@ -848,5 +757,5 @@
     while (!Send(&msg));
 
-    lout << "- All CobIds disabled." << endl;
+    gLog << "- All CobIds disabled." << endl;
 }
 
@@ -876,5 +785,5 @@
     while (!Send(&msg));
 #ifdef EXPERT
-    lout << "- CobId 0x" << hex << setfill('0') << setw(3) << cobid << " enabled." << endl;
+    gLog << "- CobId 0x" << hex << setfill('0') << setw(3) << cobid << " enabled." << endl;
 #endif
 }
@@ -934,10 +843,10 @@
 //  and switch the can bus communication on
 //
-VmodIcan::VmodIcan(const char *dev, const int baud, MLog &out) : Log(out), MThread(false, 1)//: CanDriver(dev, baud)
+VmodIcan::VmodIcan(CanOpen *rx, const char *dev, const int baud) : Interface(rx)
 {
     //
     // Set priority of receiving thread and detach the receiving thread
     //
-    Detach();
+//    Detach();
 
     if (!Open(dev))
@@ -959,5 +868,5 @@
      */
 
-    lout << "- VmodIcan initialized." << endl;
+    gLog << "- VmodIcan initialized." << endl;
 
 }
@@ -970,21 +879,8 @@
 VmodIcan::~VmodIcan()
 {
-    lout << "- Stopping VmodIcan module." << endl;
-    Stop();
+    gLog << "- Stopping VmodIcan module." << endl;
+    CancelThread();
     DisableCanBusConnection();
     Close();
-    lout << "- VmodIcan stopped." << endl;
-}
-
-// --------------------------------------------------------------------------
-//
-// Sends a can frame with the given cobid and the given eight bytes
-// through the can network
-//
-void VmodIcan::SendCanFrame(WORD_t cobid,
-                            BYTE_t m0, BYTE_t m1, BYTE_t m2, BYTE_t m3,
-                            BYTE_t m4, BYTE_t m5, BYTE_t m6, BYTE_t m7)
-{
-    BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
-    SendCanFrame(cobid, msg);
-}
+    gLog << "- VmodIcan stopped." << endl;
+}
Index: /trunk/MagicSoft/Cosy/candrv/vmodican.h
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/vmodican.h	(revision 8830)
+++ /trunk/MagicSoft/Cosy/candrv/vmodican.h	(revision 8831)
@@ -1,10 +1,4 @@
 #ifndef COSY_VmodIcan
 #define COSY_VmodIcan
-
-#include <pthread.h>
-
-#include <unistd.h>          // gettimeofday
-
-#include "log.h"
 
 #ifdef __CINT__
@@ -12,33 +6,30 @@
 typedef UShort_t WORD_t;
 typedef Byte_t   BYTE_t;
+struct Message;
+struct FastMessage;
 #else
 #include "gendef.h"
+#include "dpm.h"
 #endif
 
-#ifndef COSY_MThread
+#ifndef MARS_MThread
 #include "MThread.h"
 #endif
 
-class Message;
-class FastMessage;
+#ifndef COSY_Interface
+#include "interface.h"
+#endif
 
 typedef struct timeval timeval_t;
 
-class VmodIcan : public Log, protected MThread
+class CanOpen;
+
+class VmodIcan : public Interface, public MyThreadX
 {
-    friend class VmodIcanRX;
-
 private:
     int fd; // file descriptor for can module
 
+    // Specific tp the VmodIcan module
     int Ioctl(int msg, void *arg);
-
-    void *Thread();
-
-    void HandleMessage(Message *msg, timeval_t *tv);
-    virtual void HandleCanMessage(WORD_t cobid, BYTE_t *data, timeval_t *tv) {}
-
-    int Receive(Message *pm);
-    int ReceiveFast(FastMessage *pm);
 
     void SetBaudRate(int rate);
@@ -62,21 +53,19 @@
     int Open(const char *devname);          /* pathname of device */
 
-    WORD_t MsgDescr(const WORD_t cobid, const BYTE_t dlc, const BYTE_t rtr=0)
-    {
-        return (cobid<<5) | ((rtr&0x1)<<4) | (dlc&0xf);
-    }
+    // Receiver
+    int Receive(Message *pm);
+    //int ReceiveFast(FastMessage *pm);
+    Int_t Thread();
 
-    virtual void TerminateApp() { exit(-1); }
+    void Start() { RunThread(); }
+    void Stop()  { CancelThread(); }
 
 public:
-    VmodIcan(const char *dev, const int baud, MLog &out=gLog);
+    VmodIcan(CanOpen *rx, const char *dev, const int baud);
     virtual ~VmodIcan();
 
+    // Sending interface inherited from Interface
     void EnableCobId(WORD_t cobid, int flag=TRUE);
-
     void SendCanFrame(WORD_t cobid, BYTE_t m[8], BYTE_t rtr=0);
-    void SendCanFrame(WORD_t cobid,
-                      BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
-                      BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0);
 
     ClassDef(VmodIcan, 0) // hardware interface to the vmodican can module (Janz)
