Skip to content

Commit

Permalink
Merge branch 'uavos:main' into console-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
skooulhk authored Feb 20, 2024
2 parents c16eefd + 2069599 commit 1614f6a
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 53 deletions.
2 changes: 1 addition & 1 deletion shared
100 changes: 86 additions & 14 deletions src/Plugins/System/HttpService/HttpService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,70 @@
#include <Datalink/Datalink.h>
#include <Vehicles/Vehicles.h>

namespace {
namespace HttpMessage {

using HttpCode = uint16_t;

enum ContentType {

ApplicationXml,
TextHtml,
};

QHash<HttpCode, QString> httpCodesStrTable{
{200, "HTTP/1.0 200 OK\r\n"},
{404, "HTTP/1.0 404 Not Found\r\n"},
};

QHash<ContentType, QString> contentTypeStrTable{
{ContentType::ApplicationXml, "Content-Type: application/xml; charset=\"utf-8\"\r\n\r\n"},
{ContentType::TextHtml, "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"},
};

QString buildHttpHeader(HttpCode code)
{
auto it = httpCodesStrTable.find(code);
return it != httpCodesStrTable.end() ? *it : httpCodesStrTable[404];
}

QString buildContentType(ContentType c_type)
{
auto it = contentTypeStrTable.find(c_type);
return it != contentTypeStrTable.end() ? *it : contentTypeStrTable[ContentType::TextHtml];
}

QString httpServerHostInfo(const QString &localAddr, quint16 localPort)
{
return QString("<b>GCS HTTP Server</b> (%1:%2)").arg(localAddr).arg(localPort);
}

QString buildPayloadWithInfo()
{
QString payload{};
QTextStream s_payload{&payload};
s_payload << "<hr size=1>";
s_payload << QString("<a href=%1>%1</a> - %2<br>").arg("/kml").arg("Google Earth KML");
s_payload << QString("<a href=%1>%1</a> - %2<br>")
.arg("/datalink")
.arg("Datalink stream [uint16 packet size][CRC_16_IBM][packet data]");
s_payload << QString("<a href=%1>%1</a> - %2<br>")
.arg("/mandala")
.arg("Mandala XML data and commands");
s_payload << QString("<br>More info here: <a href=%1>%1</a>").arg("https://docs.uavos.com/");
return payload;
}
QString buildGcsVersionPayload()
{
return QString("<br>GCS Version: <a>%1</a><br>").arg(AppBase::version());
}
QString buildPageNotFoundPayload(QString &req)
{
return QString("<br>No service for '%1'").arg(req);
}
} // namespace HttpMessage
} // namespace

HttpService::HttpService(QObject *parent)
: QObject(parent)
{
Expand All @@ -48,25 +112,33 @@ void HttpService::vehicleSelected(Vehicle *vehicle)
c_pitch = vehicle->f_mandala->fact(mandala::est::nav::att::pitch::uid);
}

