Commit 602ec99c authored by gladish's avatar gladish

Worked on Bridge class. Needs more work.

parent 3bcc517c
#include "Bridge.h"
#include "Common/Log.h"
#include "Bridge/BridgeDevice.h"
#include <alljoyn/Init.h>
namespace
{
DSB_DECLARE_LOGNAME(DeviceSystemBridge);
shared_ptr<Bridge::DeviceSystemBridge> g_Instance;
int32_t const kWaitTimeoutForAdapterOperation = 20000; //ms
std::string const kDeviceArrivalSignal = "Device_Arrival";
std::string const kDeviceArriaveDeviceHandle = "Device_Handle";
std::string const kDeviceRemovalSignal = "Device_Removal";
std::string const kDeviceRemovalDeviceHandle = "Device_Handle";
}
Bridge::DeviceSystemBridge::DeviceSystemBridge(shared_ptr<IAdapter> const& adapter)
: m_alljoynInitialized(false)
, m_adapter(adapter)
{
g_Instance = shared_from_this();
m_adapterSignaListener = shared_ptr<AdapterSignalListener>(new AdapterSignalListener(shared_from_this()));
}
Bridge::DeviceSystemBridge::~DeviceSystemBridge()
{
Shutdown();
m_adapter.reset();
}
QStatus Bridge::DeviceSystemBridge::Initialize()
{
QStatus st = ER_OK;
DSBLOG_DEBUG(__FUNCTION__);
if (!m_alljoynInitialized)
{
st = AllJoynInit();
if (st != ER_OK)
{
DSBLOG_WARN("Failed to initialize AllJoyn: 0x%x", st);
goto Leave;
}
}
st = InitializeInternal();
if (st != ER_OK)
goto Leave;
Leave:
if (st != ER_OK)
Shutdown();
return st;
}
QStatus Bridge::DeviceSystemBridge::InitializeInternal()
{
QStatus st = ER_OK;
// st = m_configManager.Initialize(this);
if (st != ER_OK)
goto Leave;
st = InitializeAdapter();
if (st != ER_OK)
{
DSBLOG_WARN("Failed to intialize adapter: 0x%x", st);
goto Leave;
}
st = InitializeDevices();
if (st != ER_OK)
{
DSBLOG_WARN("Failed to initialize devices: 0x%x", st);
goto Leave;
}
st = RegisterAdapterSignalHandlers(true);
if (st != ER_OK)
{
DSBLOG_WARN("Failed to register adapter signal handlers: 0x%x", st);
}
Leave:
return st;
}
QStatus Bridge::DeviceSystemBridge::Shutdown()
{
QStatus st = ER_OK;
st = ShutdownInternal();
if (st != ER_OK)
{
DSBLOG_WARN("failed to shutdown internal: 0x%x", st);
return st;
}
if (m_alljoynInitialized)
{
AllJoynShutdown();
m_alljoynInitialized = false;
}
return st;
}
QStatus Bridge::DeviceSystemBridge::InitializeAdapter()
{
if (!m_adapter)
{
DSBLOG_ERROR("can't initialize null adapter");
return ER_FAIL;
}
return m_adapter->Initialize();
}
QStatus Bridge::DeviceSystemBridge::InitializeDevices(bool isUpdate)
{
AdapterDeviceVector deviceList;
shared_ptr<IAdapterIoRequest> request;
EnumDeviceOptions opts = EnumDeviceOptions::CacheOnly;
if (isUpdate)
opts = EnumDeviceOptions::ForceRefresh;
QStatus st = m_adapter->EnumDevices(opts, deviceList, &request);
if (st == ER_OK)
{
if ((st = request->Wait(kWaitTimeoutForAdapterOperation)) != ER_OK)
DSBLOG_WARN("timeout waiting for EnumDevices to complete");
}
if (st != ER_OK)
goto Leave;
for (AdapterDeviceVector::iterator itr = deviceList.begin(); itr != deviceList.end(); ++itr)
{
// TODO
}
Leave:
return st;
}
void Bridge::DeviceSystemBridge::OnAdapterSignal(IAdapterSignal const& signal, void* argp)
{
// TODO
shared_ptr<IAdapterDevice> adapterDevice;
std::string const name = signal.GetName();
if (name == kDeviceArrivalSignal || name ==kDeviceArriaveDeviceHandle)
{
AdapterValueVector params = signal.GetParams();
for (AdapterValueVector::const_iterator itr = params.begin(); itr != params.end(); ++itr)
{
if ((*itr)->GetName() == kDeviceArriaveDeviceHandle)
{
// TODO: figure out where this signal is generated and figure out how we're
// going to put an arbitrary pointer inside.
//
// adapterDevice = param->GetData();
}
if (!adapterDevice)
goto Leave;
}
}
if (name == kDeviceArrivalSignal)
{
}
else if (name == kDeviceRemovalSignal)
{
UpdateDevice(adapterDevice, false);
}
Leave:
return;
}
QStatus Bridge::DeviceSystemBridge::RegisterAdapterSignalHandlers(bool isRegister)
{
QStatus ret = ER_OK;
shared_ptr<IAdapterSignalListener> listener =
dynamic_pointer_cast<IAdapterSignalListener>(g_Instance);
AdapterSignalVector signals = m_adapter->GetSignals();
for (AdapterSignalVector::const_iterator itr = signals.begin(); itr != signals.end(); ++itr)
{
if (isRegister)
{
QStatus st = m_adapter->RegisterSignalListener(*itr, listener, NULL);
if (st != ER_OK)
{
DSBLOG_WARN("failed to register signal listener on adapter: 0x%x", st);
if (ret == ER_OK)
ret = st;
}
}
else
{
QStatus st = m_adapter->UnregisterSignalListener(*itr, listener);
if (st != ER_OK)
{
DSBLOG_WARN("failed to unregister signal listener on adapter: 0x%x", st);
if (ret == ER_OK)
ret = st;
}
}
}
return ret;
}
QStatus Bridge::DeviceSystemBridge::ShutdownInternal()
{
// m_configManager.Shutdown();
if (m_adapter)
{
RegisterAdapterSignalHandlers(false);
m_adapter->Shutdown();
}
for (BridgeDeviceList::iterator itr = m_deviceList.begin(); itr != m_deviceList.end(); ++itr)
itr->second->Shutdown();
m_deviceList.clear();
return ER_OK;
}
#pragma once
#include "Common/defines.h"
#include "Bridge/IAdapter.h"
#include <alljoyn/Status.h>
#include <map>
#include <string>
namespace Bridge
{
class DeviceSystemBridge
class IAdapter;
class BridgeDevice;
typedef std::map<int, shared_ptr<BridgeDevice> > BridgeDeviceList;
class DeviceSystemBridge : public enable_shared_from_this<DeviceSystemBridge>
{
public:
static DeviceSystemBridge& Instance();
// TODO: really should be scoped_ptr as onwership is transferred
DeviceSystemBridge(shared_ptr<IAdapter> const& adapter);
virtual ~DeviceSystemBridge();
QStatus Initialize();
QStatus Shutdown();
inline shared_ptr<IAdapter> GetAdapter() const
{ return m_adapter; }
//inline ConfigManager *GetConfigManager()
// { return &m_configManager; }
QStatus InitializeDevices(bool isUpdate = false);
inline BridgeDeviceList GetDeviceList() const
{ return m_deviceList; }
private:
QStatus RegisterAdapterSignalHandlers(bool isRegister);
QStatus InitializeAdapter();
QStatus InitializeInternal();
QStatus ShutdownInternal();
QStatus CreateDevice(shared_ptr<IAdapterDevice> const& device);
QStatus UpdateDevice(shared_ptr<IAdapterDevice> const& device, bool exposedOnAllJoynBus);
void OnAdapterSignal(IAdapterSignal const& signal, void* argp);
private:
class AdapterSignalListener : public IAdapterSignalListener
{
public:
AdapterSignalListener(shared_ptr<DeviceSystemBridge> const& parent)
: m_parent(parent) { }
virtual void AdapterSignalHandler(IAdapterSignal const& signal, void* argp)
{
shared_ptr<DeviceSystemBridge> dsb = m_parent.lock();
if (dsb)
dsb->OnAdapterSignal(signal, argp);
}
private:
weak_ptr<DeviceSystemBridge> m_parent;
};
private:
bool m_alljoynInitialized;
shared_ptr<IAdapter> m_adapter;
BridgeDeviceList m_deviceList;
shared_ptr<AdapterSignalListener> m_adapterSignaListener;
};
}
#pragma once
namespace Bridge
{
class BridgeDevice
{
public:
QStatus Shutdown()
{
return ER_OK;
}
};
}
#pragma once
#include "defines.h"
#include "Common/defines.h"
#include <alljoyn/MsgArg.h>
#include <alljoyn/Status.h>
#include <string>
#include <vector>
......@@ -23,6 +25,7 @@ namespace Bridge
class IAdapterValue
{
public:
virtual ~IAdapterValue() { }
virtual std::string GetName() = 0;
virtual ajn::MsgArg GetData() = 0;
virtual void SetData(ajn::MsgArg const& msg) = 0;
......@@ -31,6 +34,7 @@ namespace Bridge
class IAdapterProperty
{
public:
virtual ~IAdapterProperty() { }
virtual std::string GetName() = 0;
virtual AdapterValueVector GetAttributes() = 0;
};
......@@ -38,6 +42,7 @@ namespace Bridge
class IAdapterMethod
{
public:
virtual ~IAdapterMethod() { }
virtual std::string GetName() = 0;
virtual std::string GetDescription() = 0;
......@@ -53,19 +58,22 @@ namespace Bridge
class IAdapterSignal
{
public:
virtual std::string GetName() = 0;
virtual AdapterValueVector GetParams() = 0;
virtual ~IAdapterSignal() { }
virtual std::string GetName() const = 0;
virtual AdapterValueVector GetParams() const = 0;
};
class IAdapterSignalListener
{
public:
virtual ~IAdapterSignalListener() { }
virtual void AdapterSignalHandler(IAdapterSignal const& signal, void* argp) = 0;
};
class IAdapterDevice
{
public:
virtual ~IAdapterDevice() { }
virtual std::string GetName() = 0;
virtual std::string GetVendor() = 0;
virtual std::string GetModel() = 0;
......@@ -82,10 +90,10 @@ namespace Bridge
class IAdapterIoRequest
{
public:
virtual uint32_t Status() = 0;;
virtual uint32_t Wait(uint32_t timeoutMillis) = 0;
virtual uint32_t Cancel() = 0;
virtual uint32_t Release() = 0;
virtual QStatus Status() = 0;;
virtual QStatus Wait(uint32_t timeoutMillis) = 0;
virtual QStatus Cancel() = 0;
virtual QStatus Release() = 0;
};
enum class EnumDeviceOptions
......@@ -97,6 +105,7 @@ namespace Bridge
class IAdapter
{
public:
virtual ~IAdapter() { }
virtual std::string GetVendor() = 0;
virtual std::string GetAdapterName() = 0;
virtual std::string GetVersion() = 0;
......@@ -105,46 +114,45 @@ namespace Bridge
virtual std::string GetExposedApplicationGuid() = 0;
virtual AdapterSignalVector GetSignals() = 0;
virtual uint32_t Initialize() = 0;
virtual uint32_t Shutdown();
virtual QStatus Initialize() = 0;
virtual QStatus Shutdown();
virtual uint32_t EnumDevices(
virtual QStatus EnumDevices(
EnumDeviceOptions opts,
shared_ptr<AdapterDeviceVector>& deviceList,
AdapterDeviceVector& deviceList,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual uint32_t GetProperty(
virtual QStatus GetProperty(
shared_ptr<IAdapterProperty>& prop,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual uint32_t SetProperty(
virtual QStatus SetProperty(
shared_ptr<IAdapterProperty> const& prop,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual uint32_t GetPropertyValue(
virtual QStatus GetPropertyValue(
shared_ptr<IAdapterProperty> const& prop,
std::string const& attributeName,
shared_ptr<IAdapterValue>& value,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual uint32_t SetPropertyValue(
virtual QStatus SetPropertyValue(
shared_ptr<IAdapterProperty> const& prop,
shared_ptr<IAdapterValue> const& value,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual uint32_t CallMethod(
virtual QStatus CallMethod(
shared_ptr<IAdapterMethod>& method,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual uint32_t RegisterSignalListener(
virtual QStatus RegisterSignalListener(
shared_ptr<IAdapterSignal> const& signal,
shared_ptr<IAdapterSignalListener> const& listener,
void* argp) = 0;
virtual uint32_t UnregisterSignalListener(
virtual QStatus UnregisterSignalListener(
shared_ptr<IAdapterSignal> const& signal,
shared_ptr<IAdapterSignalListener> const& listener) = 0;
};
}
#include "Common/Log.h"
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <map>
namespace
{
FILE* gLog = stdout;
struct StringCompare
{
bool operator()(char const* s, char const* t) const
{ return strcmp(s, t) < 0; }
};
char const* LevelToString(Common::Logger::Level l)
{
char const* s = "unknown";
switch (l)
{
case Common::Logger::DSB_LOGLEVEL_OFF:
s = "OFF";
break;
case Common::Logger::DSB_LOGLEVEL_DEBUG:
s = "DEBUG";
break;
case Common::Logger::DSB_LOGLEVEL_INFO:
s = "INFO";
break;
case Common::Logger::DSB_LOGLEVEL_WARN:
s = "WARN";
break;
case Common::Logger::DSB_LOGLEVEL_ERROR:
s = "ERROR";
break;
default:
break;
}
return s;
}
typedef std::map<char const*, Common::Logger::Level, StringCompare> LevelMap;
LevelMap gLevelMap;
Common::Logger::Level sDefaultLoggingLevel = Common::Logger::DSB_LOGLEVEL_INFO;
#ifdef __APPLE__
#define ThreadId_FMT "%p"
pthread_t GetCurrentThreadId() { return pthread_self(); }
#else
#define ThreadId_FMT "%d"
int32_t GetCurrentThreadId() { return syscall(SYS_gettid); }
#endif
}
void Common::Logger::Write(char const* module, Level level, char const* file,
int line, char const* format, ...)
{
va_list args;
va_start(args, format);
VaLog(module, level, file, line, format, args);
va_end(args);
}
bool Common::Logger::IsLevelEnabled(char const* module, Level level)
{
Common::Logger::Level minLevel = sDefaultLoggingLevel;
LevelMap::const_iterator itr = gLevelMap.find(module);
if (itr != gLevelMap.end())
minLevel = itr->second;
return level >= itr->second;
}
void Common::Logger::VaLog(const char* module, Level level, const char* /*file*/, int /*line*/,
const char* format, va_list args)
{
struct tm result;
time_t const clock = time(0);
localtime_r(&clock, &result);
char buff[64];
size_t n = strftime(buff, sizeof(buff) - 1, "%Y-%m-%dT%T", &result);
if (n > 0)
buff[n] = '\0';
fprintf(gLog, "%s (%5s) Thread-" ThreadId_FMT " [%s] - ", buff, LevelToString(level), GetCurrentThreadId(), module);
vfprintf(gLog, format, args);
fprintf(gLog, "\n");
}
#pragma once
#if defined(Q_CC_GNU)
#define PRINTF_FORMAT(IDX, FIRST) __attribute__ ((format (printf, IDX, FIRST)))
#else
#define PRINTF_FORMAT(IDX, FIRST)
#endif
#include <stdarg.h>
namespace Common
{
class Logger
{
public:
enum Level
{
DSB_LOGLEVEL_OFF = -1,
DSB_LOGLEVEL_DEBUG = 1,
DSB_LOGLEVEL_INFO = 2,
DSB_LOGLEVEL_WARN = 3,
DSB_LOGLEVEL_ERROR = 4,
DSB_LOGLEVEL_FATAL = 5
};
static void Write(char const* module, Level level, const char* file,
int line, const char* format, ...) PRINTF_FORMAT(6, 7);
static bool IsLevelEnabled(const char* module, Level level);
private:
static void VaLog(const char* module, Level level, const char* file, int line, const char* format, va_list args);
};
}
#define DSB_DECLARE_LOGNAME(LOGNAME) char const __dsb_logger_module_name__[] = { #LOGNAME };
#define DSBLOG_WITH_LEVEL(LEVEL, FORMAT, ...) \
do { if (Common::Logger::IsLevelEnabled(__dsb_logger_module_name__, LEVEL)) { \
Common::Logger::Write(__dsb_logger_module_name__, LEVEL, __FILE__, __LINE__ - 2, FORMAT, ##__VA_ARGS__); \
} } while (0)
#define DSBLOG(LEVEL, NAME, FORMAT, ...) \
do { if (Common::Logger::IsLevelEnabled(NAME, Common::Logger::LEVEL)) { \
Common::Logger::Write(NAME, Common::Logger::LEVEL, __FILE__, __LINE__ - 2, FORMAT, ##__VA_ARGS__); \
} } while (0)
#define DSBLOG_DEBUG(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_DEBUG, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_INFO(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_INFO, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_WARN(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_WARN, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_ERROR(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_ERROR, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
......@@ -5,11 +5,13 @@
using std::shared_ptr;
using std::weak_ptr;
using std::enable_shared_from_this;
using std::dynamic_pointer_cast;
#else
#include <tr1/memory>
using std::tr1::shared_ptr;
using std::tr1::weak_ptr;
using std::tr1::enable_shared_from_this;
using std::tr1::dynamic_pointer_cast;
#endif
class AllJoynBusObject;
......
#pragma once
#include "defines.h"
#include "AllJoynStatus.h"
#include "Common/defines.h"
#include "DeviceProviders/AllJoynStatus.h"
#include <alljoyn/AboutListener.h>
#include <map>
......
#pragma once
#include "defines.h"
#include "Common/defines.h"
#include <alljoyn/BusAttachment.h>