MockAdapter.cpp 7.87 KB
Newer Older
1 2 3 4 5
#include "MockAdapter.h"

#include "MockDevices.h"
#include "MockAdapterDevice.h"
#include "Common/Log.h"
gladish's avatar
gladish committed
6

7
#include <algorithm>
jacobgladish's avatar
jacobgladish committed
8 9
#include <sstream>
#include <vector>
10

gladish's avatar
gladish committed
11 12
namespace
{
13
  DSB_DECLARE_LOGNAME(MockAdapter);
gladish's avatar
gladish committed
14 15
  bridge::IAdapter::RegistrationHandle nextHandle = 0;
  bridge::IAdapter::RegistrationHandle GetNextRegistrationHandle()
gladish's avatar
gladish committed
16 17 18 19 20
  {
    return ++nextHandle;
  }
}

gladish's avatar
gladish committed
21
adapters::mock::MockAdapter::MockAdapter()
gladish's avatar
gladish committed
22
  : m_vendor("Acme")
23
  , m_adapterName("MockAdapter")
gladish's avatar
gladish committed
24 25
  , m_exposedAdapterPrefix("com." + m_vendor)
  , m_exposedApplicationName("DeviceSystemBridge")
26
  , m_exposedApplicationGuid(common::Guid::Parse("C27BC425-0058-4829-8775-441B5D8740C0"))
gladish's avatar
gladish committed
27 28 29 30
{
  // TODO: get m_exposedApplicatioName and Prefix from config
}

gladish's avatar
gladish committed
31
adapters::mock::MockAdapter::~MockAdapter()
gladish's avatar
gladish committed
32
{
33
  Shutdown();
gladish's avatar
gladish committed
34 35
}

gladish's avatar
gladish committed
36 37
std::string
adapters::mock::MockAdapter::GetVendor()
gladish's avatar
gladish committed
38 39 40 41
{
  return m_vendor;
}

gladish's avatar
gladish committed
42 43
std::string
adapters::mock::MockAdapter::GetAdapterName()
gladish's avatar
gladish committed
44 45 46 47
{
  return m_adapterName;
}

gladish's avatar
gladish committed
48 49
std::string
adapters::mock::MockAdapter::GetVersion()
gladish's avatar
gladish committed
50 51 52 53
{
  return m_version;
}

gladish's avatar
gladish committed
54 55
std::string
adapters::mock::MockAdapter::GetExposedAdapterPrefix()
gladish's avatar
gladish committed
56 57 58 59
{
  return m_exposedAdapterPrefix;
}

gladish's avatar
gladish committed
60 61
std::string
adapters::mock::MockAdapter::GetExposedApplicationName()
gladish's avatar
gladish committed
62 63 64 65
{
  return m_exposedApplicationName;
}

66
common::Guid
gladish's avatar
gladish committed
67
adapters::mock::MockAdapter::GetExposedApplicationGuid()
gladish's avatar
gladish committed
68 69 70 71
{
  return m_exposedApplicationGuid;
}

gladish's avatar
gladish committed
72
bridge::AdapterSignalVector adapters::mock::MockAdapter::GetSignals()
gladish's avatar
gladish committed
73
{
gladish's avatar
gladish committed
74
  return bridge::AdapterSignalVector();
gladish's avatar
gladish committed
75 76 77
}


78
bridge::AdapterStatus
79
adapters::mock::MockAdapter::Initialize(std::shared_ptr<bridge::IAdapterLog> const& log)
gladish's avatar
gladish committed
80
{
81 82
  CreateMockDevices();
  CreateSignals();
jacobgladish's avatar
jacobgladish committed
83
  m_log = log;
84
  return ER_OK;
gladish's avatar
gladish committed
85 86 87
}


88
bridge::AdapterStatus
gladish's avatar
gladish committed
89
adapters::mock::MockAdapter::Shutdown()
gladish's avatar
gladish committed
90
{
91 92 93 94 95 96 97 98 99 100 101
  m_vendor.clear();
  m_adapterName.clear();
  m_version.clear();
  m_exposedAdapterPrefix.clear();
  m_exposedApplicationName.clear();

  m_devices.clear();
  m_signals.clear();

  m_signalListeners.clear();
  return ER_OK;
gladish's avatar
gladish committed
102 103 104
}


105
bridge::AdapterStatus
gladish's avatar
gladish committed
106 107 108
adapters::mock::MockAdapter::EnumDevices(
    bridge::EnumDeviceOptions /*opts*/,
    bridge::AdapterDeviceVector& deviceList,
109
    std::shared_ptr<bridge::IAdapterIoRequest>* req)
gladish's avatar
gladish committed
110
{
111
  if (req)
Jacob Gladish's avatar
Jacob Gladish committed
112 113
    req->reset();
    
114 115 116 117 118

  deviceList.clear();
  std::copy(m_devices.begin(), m_devices.end(), std::back_inserter(deviceList));

  return ER_OK;
gladish's avatar
gladish committed
119 120 121
}


