Commit ddafd813 authored by gladish's avatar gladish

Added BridgeConfig (still unfinished)

parent 602ec99c
......@@ -15,6 +15,11 @@ namespace
std::string const kDeviceArriaveDeviceHandle = "Device_Handle";
std::string const kDeviceRemovalSignal = "Device_Removal";
std::string const kDeviceRemovalDeviceHandle = "Device_Handle";
inline Bridge::BridgeDeviceList::key_type GetKey(shared_ptr<Bridge::IAdapterDevice> const& dev)
{
return dev.get();
}
}
Bridge::DeviceSystemBridge::DeviceSystemBridge(shared_ptr<IAdapter> const& adapter)
......@@ -235,5 +240,33 @@ QStatus Bridge::DeviceSystemBridge::ShutdownInternal()
return ER_OK;
}
QStatus Bridge::DeviceSystemBridge::UpdateDevice(shared_ptr<IAdapterDevice> const& dev, bool exposedOnAllJoynBus)
{
QStatus st = ER_OK;
BridgeDeviceList::const_iterator itr = m_deviceList.find(GetKey(dev));
if (itr == m_deviceList.end() && exposedOnAllJoynBus)
{
st = CreateDevice(dev);
}
else if (itr != m_deviceList.end() && !exposedOnAllJoynBus)
{
m_deviceList.erase(itr);
if ((st = itr->second->Shutdown()) != ER_OK)
DSBLOG_WARN("failed to shutdown BridgeDevice: 0x%x", st);
}
return st;
}
QStatus Bridge::DeviceSystemBridge::CreateDevice(shared_ptr<IAdapterDevice> const& dev)
{
shared_ptr<BridgeDevice> newDevice(new BridgeDevice());
QStatus st = newDevice->Initialize(dev);
if (st == ER_OK)
m_deviceList.insert(std::make_pair(GetKey(dev), newDevice));
return st;
}
......@@ -13,7 +13,7 @@ namespace Bridge
class IAdapter;
class BridgeDevice;
typedef std::map<int, shared_ptr<BridgeDevice> > BridgeDeviceList;
typedef std::map<IAdapterDevice*, shared_ptr<BridgeDevice> > BridgeDeviceList;
class DeviceSystemBridge : public enable_shared_from_this<DeviceSystemBridge>
{
......
#include "BridgeConfig.h"
#include "Common/Log.h"
#include <string>
#include <sstream>
#include <strings.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xmlsave.h>
namespace
{
DSB_DECLARE_LOGNAME(BridgeConfig);
char const* kAlljoynObjectPathPrefix = "/BridgeConfig/AdapterDevices/Device[@Id=\"";
char const* kAlljoynObjectPathSuffix = "\"]";
char const* kVisibleAttribute = "Visible";
char const* kIdAttribute = "Id";
std::string MakePathId(std::string const& id)
{
std::stringstream buff;
buff << kAlljoynObjectPathPrefix;
buff << id;
buff << kAlljoynObjectPathSuffix;
return buff.str();
}
xmlChar* GetNodeAttribute(const xmlNodePtr node, char const* attr)
{
if (node)
{
xmlAttr* attrs = node->properties;
while (attrs && attrs->name && attrs->children)
{
if (strcmp(reinterpret_cast<char const *>(attrs->name), attr) == 0)
return xmlNodeListGetString(node->doc, attrs->children, 1);
attrs = attrs->next;
}
}
return NULL;
}
bool ToBoolean(std::string const& s)
{
return strcasecmp(s.c_str(), "true") == 0 ? true : false;
}
xmlNodePtr SelectSingleNode(xmlXPathContextPtr ctx, char const* xpath)
{
xmlNodePtr node = NULL;
DSBLOG_INFO("select: %s", xpath);
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(reinterpret_cast<xmlChar const *>(xpath), ctx);
if (xpathObj)
{
int n = 0;
if (xpathObj->nodesetval)
n = xpathObj->nodesetval->nodeNr;
if (n == 1)
node = xpathObj->nodesetval->nodeTab[0];
}
return node;
}
std::string GetElementText(const xmlDocPtr doc, char const* xpath)
{
xmlXPathContextPtr ctx = xmlXPathNewContext(doc);
if (!ctx)
return NULL;
std::string content;
xmlNodePtr node = SelectSingleNode(ctx, xpath);
if (node)
{
xmlChar* s = xmlNodeGetContent(node);
content = std::string(reinterpret_cast<char const *>(s));
xmlXPathFreeContext(ctx);
}
DSBLOG_INFO("%s == %s", xpath, content.c_str());
return content;
}
}
Bridge::BridgeConfig::BridgeConfig()
: m_doc(NULL)
{
}
Bridge::BridgeConfig::~BridgeConfig()
{
if (m_doc)
xmlFreeDoc(m_doc);
}
QStatus Bridge::BridgeConfig::FromFile(std::string const& fileName)
{
QStatus st = ER_OK;
m_fileName = fileName;
xmlInitParser();
xmlDocPtr doc = xmlParseFile(m_fileName.c_str());
if (!doc)
{
DSBLOG_WARN("failed to parse: %s", m_fileName.c_str());
st = ER_FAIL;
}
if (m_doc)
xmlFreeDoc(m_doc);
m_doc = doc;
return ER_OK;
}
QStatus Bridge::BridgeConfig::ToFile(std::string const& fileName)
{
if (!m_doc)
return ER_FAIL;
char const* outfile = m_fileName.c_str();
if (!fileName.empty())
outfile = fileName.c_str();
xmlSaveCtxtPtr ctx = xmlSaveToFilename(outfile, NULL, XML_SAVE_FORMAT);
xmlSaveDoc(ctx, m_doc);
xmlSaveClose(ctx);
return ER_OK;
}
QStatus Bridge::BridgeConfig::ToString(std::string& out)
{
xmlBufferPtr buff = xmlBufferCreate();
xmlSaveCtxtPtr ctx = xmlSaveToBuffer(buff, NULL, XML_SAVE_FORMAT);
xmlSaveDoc(ctx, m_doc);
xmlSaveClose(ctx);
out = std::string(reinterpret_cast<char const *>(buff->content));
xmlBufferFree(buff);
return ER_OK;
}
QStatus Bridge::BridgeConfig::FindObject(std::string const& id, DsbObjectConfig& obj)
{
if (!m_doc)
return ER_FAIL;
xmlXPathContextPtr ctx = xmlXPathNewContext(m_doc);
if (!ctx)
return ER_FAIL;
std::string path = MakePathId(id);
xmlNodePtr node = SelectSingleNode(ctx, path.c_str());
if (!node)
return ER_BUS_OBJ_NOT_FOUND;
obj.SetId(path);
xmlChar* attr = GetNodeAttribute(node, kVisibleAttribute);
if (attr)
{
bool b = strcasecmp(reinterpret_cast<char const *>(attr), "true") == 0
? true
: false;
obj.SetIsVisible(b);
xmlFree(attr);
}
xmlChar* desc = xmlNodeGetContent(node);
obj.SetDescription( std::string(reinterpret_cast<char const *>(desc)) );
xmlXPathFreeContext(ctx);
return ER_OK;
}
std::string Bridge::BridgeConfig::GetBridgeKeyX() const
{
return GetElementText(m_doc, "/BridgeConfig/Settings/Bridge/KEYX");
}
std::string Bridge::BridgeConfig::GetDeviceKeyX() const
{
return GetElementText(m_doc, "/BridgeConfig/Settings/Device/KEYX");
}
std::string Bridge::BridgeConfig::GetDeviceUsername() const
{
return GetElementText(m_doc, "/BridgeConfig/Settings/Device/USERNAME");
}
std::string Bridge::BridgeConfig::GetDevicePassword() const
{
return GetElementText(m_doc, "/BridgeConfig/Settings/Device/PASSWORD");
}
std::string Bridge::BridgeConfig::GetDeviceEcdheEcdsaPrivateKey() const
{
return GetElementText(m_doc, "/BridgeConfig/Settings/Device/ECDHEECDSAPRIVATEKEY");
}
std::string Bridge::BridgeConfig::GetDeviceEcdheEcdsaCertChain() const
{
return GetElementText(m_doc, "/BridgeConfig/Settings/Device/ECDHEECDSACERTCHAIN");
}
bool Bridge::BridgeConfig::GetDefaultVisibility() const
{
std::string s = GetElementText(m_doc, "/BridgeConfig/Settings/Device/DefaultVisibility");
return ToBoolean(s);
}
#pragma once
#include <string>
#include <alljoyn/Status.h>
struct _xmlDoc;
namespace Bridge
{
class BridgeConfig;
class DsbObjectConfig
{
friend class BridgeConfig;
public:
std::string GetId() const
{ return m_id; }
std::string GetDescription() const
{ return m_description; }
bool IsVisible() const
{ return m_isVisible; }
bool operator == (DsbObjectConfig const& rhs) const
{
return m_id == rhs.m_id && m_isVisible == rhs.m_isVisible &&
m_description == rhs.m_description;
}
bool operator != (DsbObjectConfig const& rhs) const
{
return m_id != rhs.m_id || m_isVisible != rhs.m_isVisible ||
m_description != rhs.m_description;
}
private:
void SetId(std::string const& id)
{ m_id = id; }
void SetDescription(std::string const& desc)
{ m_description = desc; }
void SetIsVisible(bool visible)
{ m_isVisible = visible; }
private:
std::string m_id;
bool m_isVisible;
std::string m_description;
};
class BridgeConfig
{
public:
BridgeConfig();
virtual ~BridgeConfig();
QStatus Initialize(std::string const& fileName, bool failIfNotPresent = false);
QStatus FromFile(std::string const& fileName);
QStatus ToFile(std::string const& fileName = "");
QStatus ToString(std::string& out);
QStatus FindObject(std::string const& id, DsbObjectConfig& obj);
QStatus AddObject(DsbObjectConfig& obj);
QStatus RemoveObject(std::string const& id);
std::string GetFilename() const
{ return m_fileName; }
std::string GetBridgeKeyX() const;
std::string GetDeviceKeyX() const;
std::string GetDeviceUsername() const;
std::string GetDevicePassword() const;
std::string GetDeviceEcdheEcdsaPrivateKey() const;
std::string GetDeviceEcdheEcdsaCertChain() const;
bool GetDefaultVisibility() const;
private:
QStatus InitXml();
QStatus AddTextNode(std::string const& nodeName, std::string const& text, void* parentNode);
QStatus ConvertXsBooleanToBool(std::string const& s, bool& val);
private:
std::string m_fileName;
_xmlDoc* m_doc;
};
}
#include "Bridge/BridgeDevice.h"
QStatus Bridge::BridgeDevice::Shutdown()
{
QStatus st = ER_OK;
return st;
}
QStatus Bridge::BridgeDevice::Initialize(shared_ptr<IAdapterDevice> const& dev)
{
QStatus st = ER_OK;
return st;
}
#pragma once
#include "Bridge/IAdapter.h"
namespace Bridge
{
class BridgeDevice
{
public:
QStatus Shutdown()
{
return ER_OK;
}
QStatus Shutdown();
QStatus Initialize(shared_ptr<IAdapterDevice> const& dev);
private:
};
}
#include "Bridge.h"
#include "IAdapter.h"
#include "Bridge/BridgeConfig.h"
#include <iostream>
int main(int /*argc*/, char* /*argv*/ [])
{
Bridge::BridgeConfig config;
config.FromFile("/Users/jgladi200/Work/DeviceSystemBridge/dsb/Samples/ZWaveAdapter/BridgeConfig.xml");
std::cout << config.GetBridgeKeyX() << std::endl;
std::cout << config.GetDeviceKeyX() << std::endl;
std::cout << config.GetDeviceUsername() << std::endl;
std::cout << config.GetDevicePassword() << std::endl;
std::cout << config.GetDeviceEcdheEcdsaPrivateKey() << std::endl;
std::cout << config.GetDeviceEcdheEcdsaCertChain() << std::endl;
std::cout << config.GetDefaultVisibility() << std::endl;
Bridge::DsbObjectConfig obj;
QStatus st = config.FindObject("/my/bus/object", obj);
std::cout << "Found:" << st << std::endl;
return 0;
}
......@@ -75,7 +75,7 @@ bool Common::Logger::IsLevelEnabled(char const* module, Level level)
if (itr != gLevelMap.end())
minLevel = itr->second;
return level >= itr->second;
return level >= minLevel;
}
void Common::Logger::VaLog(const char* module, Level level, const char* /*file*/, int /*line*/,
......
SRCS=DeviceProviders/AllJoynProvider.cpp \
DeviceProviders/AllJoynService.cpp \
Bridge/Bridge.cpp \
Bridge/BridgeConfig.cpp \
Bridge/BridgeDevice.cpp \
Bridge/main.cpp \
Common/Log.cpp
LIBXML_INC=/usr/include/libxml2
ALLJOYN_INSTALL_DIR=/Users/jgladi200/Work/alljoyn/alljoyn-15.04.00-src/build/darwin/x86/debug/dist/cpp
CXXFLAGS=-D QCC_OS_GROUP_POSIX -Wall -Wextra -g -std=c++0x -I. -I$(ALLJOYN_INSTALL_DIR)/inc
LDFLAGS=-L $(ALLJOYN_INSTALL_DIR)/lib -lalljoyn -lcrypto
CXXFLAGS=-D QCC_OS_GROUP_POSIX -Wall -Wextra -g -std=c++0x -I. -I$(ALLJOYN_INSTALL_DIR)/inc -I$(LIBXML_INC)
LDFLAGS=-L $(ALLJOYN_INSTALL_DIR)/lib -lalljoyn -lcrypto -lxml2
DEV_PROVIDER_OBJS=$(patsubst %.cpp, %.o,$(SRCS))
OBJS=$(DEV_PROVIDER_OBJS)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment