Skip to content

Commit

Permalink
support configurable interface name for wpantund.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangLe2016 committed May 30, 2017
1 parent d86a1ca commit ea69ea0
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 264 deletions.
59 changes: 56 additions & 3 deletions src/web/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,65 @@
#define WEB_FILE_PATH "/usr/local/share/border-router/frontend"
#endif

#include <errno.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>

#include "otbr-config.h"
#include "web-service/web_service.hpp"
#include "common/code_utils.hpp"

static const char kSyslogIdent[] = "otWeb";
static const char kDefaultInterfaceName[] = "wpan0";

void PrintVersion(void)
{
printf("%s\n", PACKAGE_VERSION);
}

int main(int argc, char **argv)
{
ot::Web::WebServer server;
const char *interfaceName = NULL;
int ret = 0;
int opt;

ot::Web::WebServer *server;

while ((opt = getopt(argc, argv, "vI:")) != -1)
{
switch (opt)
{
case 'I':
interfaceName = optarg;
break;

case 'v':
PrintVersion();
ExitNow();
break;

default:
fprintf(stderr, "Usage: %s [-I interfaceName] [-v]\n", argv[0]);
ExitNow(ret = -1);
break;
}
}

if (interfaceName == NULL)
{
interfaceName = kDefaultInterfaceName;
printf("interfaceName not specified, using default %s\n", interfaceName);
}

openlog(kSyslogIdent, LOG_CONS | LOG_PID, LOG_USER);
syslog(LOG_INFO, "border router started on %s", interfaceName);
server = new ot::Web::WebServer();
server->StartWebServer(interfaceName);

closelog();

server.StartWebServer();
return 0;
exit:
server = NULL;
return ret;
}
209 changes: 97 additions & 112 deletions src/web/web-service/web_service.cpp

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/web/web-service/web_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ namespace Web {

typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;


class WebServer
{
public:
WebServer(void);
~WebServer(void);
void StartWebServer(void);
void StartWebServer(const char *aIfName);

enum
{
Expand All @@ -71,9 +72,9 @@ class WebServer
};

private:
typedef std::string (*HttpRequestCallback)(boost::property_tree::ptree &aPtreeObject);
typedef std::string (*HttpRequestCallback)(boost::property_tree::ptree &aPtreeObject, const char *aIfName);

void HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback);
void HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback, const char *aIfName);
void JoinNetworkResponse(void);
void FormNetworkResponse(void);
void AddOnMeshPrefix(void);
Expand All @@ -88,6 +89,7 @@ class WebServer
static int sNetworksCount;
static std::string sNetowrkName, sExtPanId;
static bool sIsStarted;
char mIfName[DBUS_MAXIMUM_NAME_LENGTH + 1];
};

} //namespace Web
Expand Down
92 changes: 70 additions & 22 deletions src/web/wpan-controller/dbus_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ namespace Dbus {

DBusConnection *DBusBase::GetConnection(void)
{
dbus_error_init(&error);
mConnection = dbus_bus_get(DBUS_BUS_STARTER, &error);
dbus_error_init(&mError);
mConnection = dbus_bus_get(DBUS_BUS_STARTER, &mError);
if (!mConnection)
{
syslog(LOG_ERR, "mConnection is NULL.\n");
syslog(LOG_ERR, "connection is NULL.\n");

dbus_error_free(&error);
dbus_error_init(&error);
mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
dbus_error_free(&mError);
dbus_error_init(&mError);
mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &mError);
}
return mConnection;
}
Expand All @@ -62,14 +62,18 @@ DBusMessage *DBusBase::GetMessage(void)
{
int ret = kWpantundStatus_Ok;

VerifyOrExit(mDestination != NULL, ret = kWpantundStatus_InvalidArgument);
VerifyOrExit(mPath != NULL, ret = kWpantundStatus_InvalidArgument);
VerifyOrExit(mIface != NULL, ret = kWpantundStatus_InvalidArgument);
VerifyOrExit(mMethod != NULL, ret = kWpantundStatus_InvalidArgument);
mMessage = dbus_message_new_method_call(mDestination, mPath, mIface,
mMethod);
VerifyOrExit(mMessage != NULL, ret = kWpantundStatus_GetNullMessage);

exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "mMessage is NULL\n");
syslog(LOG_ERR, "message is NULL\n");
}
return mMessage;
}
Expand All @@ -79,14 +83,14 @@ DBusMessage *DBusBase::GetReply(void)
int ret = kWpantundStatus_Ok;

