summaryrefslogtreecommitdiffstats
path: root/src/progs/gpsim/base/gpsim.cpp
blob: 6803686c756787fd4de6a68e9228089dcdaee0c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "gpsim.h"

#include <tqregexp.h>
#include "progs/base/generic_prog.h"
#include "progs/base/generic_debug.h"

//-----------------------------------------------------------------------------
GPSim::Process::Process(Log::Base *base)
  : ::Process::LineOutput(0, "gpsim_process"), Log::Base(base), _ready(false)
{
  connect(this, TQT_SIGNAL(stdoutDataReceived()), TQT_SLOT(stdoutDataReceivedSlot()));
}

void GPSim::Process::stdoutDataReceivedSlot()
{
  if ( _stdout.startsWith("**gpsim> ") || _ready ) {
    log(Log::DebugLevel::Extra, "received console prompt", Log::Delayed);
    _ready = true;
    emit requestSynchronousStop();
  }
}

void GPSim::Process::addStdoutLine(const TQString &line)
{
  log(Log::DebugLevel::Extra, "  " + line, Log::Delayed);
  if ( line.startsWith("***ERROR:") ) {
    log(Log::LineType::Error, line);
    return;
  }
  TQString s = line;
  TQString prompt = "**gpsim> ";
  while ( s.startsWith(prompt) ) {
    _ready = true;
    s = s.mid(prompt.length());
  }
  s = s.stripWhiteSpace();
  _stdoutLines += s;
}

//-----------------------------------------------------------------------------
GPSim::ProcessManager::ProcessManager(Log::Base *base)
  : Log::Base(base), _process(base)
{}

bool GPSim::ProcessManager::start()
{
  _process._ready = false;
  _process.setup("gpsim", TQStringList("-i"), false);
  if ( !_process.start(0) ) {
    log(Log::LineType::Error, i18n("Failed to start \"gpsim\"."));
    return false;
  }
  return runSynchronously();
}

bool GPSim::ProcessManager::runSynchronously()
{
  ::Process::State state = ::Process::runSynchronously(_process, ::Process::NoRunAction, 5000);
  if ( !_process.isRunning() ) {
    log(Log::LineType::Error, i18n("\"gpsim\" unexpectedly exited."));
    return false;
  }
  if ( state==::Process::Timedout ) {
    log(Log::LineType::Error, i18n("Timeout waiting for \"gpsim\"."));
    return false;
  }
  return true;
}

bool GPSim::ProcessManager::sendCommand(const TQString &cmd, bool synchronous)
{
  _process._ready = false;
  _process._stdoutLines.clear();
  _process._stdout = TQString();
  _process.writeToStdin(cmd + '\n');
  if (synchronous) return runSynchronously();
  return true;
}

bool GPSim::ProcessManager::sendSignal(uint n, bool synchronous)
{
  _process._ready = false;
  _process._stdoutLines.clear();
  _process._stdout = TQString();
  if ( !_process.signal(n) ) {
    log(Log::LineType::Error, i18n("Error send a signal to the subprocess."));
    return false;
  }
  if (synchronous) return runSynchronously();
  return true;
}

bool GPSim::ProcessManager::getVersion(VersionData &version)
{
  if ( !sendCommand("version", true) ) return false;
  TQRegExp reg("\\w*\\s*(\\d+\\.\\d+\\.\\d+).*");
  if ( _process.sout().count()==0 || !reg.exactMatch(_process.sout()[0]) ) {
    version = VersionData();
    return true;
  }
  version = VersionData::fromString(reg.cap(1));
  return true;
}

//-----------------------------------------------------------------------------
GPSim::Hardware::~Hardware()
{
  delete _manager;
}

bool GPSim::Hardware::internalConnectHardware()
{
  delete _manager;
  _manager = new ProcessManager(this);
  _manager->process().setWorkingDirectory(_base.debugger()->directory());
  if ( !_manager->start() ) return false;
  if ( !_manager->getVersion(_version) ) return false;
  if ( !_version.isValid() ) {
    log(Log::LineType::Error, i18n("Could not recognize gpsim version."));
    return false;
  }
  return true;
}

void GPSim::Hardware::internalDisconnectHardware()
{
  delete _manager;
  _manager = 0;
}

bool GPSim::Hardware::execute(const TQString &command, bool synchronous, TQStringList *output)
{
  log(Log::DebugLevel::Normal, TQString("command: %1").arg(command));
  if (output) output->clear();
  if ( !_manager->sendCommand(command, synchronous) ) return false;
  if (output) *output = _manager->process().sout();
  return true;
}

bool GPSim::Hardware::signal(uint n, bool synchronous, TQStringList *output)
{
  log(Log::DebugLevel::Normal, TQString("signal: %1").arg(n));
  if (output) output->clear();
  if ( !_manager->sendSignal(n, synchronous) ) return false;
  if (output) *output = _manager->process().sout();
  return true;
}