blob: e761e0c9937336bfab2459cd83b85cbc3827929b [file] [log] [blame]
// Copyright 2015 The Weave 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 LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_
#define LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_
#include <memory>
#include <set>
#include <string>
#include <weave/command.h>
#include <weave/export.h>
#include <weave/provider/bluetooth.h>
#include <weave/provider/config_store.h>
#include <weave/provider/dns_service_discovery.h>
#include <weave/provider/http_client.h>
#include <weave/provider/http_server.h>
#include <weave/provider/network.h>
#include <weave/provider/task_runner.h>
#include <weave/provider/wifi.h>
namespace weave {
// States of Gcd connection.
enum class GcdState {
kUnconfigured, // We have no credentials.
kConnecting, // We have credentials but not yet connected.
kConnected, // We're registered and connected to the cloud.
kInvalidCredentials, // Our registration has been revoked.
};
class Device {
public:
virtual ~Device() = default;
// Returns reference the current settings.
virtual const Settings& GetSettings() const = 0;
// Callback type for AddSettingsChangedCallback.
using SettingsChangedCallback =
base::Callback<void(const Settings& settings)>;
// Subscribes to notification settings changes.
virtual void AddSettingsChangedCallback(
const SettingsChangedCallback& callback) = 0;
// Adds provided commands definitions. Can be called multiple times with
// condition that definitions do not conflict.
// Invalid value is fatal.
virtual void AddCommandDefinitionsFromJson(const std::string& json) = 0;
virtual void AddCommandDefinitions(const base::DictionaryValue& dict) = 0;
// Callback type for AddCommandHandler.
using CommandHandlerCallback =
base::Callback<void(const std::weak_ptr<Command>& command)>;
// Sets handler for new commands added to the queue.
// |command_name| is the full command name of the command to handle. e.g.
// "base.reboot". Each command can have no more than one handler.
// Empty |command_name| sets default handler for all unhanded commands.
// No new command handlers can be set after default handler was set.
virtual void AddCommandHandler(const std::string& command_name,
const CommandHandlerCallback& callback) = 0;
// Adds a new command to the command queue.
virtual bool AddCommand(const base::DictionaryValue& command,
std::string* id,
ErrorPtr* error) = 0;
// Finds a command by the command |id|. Returns nullptr if the command with
// the given |id| is not found. The returned pointer should not be persisted
// for a long period of time.
virtual Command* FindCommand(const std::string& id) = 0;
// Sets callback which is called when stat is changed.
virtual void AddStateChangedCallback(const base::Closure& callback) = 0;
// Adds provided state definitions. Can be called multiple times with
// condition that definitions do not conflict.
// Invalid value is fatal.
virtual void AddStateDefinitionsFromJson(const std::string& json) = 0;
virtual void AddStateDefinitions(const base::DictionaryValue& dict) = 0;
// Sets value of multiple properties of the state.
// It's recommended to call this to initialize state defined by
// AddStateDefinitions.
// Example:
// device->SetStatePropertiesFromJson("{'base':{'firmwareVersion':'123'}}")
// Method completely replaces properties included |json| or |dict|.
// Properties of the state not included |json| or |dict| will stay unchanged.
virtual bool SetStatePropertiesFromJson(const std::string& json,
ErrorPtr* error) = 0;
virtual bool SetStateProperties(const base::DictionaryValue& dict,
ErrorPtr* error) = 0;
// Returns value of the single property.
// |name| is full property name, including package name. e.g. "base.network".
virtual std::unique_ptr<base::Value> GetStateProperty(
const std::string& name) const = 0;
// Sets value of the single property.
// |name| is full property name, including package name. e.g. "base.network".
virtual bool SetStateProperty(const std::string& name,
const base::Value& value,
ErrorPtr* error) = 0;
// Returns aggregated state properties across all registered packages.
virtual std::unique_ptr<base::DictionaryValue> GetState() const = 0;
// Returns current state of GCD connection.
virtual GcdState GetGcdState() const = 0;
// Callback type for GcdStatusCallback.
using GcdStateChangedCallback = base::Callback<void(GcdState state)>;
// Sets callback which is called when state of server connection changed.
virtual void AddGcdStateChangedCallback(
const GcdStateChangedCallback& callback) = 0;
// Registers the device.
// This is testing method and should not be used by applications.
virtual void Register(const std::string& ticket_id,
const DoneCallback& callback) = 0;
// Handler should display pin code to the user.
using PairingBeginCallback =
base::Callback<void(const std::string& session_id,
PairingType pairing_type,
const std::vector<uint8_t>& code)>;
// Handler should stop displaying pin code.
using PairingEndCallback =
base::Callback<void(const std::string& session_id)>;
// Subscribes to notification about client pairing events.
virtual void AddPairingChangedCallbacks(
const PairingBeginCallback& begin_callback,
const PairingEndCallback& end_callback) = 0;
LIBWEAVE_EXPORT static std::unique_ptr<Device> Create(
provider::ConfigStore* config_store,
provider::TaskRunner* task_runner,
provider::HttpClient* http_client,
provider::Network* network,
provider::DnsServiceDiscovery* dns_sd,
provider::HttpServer* http_server,
provider::Wifi* wifi,
provider::Bluetooth* bluetooth_provider);
};
} // namespace weave
#endif // LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_