mReply = dbus_connection_send_with_reply_and_block(mConnection, mMessage,
DEFAULT_TIMEOUT_IN_MILLISECONDS, &error);
DEFAULT_TIMEOUT_IN_MILLISECONDS, &mError);

VerifyOrExit(mReply != NULL, ret = kWpantundStatus_GetNullReply);

exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "mReply is NULL; error: %s\n", error.message);
syslog(LOG_ERR, "reply is NULL; mError: %s\n", mError.message);
}
return mReply;
}
Expand All @@ -102,7 +106,7 @@ DBusPendingCall *DBusBase::GetPending(void)
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "mPending is NULL; error: %s\n", error.message);
syslog(LOG_ERR, "pending is NULL; mError: %s\n", mError.message);
}
return mPending;
}
Expand All @@ -118,12 +122,12 @@ void DBusBase::free()
if (mReply)
dbus_message_unref(mReply);

dbus_error_free(&error);
dbus_error_free(&mError);
}

DBusError DBusBase::GetError(void)
{
return error;
return mError;
}

int DBusBase::ProcessReply(void)
Expand All @@ -138,29 +142,73 @@ char *DBusBase::GetDBusName(void)

void DBusBase::SetDestination(const char *aDestination)
{
mDestination = aDestination;
}
void DBusBase::SetPath(const char *aPath)
{
mPath = aPath;
int ret = kWpantundStatus_Ok;

VerifyOrExit(aDestination != NULL, ret = kWpantundStatus_InvalidArgument);
strcpy(mDestination, aDestination);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "destination is NULL; mError: %s\n", mError.message);
}
}

void DBusBase::SetIface(const char *aIface)
{
mIface = aIface;
int ret = kWpantundStatus_Ok;

VerifyOrExit(aIface != NULL, ret = kWpantundStatus_InvalidArgument);
strcpy(mIface, aIface);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "interface is NULL; mError: %s\n", mError.message);
}
}

void DBusBase::SetMethod(const char *aMethod)
{
mMethod = aMethod;
}

void DBusBase::SetInterfaceName(const char *aInterfaceName)
{
strncpy(mInterfaceName, aInterfaceName, strlen(aInterfaceName));
mInterfaceName[strlen(aInterfaceName)] = '\0';
int ret = kWpantundStatus_Ok;

VerifyOrExit(aInterfaceName != NULL, ret = kWpantundStatus_InvalidArgument);
strcpy(mInterfaceName, aInterfaceName);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "interface name is NULL; mError: %s\n", mError.message);
}
}

void DBusBase::SetPath(const char *aPath)
{
int ret = kWpantundStatus_Ok;

VerifyOrExit(aPath != NULL, ret = kWpantundStatus_InvalidArgument);
strcpy(mPath, aPath);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "path is NULL; mError: %s\n", mError.message);

}
}

void DBusBase::SetDBusName(const char *aDBusName)
{
strncpy(mDBusName, aDBusName, strlen(aDBusName));
mInterfaceName[strlen(aDBusName)] = '\0';
int ret = kWpantundStatus_Ok;

VerifyOrExit(aDBusName != NULL, ret = kWpantundStatus_InvalidDBusName);
strcpy(mDBusName, aDBusName);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "pending is NULL; mError: %d\n", ret);
}
}

} //namespace Dbus
Expand Down
14 changes: 7 additions & 7 deletions src/web/wpan-controller/dbus_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ class DBusBase
void free(void);

void SetDestination(const char *aDestination);
void SetPath(const char *aPath);
void SetIface(const char *aIface);
void SetMethod(const char *aMethod);
void SetPath(const char *aPath);
void SetInterfaceName(const char *aInterfaceName);
void SetDBusName(const char *aDBusName);

