IAdapter.h 9.56 KB
Newer Older
jgladish's avatar
jgladish committed
1 2
#pragma once

3
#include "Common/Guid.h"
4
#include "Common/Variant.h"
jgladish's avatar
jgladish committed
5

6
#include <condition_variable>
7
#include <map>
8
#include <memory>
9
#include <mutex>
jgladish's avatar
jgladish committed
10 11 12
#include <string>
#include <vector>

gladish's avatar
gladish committed
13
namespace bridge
jgladish's avatar
jgladish committed
14
{
15 16 17 18 19 20 21
  typedef uint32_t AdapterStatus;

  inline bool IsOk(AdapterStatus st)
  {
    return st == 0;
  }

22 23 24 25 26 27 28 29
  std::string const kDeviceArrivalSignal = "Device_Arrival";
  std::string const kDeviceArravalHandle = "Device_Handle";
  std::string const kDeviceRemovalSignal = "Device_Removal";
  std::string const kDeviceRemovalDeviceHandle = "Device_Handle";
  std::string const kChangeOfValueSignal = "Change_Of_Value";
  std::string const kCovPropertyHandle = "Property_Handle";
  std::string const kCovAttributeHandle = "Attribute_Handle";

gladish's avatar
gladish committed
30 31 32
  std::string const kDsbDeviceNotificationSignal = "DeviceNotificationSignal";
  std::string const kDsbSendMsgToDeviceMethod = "SendMessageToDeviceSynchronous";

jgladish's avatar
jgladish committed
33 34 35 36 37
  class IAdapterValue;
  class IAdapterProperty;
  class IAdapterMethod;
  class IAdapterSignal;
  class IAdapterDevice;
jacobgladish's avatar
jacobgladish committed
38
  class IAdapterLog;
jgladish's avatar
jgladish committed
39

40 41 42 43 44
  typedef std::vector< std::shared_ptr<IAdapterValue> >    AdapterValueVector;
  typedef std::vector< std::shared_ptr<IAdapterProperty> > AdapterPropertyVector;
  typedef std::vector< std::shared_ptr<IAdapterMethod> >   AdapterMethodVector;
  typedef std::vector< std::shared_ptr<IAdapterSignal> >   AdapterSignalVector;
  typedef std::vector< std::shared_ptr<IAdapterDevice> >   AdapterDeviceVector;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  typedef std::map< std::string, std::string>         AnnotationMap;

  enum class AccessType
  {
    Read,
    Write,
    ReadWrite
  };

  enum class SignalBehavior
  {
    Unspecified,
    Never,
    Always,
    AlwaysWithNoValue
  };

jacobgladish's avatar
jacobgladish committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  enum class AdapterLogLevel
  {
    Off = -1,
    Debug = 1,
    Info = 2,
    Warn = 3,
    Error = 4,
    Fatal = 5
  };

  class IAdapterLog
  {
  public:
    virtual void Write(AdapterLogLevel level, char const* file, int line, char const* format, ...) = 0;
    virtual bool IsLevelEnabled(AdapterLogLevel level) = 0;
  };

79 80 81
  class IAdapterAttribute
  {
  public:
82
    virtual std::shared_ptr<IAdapterValue> GetValue() = 0;
83 84 85 86
    virtual AnnotationMap GetAnnotations() = 0;
    virtual AccessType GetAccess() = 0;
    virtual SignalBehavior GetChangeOfValueBehavior() = 0;
  };
jgladish's avatar
jgladish committed
87 88 89 90

  class IAdapterValue
  {
  public:
91
    virtual ~IAdapterValue() { }
jgladish's avatar
jgladish committed
92
    virtual std::string GetName() = 0;
93
    virtual common::Variant const& GetData() const = 0;
gladish's avatar
gladish committed
94
    virtual void SetData(common::Variant const& msg) = 0;
jgladish's avatar
jgladish committed
95 96 97 98 99
  };

  class IAdapterProperty
  {
  public:
100
    virtual ~IAdapterProperty() { }
jgladish's avatar
jgladish committed
101
    virtual std::string GetName() = 0;
102
    virtual std::string GetInterfaceHint() = 0;
jgladish's avatar
jgladish committed
103 104 105 106 107 108
    virtual AdapterValueVector GetAttributes() = 0;
  };

  class IAdapterMethod
  {
  public:
109
    virtual ~IAdapterMethod() { }
110
    virtual std::string GetName() const = 0;
jgladish's avatar
jgladish committed
111 112
    virtual std::string GetDescription() = 0;

113 114
    virtual AdapterValueVector const& GetInputParams() const = 0;
    virtual AdapterValueVector const& GetOutputParams() const = 0;
jgladish's avatar
jgladish committed
115 116 117 118

    virtual void SetInputParams(AdapterValueVector const& params) = 0;
    virtual void SetOutputParams(AdapterValueVector const& params) = 0;

119
    virtual int32_t GetResult() = 0;
jgladish's avatar
jgladish committed
120 121 122 123 124
  };

  class IAdapterSignal
  {
  public:
125 126
    virtual ~IAdapterSignal() { }
    virtual std::string GetName() const = 0;
127
    virtual AdapterValueVector const& GetParams() const = 0;
jgladish's avatar
jgladish committed
128 129 130 131 132
  };

  class IAdapterSignalListener
  {
  public:
133
    virtual ~IAdapterSignalListener() { }
jgladish's avatar
jgladish committed
134 135 136
    virtual void AdapterSignalHandler(IAdapterSignal const& signal, void* argp) = 0;
  };

137 138 139 140 141 142 143 144
  class IAdapterIcon
  {
  public:
    virtual std::vector<uint8_t> GetImage() = 0;
    virtual std::string GetMimeType() = 0;
    virtual std::string GetUrl() = 0;
  };

jgladish's avatar
jgladish committed
145 146 147
  class IAdapterDevice
  {
  public:
148
    virtual ~IAdapterDevice() { }
jgladish's avatar
jgladish committed
149 150 151 152 153 154 155
    virtual std::string GetName() = 0;
    virtual std::string GetVendor() = 0;
    virtual std::string GetModel() = 0;
    virtual std::string GetVersion() = 0;
    virtual std::string GetFirmwareVersion() = 0;
    virtual std::string GetSerialNumber() = 0;
    virtual std::string GetDescription() = 0;
156
    virtual std::shared_ptr<IAdapterIcon> GetIcon() = 0;
jgladish's avatar
jgladish committed
157

158 159 160
    virtual AdapterPropertyVector const& GetProperties() const = 0;
    virtual AdapterMethodVector const& GetMethods() const = 0;
    virtual AdapterSignalVector const& GetSignals() const = 0;
jgladish's avatar
jgladish committed
161 162 163 164 165
  };

  class IAdapterIoRequest
  {
  public:
166 167 168
    virtual ~IAdapterIoRequest() { }
    virtual int32_t GetStatus() = 0;;
    virtual int32_t Wait(uint32_t timeoutMillis = std::numeric_limits<uint32_t>::max()) = 0;
169
    virtual int32_t Cancel() = 0;
jgladish's avatar
jgladish committed
170 171 172 173 174 175 176 177 178 179
  };

  enum class EnumDeviceOptions
  {
    CacheOnly = 0,
    ForceRefresh = 1
  };

  class IAdapter
  {
gladish's avatar
gladish committed
180 181
  protected:
    IAdapter() { }
jgladish's avatar
jgladish committed
182
  public:
183

gladish's avatar
gladish committed
184 185
    typedef int32_t RegistrationHandle;

186
    virtual ~IAdapter() { }
jgladish's avatar
jgladish committed
187 188 189 190 191
    virtual std::string GetVendor() = 0;
    virtual std::string GetAdapterName() = 0;
    virtual std::string GetVersion() = 0;
    virtual std::string GetExposedAdapterPrefix() = 0;
    virtual std::string GetExposedApplicationName() = 0;
192
    virtual common::Guid GetExposedApplicationGuid() = 0;
jgladish's avatar
jgladish committed
193 194
    virtual AdapterSignalVector GetSignals() = 0;

195 196
    virtual AdapterStatus SetConfiguration(std::vector<uint8_t> const& configData) = 0;
    virtual AdapterStatus GetConfiguration(std::vector<uint8_t>* configData) = 0;
197

198 199
    virtual AdapterStatus Initialize(std::shared_ptr<IAdapterLog> const& log) = 0;
    virtual AdapterStatus Shutdown() = 0;
jgladish's avatar
jgladish committed
200

201
    virtual AdapterStatus EnumDevices(
jgladish's avatar
jgladish committed
202
      EnumDeviceOptions opts,
203
      AdapterDeviceVector& deviceList,
204
      std::shared_ptr<IAdapterIoRequest>* req) = 0;
jgladish's avatar
jgladish committed
205

206
    virtual AdapterStatus GetProperty(
207 208
      std::shared_ptr<IAdapterProperty>& prop,
      std::shared_ptr<IAdapterIoRequest>* req) = 0;
jgladish's avatar
jgladish committed
209

210
    virtual AdapterStatus SetProperty(
211 212
      std::shared_ptr<IAdapterProperty> const& prop,
      std::shared_ptr<IAdapterIoRequest>* req) = 0;
jgladish's avatar
jgladish committed
213

214
    virtual AdapterStatus GetPropertyValue(
215
      std::shared_ptr<IAdapterProperty> const& prop,
jgladish's avatar
jgladish committed
216
      std::string const& attributeName,
217 218
      std::shared_ptr<IAdapterValue>& value,
      std::shared_ptr<IAdapterIoRequest>* req) = 0;
jgladish's avatar
jgladish committed
219

220
    virtual AdapterStatus SetPropertyValue(
221 222 223
      std::shared_ptr<IAdapterProperty> const& prop,
      std::shared_ptr<IAdapterValue> const& value,
      std::shared_ptr<IAdapterIoRequest>* req) = 0;
jgladish's avatar
jgladish committed
224

225
    virtual AdapterStatus CallMethod(
226 227
      std::shared_ptr<IAdapterMethod>& method,
      std::shared_ptr<IAdapterIoRequest>* req) = 0;
jgladish's avatar
jgladish committed
228

229
    virtual AdapterStatus RegisterSignalListener(
gladish's avatar
gladish committed
230
      std::string const& signalName,
231
      std::shared_ptr<IAdapterSignalListener> const& listener,
gladish's avatar
gladish committed
232 233
      void* argp, 
      RegistrationHandle& handle) = 0;
jgladish's avatar
jgladish committed
234

235
    virtual AdapterStatus UnregisterSignalListener(RegistrationHandle const& h) = 0;
jgladish's avatar
jgladish committed
236
  };
237 238 239 240 241 242 243


  // concrete implementations of some of the simple interfaces

  class AdapterAttribute : public IAdapterAttribute
  {
  public:
244
    AdapterAttribute(std::shared_ptr<IAdapterValue> const& value, AnnotationMap const& annotations,
245 246 247 248 249 250 251 252 253 254
      AccessType accessType, SignalBehavior signalBehavior)
        : m_adapterValue(value)
        , m_annotationMap(annotations)
        , m_accessType(accessType)
        , m_signalBehavior(signalBehavior)
    {
      // empty
    }

  public:
255
    virtual std::shared_ptr<IAdapterValue> GetValue()
256 257 258 259 260 261 262 263 264 265 266 267
      { return m_adapterValue; }

    virtual AnnotationMap GetAnnotations()
      { return m_annotationMap; }

    virtual AccessType GetAccess()
      { return m_accessType; }

    virtual SignalBehavior GetChangeOfValueBehavior()
      { return m_signalBehavior; }

  private:
268
    std::shared_ptr<IAdapterValue> m_adapterValue;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    AnnotationMap             m_annotationMap;
    AccessType                m_accessType;
    SignalBehavior            m_signalBehavior;
  };

  class AdapterValue : public IAdapterValue
  {
  public:
    AdapterValue(std::string const& name, common::Variant const& data)
      : m_name(name)
      , m_data(data)
    {
      // empty
    }

    virtual std::string GetName()
      { return m_name; }

    virtual common::Variant const& GetData() const
      { return m_data; }

  private:
    std::string       m_name;
    common::Variant   m_data;
  };
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

  class AdapterIoRequest : public IAdapterIoRequest
  {
  public:
    AdapterIoRequest()
      : m_status(0)
      , m_canceled(false)
      , m_completed(false)
    {
    }

    virtual ~AdapterIoRequest()
    {
    }

    virtual int32_t GetStatus()
      { return m_status; }

    virtual int32_t Wait(uint32_t millis = std::numeric_limits<uint32_t>::max())
    {
      if (millis != std::numeric_limits<uint32_t>::max())
      {
        auto delay = std::chrono::system_clock::now() + std::chrono::milliseconds(millis);

        std::unique_lock<std::mutex> lk(m_mutex);
        if (m_cond.wait_until(lk, delay, [this] { return this->m_canceled || this->m_completed; }))
          return 0;
        else
          return -1; // timedout
      }
      else
      {
        std::unique_lock<std::mutex> lk(m_mutex);
        m_cond.wait(lk, [this] { return this->m_canceled || this->m_completed; });
      }

      return 0;
    }

    virtual int32_t Cancel()
    {
      std::unique_lock<std::mutex> lk(m_mutex);
      m_canceled = true;
      m_completed = true;
      lk.unlock();
      m_cond.notify_all();

      return 0;
    }

    void SetComplete(int32_t status)
    {
      std::unique_lock<std::mutex> lk(m_mutex);
      m_status = status;
      m_canceled = false;
      m_completed = true;
      lk.unlock();
      m_cond.notify_all();
    }

  private:
    int32_t                   m_status;
    bool                      m_canceled;
    bool                      m_completed;
    std::mutex                m_mutex;
    std::condition_variable   m_cond;
  };
jgladish's avatar
jgladish committed
361 362
}