]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/reply.cpp
Forgot to add the new files
[r2c2.git] / source / libmarklin / reply.cpp
index ede23ccb3aa939dd28665e8f0000786d7a85e5d9..1d7d536ba1dbb506c94289e23499b138bba44f86 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -18,9 +18,9 @@ namespace {
 
 unsigned read_all(int fd, char *buf, unsigned size)
 {
-       unsigned pos=0;
+       unsigned pos = 0;
        while(pos<size)
-               pos+=read(fd, buf+pos, size-pos);
+               pos += read(fd, buf+pos, size-pos);
 
        return pos;
 }
@@ -40,13 +40,13 @@ Reply Reply::read(int fd, Cmd cmd)
 {
        Reply result;
 
-       char *data=reinterpret_cast<char *>(result.data);
+       char *data = reinterpret_cast<char *>(result.data);
 
        if(cmd==CMD_EVENT)
        {
                for(unsigned i=0; i<3; ++i)
                {
-                       result.len+=read_all(fd, data+i, 1);
+                       result.len += read_all(fd, data+i, 1);
                        if(!(result.data[i]&0x80))
                                break;
                }
@@ -55,59 +55,98 @@ Reply Reply::read(int fd, Cmd cmd)
        {
                for(unsigned i=0;; i+=5)
                {
-                       result.len+=read_all(fd, data+i, 1);
+                       result.len += read_all(fd, data+i, 1);
 
                        if(result.data[i]&0x80)
                                break;
 
-                       result.len+=read_all(fd, data+i+1, 4);
+                       result.len += read_all(fd, data+i+1, 4);
                }
        }
        else if(cmd==CMD_EVENT_TURNOUT)
        {
-               result.len+=read_all(fd, data, 1);
-               result.len+=read_all(fd, data+1, result.data[0]*2);
+               result.len += read_all(fd, data, 1);
+               result.len += read_all(fd, data+1, result.data[0]*2);
        }
        else if(cmd==CMD_EVENT_SENSOR)
        {
                for(unsigned i=0;; i+=3)
                {
-                       result.len+=read_all(fd, data+i, 1);
+                       result.len += read_all(fd, data+i, 1);
 
                        if(result.data[i]==0)
                                break;
 
-                       result.len+=read_all(fd, data+i+1, 2);
+                       result.len += read_all(fd, data+i+1, 2);
                }
        }
        else
        {
-               bool expect_errcode=(cmd!=CMD_STATUS);
+               bool expect_errcode = (cmd!=CMD_STATUS);
 
-               unsigned expected_bytes=0;
+               unsigned expected_bytes = 0;
                if(cmd==CMD_STATUS || cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS)
-                       expected_bytes=1;
+                       expected_bytes = 1;
                if(cmd==CMD_SENSOR_STATUS || cmd==CMD_TURNOUT_GROUP_STATUS)
-                       expected_bytes=2;
+                       expected_bytes = 2;
                if(cmd==CMD_LOK_STATUS)
-                       expected_bytes=3;
+                       expected_bytes = 3;
                if(cmd==CMD_LOK_CONFIG)
-                       expected_bytes=4;
+                       expected_bytes = 4;
 
                if(expect_errcode)
                {
                        char c;
                        read_all(fd, &c, 1);
-                       result.err=static_cast<Error>(c);
+                       result.err = static_cast<Error>(c);
                }
 
                if(result.err==ERR_NO_ERROR)
-                       result.len+=read_all(fd, data, expected_bytes);
+                       result.len += read_all(fd, data, expected_bytes);
        }
 
        return result;
 }
 
+Reply Reply::simulate(Cmd cmd)
+{
+       Reply result;
+
+       if(cmd==CMD_STATUS)
+       {
+               result.data[0] = 0x80;
+               result.len = 1;
+       }
+       if(cmd==CMD_EVENT)
+               result.len = 1;
+       else if(cmd==CMD_TURNOUT)
+               ;
+       else if(cmd==CMD_TURNOUT_STATUS)
+       {
+               result.data[0] = 0x04;
+               result.len = 1;
+       }
+       else if(cmd==CMD_LOK)
+               ;
+       else if(cmd==CMD_LOK_STATUS)
+       {
+               result.data[1] = 0x20;
+               result.len = 3;
+       }
+       else if(cmd==CMD_SENSOR_PARAM_SET)
+               ;
+       else if(cmd==CMD_SENSOR_REPORT)
+               ;
+       else if(cmd==CMD_POWER_ON)
+               ;
+       else if(cmd==CMD_POWER_OFF)
+               ;
+       else
+               result.err = ERR_SYS_ERROR;
+
+       return result;
+}
+
 ostream &operator<<(ostream &out, const Reply &reply)
 {
        out<<reply.err;