| 1 | #include "OpenMSXConnection.h" |
| 2 | #include <QXmlStreamReader> |
| 3 | #include <cassert> |
| 4 | |
| 5 | |
| 6 | void SimpleCommand::replyOk (const QString& /*message*/) |
| 7 | { |
| 8 | delete this; |
| 9 | } |
| 10 | |
| 11 | void SimpleCommand::replyNok(const QString& /*message*/) |
| 12 | { |
| 13 | cancel(); |
| 14 | } |
| 15 | |
| 16 | void SimpleCommand::cancel() |
| 17 | { |
| 18 | delete this; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | Command::Command(QString command_, |
| 23 | std::function<void(const QString&)> okCallback_, |
| 24 | std::function<void(const QString&)> errorCallback_) |
| 25 | : command(std::move(command_)) |
| 26 | , okCallback(std::move(okCallback_)) |
| 27 | , errorCallback(std::move(errorCallback_)) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | void Command::replyOk(const QString& message) |
| 32 | { |
| 33 | okCallback(message); |
| 34 | delete this; |
| 35 | } |
| 36 | |
| 37 | void Command::replyNok(const QString& message) |
| 38 | { |
| 39 | if (errorCallback != nullptr) |
| 40 | errorCallback(message); |
| 41 | cancel(); |
| 42 | } |
| 43 | |
| 44 | QString Command::getCommand() const |
| 45 | { |
| 46 | return command; |
| 47 | } |
| 48 | |
| 49 | void Command::cancel() |
| 50 | { |
| 51 | delete this; |
| 52 | } |
| 53 | |
| 54 | static QString createDebugCommand(const QString& debuggable, |
| 55 | unsigned offset, unsigned size) |
| 56 | { |
| 57 | return QString("debug_bin2hex [ debug read_block %1 %2 %3 ]") |
| 58 | .arg(debuggable).arg(offset).arg(size); |
| 59 | } |
| 60 | |
| 61 | ReadDebugBlockCommand::ReadDebugBlockCommand(const QString& commandString, |
| 62 | unsigned size_, unsigned char* target_) |
| 63 | : SimpleCommand(commandString) |
| 64 | , size(size_), target(target_) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | ReadDebugBlockCommand::ReadDebugBlockCommand(const QString& debuggable, |
| 69 | unsigned offset, unsigned size_, unsigned char* target_) |
| 70 | : SimpleCommand(createDebugCommand(debuggable, offset, size_)) |
| 71 | , size(size_), target(target_) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | static QString createDebugWriteCommand(const QString& debuggable, |
| 76 | unsigned offset, unsigned size, unsigned char *data) |
| 77 | { |
| 78 | QString cmd = QString("debug write_block %1 %2 [ debug_hex2bin \"") |
| 79 | .arg(debuggable).arg(offset); |
| 80 | for (unsigned i = offset; i < offset + size; ++i) { |
| 81 | cmd += QString("%1").arg(int(data[i]), 2, 16, QChar('0')).toUpper(); |
| 82 | } |
| 83 | cmd += "\" ]"; |
| 84 | return cmd; |
| 85 | } |
| 86 | WriteDebugBlockCommand::WriteDebugBlockCommand(const QString& debuggable, |
| 87 | unsigned offset, unsigned size_, unsigned char* source_) |
| 88 | : SimpleCommand(createDebugWriteCommand(debuggable, offset, size_, source_)) |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static unsigned char hex2val(char c) |
| 94 | { |
| 95 | return (c <= '9') ? (c - '0') : (c - 'A' + 10); |
| 96 | } |
| 97 | void ReadDebugBlockCommand::copyData(const QString& message) |
| 98 | { |
| 99 | assert(static_cast<unsigned>(message.size()) == 2 * size); |
| 100 | for (unsigned i = 0; i < size; ++i) { |
| 101 | target[i] = (hex2val(message[2 * i + 0].toLatin1()) << 4) + |
| 102 | (hex2val(message[2 * i + 1].toLatin1()) << 0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | OpenMSXConnection::OpenMSXConnection(QAbstractSocket* socket_) |
| 108 | : socket(socket_) |
| 109 | , reader(new QXmlStreamReader()) |
| 110 | , connected(true) |
| 111 | { |
| 112 | assert(socket->isValid()); |
| 113 | |
| 114 | connect(socket, &QAbstractSocket::readyRead, |
| 115 | this, &OpenMSXConnection::processData); |
| 116 | connect(socket, &QAbstractSocket::stateChanged, |
| 117 | this, &OpenMSXConnection::socketStateChanged); |
| 118 | connect(socket, &QAbstractSocket::errorOccurred, |
| 119 | this, &OpenMSXConnection::socketError); |
| 120 | |
| 121 | socket->write("<openmsx-control>\n"); |
| 122 | } |
| 123 | |
| 124 | OpenMSXConnection::~OpenMSXConnection() |
| 125 | { |
| 126 | cleanup(); |
| 127 | assert(commands.empty()); |
| 128 | assert(!connected); |
| 129 | socket->deleteLater(); |
| 130 | } |
| 131 | |
| 132 | void OpenMSXConnection::sendCommand(CommandBase* command) |
| 133 | { |
| 134 | assert(command); |
| 135 | if (connected && socket->isValid()) { |
| 136 | commands.enqueue(command); |
| 137 | QString cmd = "<command>" + command->getCommand() + "</command>"; |
| 138 | socket->write(cmd.toUtf8()); |
| 139 | } else { |
| 140 | command->cancel(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void OpenMSXConnection::cleanup() |
| 145 | { |
| 146 | if (!connected) return; |
| 147 | |
| 148 | connected = false; |
| 149 | if (socket->isValid()) { |
| 150 | socket->disconnect(this); |
| 151 | socket->write("</openmsx-control>\n"); |
| 152 | socket->disconnectFromHost(); |
| 153 | } |
| 154 | cancelPending(); |
| 155 | emit disconnected(); |
| 156 | } |
| 157 | |
| 158 | void OpenMSXConnection::cancelPending() |
| 159 | { |
| 160 | assert(!connected); |
| 161 | while (!commands.empty()) { |
| 162 | CommandBase* command = commands.dequeue(); |
| 163 | command->cancel(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void OpenMSXConnection::socketStateChanged(QAbstractSocket::SocketState state) |
| 168 | { |
| 169 | if (state != QAbstractSocket::ConnectedState) { |
| 170 | cleanup(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void OpenMSXConnection::socketError(QAbstractSocket::SocketError /*state*/) |
| 175 | { |
| 176 | cleanup(); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | void OpenMSXConnection::processData() |
| 181 | { |
| 182 | if (!reader->device()) { |
| 183 | reader->setDevice(socket); |
| 184 | } |
| 185 | |
| 186 | while (!reader->atEnd()) { |
| 187 | reader->readNext(); |
| 188 | if (reader->isStartElement()) { |
| 189 | startElement(reader->name(), reader->attributes()); |
| 190 | } else if (reader->isEndElement()) { |
| 191 | endElement(reader->name()); |
| 192 | } else if (reader->isCharacters()) { |
| 193 | characters(reader->text()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (reader->hasError() && reader->error() != QXmlStreamReader::PrematureEndOfDocumentError) { |
| 198 | qWarning("Fatal error on line %lli, column %lli: %s", |
| 199 | reader->lineNumber(), reader->columnNumber(), |
| 200 | reader->errorString().toLatin1().data()); |
| 201 | cleanup(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | bool OpenMSXConnection::startElement(const QStringRef& /*qName*/, const QXmlStreamAttributes& atts) |
| 206 | { |
| 207 | xmlAttrs = atts; |
| 208 | xmlData.clear(); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool OpenMSXConnection::endElement(const QStringRef& qName) |
| 213 | { |
| 214 | if (qName == "openmsx-output") { |
| 215 | // ignore |
| 216 | } else if (qName == "reply") { |
| 217 | if (connected) { |
| 218 | CommandBase* command = commands.dequeue(); |
| 219 | if (xmlAttrs.value("result") == "ok") { |
| 220 | command->replyOk (xmlData); |
| 221 | } else { |
| 222 | command->replyNok(xmlData); |
| 223 | } |
| 224 | } else { |
| 225 | // still receive a reply while we're already closing |
| 226 | // the connection, ignore it |
| 227 | } |
| 228 | } else if (qName == "log") { |
| 229 | emit logParsed(xmlAttrs.value("level").toString(), xmlData); |
| 230 | } else if (qName == "update") { |
| 231 | emit updateParsed(xmlAttrs.value("type").toString(), xmlAttrs.value("name").toString(), xmlData); |
| 232 | } else { |
| 233 | qWarning("Unknown XML tag: %s", qName.toLatin1().data()); |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | bool OpenMSXConnection::characters(const QStringRef& ch) |
| 239 | { |
| 240 | xmlData += ch; |
| 241 | return true; |
| 242 | } |