122
bridge::AdapterStatus
gladish's avatar
gladish committed
123
adapters::mock::MockAdapter::GetProperty(
124 125
    std::shared_ptr<bridge::IAdapterProperty>& prop,
    std::shared_ptr<bridge::IAdapterIoRequest>* req)
gladish's avatar
gladish committed
126
{
127
  if (req)
Jacob Gladish's avatar
Jacob Gladish committed
128
    req->reset();
129

130
  std::shared_ptr<MockAdapterProperty> mockAdapterProperty = std::dynamic_pointer_cast<MockAdapterProperty>(prop);
131 132 133
  if (!mockAdapterProperty)
    return ER_BAD_ARG_1;

134
  std::shared_ptr<MockAdapterDevice> device = mockAdapterProperty->GetParent();
135 136 137
  if (!device)
    return ER_INVALID_ADDRESS;

138
  std::shared_ptr<MockAdapter> adapter = device->GetParent();
139 140 141 142 143 144
  if (!adapter)
    return ER_INVALID_ADDRESS;

  // TODO: set prop with latest stuff
  
  return ER_OK;
gladish's avatar
gladish committed
145 146 147
}


148
bridge::AdapterStatus
gladish's avatar
gladish committed
149
adapters::mock::MockAdapter::SetProperty(
150 151
    std::shared_ptr<bridge::IAdapterProperty> const& prop,
    std::shared_ptr<bridge::IAdapterIoRequest>* req)
gladish's avatar
gladish committed
152
{
153
  if (req)
Jacob Gladish's avatar
Jacob Gladish committed
154
    req ->reset();
155

156
  std::shared_ptr<MockAdapterProperty> mockAdapterProperty = std::dynamic_pointer_cast<MockAdapterProperty>(prop);
157 158 159
  if (!mockAdapterProperty)
    return ER_BAD_ARG_1;

160
  std::shared_ptr<MockAdapterDevice> device = mockAdapterProperty->GetParent();
161 162 163
  if (!device)
    return ER_INVALID_ADDRESS;

164
  std::shared_ptr<MockAdapter> adapter = device->GetParent();
165 166 167 168 169 170
  if (!adapter)
    return ER_INVALID_ADDRESS;

  // TODO: set internal state from prop->GetData()
  
  return ER_OK;
gladish's avatar
gladish committed
171 172 173
}


174
bridge::AdapterStatus
gladish's avatar
gladish committed
175
adapters::mock::MockAdapter::GetPropertyValue(
176
    std::shared_ptr<bridge::IAdapterProperty> const& prop,
gladish's avatar
gladish committed
177
    std::string const& attributeName,
178 179
    std::shared_ptr<bridge::IAdapterValue>& value,
    std::shared_ptr<bridge::IAdapterIoRequest>* req)
gladish's avatar
gladish committed
180
{
181
  if (req)
Jacob Gladish's avatar
Jacob Gladish committed
182
    req->reset();
183

184
  std::shared_ptr<MockAdapterProperty> mockAdapterProperty = std::dynamic_pointer_cast<MockAdapterProperty>(prop);
185 186 187
  if (!mockAdapterProperty)
    return ER_BAD_ARG_1;

188
  std::shared_ptr<MockAdapterValue> attr = mockAdapterProperty->GetAttributeByName(attributeName);
189 190 191
  if (!attr)
    return ER_INVALID_ADDRESS;

192
  value = std::dynamic_pointer_cast<bridge::IAdapterValue>(attr);
193 194

  return ER_OK;
gladish's avatar
gladish committed
195 196 197
}


198
bridge::AdapterStatus
gladish's avatar
gladish committed
199
adapters::mock::MockAdapter::SetPropertyValue(
200 201 202
    std::shared_ptr<bridge::IAdapterProperty> const& prop,
    std::shared_ptr<bridge::IAdapterValue> const& value,
    std::shared_ptr<bridge::IAdapterIoRequest>* req)
gladish's avatar
gladish committed
203
{
204
  if (req)
Jacob Gladish's avatar
Jacob Gladish committed
205
    req->reset(); 
206

207
  std::shared_ptr<MockAdapterProperty> mockAdapterProperty = std::dynamic_pointer_cast<MockAdapterProperty>(prop);
208 209 210
  if (!mockAdapterProperty)
    return ER_BAD_ARG_1;

211
  std::shared_ptr<MockAdapterValue> attr = mockAdapterProperty->GetAttributeByName(value->GetName());
212 213 214 215 216 217
  if (!attr)
    return ER_INVALID_ADDRESS;

  attr->SetData(value->GetData());

  return ER_OK;
gladish's avatar
gladish committed
218 219 220
}


221
bridge::AdapterStatus
gladish's avatar
gladish committed
222
adapters::mock::MockAdapter::CallMethod(
223 224
    std::shared_ptr<bridge::IAdapterMethod>& method,
    std::shared_ptr<bridge::IAdapterIoRequest>* req)