char mDBusName[DBUS_MAXIMUM_NAME_LENGTH + 1];
char mInterfaceName[DBUS_MAXIMUM_NAME_LENGTH + 1];
char mDBusName[DBUS_MAXIMUM_NAME_LENGTH + 1];
char mInterfaceName[DBUS_MAXIMUM_NAME_LENGTH + 1];
DBusError mError;

private:
DBusConnection *mConnection;
DBusMessage *mMessage;
DBusMessage *mReply;
DBusError error;
DBusPendingCall *mPending;

const char *mDestination;
const char *mPath;
const char *mIface;
const char *mMethod;
char mDestination[DBUS_MAXIMUM_NAME_LENGTH + 1];
char mPath[DBUS_MAXIMUM_NAME_LENGTH + 1];
char mIface[DBUS_MAXIMUM_NAME_LENGTH + 1];
};

} //namespace Dbus
Expand Down
14 changes: 2 additions & 12 deletions src/web/wpan-controller/dbus_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,12 @@ namespace Dbus {
int DBusForm::ProcessReply(void)
{
int ret = 0;
char path[DBUS_MAXIMUM_NAME_LENGTH + 1];
const char *iface = "com.nestlabs.WPANTunnelDriver";
const char *method = "Form";
const char *interfaceName = "wpan0";
DBusMessage *messsage = NULL;
DBusMessage *reply = NULL;
DBusError error;

VerifyOrExit(GetConnection() != NULL, ret = kWpantundStatus_InvalidConnection);
snprintf(path, sizeof(path), "%s/%s", WPAN_TUNNEL_DBUS_PATH,
interfaceName);
SetIface(iface);
SetMethod(method);
SetInterfaceName(interfaceName);
SetPath(path);

VerifyOrExit((messsage = GetMessage()) != NULL, ret = kWpantundStatus_InvalidMessage);
VerifyOrExit(mNetworkName != NULL, ret = kWpantundStatus_InvalidArgument);
Expand All @@ -67,16 +58,15 @@ int DBusForm::ProcessReply(void)
DBUS_TYPE_INVALID);

VerifyOrExit((reply = GetReply()) != NULL, ret = kWpantundStatus_InvalidReply);
error = GetError();
dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &ret,
dbus_message_get_args(reply, &mError, DBUS_TYPE_INT32, &ret,
DBUS_TYPE_INVALID);
if (!ret)
{
syslog(LOG_ERR, "Successfully formed!\n");
}
else
{
syslog(LOG_ERR, "Error: Failed to formed! %s\n", error.message);
syslog(LOG_ERR, "Error: Failed to formed! %s\n", mError.message);
}
exit:
free();
Expand Down
13 changes: 2 additions & 11 deletions src/web/wpan-controller/dbus_gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,12 @@ DBusGateway::DBusGateway(void) :
int DBusGateway::ProcessReply(void)
{
int ret = 0;
char path[DBUS_MAXIMUM_NAME_LENGTH + 1];
const char *iface = "org.wpantund.v1";
const char *method = "ConfigGateway";
const char *interfaceName = "wpan0";
DBusMessage *messsage = NULL;
DBusMessage *reply = NULL;
DBusError error;

VerifyOrExit(GetConnection() != NULL, ret = kWpantundStatus_InvalidConnection);
snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, interfaceName);
SetIface(iface);
SetMethod(method);
SetInterfaceName(interfaceName);
SetPath(path);

VerifyOrExit((messsage = GetMessage()) != NULL, ret = kWpantundStatus_InvalidMessage);

Expand Down Expand Up @@ -95,16 +87,15 @@ int DBusGateway::ProcessReply(void)
&mValidLifetime, DBUS_TYPE_INVALID);

VerifyOrExit((reply = GetReply()) != NULL, ret = kWpantundStatus_InvalidReply);
error = GetError();
dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &ret,
dbus_message_get_args(reply, &mError, DBUS_TYPE_INT32, &ret,
DBUS_TYPE_INVALID);
if (!ret)
{
syslog(LOG_ERR, "Gateway configured successfully!\n");
}
else
{
syslog(LOG_ERR, "Error: Failed to configure! %s\n", error.message);
syslog(LOG_ERR, "Error: Failed to configure! %s\n", mError.message);
}
exit:
free();
Expand Down
Loading

0 comments on commit ea69ea0

Please sign in to comment.