]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/control.cpp
Improve the block reservation algorithm
[r2c2.git] / source / libmarklin / control.cpp
index 6f7ef74dbf30bc18cd83bd111e994ef7e606c619..30e99e6e4f5266cfa1878735bf3a4a21ded55920 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2007-2008  Mikkosoft Productions, Mikko Rasa
+Copyright © 2007-2009  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -36,7 +36,8 @@ Control::~Control()
                delete i->second;
        for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
                delete i->second;
-       close(serial_fd);
+       if(serial_fd>=0)
+               close(serial_fd);
 }
 
 void Control::open(const string &dev)
@@ -212,7 +213,14 @@ void Control::tick()
                if(debug)
                        cout<<"W: "<<queue.front()<<'\n';
 
-               queue.front().send(serial_fd);
+               if(serial_fd>=0)
+                       queue.front().send(serial_fd);
+               else
+               {
+                       Reply reply = Reply::simulate(queue.front().get_command());
+                       queue.front().signal_done.emit(reply);
+                       queue.erase(queue.begin());
+               }
        }
 }
 
@@ -236,7 +244,7 @@ void Control::event_query_done(const Reply &reply)
                command(CMD_EVENT_TURNOUT).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done));
        if(data[0]&0x04)
                command(CMD_EVENT_SENSOR).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done));
-       if(data[1]&0x40)
+       if((data[0]&0x80) && (data[1]&0x40))
                command(CMD_STATUS).signal_done.connect(sigc::mem_fun(this, &Control::status_done));
 }