Skip to content

Commit

Permalink
Add PUT /config/wifi/scanresult
Browse files Browse the repository at this point in the history
  • Loading branch information
manup committed Mar 16, 2018
1 parent 1109954 commit 1d9326a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
8 changes: 0 additions & 8 deletions database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,6 @@ static int sqliteLoadConfigCallback(void *user, int ncols, char **colval , char
d->gwWifi = val;
}
}
else if (strcmp(colval[0], "availablewifi") == 0)
{
if (!val.isEmpty())
{
d->gwConfig["availablewifi"] = val;
d->gwAvailableWifi = val;
}
}
else if (strcmp(colval[0], "wifichannel") == 0)
{
if (!val.isEmpty())
Expand Down
3 changes: 2 additions & 1 deletion de_web_plugin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ class DeRestPluginPrivate : public QObject
int getWifiState(const ApiRequest &req, ApiResponse &rsp);
int configureWifi(const ApiRequest &req, ApiResponse &rsp);
int restoreWifiConfig(const ApiRequest &req, ApiResponse &rsp);
int putWifiScanResult(const ApiRequest &req, ApiResponse &rsp);

void configToMap(const ApiRequest &req, QVariantMap &map);
void basicConfigToMap(QVariantMap &map);
Expand Down Expand Up @@ -1147,7 +1148,7 @@ public Q_SLOTS:
int gwPermitJoinResend; // permit join of values > 255
uint16_t gwNetworkOpenDuration; // user setting how long network remains open
QString gwWifi; // not-configured | not-installed | not-running | running
QString gwAvailableWifi;
QVariantList gwWifiAvailable;
QString gwWifiType; // accesspoint | ad-hoc | client
QString gwWifiName;
QString gwWifiClientName;
Expand Down
43 changes: 38 additions & 5 deletions rest_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ void DeRestPluginPrivate::initTimezone()
/*! Init WiFi parameters if necessary. */
void DeRestPluginPrivate::initWiFi()
{

#if !defined(ARCH_ARMV6) && !defined (ARCH_ARMV7)
gwWifi = QLatin1String("not-available");
return;
Expand Down Expand Up @@ -352,6 +351,11 @@ int DeRestPluginPrivate::handleConfigurationApi(const ApiRequest &req, ApiRespon
{
return restoreWifiConfig(req, rsp);
}
// PUT /api/<apikey>/config/wifi/scanresult
else if ((req.path.size() == 5) && (req.hdr.method() == "PUT") && (req.path[2] == "config") && (req.path[3] == "wifi") && (req.path[4] == "scanresult"))
{
return putWifiScanResult(req, rsp);
}
// PUT, PATCH /api/<apikey>/config
else if ((req.path.size() == 3) && (req.hdr.method() == "PUT" || req.hdr.method() == "PATCH") && (req.path[2] == "config"))
{
Expand Down Expand Up @@ -686,18 +690,20 @@ void DeRestPluginPrivate::configToMap(const ApiRequest &req, QVariantMap &map)
#if defined(ARCH_ARMV6) || defined (ARCH_ARMV7)
#ifdef Q_OS_LINUX
map["system"] = "linux-gw";
#endif
#endif
map["wifi"] = gwWifi;
map["availablewifi"] = gwAvailableWifi;
#else
map["wifi"] = QLatin1String("not-available");
#endif
map["wifiavailable"] = gwWifiAvailable;
map["wifitype"] = gwWifiType;
map["wifiname"] = gwWifiName;
map["wificlientname"] = gwWifiClientName;
map["wifichannel"] = gwWifiChannel;
map["wifiip"] = gwWifiIp;
// map["wifiappw"] = gwWifiPw;
map["wifiappw"] = QLatin1String(""); // TODO add secured transfer via PKI
map["wificlientpw"] = QLatin1String(""); // TODO add secured transfer via PKI
map["wifiappw"] = QString(); // TODO add secured transfer via PKI
map["wificlientpw"] = QString(); // TODO add secured transfer via PKI
}
else
{
Expand Down Expand Up @@ -2806,6 +2812,33 @@ int DeRestPluginPrivate::restoreWifiConfig(const ApiRequest &req, ApiResponse &r
return REQ_READY_SEND;
}

/*! PUT /api/config/wifi/scanresult
\return REQ_READY_SEND
REQ_NOT_HANDLED
*/
int DeRestPluginPrivate::putWifiScanResult(const ApiRequest &req, ApiResponse &rsp)
{
QHostAddress localHost(QHostAddress::LocalHost);
rsp.httpStatus = HttpStatusForbidden;

if (req.sock->peerAddress() != localHost)
{
rsp.list.append(errorToMap(ERR_UNAUTHORIZED_USER, req.path.join("/"), "unauthorized user"));
return REQ_READY_SEND;
}

rsp.httpStatus = HttpStatusOk;

bool ok;
QVariant var = Json::parse(req.content, ok);
if (ok)
{
gwWifiAvailable = var.toList();
}

return REQ_READY_SEND;
}

/*! check wifi state on raspberry pi.
*/
void DeRestPluginPrivate::checkWifiState()
Expand Down

0 comments on commit 1d9326a

Please sign in to comment.