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 a2bf4eb
Show file tree
Hide file tree
Showing 16 changed files with 453 additions and 302 deletions.
6 changes: 6 additions & 0 deletions src/web/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ libotbr_web_la_LIBADD = \
-lpthread \
$(top_builddir)/third_party/mbedtls/libmbedtls.la \
$(top_builddir)/src/utils/libutils.la \
$(top_builddir)/third_party/wpantund/libwpanctl.la \
$(NULL)

libotbr_web_la_SOURCES = \
Expand Down Expand Up @@ -86,6 +87,11 @@ libotbr_web_la_CPPFLAGS = \
-I$(top_srcdir)/third_party/Simple-web-server/repo/ \
-I$(top_srcdir)/third_party/mbedtls/repo/configs \
-I$(top_srcdir)/third_party/mbedtls/repo/include \
-I$(top_builddir)/third_party/wpantund/repo/src \
-I$(top_srcdir)/third_party/wpantund/repo/src \
-I$(top_srcdir)/third_party/wpantund/repo/src/ipc-dbus \
-I$(top_srcdir)/third_party/wpantund/repo/src/wpanctl \
-I$(top_srcdir)/third_party/wpantund/repo/src/wpantund \
-DMBEDTLS_CONFIG_FILE='<config-thread.h>' \
-DWEB_FILE_PATH=\"$(datadir)/border-router/frontend\" \
-std=c++11 \
Expand Down
63 changes: 60 additions & 3 deletions src/web/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,69 @@
#define WEB_FILE_PATH "/usr/local/share/border-router/frontend"
#endif

#include "otbr-config.h"

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

#include "common/code_utils.hpp"
#include "web-service/web_service.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:
if (server != NULL)
{
delete server;
}
return ret;
}
209 changes: 97 additions & 112 deletions src/web/web-service/web_service.cpp

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/web/web-service/web_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class WebServer
public:
WebServer(void);
~WebServer(void);
void StartWebServer(void);
void StartWebServer(const char *aIfName);

enum
{
Expand All @@ -71,9 +71,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 +88,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
142 changes: 119 additions & 23 deletions src/web/wpan-controller/dbus_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,52 @@ namespace Dbus {

DBusConnection *DBusBase::GetConnection(void)
{
DBusError error;

dbus_error_init(&error);
mConnection = dbus_bus_get(DBUS_BUS_STARTER, &error);
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);
}
if (dbus_error_is_set(&error))
{
syslog(LOG_ERR, "connection error: %s", error.message);
}
dbus_error_free(&error);
return mConnection;
}

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;
}

DBusMessage *DBusBase::GetReply(void)
{
int ret = kWpantundStatus_Ok;
int ret = kWpantundStatus_Ok;
DBusError error;

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

Expand All @@ -86,24 +99,36 @@ DBusMessage *DBusBase::GetReply(void)
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "mReply is NULL; error: %s\n", error.message);
syslog(LOG_ERR, "reply is NULL; error: %s\n", error.message);
}
if (dbus_error_is_set(&error))
{
syslog(LOG_ERR, "reply error: %s", error.message);
}
dbus_error_free(&error);
return mReply;
}

DBusPendingCall *DBusBase::GetPending(void)
{
int ret = kWpantundStatus_Ok;
int ret = kWpantundStatus_Ok;
DBusError error;

dbus_error_init(&error);
dbus_connection_send_with_reply(mConnection, mMessage, &mPending, DEFAULT_TIMEOUT_IN_MILLISECONDS);

VerifyOrExit(mPending != NULL, ret = kWpantundStatus_GetNullPending);

exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "mPending is NULL; error: %s\n", error.message);
syslog(LOG_ERR, "pending is NULL; error: %s\n", error.message);
}
if (dbus_error_is_set(&error))
{
syslog(LOG_ERR, "pending error: %s", error.message);
}
dbus_error_free(&error);
return mPending;
}

Expand All @@ -117,13 +142,6 @@ void DBusBase::free()

if (mReply)
dbus_message_unref(mReply);

dbus_error_free(&error);
}

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

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

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

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

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

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

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;
DBusError error;

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

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

dbus_error_init(&error);
VerifyOrExit(aPath != NULL, ret = kWpantundStatus_InvalidArgument);
strcpy(mPath, aPath);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "path is NULL; error: %s\n", error.message);
}
if (dbus_error_is_set(&error))
{
syslog(LOG_ERR, "set path error: %s", error.message);
}
dbus_error_free(&error);
}

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

dbus_error_init(&error);
VerifyOrExit(aDBusName != NULL, ret = kWpantundStatus_InvalidDBusName);
strcpy(mDBusName, aDBusName);
exit:
if (ret != kWpantundStatus_Ok)
{
syslog(LOG_ERR, "pending is NULL; error: %d\n", ret);
}
if (dbus_error_is_set(&error))
{
syslog(LOG_ERR, "set dbus name error: %s", error.message);
}
dbus_error_free(&error);
}

} //namespace Dbus
Expand Down
10 changes: 4 additions & 6 deletions src/web/wpan-controller/dbus_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ class DBusBase
DBusMessage *GetMessage(void);
DBusMessage *GetReply(void);
DBusPendingCall *GetPending(void);
DBusError GetError(void);
int ProcessReply(void);
char *GetDBusName(void);
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);

Expand All @@ -67,13 +66,12 @@ class DBusBase
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
Loading

0 comments on commit a2bf4eb

Please sign in to comment.