gladish's avatar
gladish committed
225
{
226
  std::shared_ptr<MockAdapterMethod> m = std::dynamic_pointer_cast<MockAdapterMethod>(method);
227 228 229
  if (!m)
    return ER_BAD_ARG_1;

230
  std::shared_ptr<MockAdapterDevice> device = m->GetParent();
231 232 233 234
  if (!device)
    return ER_INVALID_ADDRESS;

  return device->DispatchMethod(m, req);
gladish's avatar
gladish committed
235 236 237
}


238
bridge::AdapterStatus
gladish's avatar
gladish committed
239
adapters::mock::MockAdapter::RegisterSignalListener(
gladish's avatar
gladish committed
240
    std::string const& signalName,
241
    std::shared_ptr<bridge::IAdapterSignalListener> const& listener,
gladish's avatar
gladish committed
242
    void* argp,
gladish's avatar
gladish committed
243
    bridge::IAdapter::RegistrationHandle& handle)
gladish's avatar
gladish committed
244
{
gladish's avatar
gladish committed
245 246 247 248 249 250 251
  RegisteredSignal reg;
  reg.Listener = listener;
  reg.Context = argp;
  reg.RegHandle = GetNextRegistrationHandle();
  m_signalListeners.insert(SignalMap::value_type(signalName, reg));
  handle = reg.RegHandle;
  return ER_OK;
gladish's avatar
gladish committed
252 253
}

254
bridge::AdapterStatus
gladish's avatar
gladish committed
255
adapters::mock::MockAdapter::UnregisterSignalListener(bridge::IAdapter::RegistrationHandle const& h)
gladish's avatar
gladish committed
256
{
257
  bridge::AdapterStatus st = ER_FAIL;
gladish's avatar
gladish committed
258 259 260 261 262 263 264 265 266 267 268
  for (SignalMap::iterator begin = m_signalListeners.begin(), end = m_signalListeners.end();
    begin != end; ++begin)
  {
    if (begin->second.RegHandle == h)
    {
      m_signalListeners.erase(begin);
      st = ER_OK;
      break;
    }
  }
  return st;
gladish's avatar
gladish committed
269 270
}

gladish's avatar
gladish committed
271 272
void
adapters::mock::MockAdapter::CreateMockDevices()
gladish's avatar
gladish committed
273
{
274
  std::shared_ptr<MockAdapter> self = std::dynamic_pointer_cast<MockAdapter>(shared_from_this());
275

gladish's avatar
gladish committed
276
  typedef std::vector<adapters::mock::MockDeviceDescriptor> vector;
gladish's avatar
gladish committed
277

gladish's avatar
gladish committed
278
  vector devices = adapters::mock::GetMockDevices();
gladish's avatar
gladish committed
279 280 281
  for (vector::const_iterator begin = devices.begin(), end = devices.end();
    begin != end; ++begin)
  {
282
    std::shared_ptr<MockAdapterDevice> dev = MockAdapterDevice::Create(*begin, self);
283
    m_devices.push_back(dev);
gladish's avatar
gladish committed
284 285 286
  }
}

gladish's avatar
gladish committed
287 288
void
adapters::mock::MockAdapter::CreateSignals()
gladish's avatar
gladish committed
289
{
290
  std::shared_ptr<MockAdapterDevice> parent;
291

gladish's avatar
gladish committed
292
  bridge::AdapterValueVector values;
293

294 295
  std::shared_ptr<MockAdapterValue> val(new MockAdapterValue(bridge::kDeviceArravalHandle));
  std::shared_ptr<MockAdapterSignal> signal(new MockAdapterSignal(bridge::kDeviceArrivalSignal,
296 297
    parent, values));
  m_signals.push_back(signal);
gladish's avatar
gladish committed
298 299
}

300
bridge::AdapterStatus
301
adapters::mock::MockAdapter::NotifySignalListeners(std::shared_ptr<MockAdapterSignal> const& signal)
gladish's avatar
gladish committed
302
{
303
  bridge::AdapterStatus st = ER_FAIL;
304 305
  if (!signal)
    return st;
gladish's avatar
gladish committed
306

307 308
  std::pair< SignalMap::iterator, SignalMap::iterator > range = 
    m_signalListeners.equal_range(signal->GetName());
gladish's avatar
gladish committed
309

310 311 312 313 314 315 316 317
  for (SignalMap::iterator begin = range.first, end = range.second; begin != end; ++begin)
  {
    RegisteredSignal& handler = begin->second;
    handler.Listener->AdapterSignalHandler(*signal, handler.Context);
  }

  return st;
}
gladish's avatar
gladish committed
318

jacobgladish's avatar
jacobgladish committed
319 320 321 322 323 324 325 326 327 328 329 330
std::string
adapters::mock::MockAdapter::GetStatusText(bridge::AdapterStatus st)
{
  if (st == 0)
    return std::string("ok");

  std::stringstream buff;
  buff << "MockError:";
  buff << static_cast<uint32_t>(st);
  return buff.str();
}