diff --git a/src/web/main.cpp b/src/web/main.cpp index 14f8c27f478..d50d97900dd 100644 --- a/src/web/main.cpp +++ b/src/web/main.cpp @@ -34,12 +34,65 @@ #define WEB_FILE_PATH "/usr/local/share/border-router/frontend" #endif +#include +#include +#include +#include + +#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; } diff --git a/src/web/web-service/web_service.cpp b/src/web/web-service/web_service.cpp index b774d635b3e..65aed5664f9 100644 --- a/src/web/web-service/web_service.cpp +++ b/src/web/web-service/web_service.cpp @@ -103,30 +103,30 @@ static void SetNetworkInfo(const char *networkName, const char *extPanId) sExtPanId = extPanId; } -static std::string OnJoinNetworkRequest(boost::property_tree::ptree &aJoinRequest) +static std::string OnJoinNetworkRequest(boost::property_tree::ptree &aJoinRequest, const char *aIfName) { char extPanId[EXTENED_PANID_LENGTH * 2 + 1]; int ret = ot::Dbus::kWpantundStatus_Ok; int index = aJoinRequest.get("index"); - std::string networkKey = aJoinRequest.get("networkKey"); - std::string prefix = aJoinRequest.get("prefix"); - bool defaultRoute = aJoinRequest.get("defaultRoute"); + std::string networkKey = aJoinRequest.get("networkKey"); + std::string prefix = aJoinRequest.get("prefix"); + bool defaultRoute = aJoinRequest.get("defaultRoute"); + ot::Dbus::WPANController wpanController; - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_StartFailed); - VerifyOrExit(ot::Dbus::WPANController::Leave() == ot::Dbus::kWpantundStatus_Ok, + wpanController.SetInterfaceName(aIfName); + VerifyOrExit(wpanController.Leave() == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_LeaveFailed); - VerifyOrExit(ot::Dbus::WPANController::Set(WebServer::kPropertyType_Data, - "NetworkKey", - networkKey.c_str()) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Set(WebServer::kPropertyType_Data, + "NetworkKey", + networkKey.c_str()) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_SetFailed); - VerifyOrExit(ot::Dbus::WPANController::Join(sNetworks[index].mNetworkName, - sNetworks[index].mChannel, - sNetworks[index].mExtPanId, - sNetworks[index].mPanId) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Join(sNetworks[index].mNetworkName, + sNetworks[index].mChannel, + sNetworks[index].mExtPanId, + sNetworks[index].mPanId) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_JoinFailed); - VerifyOrExit(ot::Dbus::WPANController::Gateway(prefix.c_str(), 64, defaultRoute) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Gateway(prefix.c_str(), 64, defaultRoute) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_GatewayFailed); ot::Utils::Long2Hex(sNetworks[index].mExtPanId, extPanId); @@ -139,53 +139,53 @@ static std::string OnJoinNetworkRequest(boost::property_tree::ptree &aJoinReques return HttpReponse(ret); } -static std::string OnFormNetworkRequest(boost::property_tree::ptree &aFormRequest) +static std::string OnFormNetworkRequest(boost::property_tree::ptree &aFormRequest, const char *aIfName) { int ret = ot::Dbus::kWpantundStatus_Ok; - std::string networkKey = aFormRequest.get("networkKey"); - std::string prefix = aFormRequest.get("prefix"); - int channel = aFormRequest.get("channel"); - std::string networkName = aFormRequest.get("networkName"); - std::string passphrase = aFormRequest.get("passphrase"); - std::string panId = aFormRequest.get("panId"); - std::string extPanId = aFormRequest.get("extPanId"); - bool defaultRoute = aFormRequest.get("defaultRoute"); - ot::Psk::Pskc psk; - char pskcStr[PSKC_MAX_LENGTH*2]; - uint8_t extPanIdBytes[EXTENED_PANID_LENGTH]; - - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_StartFailed); - VerifyOrExit(ot::Dbus::WPANController::Leave() == ot::Dbus::kWpantundStatus_Ok, + std::string networkKey = aFormRequest.get("networkKey"); + std::string prefix = aFormRequest.get("prefix"); + int channel = aFormRequest.get("channel"); + std::string networkName = aFormRequest.get("networkName"); + std::string passphrase = aFormRequest.get("passphrase"); + std::string panId = aFormRequest.get("panId"); + std::string extPanId = aFormRequest.get("extPanId"); + bool defaultRoute = aFormRequest.get("defaultRoute"); + ot::Psk::Pskc psk; + char pskcStr[PSKC_MAX_LENGTH*2]; + uint8_t extPanIdBytes[EXTENED_PANID_LENGTH]; + ot::Dbus::WPANController wpanController; + + wpanController.SetInterfaceName(aIfName); + VerifyOrExit(wpanController.Leave() == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_LeaveFailed); - VerifyOrExit(ot::Dbus::WPANController::Set(WebServer::kPropertyType_Data, - kWPANTUNDProperty_NetworkKey, - networkKey.c_str()) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Set(WebServer::kPropertyType_Data, + kWPANTUNDProperty_NetworkKey, + networkKey.c_str()) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_SetFailed); - VerifyOrExit(ot::Dbus::WPANController::Set(WebServer::kPropertyType_String, - kWPANTUNDProperty_NetworkPANID, - panId.c_str()) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Set(WebServer::kPropertyType_String, + kWPANTUNDProperty_NetworkPANID, + panId.c_str()) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_SetFailed); - VerifyOrExit(ot::Dbus::WPANController::Set(WebServer::kPropertyType_Data, - kWPANTUNDProperty_NetworkXPANID, - extPanId.c_str()) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Set(WebServer::kPropertyType_Data, + kWPANTUNDProperty_NetworkXPANID, + extPanId.c_str()) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_SetFailed); ot::Utils::Hex2Bytes(extPanId.c_str(), extPanIdBytes, EXTENED_PANID_LENGTH); psk.SetSalt(extPanIdBytes, networkName.c_str()); psk.SetPassphrase(passphrase.c_str()); ot::Utils::Bytes2Hex(psk.GetPskc(), PSKC_MAX_LENGTH, pskcStr); - VerifyOrExit(ot::Dbus::WPANController::Set(WebServer::kPropertyType_Data, - kWPANTUNDProperty_NetworkPskc, - pskcStr) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Set(WebServer::kPropertyType_Data, + kWPANTUNDProperty_NetworkPskc, + pskcStr) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_SetFailed); - VerifyOrExit(ot::Dbus::WPANController::Form(networkName.c_str(), channel) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Form(networkName.c_str(), channel) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_FormFailed); - VerifyOrExit(ot::Dbus::WPANController::Gateway(prefix.c_str(), 64, defaultRoute) == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Gateway(prefix.c_str(), 64, defaultRoute) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_GatewayFailed); SetNetworkInfo(networkName.c_str(), extPanId.c_str()); exit: @@ -196,16 +196,16 @@ static std::string OnFormNetworkRequest(boost::property_tree::ptree &aFormReques return HttpReponse(ret); } -static std::string OnAddPrefixRequest(boost::property_tree::ptree &aAddPrefixRequest) +static std::string OnAddPrefixRequest(boost::property_tree::ptree &aAddPrefixRequest, const char *aIfName) { int ret = ot::Dbus::kWpantundStatus_Ok; - std::string prefix = aAddPrefixRequest.get("prefix"); - bool defaultRoute = aAddPrefixRequest.get("defaultRoute"); + std::string prefix = aAddPrefixRequest.get("prefix"); + bool defaultRoute = aAddPrefixRequest.get("defaultRoute"); + ot::Dbus::WPANController wpanController; - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_StartFailed); - VerifyOrExit(ot::Dbus::WPANController::Gateway(prefix.c_str(), 64, defaultRoute) == ot::Dbus::kWpantundStatus_Ok, + wpanController.SetInterfaceName(aIfName); + VerifyOrExit(wpanController.Gateway(prefix.c_str(), 64, defaultRoute) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_GatewayFailed); exit: if (ret != ot::Dbus::kWpantundStatus_Ok) @@ -216,15 +216,15 @@ static std::string OnAddPrefixRequest(boost::property_tree::ptree &aAddPrefixReq } -static std::string OnDeletePrefixRequest(boost::property_tree::ptree &aDeleteRequest) +static std::string OnDeletePrefixRequest(boost::property_tree::ptree &aDeleteRequest, const char *aIfName) { int ret = ot::Dbus::kWpantundStatus_Ok; - std::string prefix = aDeleteRequest.get("prefix"); + std::string prefix = aDeleteRequest.get("prefix"); + ot::Dbus::WPANController wpanController; - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_StartFailed); - VerifyOrExit(ot::Dbus::WPANController::RemoveGateway(prefix.c_str(), 64) == ot::Dbus::kWpantundStatus_Ok, + wpanController.SetInterfaceName(aIfName); + VerifyOrExit(wpanController.RemoveGateway(prefix.c_str(), 64) == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_GatewayFailed); exit: if (ret != ot::Dbus::kWpantundStatus_Ok) @@ -234,54 +234,49 @@ static std::string OnDeletePrefixRequest(boost::property_tree::ptree &aDeleteReq return HttpReponse(ret); } -static std::string OnGetNetworkRequest(boost::property_tree::ptree &aGetNetworkRequest) +static std::string OnGetNetworkRequest(boost::property_tree::ptree &aGetNetworkRequest, const char *aIfName) { boost::property_tree::ptree root, networkInfo; int ret = ot::Dbus::kWpantundStatus_Ok; - - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_StartFailed); - networkInfo.put("NCP:State", ot::Dbus::WPANController::Get("NCP:State")); - networkInfo.put("Daemon:Enabled", ot::Dbus::WPANController::Get("Daemon:Enabled")); - networkInfo.put("NCP:Version", ot::Dbus::WPANController::Get("NCP:Version")); - networkInfo.put("Daemon:Version", ot::Dbus::WPANController::Get("Daemon:Version")); - networkInfo.put("Config:NCP:DriverName", ot::Dbus::WPANController::Get("Config:NCP:DriverName")); - networkInfo.put("NCP:HardwareAddress", ot::Dbus::WPANController::Get("NCP:HardwareAddress")); - networkInfo.put("NCP:Channel", ot::Dbus::WPANController::Get("NCP:Channel")); - networkInfo.put("Network:NodeType", ot::Dbus::WPANController::Get("Network:NodeType")); - networkInfo.put("Network:Name", ot::Dbus::WPANController::Get("Network:Name")); - networkInfo.put("Network:XPANID", ot::Dbus::WPANController::Get("Network:XPANID")); - networkInfo.put("Network:PANID", ot::Dbus::WPANController::Get("Network:PANID")); - networkInfo.put("IPv6:LinkLocalAddress", ot::Dbus::WPANController::Get("IPv6:LinkLocalAddress")); - networkInfo.put("IPv6:MeshLocalAddress", ot::Dbus::WPANController::Get("IPv6:MeshLocalAddress")); - networkInfo.put("IPv6:MeshLocalPrefix", ot::Dbus::WPANController::Get("IPv6:MeshLocalPrefix")); + ot::Dbus::WPANController wpanController; + + wpanController.SetInterfaceName(aIfName); + networkInfo.put("NCP:State", wpanController.Get("NCP:State")); + networkInfo.put("Daemon:Enabled", wpanController.Get("Daemon:Enabled")); + networkInfo.put("NCP:Version", wpanController.Get("NCP:Version")); + networkInfo.put("Daemon:Version", wpanController.Get("Daemon:Version")); + networkInfo.put("Config:NCP:DriverName", wpanController.Get("Config:NCP:DriverName")); + networkInfo.put("NCP:HardwareAddress", wpanController.Get("NCP:HardwareAddress")); + networkInfo.put("NCP:Channel", wpanController.Get("NCP:Channel")); + networkInfo.put("Network:NodeType", wpanController.Get("Network:NodeType")); + networkInfo.put("Network:Name", wpanController.Get("Network:Name")); + networkInfo.put("Network:XPANID", wpanController.Get("Network:XPANID")); + networkInfo.put("Network:PANID", wpanController.Get("Network:PANID")); + networkInfo.put("IPv6:LinkLocalAddress", wpanController.Get("IPv6:LinkLocalAddress")); + networkInfo.put("IPv6:MeshLocalAddress", wpanController.Get("IPv6:MeshLocalAddress")); + networkInfo.put("IPv6:MeshLocalPrefix", wpanController.Get("IPv6:MeshLocalPrefix")); root.add_child("result", networkInfo); -exit: - if (ret != ot::Dbus::kWpantundStatus_Ok) - { - root.put("result", "failed"); - syslog(LOG_ERR, "Error is %d\n", ret); - } root.put("error", ret); std::stringstream ss; write_json(ss, root, false); return ss.str(); } -static std::string OnGetAvailableNetworkResponse(boost::property_tree::ptree &aGetAvailableNetworkRequest) +static std::string OnGetAvailableNetworkResponse(boost::property_tree::ptree &aGetAvailableNetworkRequest, + const char *aIfName) { boost::property_tree::ptree root, networks, networkInfo; int ret = ot::Dbus::kWpantundStatus_Ok; + ot::Dbus::WPANController wpanController; - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_LookUpFailed); - VerifyOrExit(ot::Dbus::WPANController::Leave() == ot::Dbus::kWpantundStatus_Ok, + wpanController.SetInterfaceName(aIfName); + VerifyOrExit(wpanController.Leave() == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_LeaveFailed); - VerifyOrExit(ot::Dbus::WPANController::Scan() == ot::Dbus::kWpantundStatus_Ok, + VerifyOrExit(wpanController.Scan() == ot::Dbus::kWpantundStatus_Ok, ret = ot::Dbus::kWpantundStatus_ScanFailed); - sNetworksCount = ot::Dbus::WPANController::GetScanNetworksInfoCount(); + sNetworksCount = wpanController.GetScanNetworksInfoCount(); VerifyOrExit(sNetworksCount > 0, ret = ot::Dbus::kWpantundStatus_NetworkNotFound); - memcpy(sNetworks, ot::Dbus::WPANController::GetScanNetworksInfo(), + memcpy(sNetworks, wpanController.GetScanNetworksInfo(), sNetworksCount * sizeof(ot::Dbus::WpanNetworkInfo)); for (int i = 0; i < sNetworksCount; i++) @@ -310,7 +305,7 @@ static std::string OnGetAvailableNetworkResponse(boost::property_tree::ptree &aG return ss.str(); } -static std::string OnBootMdnsRequest(boost::property_tree::ptree &aBootMdnsRequest) +static std::string OnBootMdnsRequest(boost::property_tree::ptree &aBootMdnsRequest, const char *aIfName) { std::thread mdnsPublisherThread([]() { ot::Mdns::Publisher::SetServiceName(sNetworkName.c_str()); @@ -340,9 +335,10 @@ WebServer::~WebServer(void) delete &mServer; } -void WebServer::StartWebServer(void) +void WebServer::StartWebServer(const char *aIfName) { mServer->config.port = 80; + strcpy(mIfName, aIfName); JoinNetworkResponse(); FormNetworkResponse(); AddOnMeshPrefix(); @@ -359,43 +355,45 @@ void WebServer::StartWebServer(void) void WebServer::JoinNetworkResponse(void) { - HandleHttpRequest(JOIN_NETWORK_URL, REQUEST_METHOD_POST, OnJoinNetworkRequest); + HandleHttpRequest(JOIN_NETWORK_URL, REQUEST_METHOD_POST, OnJoinNetworkRequest, mIfName); } void WebServer::FormNetworkResponse(void) { - HandleHttpRequest(FORM_NETWORK_URL, REQUEST_METHOD_POST, OnFormNetworkRequest); + HandleHttpRequest(FORM_NETWORK_URL, REQUEST_METHOD_POST, OnFormNetworkRequest, mIfName); } void WebServer::AddOnMeshPrefix(void) { - HandleHttpRequest(ADD_PREFIX_URL, REQUEST_METHOD_POST, OnAddPrefixRequest); + HandleHttpRequest(ADD_PREFIX_URL, REQUEST_METHOD_POST, OnAddPrefixRequest, mIfName); } void WebServer::DeleteOnMeshPrefix(void) { - HandleHttpRequest(DELETE_PREFIX_URL, REQUEST_METHOD_POST, OnDeletePrefixRequest); + HandleHttpRequest(DELETE_PREFIX_URL, REQUEST_METHOD_POST, OnDeletePrefixRequest, mIfName); } void WebServer::GetNetworkResponse(void) { - HandleHttpRequest(GET_NETWORK_URL, REQUEST_METHOD_GET, OnGetNetworkRequest); + HandleHttpRequest(GET_NETWORK_URL, REQUEST_METHOD_GET, OnGetNetworkRequest, mIfName); } void WebServer::AvailableNetworkResponse(void) { - HandleHttpRequest(AVAILABLE_NETWORK_URL, REQUEST_METHOD_GET, OnGetAvailableNetworkResponse); + HandleHttpRequest(AVAILABLE_NETWORK_URL, REQUEST_METHOD_GET, OnGetAvailableNetworkResponse, mIfName); } void WebServer::BootMdnsPublisher(void) { - HandleHttpRequest(BOOT_MDNS_URL, REQUEST_METHOD_GET, OnBootMdnsRequest); + HandleHttpRequest(BOOT_MDNS_URL, REQUEST_METHOD_GET, OnBootMdnsRequest, mIfName); } -void WebServer::HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback) +void WebServer::HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback, + const char *aIfName) { mServer->resource[aUrl][aMethod] = - [aCallback](std::shared_ptr response, std::shared_ptr request) + [aCallback, aIfName](std::shared_ptr response, + std::shared_ptr request) { try { @@ -407,7 +405,7 @@ void WebServer::HandleHttpRequest(const char *aUrl, const char *aMethod, HttpReq { read_json(request->content, pt); } - httpResponse = aCallback(pt); + httpResponse = aCallback(pt, aIfName); } *response << RESPONSE_SUCCESS_STATUS @@ -430,8 +428,6 @@ void WebServer::HandleHttpRequest(const char *aUrl, const char *aMethod, HttpReq void WebServer::DefaultHttpResponse(void) { - int ret = ot::Dbus::kWpantundStatus_Ok; - mServer->default_resource[REQUEST_METHOD_GET] = [this](std::shared_ptr response, std::shared_ptr request) { @@ -487,17 +483,6 @@ void WebServer::DefaultHttpResponse(void) << content; } }; - - VerifyOrExit(ot::Dbus::WPANController::Start() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_StartFailed); - VerifyOrExit(ot::Dbus::WPANController::Leave() == ot::Dbus::kWpantundStatus_Ok, - ret = ot::Dbus::kWpantundStatus_LeaveFailed); -exit: - - if (ret != ot::Dbus::kWpantundStatus_Ok) - { - syslog(LOG_ERR, "Error is %d\n", ret); - } } void DefaultResourceSend(const HttpServer &aServer, const std::shared_ptr &aResponse, diff --git a/src/web/web-service/web_service.hpp b/src/web/web-service/web_service.hpp index 62bd28eb266..0b46195c113 100644 --- a/src/web/web-service/web_service.hpp +++ b/src/web/web-service/web_service.hpp @@ -62,7 +62,7 @@ class WebServer public: WebServer(void); ~WebServer(void); - void StartWebServer(void); + void StartWebServer(const char *aIfName); enum { @@ -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); @@ -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 diff --git a/src/web/wpan-controller/dbus_base.cpp b/src/web/wpan-controller/dbus_base.cpp index c0c3b1a75e6..21cb1371078 100644 --- a/src/web/wpan-controller/dbus_base.cpp +++ b/src/web/wpan-controller/dbus_base.cpp @@ -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; } @@ -62,6 +62,10 @@ 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); @@ -69,7 +73,7 @@ DBusMessage *DBusBase::GetMessage(void) exit: if (ret != kWpantundStatus_Ok) { - syslog(LOG_ERR, "mMessage is NULL\n"); + syslog(LOG_ERR, "message is NULL\n"); } return mMessage; } @@ -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; } @@ -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; } @@ -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) @@ -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 diff --git a/src/web/wpan-controller/dbus_base.hpp b/src/web/wpan-controller/dbus_base.hpp index 84138601431..f1be4615fff 100644 --- a/src/web/wpan-controller/dbus_base.hpp +++ b/src/web/wpan-controller/dbus_base.hpp @@ -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 diff --git a/src/web/wpan-controller/dbus_form.cpp b/src/web/wpan-controller/dbus_form.cpp index 03d07d4b357..2d10e7c2784 100644 --- a/src/web/wpan-controller/dbus_form.cpp +++ b/src/web/wpan-controller/dbus_form.cpp @@ -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); @@ -67,8 +58,7 @@ 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) { @@ -76,7 +66,7 @@ int DBusForm::ProcessReply(void) } 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(); diff --git a/src/web/wpan-controller/dbus_gateway.cpp b/src/web/wpan-controller/dbus_gateway.cpp index 95ecb5eae50..dbab9fb0482 100644 --- a/src/web/wpan-controller/dbus_gateway.cpp +++ b/src/web/wpan-controller/dbus_gateway.cpp @@ -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); @@ -95,8 +87,7 @@ 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) { @@ -104,7 +95,7 @@ int DBusGateway::ProcessReply(void) } 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(); diff --git a/src/web/wpan-controller/dbus_get.cpp b/src/web/wpan-controller/dbus_get.cpp index 3d6864dd43e..34e1b44fbbb 100644 --- a/src/web/wpan-controller/dbus_get.cpp +++ b/src/web/wpan-controller/dbus_get.cpp @@ -166,19 +166,12 @@ static void DumpInfoFromIter(char *aOutput, DBusMessageIter *aIter, int aIndent, int DBusGet::ProcessReply(void) { int ret = 0; - char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; - const char *iface = "org.wpantund.v1"; const char *method = "PropGet"; - const char *interfaceName = "wpan0"; DBusMessage *messsage = NULL; DBusMessage *reply = NULL; 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); dbus_message_append_args(messsage, DBUS_TYPE_STRING, &mPropertyName, DBUS_TYPE_INVALID); diff --git a/src/web/wpan-controller/dbus_ifname.cpp b/src/web/wpan-controller/dbus_ifname.cpp index fa2f1fc4a58..3cb21093ab0 100644 --- a/src/web/wpan-controller/dbus_ifname.cpp +++ b/src/web/wpan-controller/dbus_ifname.cpp @@ -52,7 +52,6 @@ int DBusIfname::ProcessReply(void) SetPath("/com/nestlabs/WPANTunnelDriver"); SetMethod("GetInterfaces"); SetIface("com.nestlabs.WPANTunnelDriver"); - SetInterfaceName("wpan0"); VerifyOrExit(GetMessage() != NULL, ret = kWpantundStatus_InvalidMessage); VerifyOrExit((reply = GetReply()) != NULL, ret = kWpantundStatus_InvalidReply); diff --git a/src/web/wpan-controller/dbus_join.cpp b/src/web/wpan-controller/dbus_join.cpp index 803b25b9623..583b28a51a7 100644 --- a/src/web/wpan-controller/dbus_join.cpp +++ b/src/web/wpan-controller/dbus_join.cpp @@ -41,21 +41,12 @@ namespace Dbus { int DBusJoin::ProcessReply(void) { int ret = 0; - char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; - const char *iface = "com.nestlabs.WPANTunnelDriver"; const char *method = "Join"; - 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); @@ -80,8 +71,7 @@ int DBusJoin::ProcessReply(void) DBUS_TYPE_INVALID); VerifyOrExit((reply = GetReply()) != NULL); - 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) { @@ -89,7 +79,7 @@ int DBusJoin::ProcessReply(void) } else { - syslog(LOG_ERR, "Error: Failed to join! %s\n", error.message); + syslog(LOG_ERR, "Error: Failed to join! %s\n", mError.message); } exit: free(); diff --git a/src/web/wpan-controller/dbus_leave.cpp b/src/web/wpan-controller/dbus_leave.cpp index 5fad7b2f9c3..4475b8ddf3c 100644 --- a/src/web/wpan-controller/dbus_leave.cpp +++ b/src/web/wpan-controller/dbus_leave.cpp @@ -41,35 +41,25 @@ namespace Dbus { int DBusLeave::ProcessReply(void) { int ret = 0; - char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; - const char *iface = "org.wpantund.v1"; const char *method = "Leave"; - 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); 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); +exit: if (!ret) { syslog(LOG_INFO, "Successfully left!\n"); } else { - syslog(LOG_ERR, "Error: Failed to left! %s\n", error.message); + syslog(LOG_ERR, "Error: Failed to left! %s\n", mError.message); } -exit: free(); return ret; } diff --git a/src/web/wpan-controller/dbus_scan.cpp b/src/web/wpan-controller/dbus_scan.cpp index 27dc656bd21..9d0ad4b3d2a 100644 --- a/src/web/wpan-controller/dbus_scan.cpp +++ b/src/web/wpan-controller/dbus_scan.cpp @@ -44,32 +44,23 @@ WpanNetworkInfo DBusScan::mAvailableNetworks[SCANNED_NET_BUFFER_SIZE]; int DBusScan::ProcessReply(void) { int ret = 0; - char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; - const char *iface = WPANTUND_DBUS_APIv1_INTERFACE; const char *method = "NetScanStart"; - const char *interfaceName = "wpan0"; DBusMessage *messsage = NULL; DBusMessage *reply = NULL; DBusConnection *dbusConnection = NULL; DBusPendingCall *pending = NULL; static const char dbusObjectManagerMatchString[] = "type='signal'"; DBusMessageIter iter; - DBusError error; VerifyOrExit((dbusConnection = GetConnection()) != NULL, ret = kWpantundStatus_InvalidConnection); - error = GetError(); - dbus_bus_add_match(dbusConnection, dbusObjectManagerMatchString, &error); - VerifyOrExit(error.name == NULL, ret = kWpantundStatus_Failure); + dbus_bus_add_match(dbusConnection, dbusObjectManagerMatchString, &mError); + VerifyOrExit(mError.name == NULL, ret = kWpantundStatus_Failure); memset(mAvailableNetworks, 0, sizeof(mAvailableNetworks)); mAvailableNetworksCnt = 0; dbus_connection_add_filter(dbusConnection, &DbusBeaconHandler, NULL, NULL); - 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); dbus_message_append_args(messsage, DBUS_TYPE_UINT32, &mChannelMask, DBUS_TYPE_INVALID); diff --git a/src/web/wpan-controller/dbus_set.cpp b/src/web/wpan-controller/dbus_set.cpp index 94b94b1793d..129a84df881 100644 --- a/src/web/wpan-controller/dbus_set.cpp +++ b/src/web/wpan-controller/dbus_set.cpp @@ -42,20 +42,12 @@ namespace Dbus { int DBusSet::ProcessReply(void) { int ret = 0; - char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; - const char *iface = "org.wpantund.v1"; const char *method = "PropSet"; - 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); @@ -82,8 +74,7 @@ int DBusSet::ProcessReply(void) } 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) { @@ -91,7 +82,7 @@ int DBusSet::ProcessReply(void) } else { - syslog(LOG_ERR, "Error: Failed to set! %s\n", error.message); + syslog(LOG_ERR, "Error: Failed to set! %s\n", mError.message); } exit: free(); diff --git a/src/web/wpan-controller/wpan_controller.cpp b/src/web/wpan-controller/wpan_controller.cpp index 8e05acb8417..42c64a51d7f 100644 --- a/src/web/wpan-controller/wpan_controller.cpp +++ b/src/web/wpan-controller/wpan_controller.cpp @@ -53,48 +53,62 @@ namespace ot { namespace Dbus { -WpanNetworkInfo gScanedNetowrks[SCANNED_NET_BUFFER_SIZE]; -int gScannedNetworkCount = 0; -static DBusIfname *gInterfaceName = NULL; - int WPANController::Scan(void) { int ret = 0; - DBusScan scanNetwork; - - scanNetwork.SetChannelMask(0); - scanNetwork.SetDestination(gInterfaceName->GetDBusName()); - VerifyOrExit((ret = scanNetwork.ProcessReply()) == 0); - - VerifyOrExit((gScannedNetworkCount = scanNetwork.GetNetworksCount()) > 0, + DBusScan scannedNetwork; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; + + scannedNetwork.SetChannelMask(0); + scannedNetwork.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, + mIfName); + scannedNetwork.SetPath(path); + scannedNetwork.SetDestination(GetDBusInterfaceName()); + scannedNetwork.SetIface(WPANTUND_DBUS_APIv1_INTERFACE); + VerifyOrExit((ret = scannedNetwork.ProcessReply()) == 0); + + VerifyOrExit((mScannedNetworkCount = scannedNetwork.GetNetworksCount()) > 0, ret = kWpantundStatus_NetworkNotFound); - memcpy(gScanedNetowrks, scanNetwork.GetNetworks(), - gScannedNetworkCount * sizeof(Dbus::WpanNetworkInfo)); + memcpy(mScannedNetworks, scannedNetwork.GetNetworks(), + mScannedNetworkCount * sizeof(Dbus::WpanNetworkInfo)); exit: return ret; } const WpanNetworkInfo *WPANController::GetScanNetworksInfo(void) { - return gScanedNetowrks; + return mScannedNetworks; } int WPANController::GetScanNetworksInfoCount(void) { - return gScannedNetworkCount; + return mScannedNetworkCount; } -int WPANController::Start(void) +const char *WPANController::GetDBusInterfaceName(void) { - gInterfaceName = new DBusIfname(); - return gInterfaceName->ProcessReply(); + + DBusIfname dbusIfName; + int ret = kWpantundStatus_Ok; + + dbusIfName.SetInterfaceName(mIfName); + VerifyOrExit(dbusIfName.ProcessReply() == kWpantundStatus_Ok, ret = kWpantundStatus_InvalidDBusName); +exit: + return ret ? NULL : dbusIfName.GetDBusName(); } int WPANController::Leave(void) { DBusLeave leaveNetwork; - - leaveNetwork.SetDestination(gInterfaceName->GetDBusName()); + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; + + leaveNetwork.SetDestination(GetDBusInterfaceName()); + leaveNetwork.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, + mIfName); + leaveNetwork.SetPath(path); + leaveNetwork.SetIface(WPANTUND_DBUS_APIv1_INTERFACE); return leaveNetwork.ProcessReply(); } @@ -102,36 +116,48 @@ int WPANController::Form(const char *aNetworkname, int aChannel) { DBusForm formNetwork; int ret = 0; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; VerifyOrExit(aNetworkname != NULL, ret = kWpantundStatus_InvalidArgument); formNetwork.SetNetworkName(aNetworkname); VerifyOrExit((aChannel <= 26 && aChannel >= 11), ret = kWpantundStatus_InvalidArgument); formNetwork.SetChannelMask(aChannel); + formNetwork.SetInterfaceName(mIfName); formNetwork.SetNodeType(LEADER_ROLE); - formNetwork.SetDestination(gInterfaceName->GetDBusName()); + snprintf(path, sizeof(path), "%s/%s", WPAN_TUNNEL_DBUS_PATH, + mIfName); + formNetwork.SetPath(path); + formNetwork.SetDestination(GetDBusInterfaceName()); + formNetwork.SetIface(WPAN_TUNNEL_DBUS_INTERFACE); ret = formNetwork.ProcessReply(); exit: return ret; } -int WPANController::Join(char *aNetworkname, +int WPANController::Join(const char *aNetworkname, int16_t aChannel, uint64_t aExtPanId, uint16_t aPanId) { int ret = 0; DBusJoin joinNetwork; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; VerifyOrExit(aNetworkname != NULL, ret = kWpantundStatus_InvalidArgument); joinNetwork.SetNetworkName(aNetworkname); joinNetwork.SetNodeType(ROUTER_ROLE); + joinNetwork.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPAN_TUNNEL_DBUS_PATH, + mIfName); + joinNetwork.SetPath(path); VerifyOrExit((aChannel <= 26 && aChannel >= 11), ret = kWpantundStatus_InvalidArgument); joinNetwork.SetChannel(aChannel); VerifyOrExit(aExtPanId != 0, ret = kWpantundStatus_InvalidArgument); joinNetwork.SetExtPanId(aExtPanId); VerifyOrExit(aPanId != 0, ret = kWpantundStatus_InvalidArgument); joinNetwork.SetPanId(aPanId); - joinNetwork.SetDestination(gInterfaceName->GetDBusName()); + joinNetwork.SetDestination(GetDBusInterfaceName()); + joinNetwork.SetIface(WPAN_TUNNEL_DBUS_INTERFACE); ret = joinNetwork.ProcessReply(); exit: return ret; @@ -140,17 +166,22 @@ int WPANController::Join(char *aNetworkname, const char *WPANController::Get(const char *aPropertyName) { DBusGet getProp; - - int ret = kWpantundStatus_Ok; + int ret = kWpantundStatus_Ok; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; VerifyOrExit(aPropertyName != NULL, ret = kWpantundStatus_InvalidArgument); - getProp.SetDestination(gInterfaceName->GetDBusName()); + getProp.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, + mIfName); + getProp.SetIface(WPANTUND_DBUS_APIv1_INTERFACE); + getProp.SetPath(path); + getProp.SetDestination(GetDBusInterfaceName()); exit: if (ret != kWpantundStatus_Ok) { - syslog(LOG_ERR, "Property name is NULL; error: %d\n", ret); + syslog(LOG_ERR, "property name is NULL; error: %d\n", ret); } return ret ? "" : getProp.GetPropertyValue(aPropertyName); @@ -159,7 +190,8 @@ const char *WPANController::Get(const char *aPropertyName) int WPANController::Set(uint8_t aType, const char *aPropertyName, const char *aPropertyValue) { DBusSet setProp; - int ret = 0; + int ret = kWpantundStatus_Ok; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; VerifyOrExit((aType == kPropertyType_String || aType == kPropertyType_Data), ret = kWpantundStatus_InvalidArgument); @@ -168,7 +200,12 @@ int WPANController::Set(uint8_t aType, const char *aPropertyName, const char *aP setProp.SetPropertyName(aPropertyName); VerifyOrExit(aPropertyValue != NULL, ret = kWpantundStatus_InvalidArgument); setProp.SetPropertyValue(aPropertyValue); - setProp.SetDestination(gInterfaceName->GetDBusName()); + setProp.SetDestination(GetDBusInterfaceName()); + setProp.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, + mIfName); + setProp.SetPath(path); + setProp.SetIface(WPANTUND_DBUS_APIv1_INTERFACE); ret = setProp.ProcessReply(); exit: return ret; @@ -177,14 +214,20 @@ int WPANController::Set(uint8_t aType, const char *aPropertyName, const char *aP int WPANController::Gateway(const char *aPrefix, uint8_t aLength, bool aIsDefaultRoute) { DBusGateway gateway; - int ret = 0; + int ret = kWpantundStatus_Ok; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; gateway.SetDefaultRoute(aIsDefaultRoute); VerifyOrExit(aPrefix != NULL, ret = kWpantundStatus_InvalidArgument); gateway.SetPrefix(aPrefix); VerifyOrExit((aLength > 0 && aLength < 128), ret = kWpantundStatus_InvalidArgument); gateway.SetPrefixLength(aLength); - gateway.SetDestination(gInterfaceName->GetDBusName()); + gateway.SetDestination(GetDBusInterfaceName()); + gateway.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, + mIfName); + gateway.SetPath(path); + gateway.SetIface(WPANTUND_DBUS_APIv1_INTERFACE); ret = gateway.ProcessReply(); exit: return ret; @@ -193,7 +236,8 @@ int WPANController::Gateway(const char *aPrefix, uint8_t aLength, bool aIsDefaul int WPANController::RemoveGateway(const char *aPrefix, uint8_t aLength) { DBusGateway gateway; - int ret = 0; + int ret = kWpantundStatus_Ok; + char path[DBUS_MAXIMUM_NAME_LENGTH + 1]; gateway.SetDefaultRoute(true); VerifyOrExit(aPrefix != NULL, ret = kWpantundStatus_InvalidArgument); @@ -202,11 +246,21 @@ int WPANController::RemoveGateway(const char *aPrefix, uint8_t aLength) gateway.SetPrefixLength(aLength); gateway.SetValidLifeTime(0); gateway.SetPreferredLifetime(0); - gateway.SetDestination(gInterfaceName->GetDBusName()); + gateway.SetDestination(GetDBusInterfaceName()); + gateway.SetInterfaceName(mIfName); + snprintf(path, sizeof(path), "%s/%s", WPANTUND_DBUS_PATH, + mIfName); + gateway.SetPath(path); + gateway.SetIface(WPANTUND_DBUS_APIv1_INTERFACE); ret = gateway.ProcessReply(); exit: return ret; } +void WPANController::SetInterfaceName(const char *aIfName) +{ + strcpy(mIfName, aIfName); +} + } //namespace Dbus } //namespace ot diff --git a/src/web/wpan-controller/wpan_controller.hpp b/src/web/wpan-controller/wpan_controller.hpp index 290d9065d85..be8ef7293dc 100644 --- a/src/web/wpan-controller/wpan_controller.hpp +++ b/src/web/wpan-controller/wpan_controller.hpp @@ -37,6 +37,8 @@ #define WPAN_TUNNEL_DBUS_PATH "/com/nestlabs/WPANTunnelDriver" #define WPANTUND_DBUS_PATH "/org/wpantund" #define WPANTUND_DBUS_APIv1_INTERFACE "org.wpantund.v1" +#define WPAN_TUNNEL_DBUS_INTERFACE "com.nestlabs.WPANTunnelDriver" + #define ERRORCODE_UNKNOWN (4) #define SCANNED_NET_BUFFER_SIZE 250 #define SET_MAX_DATA_SIZE 250 @@ -126,6 +128,7 @@ enum kWpantundStatus_GetNullMessage = 41, kWpantundStatus_GetNullReply = 42, kWpantundStatus_GetNullPending = 43, + kWpantundStatus_InvalidDBusName = 44, }; struct WpanNetworkInfo @@ -143,21 +146,26 @@ struct WpanNetworkInfo class WPANController { public: - static const struct WpanNetworkInfo *GetScanNetworksInfo(void); - static int GetScanNetworksInfoCount(void); - static int Start(void); - static int Leave(void); - static int Form(const char *aNetworkname, int aChannel); - static int Scan(void); - static int Join(char *aNetworkname, int16_t aChannel, uint64_t aExtPanId, - uint16_t aPanId); - static const char *Get(const char *aPropertyName); - static int Set(uint8_t aType, const char *aPropertyName, const char *aPropertyValue); - static int Gateway(const char *aPrefix, uint8_t aLength, bool aIsDefaultRoute); - static int RemoveGateway(const char *aPrefix, uint8_t aLength); + const struct WpanNetworkInfo *GetScanNetworksInfo(void); + int GetScanNetworksInfoCount(void); + const char *GetDBusInterfaceName(void); + int Leave(void); + int Form(const char *aNetworkname, int aChannel); + int Scan(void); + int Join(const char *aNetworkname, int16_t aChannel, uint64_t aExtPanId, + uint16_t aPanId); + const char *Get(const char *aPropertyName); + int Set(uint8_t aType, const char *aPropertyName, const char *aPropertyValue); + int Gateway(const char *aPrefix, uint8_t aLength, bool aIsDefaultRoute); + int RemoveGateway(const char *aPrefix, uint8_t aLength); + void SetInterfaceName(const char *aIfName); private: + char mIfName[DBUS_MAXIMUM_NAME_LENGTH + 1]; + WpanNetworkInfo mScannedNetworks[SCANNED_NET_BUFFER_SIZE]; + int mScannedNetworkCount = 0; + }; } //namespace Dbus