1// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef BUFFET_MANAGER_H_
16#define BUFFET_MANAGER_H_
17
18#include <memory>
19#include <set>
20#include <string>
21#include <vector>
22
23#include <base/files/file_path.h>
24#include <base/macros.h>
25#include <base/memory/weak_ptr.h>
26#include <base/values.h>
27#include <brillo/dbus/async_event_sequencer.h>
28#include <brillo/errors/error.h>
29#include <nativepower/power_manager_client.h>
30#include <weave/device.h>
31
32#include "android/weave/BnWeaveServiceManager.h"
33#include "buffet/binder_weave_service.h"
34#include "buffet/buffet_config.h"
35
36namespace buffet {
37
38class BluetoothClient;
39class HttpTransportClient;
40class MdnsClient;
41class ShillClient;
42class WebServClient;
43
44// The Manager is responsible for global state of Buffet.  It exposes
45// interfaces which affect the entire device such as device registration and
46// device state.
47class Manager final : public android::weave::BnWeaveServiceManager {
48 public:
49  struct Options {
50    bool xmpp_enabled = true;
51    bool disable_privet = false;
52    bool enable_ping = false;
53    std::set<std::string> device_whitelist;
54
55    BuffetConfig::Options config_options;
56  };
57
58  Manager(const Options& options, const scoped_refptr<dbus::Bus>& bus);
59  ~Manager() override;
60
61  void Start(brillo::dbus_utils::AsyncEventSequencer* sequencer);
62  void Stop();
63
64 private:
65  void RestartWeave(brillo::dbus_utils::AsyncEventSequencer* sequencer);
66  void CreateDevice();
67
68  // Binder methods for IWeaveServiceManager:
69  using WeaveServiceManagerNotificationListener =
70      android::sp<android::weave::IWeaveServiceManagerNotificationListener>;
71  android::binder::Status connect(
72      const android::sp<android::weave::IWeaveClient>& client) override;
73  android::binder::Status registerNotificationListener(
74      const WeaveServiceManagerNotificationListener& listener) override;
75  android::binder::Status getDeviceId(android::String16* id) override;
76  android::binder::Status getCloudId(android::String16* id) override;
77  android::binder::Status getDeviceName(android::String16* name) override;
78  android::binder::Status getDeviceDescription(
79      android::String16* description) override;
80  android::binder::Status getDeviceLocation(
81      android::String16* location) override;
82  android::binder::Status getOemName(android::String16* name) override;
83  android::binder::Status getModelName(android::String16* name) override;
84  android::binder::Status getModelId(android::String16* id) override;
85  android::binder::Status getPairingSessionId(android::String16* id) override;
86  android::binder::Status getPairingMode(android::String16* mode) override;
87  android::binder::Status getPairingCode(android::String16* code) override;
88  android::binder::Status getState(android::String16* state) override;
89  android::binder::Status getTraits(android::String16* traits) override;
90  android::binder::Status getComponents(android::String16* components) override;
91
92  void OnTraitDefsChanged();
93  void OnComponentTreeChanged();
94  void OnGcdStateChanged(weave::GcdState state);
95  void OnConfigChanged(const weave::Settings& settings);
96  void OnPairingStart(const std::string& session_id,
97                      weave::PairingType pairing_type,
98                      const std::vector<uint8_t>& code);
99  void OnPairingEnd(const std::string& session_id);
100
101  void CreateServicesForClients();
102  void OnClientDisconnected(
103      const android::sp<android::weave::IWeaveClient>& client);
104  void OnNotificationListenerDestroyed(
105      const WeaveServiceManagerNotificationListener& notification_listener);
106  void NotifyServiceManagerChange(const std::vector<int>& notification_ids);
107  void OnRebootDevice(const std::weak_ptr<weave::Command>& cmd);
108  void RebootDeviceNow();
109
110  Options options_;
111  scoped_refptr<dbus::Bus> bus_;
112
113  class TaskRunner;
114  std::unique_ptr<TaskRunner> task_runner_;
115  std::unique_ptr<BluetoothClient> bluetooth_client_;
116  std::unique_ptr<BuffetConfig> config_;
117  std::unique_ptr<HttpTransportClient> http_client_;
118  std::unique_ptr<ShillClient> shill_client_;
119  std::unique_ptr<MdnsClient> mdns_client_;
120  std::unique_ptr<WebServClient> web_serv_client_;
121  std::unique_ptr<weave::Device> device_;
122
123  std::vector<android::sp<android::weave::IWeaveClient>> pending_clients_;
124  std::map<android::sp<android::weave::IWeaveClient>,
125           android::sp<BinderWeaveService>> services_;
126  std::set<WeaveServiceManagerNotificationListener> notification_listeners_;
127  android::PowerManagerClient power_manager_client_;
128
129  // State properties.
130  std::string cloud_id_;
131  std::string device_id_;
132  std::string device_name_;
133  std::string device_description_;
134  std::string device_location_;
135  std::string oem_name_;
136  std::string model_name_;
137  std::string model_id_;
138  std::string pairing_session_id_;
139  std::string pairing_mode_;
140  std::string pairing_code_;
141  std::string state_;
142
143  base::WeakPtrFactory<Manager> weak_ptr_factory_{this};
144  DISALLOW_COPY_AND_ASSIGN(Manager);
145};
146
147}  // namespace buffet
148
149#endif  // BUFFET_MANAGER_H_
150