Home | History | Annotate | Download | only in weave
      1 // Copyright 2015 The Weave Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_
      6 #define LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_
      7 
      8 #include <memory>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include <weave/command.h>
     14 #include <weave/export.h>
     15 #include <weave/provider/bluetooth.h>
     16 #include <weave/provider/config_store.h>
     17 #include <weave/provider/dns_service_discovery.h>
     18 #include <weave/provider/http_client.h>
     19 #include <weave/provider/http_server.h>
     20 #include <weave/provider/network.h>
     21 #include <weave/provider/task_runner.h>
     22 #include <weave/provider/wifi.h>
     23 
     24 namespace weave {
     25 
     26 // States of Gcd connection.
     27 enum class GcdState {
     28   kUnconfigured,        // Device was not registered.
     29   kConnecting,          // We have credentials but not yet connected.
     30   kConnected,           // We're registered and connected to the cloud.
     31   kInvalidCredentials,  // Our registration has been revoked.
     32 };
     33 
     34 class Device {
     35  public:
     36   virtual ~Device() {}
     37 
     38   // Returns reference the current settings.
     39   virtual const Settings& GetSettings() const = 0;
     40 
     41   // Callback type for AddSettingsChangedCallback.
     42   using SettingsChangedCallback =
     43       base::Callback<void(const Settings& settings)>;
     44 
     45   // Subscribes to notification settings changes.
     46   virtual void AddSettingsChangedCallback(
     47       const SettingsChangedCallback& callback) = 0;
     48 
     49   // Adds new trait definitions to device.
     50   virtual void AddTraitDefinitionsFromJson(const std::string& json) = 0;
     51   virtual void AddTraitDefinitions(const base::DictionaryValue& dict) = 0;
     52 
     53   // Returns the full JSON dictionary containing trait definitions.
     54   virtual const base::DictionaryValue& GetTraits() const = 0;
     55 
     56   // Sets callback which is called when new trait definitions are added.
     57   virtual void AddTraitDefsChangedCallback(const base::Closure& callback) = 0;
     58 
     59   // Adds a new component instance to device. Traits used by this component
     60   // must be already defined.
     61   virtual bool AddComponent(const std::string& name,
     62                             const std::vector<std::string>& traits,
     63                             ErrorPtr* error) = 0;
     64 
     65   // Removes an existing component instance from device.
     66   virtual bool RemoveComponent(const std::string& name,
     67                                ErrorPtr* error) = 0;
     68 
     69   // Sets callback which is called when new components are added.
     70   virtual void AddComponentTreeChangedCallback(
     71       const base::Closure& callback) = 0;
     72 
     73   // Returns the full JSON dictionary containing component instances.
     74   virtual const base::DictionaryValue& GetComponents() const = 0;
     75 
     76   // Sets value of multiple properties of the state.
     77   // It's recommended to call this to initialize component state defined.
     78   // Example:
     79   //   device->SetStatePropertiesFromJson("myComponent",
     80   //                                      "{'base':{'firmwareVersion':'123'}}")
     81   // Method completely replaces properties included |json| or |dict|.
     82   // Properties of the state not included |json| or |dict| will stay unchanged.
     83   virtual bool SetStatePropertiesFromJson(const std::string& component,
     84                                           const std::string& json,
     85                                           ErrorPtr* error) = 0;
     86   virtual bool SetStateProperties(const std::string& component,
     87                                   const base::DictionaryValue& dict,
     88                                   ErrorPtr* error) = 0;
     89 
     90   // Returns value of the single property.
     91   // |name| is full property name, including trait name. e.g. "base.network".
     92   virtual const base::Value* GetStateProperty(const std::string& component,
     93                                               const std::string& name,
     94                                               ErrorPtr* error) const = 0;
     95 
     96   // Sets value of the single property.
     97   // |name| is full property name, including trait name. e.g. "base.network".
     98   virtual bool SetStateProperty(const std::string& component,
     99                                 const std::string& name,
    100                                 const base::Value& value,
    101                                 ErrorPtr* error) = 0;
    102 
    103   // Callback type for AddCommandHandler.
    104   using CommandHandlerCallback =
    105       base::Callback<void(const std::weak_ptr<Command>& command)>;
    106 
    107   // Sets handler for new commands added to the queue.
    108   // |component| is the name of the component for which commands should be
    109   // handled.
    110   // |command_name| is the full command name of the command to handle. e.g.
    111   // "base.reboot". Each command can have no more than one handler.
    112   // Empty |component| and |command_name| sets default handler for all unhanded
    113   // commands.
    114   // No new command handlers can be set after default handler was set.
    115   virtual void AddCommandHandler(const std::string& component,
    116                                  const std::string& command_name,
    117                                  const CommandHandlerCallback& callback) = 0;
    118 
    119   // Adds a new command to the command queue.
    120   virtual bool AddCommand(const base::DictionaryValue& command,
    121                           std::string* id,
    122                           ErrorPtr* error) = 0;
    123 
    124   // Finds a command by the command |id|. Returns nullptr if the command with
    125   // the given |id| is not found. The returned pointer should not be persisted
    126   // for a long period of time.
    127   virtual Command* FindCommand(const std::string& id) = 0;
    128 
    129   // Sets callback which is called when stat is changed.
    130   virtual void AddStateChangedCallback(const base::Closure& callback) = 0;
    131 
    132   // Returns current state of GCD connection.
    133   virtual GcdState GetGcdState() const = 0;
    134 
    135   // Callback type for GcdStatusCallback.
    136   using GcdStateChangedCallback = base::Callback<void(GcdState state)>;
    137 
    138   // Sets callback which is called when state of server connection changed.
    139   virtual void AddGcdStateChangedCallback(
    140       const GcdStateChangedCallback& callback) = 0;
    141 
    142   // Registers the device.
    143   // This is testing method and should not be used by applications.
    144   virtual void Register(const std::string& ticket_id,
    145                         const DoneCallback& callback) = 0;
    146 
    147   // Handler should display pin code to the user.
    148   using PairingBeginCallback =
    149       base::Callback<void(const std::string& session_id,
    150                           PairingType pairing_type,
    151                           const std::vector<uint8_t>& code)>;
    152 
    153   // Handler should stop displaying pin code.
    154   using PairingEndCallback =
    155       base::Callback<void(const std::string& session_id)>;
    156   // Subscribes to notification about client pairing events.
    157 
    158   virtual void AddPairingChangedCallbacks(
    159       const PairingBeginCallback& begin_callback,
    160       const PairingEndCallback& end_callback) = 0;
    161 
    162   LIBWEAVE_EXPORT static std::unique_ptr<Device> Create(
    163       provider::ConfigStore* config_store,
    164       provider::TaskRunner* task_runner,
    165       provider::HttpClient* http_client,
    166       provider::Network* network,
    167       provider::DnsServiceDiscovery* dns_sd,
    168       provider::HttpServer* http_server,
    169       provider::Wifi* wifi,
    170       provider::Bluetooth* bluetooth_provider);
    171 
    172   //========================== Deprecated APIs =========================
    173 
    174   // Adds provided commands definitions. Can be called multiple times with
    175   // condition that definitions do not conflict.
    176   // Invalid value is fatal.
    177   // DO NOT USE IN YOUR CODE: use AddTraitDefinitions() instead.
    178   LIBWEAVE_DEPRECATED virtual void AddCommandDefinitionsFromJson(
    179       const std::string& json) = 0;
    180   LIBWEAVE_DEPRECATED virtual void AddCommandDefinitions(
    181       const base::DictionaryValue& dict) = 0;
    182 
    183   // Sets handler for new commands added to the queue.
    184   // |command_name| is the full command name of the command to handle. e.g.
    185   // "base.reboot". Each command can have no more than one handler.
    186   // Empty |command_name| sets default handler for all unhanded commands.
    187   // No new command handlers can be set after default handler was set.
    188   // DO NOT USE IN YOUR CODE: use AddCommandHandler() with component parameter.
    189   LIBWEAVE_DEPRECATED virtual void AddCommandHandler(
    190       const std::string& command_name,
    191       const CommandHandlerCallback& callback) = 0;
    192 
    193   // Adds provided state definitions. Can be called multiple times with
    194   // condition that definitions do not conflict.
    195   // Invalid value is fatal.
    196   // DO NOT USE IN YOUR CODE: use AddTraitDefinitions() instead.
    197   LIBWEAVE_DEPRECATED virtual void AddStateDefinitionsFromJson(
    198       const std::string& json) = 0;
    199   LIBWEAVE_DEPRECATED virtual void AddStateDefinitions(
    200       const base::DictionaryValue& dict) = 0;
    201 
    202   // Sets value of multiple properties of the state.
    203   // It's recommended to call this to initialize state defined by
    204   // AddStateDefinitions.
    205   // Example:
    206   //   device->SetStatePropertiesFromJson("{'base':{'firmwareVersion':'123'}}")
    207   // Method completely replaces properties included |json| or |dict|.
    208   // Properties of the state not included |json| or |dict| will stay unchanged.
    209   // DO NOT USE IN YOUR CODE: use SetStateProperties() with component parameter.
    210   LIBWEAVE_DEPRECATED virtual bool SetStatePropertiesFromJson(
    211       const std::string& json,
    212       ErrorPtr* error) = 0;
    213   LIBWEAVE_DEPRECATED virtual bool SetStateProperties(
    214       const base::DictionaryValue& dict,
    215       ErrorPtr* error) = 0;
    216 
    217   // Returns value of the single property.
    218   // |name| is full property name, including package name. e.g. "base.network".
    219   // DO NOT USE IN YOUR CODE: use GetStateProperty() with component parameter.
    220   LIBWEAVE_DEPRECATED virtual const base::Value* GetStateProperty(
    221       const std::string& name) const = 0;
    222 
    223   // Sets value of the single property.
    224   // |name| is full property name, including package name. e.g. "base.network".
    225   // DO NOT USE IN YOUR CODE: use SetStateProperty() with component parameter.
    226   LIBWEAVE_DEPRECATED virtual bool SetStateProperty(const std::string& name,
    227                                                     const base::Value& value,
    228                                                     ErrorPtr* error) = 0;
    229 
    230   // Returns aggregated state properties across all registered packages.
    231   // DO NOT USE IN YOUR CODE: use GetComponents() instead.
    232   LIBWEAVE_DEPRECATED virtual const base::DictionaryValue& GetState() const = 0;
    233 };
    234 
    235 }  // namespace weave
    236 
    237 #endif  // LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_
    238