blob: e152e40922ffff615843bb558ffec286bb020a83 [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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.
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070014
15#ifndef BUFFET_MANAGER_H_
16#define BUFFET_MANAGER_H_
17
Alex Vakulenko24aa1222014-05-12 15:55:25 -070018#include <memory>
Vitaly Bukada2312c2015-06-23 20:11:36 -070019#include <set>
Alex Vakulenko24aa1222014-05-12 15:55:25 -070020#include <string>
Alex Vakulenkoacec6aa2015-04-20 11:00:54 -070021#include <vector>
Alex Vakulenko24aa1222014-05-12 15:55:25 -070022
Christopher Wiley357deca2015-02-07 18:29:32 -080023#include <base/files/file_path.h>
Alex Vakulenko274226b2014-09-04 08:59:43 -070024#include <base/macros.h>
Christopher Wileyd71774f2014-05-07 09:58:45 -070025#include <base/memory/weak_ptr.h>
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070026#include <base/values.h>
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080027#include <brillo/dbus/async_event_sequencer.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070028#include <brillo/errors/error.h>
Alex Vakulenko0371fea2016-01-07 14:46:25 -080029#include <nativepower/power_manager_client.h>
Vitaly Bukae2713ac2015-08-03 13:50:01 -070030#include <weave/device.h>
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070031
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080032#include "android/weave/BnWeaveServiceManager.h"
33#include "buffet/binder_weave_service.h"
Alex Vakulenko0022b752015-10-02 11:09:59 -070034#include "buffet/buffet_config.h"
Christopher Wiley1aa980e2014-08-11 10:51:20 -070035
Vitaly Buka3b086312015-07-09 14:59:23 -070036namespace buffet {
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070037
Robert Gindaf86123d2015-09-11 16:10:43 -070038class BluetoothClient;
Vitaly Buka00882b72015-08-06 00:32:11 -070039class HttpTransportClient;
Alex Vakulenkof0f55342015-08-18 15:51:40 -070040class MdnsClient;
Peter Qiu786a9062015-10-02 11:45:01 -070041class ShillClient;
Alex Vakulenko1642bec2015-08-19 09:34:58 -070042class WebServClient;
Vitaly Bukaa0305d32015-07-27 16:08:51 -070043
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070044// 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.
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080047class Manager final : public android::weave::BnWeaveServiceManager {
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070048 public:
Alex Vakulenko0022b752015-10-02 11:09:59 -070049 struct Options {
50 bool xmpp_enabled = true;
51 bool disable_privet = false;
52 bool enable_ping = false;
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070053 std::set<std::string> device_whitelist;
54
55 BuffetConfig::Options config_options;
Alex Vakulenko0022b752015-10-02 11:09:59 -070056 };
57
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080058 Manager(const Options& options, const scoped_refptr<dbus::Bus>& bus);
59 ~Manager() override;
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070060
Alex Vakulenko41705852015-10-13 10:12:06 -070061 void Start(brillo::dbus_utils::AsyncEventSequencer* sequencer);
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070062 void Stop();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070063
64 private:
Alex Vakulenko41705852015-10-13 10:12:06 -070065 void RestartWeave(brillo::dbus_utils::AsyncEventSequencer* sequencer);
Alex Vakulenkobe39e932015-10-09 08:10:36 -070066 void CreateDevice();
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070067
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080068 // 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;
Vitaly Buka84fd6dd2015-06-09 17:22:18 -070091
Alex Vakulenko14b3e6e2015-12-10 10:01:36 -080092 void OnTraitDefsChanged();
93 void OnComponentTreeChanged();
Alex Vakulenko0022b752015-10-02 11:09:59 -070094 void OnGcdStateChanged(weave::GcdState state);
Vitaly Buka00b83492015-07-20 00:37:48 -070095 void OnConfigChanged(const weave::Settings& settings);
Vitaly Buka7b3ba792015-06-09 17:01:54 -070096 void OnPairingStart(const std::string& session_id,
Vitaly Buka0c6dcd22015-07-10 00:12:25 -070097 weave::PairingType pairing_type,
Vitaly Buka7b3ba792015-06-09 17:01:54 -070098 const std::vector<uint8_t>& code);
99 void OnPairingEnd(const std::string& session_id);
Christopher Wiley2f772932015-02-15 15:42:04 -0800100
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800101 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);
Alex Vakulenko0371fea2016-01-07 14:46:25 -0800107 void OnRebootDevice(const std::weak_ptr<weave::Command>& cmd);
108 void RebootDeviceNow();
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700109
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700110 Options options_;
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800111 scoped_refptr<dbus::Bus> bus_;
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700112
Vitaly Bukae74c8722015-08-13 00:33:00 -0700113 class TaskRunner;
114 std::unique_ptr<TaskRunner> task_runner_;
Robert Gindaf86123d2015-09-11 16:10:43 -0700115 std::unique_ptr<BluetoothClient> bluetooth_client_;
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700116 std::unique_ptr<BuffetConfig> config_;
Vitaly Buka00882b72015-08-06 00:32:11 -0700117 std::unique_ptr<HttpTransportClient> http_client_;
Peter Qiu786a9062015-10-02 11:45:01 -0700118 std::unique_ptr<ShillClient> shill_client_;
Alex Vakulenkof0f55342015-08-18 15:51:40 -0700119 std::unique_ptr<MdnsClient> mdns_client_;
Alex Vakulenko1642bec2015-08-19 09:34:58 -0700120 std::unique_ptr<WebServClient> web_serv_client_;
Vitaly Buka0c6dcd22015-07-10 00:12:25 -0700121 std::unique_ptr<weave::Device> device_;
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800122
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_;
Alex Vakulenko0371fea2016-01-07 14:46:25 -0800127 android::PowerManagerClient power_manager_client_;
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800128
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_;
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700142
Vitaly Bukae43871f2015-05-11 15:41:33 -0700143 base::WeakPtrFactory<Manager> weak_ptr_factory_{this};
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700144 DISALLOW_COPY_AND_ASSIGN(Manager);
145};
146
147} // namespace buffet
148
149#endif // BUFFET_MANAGER_H_