summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/privet_daemon_manager_client.h
blob: 8c490d8edeff1dfbad593b1671ab1a9651ae984b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_DBUS_PRIVET_DAEMON_MANAGER_CLIENT_H_
#define CHROMEOS_DBUS_PRIVET_DAEMON_MANAGER_CLIENT_H_

#include <string>
#include <vector>

#include "base/macros.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_client.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "dbus/object_path.h"
#include "dbus/property.h"

namespace chromeos {

// PrivetDaemonManagerClient is used to communicate with the
// privetd service.  All methods should be called from
// the origin thread which initializes the DBusThreadManager instance.
class CHROMEOS_EXPORT PrivetDaemonManagerClient : public DBusClient {
 public:
  class PairingInfo {
   public:
    PairingInfo();
    ~PairingInfo();

    // Returns the value of the pairing code; not necessarily a printable
    // string.
    const std::vector<uint8_t>& code() const { return code_; }
    void set_code(const uint8_t* data, size_t length) {
      code_.assign(data, data + length);
    }

    // Returns the selected type of pairing (e.g. "pinCode", "embeddedCode").
    const std::string& mode() const { return mode_; }
    void set_mode(const std::string& mode) { mode_ = mode; }

    // Returns a unique identifier representing the pairing session.
    const std::string& session_id() const { return session_id_; }
    void set_session_id(const std::string& id) { session_id_ = id; }

    // Resets the values to empty values.
    void Clear();

   private:
    std::vector<uint8_t> code_;
    std::string mode_;
    std::string session_id_;
  };

  class PairingInfoProperty : public dbus::PropertyBase {
   public:
    bool PopValueFromReader(dbus::MessageReader* reader) override;
    void AppendSetValueToWriter(dbus::MessageWriter* writer) override;
    void ReplaceValueWithSetValue() override;

    const PairingInfo& value() const { return value_; }

   private:
    PairingInfo value_;
  };

  // Structure of properties associated with a privet Manager.
  class Properties : public dbus::PropertySet {
   public:
    Properties(dbus::ObjectProxy* object_proxy,
               const std::string& interface_name,
               const PropertyChangedCallback& callback);
    ~Properties() override;

    // State of WiFi bootstrapping.
    // Values are "disabled", "waiting", "connecting", "monitoring".
    const std::string& wifi_bootstrap_state() const {
      return wifi_bootstrap_state_.value();
    }

    // State of GCD bootstrapping.
    // Values are "disabled", "offline", "connecting", "waiting", "registering",
    // "online".
    const std::string& gcd_boostrap_state() const {
      return gcd_bootstrap_state_.value();
    }

    // State of device pairing.
    const PairingInfo& pairing_info() const { return pairing_info_.value(); }

    //  Concise note describing a peer.  Suitable for display to the user.
    const std::string& description() const { return description_.value(); }

    // Concise name describing a peer.  Suitable for display to the user.
    const std::string& name() const { return name_.value(); }

   private:
    dbus::Property<std::string> wifi_bootstrap_state_;
    dbus::Property<std::string> gcd_bootstrap_state_;
    PairingInfoProperty pairing_info_;
    dbus::Property<std::string> description_;
    dbus::Property<std::string> name_;

    DISALLOW_COPY_AND_ASSIGN(Properties);
  };

  // Interface for observing changes from a privet daemon.
  class Observer {
   public:
    virtual ~Observer();

    // Called when the manager has been added.
    virtual void ManagerAdded() = 0;

    // Called when the manager has been removed.
    virtual void ManagerRemoved() = 0;

    // Called when the manager has a change in value of the property named
    // |property_name|. Valid values are "Description", "GCDBootstrapState",
    // "Name", "PairingInfo", "WiFiBootstrapState".
    virtual void ManagerPropertyChanged(const std::string& property_name) = 0;
  };

  ~PrivetDaemonManagerClient() override;

  // Factory function, creates a new instance which is owned by the caller.
  // For normal usage, access the singleton via DBusThreadManager::Get().
  static PrivetDaemonManagerClient* Create();

  // Adds and removes observers for events on all privet daemon events.
  virtual void AddObserver(Observer* observer) = 0;
  virtual void RemoveObserver(Observer* observer) = 0;

  // Calls SetDescription method.
  // |callback| is called with its |call_status| argument set to
  // DBUS_METHOD_CALL_SUCCESS if the method call succeeds. Otherwise,
  // |callback| is called with |call_status| set to DBUS_METHOD_CALL_FAILURE.
  virtual void SetDescription(const std::string& description,
                              const VoidDBusMethodCallback& callback) = 0;

  // Obtains the properties for the manager any values should be
  // copied if needed.
  virtual const Properties* GetProperties() = 0;

 protected:
  // Create() should be used instead.
  PrivetDaemonManagerClient();

 private:
  DISALLOW_COPY_AND_ASSIGN(PrivetDaemonManagerClient);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_PRIVET_DAEMON_MANAGER_CLIENT_H_