- for(map<unsigned, Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
- delete i->second;
- for(map<unsigned, Turnout *>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
- delete i->second;
- for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
- delete i->second;
- close(serial_fd);
-}
-
-/*** private ***/
-
-void Control::read_all(int fd, char *buf, int size)
-{
- int pos=0;
- while(pos<size)
- {
- int len=read(fd, buf+pos, size-pos);
- pos+=len;
- }
-}
-
-string Control::read_reply(Cmd cmd)
-{
- string result;
- if(cmd==CMD_EVENT)
- {
- result+=ERR_NO_ERROR;
- for(unsigned i=0; i<3; ++i)
- {
- char c;
- read(serial_fd, &c, 1);
- result+=c;
- if(!(c&0x80)) break;
- }
- }
- else if(cmd==CMD_EVENT_LOK)
- {
- result+=ERR_NO_ERROR;
- char c[5];
- read(serial_fd, c+4, 1);
- result+=c[4];
- while(c[4]&0x80)
- {
- read_all(serial_fd, c, 5);
- result.append(c, 5);
- }
- }
- else if(cmd==CMD_EVENT_TURNOUT)
- {
- result+=ERR_NO_ERROR;
- char c[511];
- read(serial_fd, c, 1);
- read_all(serial_fd, c+1, c[0]*2);
- result.append(c, c[0]*2+1);
- }
- else if(cmd==CMD_EVENT_SENSOR)
- {
- result+=ERR_NO_ERROR;
- char c[3];
- read(serial_fd, c+2, 1);
- result+=c[2];
- while(c[2])
- {
- read_all(serial_fd, c, 3);
- result.append(c, 3);
- }
- }
- else
- {
- unsigned expected_bytes=1;
- if(cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS)
- expected_bytes=2;
- if(cmd==CMD_SENSOR_STATUS || cmd==CMD_TURNOUT_GROUP_STATUS)
- expected_bytes=3;
- if(cmd==CMD_LOK_STATUS)
- expected_bytes=4;
- if(cmd==CMD_LOK_CONFIG)
- expected_bytes=5;
- char c[5];
- read_all(serial_fd, c, 1);
- result+=c[0];
- if(!c[0])
- {
- read_all(serial_fd, c+1, expected_bytes-1);
- result.append(c+1, expected_bytes-1);
- }
- }
-
- return result;