Commit a11b0011 authored by gladish's avatar gladish

Added Common::Variant, fixing error handling.

parent 3cf2e6e0
......@@ -64,7 +64,7 @@ Bridge::AdapterSignalVector AdapterLib::MockAdapter::GetSignals()
}
QStatus AdapterLib::MockAdapter::Initialize()
int32_t AdapterLib::MockAdapter::Initialize()
{
CreateMockDevices();
CreateSignals();
......@@ -72,13 +72,13 @@ QStatus AdapterLib::MockAdapter::Initialize()
}
QStatus AdapterLib::MockAdapter::Shutdown()
int32_t AdapterLib::MockAdapter::Shutdown()
{
return ER_NOT_IMPLEMENTED;
}
QStatus AdapterLib::MockAdapter::EnumDevices(
int32_t AdapterLib::MockAdapter::EnumDevices(
Bridge::EnumDeviceOptions /*opts*/,
Bridge::AdapterDeviceVector& deviceList,
shared_ptr<Bridge::IAdapterIoRequest>* req)
......@@ -93,7 +93,7 @@ QStatus AdapterLib::MockAdapter::EnumDevices(
}
QStatus AdapterLib::MockAdapter::GetProperty(
int32_t AdapterLib::MockAdapter::GetProperty(
shared_ptr<Bridge::IAdapterProperty>& prop,
shared_ptr<Bridge::IAdapterIoRequest>* req)
{
......@@ -118,7 +118,7 @@ QStatus AdapterLib::MockAdapter::GetProperty(
}
QStatus AdapterLib::MockAdapter::SetProperty(
int32_t AdapterLib::MockAdapter::SetProperty(
shared_ptr<Bridge::IAdapterProperty> const& prop,
shared_ptr<Bridge::IAdapterIoRequest>* req)
{
......@@ -143,7 +143,7 @@ QStatus AdapterLib::MockAdapter::SetProperty(
}
QStatus AdapterLib::MockAdapter::GetPropertyValue(
int32_t AdapterLib::MockAdapter::GetPropertyValue(
shared_ptr<Bridge::IAdapterProperty> const& prop,
std::string const& attributeName,
shared_ptr<Bridge::IAdapterValue>& value,
......@@ -166,7 +166,7 @@ QStatus AdapterLib::MockAdapter::GetPropertyValue(
}
QStatus AdapterLib::MockAdapter::SetPropertyValue(
int32_t AdapterLib::MockAdapter::SetPropertyValue(
shared_ptr<Bridge::IAdapterProperty> const& prop,
shared_ptr<Bridge::IAdapterValue> const& value,
shared_ptr<Bridge::IAdapterIoRequest>* req)
......@@ -188,7 +188,7 @@ QStatus AdapterLib::MockAdapter::SetPropertyValue(
}
QStatus AdapterLib::MockAdapter::CallMethod(
int32_t AdapterLib::MockAdapter::CallMethod(
shared_ptr<Bridge::IAdapterMethod>& method,
shared_ptr<Bridge::IAdapterIoRequest>* req)
{
......@@ -204,7 +204,7 @@ QStatus AdapterLib::MockAdapter::CallMethod(
}
QStatus AdapterLib::MockAdapter::RegisterSignalListener(
int32_t AdapterLib::MockAdapter::RegisterSignalListener(
std::string const& signalName,
shared_ptr<Bridge::IAdapterSignalListener> const& listener,
void* argp,
......@@ -219,9 +219,9 @@ QStatus AdapterLib::MockAdapter::RegisterSignalListener(
return ER_OK;
}
QStatus AdapterLib::MockAdapter::UnregisterSignalListener(Bridge::IAdapter::RegistrationHandle const& h)
int32_t AdapterLib::MockAdapter::UnregisterSignalListener(Bridge::IAdapter::RegistrationHandle const& h)
{
QStatus st = ER_FAIL;
int32_t st = ER_FAIL;
for (SignalMap::iterator begin = m_signalListeners.begin(), end = m_signalListeners.end();
begin != end; ++begin)
{
......@@ -262,9 +262,9 @@ void AdapterLib::MockAdapter::CreateSignals()
m_signals.push_back(signal);
}
QStatus AdapterLib::MockAdapter::NotifySignalListeners(shared_ptr<MockAdapterSignal> const& signal)
int32_t AdapterLib::MockAdapter::NotifySignalListeners(shared_ptr<MockAdapterSignal> const& signal)
{
QStatus st = ER_FAIL;
int32_t st = ER_FAIL;
if (!signal)
return st;
......
......@@ -24,46 +24,46 @@ namespace AdapterLib
virtual std::string GetExposedApplicationGuid();
virtual Bridge::AdapterSignalVector GetSignals();
virtual QStatus Initialize();
virtual QStatus Shutdown();
virtual int32_t Initialize();
virtual int32_t Shutdown();
virtual QStatus EnumDevices(
virtual int32_t EnumDevices(
Bridge::EnumDeviceOptions opts,
Bridge::AdapterDeviceVector& deviceList,
shared_ptr<Bridge::IAdapterIoRequest>* req);
virtual QStatus GetProperty(
virtual int32_t GetProperty(
shared_ptr<Bridge::IAdapterProperty>& prop,
shared_ptr<Bridge::IAdapterIoRequest>* req);
virtual QStatus SetProperty(
virtual int32_t SetProperty(
shared_ptr<Bridge::IAdapterProperty> const& prop,
shared_ptr<Bridge::IAdapterIoRequest>* req);
virtual QStatus GetPropertyValue(
virtual int32_t GetPropertyValue(
shared_ptr<Bridge::IAdapterProperty> const& prop,
std::string const& attributeName,
shared_ptr<Bridge::IAdapterValue>& value,
shared_ptr<Bridge::IAdapterIoRequest>* req);
virtual QStatus SetPropertyValue(
virtual int32_t SetPropertyValue(
shared_ptr<Bridge::IAdapterProperty> const& prop,
shared_ptr<Bridge::IAdapterValue> const& value,
shared_ptr<Bridge::IAdapterIoRequest>* req);
virtual QStatus CallMethod(
virtual int32_t CallMethod(
shared_ptr<Bridge::IAdapterMethod>& method,
shared_ptr<Bridge::IAdapterIoRequest>* req);
virtual QStatus RegisterSignalListener(
virtual int32_t RegisterSignalListener(
std::string const& signalName,
shared_ptr<Bridge::IAdapterSignalListener> const& listener,
void* argp,
Bridge::IAdapter::RegistrationHandle& handle);
virtual QStatus UnregisterSignalListener(Bridge::IAdapter::RegistrationHandle const& h);
virtual int32_t UnregisterSignalListener(Bridge::IAdapter::RegistrationHandle const& h);
QStatus NotifySignalListeners(shared_ptr<MockAdapterSignal> const& signal);
int32_t NotifySignalListeners(shared_ptr<MockAdapterSignal> const& signal);
private:
void CreateMockDevices();
......
......@@ -56,21 +56,21 @@ void MockAdapterDevice::CreateMethods()
m_methods.push_back(m);
}
QStatus MockAdapterDevice::DispatchMethod(
int32_t MockAdapterDevice::DispatchMethod(
shared_ptr<MockAdapterMethod>& method,
shared_ptr<Bridge::IAdapterIoRequest>* req)
{
if (req)
*req = NULL;
QStatus st = ER_OK;
int32_t st = ER_OK;
if (method->GetName() == kDeviceResetMethod)
st = Reset(method);
return st;
}
QStatus MockAdapterDevice::SendSignal(
int32_t MockAdapterDevice::SendSignal(
std::string const& signalName,
shared_ptr<MockAdapterProperty> const& prop,
shared_ptr<MockAdapterValue> const& value)
......@@ -80,7 +80,7 @@ QStatus MockAdapterDevice::SendSignal(
if (!value)
return ER_BAD_ARG_3;
QStatus st = ER_FAIL;
int32_t st = ER_FAIL;
shared_ptr<MockAdapterSignal> signal;
shared_ptr<MockAdapter> parent = m_parent.lock();
......@@ -110,7 +110,7 @@ QStatus MockAdapterDevice::SendSignal(
shared_ptr<MockAdapterDevice> self = shared_from_this();
shared_ptr<MockAdapterValue> p(new MockAdapterValue("PropertyName"));
p->SetData(ajn::MsgArg("s", prop->GetName().c_str(), NULL));
p->SetData(Common::Variant(prop->GetName()));
shared_ptr<MockAdapterValue> v(new MockAdapterValue("PropertyValue"));
v->SetData(value->GetData());
......@@ -125,9 +125,9 @@ QStatus MockAdapterDevice::SendSignal(
return st;
}
QStatus MockAdapterDevice::Reset(shared_ptr<MockAdapterMethod>& method)
int32_t MockAdapterDevice::Reset(shared_ptr<MockAdapterMethod>& method)
{
QStatus st = ER_OK;
int32_t st = ER_OK;
Bridge::AdapterValueVector const& inParams = method->GetInputParams();
if (inParams.size() == 0)
......@@ -277,12 +277,12 @@ void MockAdapterMethod::AddOutputParam(shared_ptr<Bridge::IAdapterValue> const&
m_outputParams.push_back(p);
}
QStatus MockAdapterMethod::GetResult()
int32_t MockAdapterMethod::GetResult()
{
return m_result;
}
void MockAdapterMethod::SetResult(QStatus st)
void MockAdapterMethod::SetResult(int32_t st)
{
m_result = st;
}
......@@ -298,14 +298,14 @@ std::string MockAdapterValue::GetName()
return m_name;
}
ajn::MsgArg MockAdapterValue::GetData()
Common::Variant MockAdapterValue::GetData()
{
return m_data;
}
void MockAdapterValue::SetData(ajn::MsgArg const& msg)
void MockAdapterValue::SetData(Common::Variant const& v)
{
m_data = msg;
m_data = v;
}
MockAdapterSignal::MockAdapterSignal(
......
......@@ -51,8 +51,8 @@ namespace AdapterLib
void AddInputParam(shared_ptr<Bridge::IAdapterValue> const& p);
void AddOutputParam(shared_ptr<Bridge::IAdapterValue> const& p);
virtual QStatus GetResult();
void SetResult(QStatus st);
virtual int32_t GetResult();
void SetResult(int32_t st);
private:
shared_ptr<MockAdapterDevice> GetParent() const
......@@ -64,7 +64,7 @@ namespace AdapterLib
Bridge::AdapterValueVector m_inputParams;
Bridge::AdapterValueVector m_outputParams;
weak_ptr<MockAdapterDevice> m_parent;
QStatus m_result;
int32_t m_result;
};
class MockAdapterProperty : public Bridge::IAdapterProperty
......@@ -92,12 +92,12 @@ namespace AdapterLib
MockAdapterValue(std::string const& name);
virtual std::string GetName();
virtual ajn::MsgArg GetData();
virtual void SetData(ajn::MsgArg const& msg);
virtual Common::Variant GetData();
virtual void SetData(Common::Variant const& v);
private:
std::string m_name;
ajn::MsgArg m_data;
Common::Variant m_data;
};
class MockAdapterDevice : public Bridge::IAdapterDevice, public enable_shared_from_this<MockAdapterDevice>
......@@ -119,11 +119,11 @@ namespace AdapterLib
virtual Bridge::AdapterMethodVector const& GetMethods() const;
virtual Bridge::AdapterSignalVector const& GetSignals() const;
QStatus DispatchMethod(
int32_t DispatchMethod(
shared_ptr<MockAdapterMethod>& method,
shared_ptr<Bridge::IAdapterIoRequest>* req);
QStatus SendSignal(
int32_t SendSignal(
std::string const& signalName,
shared_ptr<MockAdapterProperty> const& prop,
shared_ptr<MockAdapterValue> const& attr);
......@@ -137,7 +137,7 @@ namespace AdapterLib
void CreateMethods();
void CreateSignals();
QStatus Reset(shared_ptr<MockAdapterMethod>& method);
int32_t Reset(shared_ptr<MockAdapterMethod>& method);
private:
std::string m_name;
......
......@@ -129,7 +129,8 @@ QStatus Bridge::DeviceSystemBridge::InitializeAdapter()
DSBLOG_ERROR("can't initialize null adapter");
return ER_FAIL;
}
return m_adapter->Initialize();
int ret = m_adapter->Initialize();
return ret == 0 ? ER_OK : ER_FAIL;
}
QStatus Bridge::DeviceSystemBridge::InitializeDevices(bool update)
......@@ -141,18 +142,24 @@ QStatus Bridge::DeviceSystemBridge::InitializeDevices(bool update)
if (update)
opts = EnumDeviceOptions::ForceRefresh;
QStatus st = m_adapter->EnumDevices(opts, deviceList, &request);
int ret = m_adapter->EnumDevices(opts, deviceList, &request);
if (st != ER_OK)
if (ret != 0)
goto Leave;
for (AdapterDeviceVector::iterator itr = deviceList.begin(); itr != deviceList.end(); ++itr)
typedef AdapterDeviceVector::iterator iterator;
for (iterator begin = deviceList.begin(), end = deviceList.end(); begin != end; ++begin)
{
// TODO
// TODO: get configuration for device
if (update)
UpdateDevice(*begin, true);
// unfinished
}
Leave:
return st;
return ret == 0 ? ER_OK : ER_FAIL;
}
void Bridge::DeviceSystemBridge::OnAdapterSignal(IAdapterSignal const& signal, void* argp)
......@@ -192,7 +199,7 @@ Leave:
QStatus Bridge::DeviceSystemBridge::RegisterAdapterSignalHandlers(bool isRegister)
{
QStatus ret = ER_OK;
QStatus st = ER_OK;
shared_ptr<IAdapterSignalListener> listener = dynamic_pointer_cast<IAdapterSignalListener>(g_Instance);
......@@ -202,11 +209,11 @@ QStatus Bridge::DeviceSystemBridge::RegisterAdapterSignalHandlers(bool isRegiste
for (AdapterSignalVector::const_iterator itr = signals.begin(); itr != signals.end(); ++itr)
{
Bridge::IAdapter::RegistrationHandle handle;
QStatus st = m_adapter->RegisterSignalListener((*itr)->GetName(), listener, NULL, handle);
if (st != ER_OK)
int ret = m_adapter->RegisterSignalListener((*itr)->GetName(), listener, NULL, handle);
if (ret != 0)
{
DSBLOG_WARN("failed to register signal listener on adapter: 0x%x", st);
if (ret == ER_OK)
if (st == ER_OK)
ret = st;
}
else
......@@ -221,17 +228,17 @@ QStatus Bridge::DeviceSystemBridge::RegisterAdapterSignalHandlers(bool isRegiste
for (iterator begin = m_registeredSignalListeners.begin(), end = m_registeredSignalListeners.end();
begin != end; ++begin)
{
QStatus st = m_adapter->UnregisterSignalListener(*begin);
if (st != ER_OK)
int ret = m_adapter->UnregisterSignalListener(*begin);
if (ret != 0)
{
DSBLOG_WARN("failed to unregister signal listener on adapter: 0x%x", st);
if (ret == ER_OK)
ret = st;
if (st == ER_OK)
st = ER_FAIL;
}
}
}
return ret;
return st;
}
QStatus Bridge::DeviceSystemBridge::ShutdownInternal()
......
#pragma once
#include "Bridge/IAdapter.h"
#include <alljoyn/Status.h>
namespace Bridge
{
......
#pragma once
#include "Common/defines.h"
#include <alljoyn/MsgArg.h>
#include <alljoyn/Status.h>
#include "Common/Variant.h"
#include <string>
#include <vector>
......@@ -35,8 +33,8 @@ namespace Bridge
public:
virtual ~IAdapterValue() { }
virtual std::string GetName() = 0;
virtual ajn::MsgArg GetData() = 0;
virtual void SetData(ajn::MsgArg const& msg) = 0;
virtual Common::Variant GetData() = 0;
virtual void SetData(Common::Variant const& msg) = 0;
};
class IAdapterProperty
......@@ -60,7 +58,7 @@ namespace Bridge
virtual void SetInputParams(AdapterValueVector const& params) = 0;
virtual void SetOutputParams(AdapterValueVector const& params) = 0;
virtual QStatus GetResult() = 0;
virtual int32_t GetResult() = 0;
};
class IAdapterSignal
......@@ -98,10 +96,10 @@ namespace Bridge
class IAdapterIoRequest
{
public:
virtual QStatus Status() = 0;;
virtual QStatus Wait(uint32_t timeoutMillis) = 0;
virtual QStatus Cancel() = 0;
virtual QStatus Release() = 0;
virtual int32_t Status() = 0;;
virtual int32_t Wait(uint32_t timeoutMillis) = 0;
virtual int32_t Cancel() = 0;
virtual int32_t Release() = 0;
};
enum class EnumDeviceOptions
......@@ -126,44 +124,44 @@ namespace Bridge
virtual std::string GetExposedApplicationGuid() = 0;
virtual AdapterSignalVector GetSignals() = 0;
virtual QStatus Initialize() = 0;
virtual QStatus Shutdown() = 0;
virtual int32_t Initialize() = 0;
virtual int32_t Shutdown() = 0;
virtual QStatus EnumDevices(
virtual int32_t EnumDevices(
EnumDeviceOptions opts,
AdapterDeviceVector& deviceList,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual QStatus GetProperty(
virtual int32_t GetProperty(
shared_ptr<IAdapterProperty>& prop,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual QStatus SetProperty(
virtual int32_t SetProperty(
shared_ptr<IAdapterProperty> const& prop,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual QStatus GetPropertyValue(
virtual int32_t GetPropertyValue(
shared_ptr<IAdapterProperty> const& prop,
std::string const& attributeName,
shared_ptr<IAdapterValue>& value,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual QStatus SetPropertyValue(
virtual int32_t SetPropertyValue(
shared_ptr<IAdapterProperty> const& prop,
shared_ptr<IAdapterValue> const& value,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual QStatus CallMethod(
virtual int32_t CallMethod(
shared_ptr<IAdapterMethod>& method,
shared_ptr<IAdapterIoRequest>* req) = 0;
virtual QStatus RegisterSignalListener(
virtual int32_t RegisterSignalListener(
std::string const& signalName,
shared_ptr<IAdapterSignalListener> const& listener,
void* argp,
RegistrationHandle& handle) = 0;
virtual QStatus UnregisterSignalListener(RegistrationHandle const& h) = 0;
virtual int32_t UnregisterSignalListener(RegistrationHandle const& h) = 0;
};
}
#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <string>
namespace Common
{
class Variant
{
public:
enum class DataType
{
Invalid,
Boolean,
UInt8,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
Double,
String
};
Variant() : m_type(DataType::Invalid) { m_data.v_uint64 = 0; }
Variant(bool b) : m_type(DataType::Boolean) { m_data.v_bool = b; }
Variant(uint8_t y) : m_type(DataType::UInt8) { m_data.v_uint8 = y; }
Variant(int16_t n) : m_type(DataType::Int16) { m_data.v_int16 = n; }
Variant(uint16_t q) : m_type(DataType::UInt16) { m_data.v_uint16 = q; }
Variant(int32_t i) : m_type(DataType::Int32) { m_data.v_int32 = i; }
Variant(uint32_t u) : m_type(DataType::UInt32) { m_data.v_uint32 = u; }
Variant(int64_t x) : m_type(DataType::Int64) { m_data.v_int64 = x; }
Variant(uint64_t t) : m_type(DataType::UInt64) { m_data.v_uint64 = t; }
Variant(double d) : m_type(DataType::Double) { m_data.v_double = d; }
Variant(std::string const& s) : m_type(DataType::String)
{
if (s.size())
m_data.v_string = strdup(s.c_str());
else
m_data.v_string = NULL;
}
Variant(char const* s) : m_type(DataType::String)
{
if (s)
m_data.v_string = strdup(s);
}
Variant(Variant const& other)
{
AssignFrom(other);
}
Variant& operator=(Variant const& other)
{
if (&other != this)
{
AssignFrom(other);
}
return *this;
}
~Variant()
{
if (m_type == DataType::String && m_data.v_string)
free(m_data.v_string);
}
inline DataType GetType() const
{ return m_type; }
inline bool ToBool(bool* ok = NULL) const
{ return Get<bool>(DataType::Boolean, ok); }
inline uint8_t ToUInt8(bool* ok = NULL) const
{ return Get<uint8_t>(DataType::UInt8, ok); }
inline int16_t ToInt16(bool* ok = NULL) const
{ return Get<int16_t>(DataType::Int16, ok); }
inline uint16_t ToUInt16(bool* ok = NULL) const
{ return Get<uint16_t>(DataType::UInt16, ok); }
inline int32_t ToInt32(bool* ok = NULL) const
{ return Get<int32_t>(DataType::Int32, ok); }
inline uint32_t ToUInt32(bool* ok = NULL) const
{ return Get<uint32_t>(DataType::UInt32, ok); }
inline int64_t ToInt64(bool* ok = NULL) const
{ return Get<int64_t>(DataType::Int64, ok); }
inline uint64_t ToUInt64(bool* ok = NULL) const
{ return Get<uint64_t>(DataType::UInt64, ok); }
inline double ToDouble(bool* ok = NULL) const
{ return Get<uint16_t>(DataType::Double, ok); }
inline std::string ToString(bool* ok = NULL) const
{
// string handled special to allow for other data types
// to be converted later.
if (CanConvert(DataType::String))
{
if (ok)
*ok = true;
if (m_data.v_string)
return std::string(m_data.v_string);
return std::string();
}
if (ok)
*ok = false;
return std::string();
}
private:
inline bool CanConvert(DataType t) const
{
// no conversion for now
return m_type == t;
}
template<class T>
T Convert(DataType t) const
{
// no conversion for now
switch (t)
{
case DataType::Boolean: return m_data.v_bool;
case DataType::UInt8: return m_data.v_uint8;
case DataType::Int16: return m_data.v_int16;
case DataType::UInt16: return m_data.v_uint16;
case DataType::Int32: return m_data.v_int32;
case DataType::UInt32: return m_data.v_uint32;
case DataType::Int64: return m_data.v_int64;
case DataType::UInt64: return m_data.v_uint64;
case DataType::Double: return m_data.v_double;
default:
DSB_ASSERT(false);
break;