void HttpService::httpRequest(QTextStream &stream, QString req, bool *ok)
void HttpService::httpRequest(QTextStream &stream, QString req, const QTcpSocket *tcp)
{
if (req.startsWith("/kml")) {
stream << "HTTP/1.0 200 Ok\r\n";
if (req == "/") {
stream << HttpMessage::buildHttpHeader(200);
stream << HttpMessage::buildContentType(HttpMessage::ContentType::TextHtml);
stream << HttpMessage::httpServerHostInfo(tcp->localAddress().toString(), tcp->localPort());
stream << HttpMessage::buildGcsVersionPayload();
stream << HttpMessage::buildPayloadWithInfo();

} else if (req.startsWith("/kml")) {
stream << HttpMessage::buildHttpHeader(200);
stream << (req.contains(".dae") ? "application/xml dae"
: "application/vnd.google-earth.kml+xml");
stream << "; charset=\"utf-8\"\r\n";
stream << "\r\n";
stream << "; charset=\"utf-8\"\r\n\r\n";
stream << reply_google(req.mid(4));
*ok = true;
return;
}
if (req.startsWith("/mandala")) {
stream << "HTTP/1.0 200 Ok\r\n";
stream << "Content-Type: application/xml; charset=\"utf-8\"\r\n";
stream << "\r\n";

} else if (req.startsWith("/mandala")) {
stream << HttpMessage::buildHttpHeader(200);
stream << HttpMessage::buildContentType(HttpMessage::ContentType::ApplicationXml);
stream << reply_mandala(req.contains('?') ? req.mid(req.indexOf("?") + 1) : "");
*ok = true;
return;

} else {
stream << HttpMessage::buildHttpHeader(404);
stream << HttpMessage::buildContentType(HttpMessage::ContentType::TextHtml);
stream << HttpMessage::httpServerHostInfo(tcp->localAddress().toString(), tcp->localPort());
stream << HttpMessage::buildPageNotFoundPayload(req);
stream << HttpMessage::buildPayloadWithInfo();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/System/HttpService/HttpService.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ private slots:
void vehicleSelected(Vehicle *vehicle);

public slots:
void httpRequest(QTextStream &stream, QString req, bool *ok);
void httpRequest(QTextStream &stream, QString req, const QTcpSocket *tcp);
};
2 changes: 1 addition & 1 deletion src/Plugins/System/HttpService/HttpServicePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <App/PluginInterface.h>
#include <QtCore>

class SoundsPlugin : public PluginInterface
class HttpServicePlugin : public PluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.uavos.gcs.PluginInterface/1.0")
Expand Down
22 changes: 13 additions & 9 deletions src/lib/ApxCore/App/AppPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ void AppPlugin::loadLib()
//check depends
depends = p->depends();
if (!depends.isEmpty()) {
sx_blacklist.remove(name);
sx_blacklist.sync();

bool ok = false;
//apxConsole() << tr("Deps").append(":") << depends;
for (auto d : *plugins) {
if (!depends.contains(d->name))
continue;
Expand All @@ -133,13 +135,13 @@ void AppPlugin::loadLib()
apxMsgW() << tr("Dependency not found").append(":") << depends;
}
apxConsole() << tr("Initializing").append(":") << name;

sx_blacklist.setValue(name, _hash);
sx_blacklist.sync();
}

//define section
switch (p->flags() & PluginInterface::PluginSectionMask) {
default:
section = tr("Other");
break;
case PluginInterface::System:
section = tr("System features");
break;
Expand All @@ -149,15 +151,16 @@ void AppPlugin::loadLib()
case PluginInterface::Map:
section = tr("Mission");
break;
default:
section = tr("Other");
break;
}

//initialize lib
p->init();
QString title = p->title();
QString descr = p->descr();
switch (p->flags() & PluginInterface::PluginTypeMask) {
default:
break;
case PluginInterface::Feature: {
control = p->createControl();
Fact *f = qobject_cast<Fact *>(control);
Expand All @@ -166,10 +169,8 @@ void AppPlugin::loadLib()
title = f->title();
if (descr.isEmpty())
descr = f->descr();
//connect(App::instance(), &App::aboutToQuit, f, &Fact::remove);
} else {
//connect(App::instance(), &App::aboutToQuit, control, &QObject::deleteLater);
}

f_enabled->setSection(section);
if (!descr.isEmpty())
descr.prepend(tr("Tool").append(": "));
Expand All @@ -181,7 +182,10 @@ void AppPlugin::loadLib()
descr.prepend(tr("Window").append(": "));
plugins->loadedWindow(this);
} break;
default:
break;
}

f_enabled->setTitle(title);
f_enabled->setDescr(descr);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/ApxGcs/Datalink/Datalink.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public slots:
signals:
void packetReceived(QByteArray packet);
void packetTransmitted(QByteArray packet);
void httpRequest(QTextStream &stream, QString req, bool *ok);
void httpRequest(QTextStream &stream, QString req, QTcpSocket *tcp);
void heartbeat();

//-----------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ApxGcs/Datalink/DatalinkServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ private slots:
void binded();

// forwarded from connections for plugins
void httpRequest(QTextStream &stream, QString req, bool *ok);
void httpRequest(QTextStream &stream, QString req, QTcpSocket *tcp);
};
2 changes: 1 addition & 1 deletion src/lib/ApxGcs/Datalink/DatalinkSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected slots:
virtual void socketStateChanged(QAbstractSocket::SocketState socketState);

signals:
void httpRequest(QTextStream &stream, QString req, bool *ok);
void httpRequest(QTextStream &stream, QString req, QTcpSocket *tcp);
void disconnected();
void error();
};
24 changes: 1 addition & 23 deletions src/lib/ApxGcs/Datalink/DatalinkTcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,30 +217,8 @@ bool DatalinkTcp::checkServerRequestHeader()
setUrl(sname);
return true;
}
//generic request
bool ok = false;
setStatus("HTTP");
emit httpRequest(stream, req, &ok);
if (!ok) {
stream << "HTTP/1.1 404 Not Found\r\n";
stream << "Content-Type: text/html; charset=\"utf-8\"\r\n";
stream << "\r\n";
stream << QString("<b>GCS HTTP Server</b> (%1:%2)")
.arg(_tcp->localAddress().toString())
.arg(_tcp->localPort());
if (req != "/")
stream << QString("<br>No service for '%1'").arg(req);
stream << "<hr size=1>";
stream << QString("<a href=%1>%1</a> - %2<br>").arg("/kml").arg("Google Earth KML");
stream << QString("<a href=%1>%1</a> - %2<br>")
.arg("/datalink")
.arg("Datalink stream [uint16 packet size][CRC_16_IBM][packet data]");
stream << QString("<a href=%1>%1</a> - %2<br>")
.arg("/mandala")
.arg("Mandala XML data and commands");
stream << QString("<br>More info here: <a href=%1>%1</a>")
.arg("https://docs.uavos.com/");
}
emit httpRequest(stream, req, _tcp);
stream.flush();
_tcp->close();
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ApxGcs/Datalink/DatalinkTcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DatalinkTcp : public DatalinkSocket
void requestDatalinkHeader();

signals:
void httpRequest(QTextStream &stream, QString req, bool *ok);
void httpRequest(QTextStream &stream, QString req, QTcpSocket *tcp);
void disconnected();
void error();
};

0 comments on commit 1614f6a

Please sign in to comment.