Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | #include "buffet/manager.h" |
| 6 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
| 11 | #include <base/bind_helpers.h> |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 12 | #include <base/json/json_reader.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 13 | #include <base/json/json_writer.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/async_event_sequencer.h> |
| 15 | #include <chromeos/dbus/exported_object_manager.h> |
| 16 | #include <chromeos/errors/error.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 17 | #include <dbus/bus.h> |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 18 | #include <dbus/object_path.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 19 | #include <dbus/values_util.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 21 | #include "buffet/commands/command_instance.h" |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 22 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 23 | #include "buffet/libbuffet/dbus_constants.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 24 | #include "buffet/states/state_manager.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 25 | |
Christopher Wiley | 2d2d92b | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 26 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | b5dd5ea | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 27 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 28 | |
| 29 | namespace buffet { |
| 30 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 31 | Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 32 | : dbus_object_(object_manager.get(), |
| 33 | object_manager->GetBus(), |
| 34 | dbus::ObjectPath(dbus_constants::kManagerServicePath)) {} |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 35 | |
Alex Vakulenko | be9c383 | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 36 | void Manager::RegisterAsync(const AsyncEventSequencer::CompletionAction& cb) { |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 37 | chromeos::dbus_utils::DBusInterface* itf = |
| 38 | dbus_object_.AddOrGetInterface(dbus_constants::kManagerInterface); |
Anton Muhin | 86d67fe | 2014-10-01 18:06:54 +0400 | [diff] [blame] | 39 | itf->AddMethodHandler(dbus_constants::kManagerStartDevice, |
| 40 | base::Unretained(this), |
| 41 | &Manager::HandleStartDevice); |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 42 | itf->AddMethodHandler(dbus_constants::kManagerCheckDeviceRegistered, |
| 43 | base::Unretained(this), |
| 44 | &Manager::HandleCheckDeviceRegistered); |
| 45 | itf->AddMethodHandler(dbus_constants::kManagerGetDeviceInfo, |
| 46 | base::Unretained(this), |
| 47 | &Manager::HandleGetDeviceInfo); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 48 | itf->AddMethodHandler(dbus_constants::kManagerRegisterDevice, |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 49 | base::Unretained(this), |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 50 | &Manager::HandleRegisterDevice); |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 51 | itf->AddMethodHandler(dbus_constants::kManagerUpdateStateMethod, |
| 52 | base::Unretained(this), |
| 53 | &Manager::HandleUpdateState); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 54 | itf->AddMethodHandler(dbus_constants::kManagerAddCommand, |
| 55 | base::Unretained(this), |
| 56 | &Manager::HandleAddCommand); |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 57 | itf->AddMethodHandler(dbus_constants::kManagerTestMethod, |
| 58 | base::Unretained(this), |
| 59 | &Manager::HandleTestMethod); |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 60 | dbus_object_.RegisterAsync(cb); |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 61 | command_manager_ = |
| 62 | std::make_shared<CommandManager>(dbus_object_.GetObjectManager()); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 63 | command_manager_->Startup(); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 64 | state_manager_ = std::make_shared<StateManager>(); |
| 65 | state_manager_->Startup(); |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 66 | device_info_ = std::unique_ptr<DeviceRegistrationInfo>( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 67 | new DeviceRegistrationInfo(command_manager_, state_manager_)); |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 68 | device_info_->Load(); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Anton Muhin | 86d67fe | 2014-10-01 18:06:54 +0400 | [diff] [blame] | 71 | void Manager::HandleStartDevice(chromeos::ErrorPtr* error) { |
| 72 | LOG(INFO) << "Received call to Manager.StartDevice()"; |
Anton Muhin | d8d3216 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 73 | |
| 74 | device_info_->StartDevice(error); |
Anton Muhin | 86d67fe | 2014-10-01 18:06:54 +0400 | [diff] [blame] | 75 | } |
| 76 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 77 | std::string Manager::HandleCheckDeviceRegistered(chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 78 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 79 | std::string device_id; |
| 80 | bool registered = device_info_->CheckRegistration(error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 81 | // If it fails due to any reason other than 'device not registered', |
| 82 | // treat it as a real error and report it to the caller. |
| 83 | if (!registered && |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 84 | !(*error)->HasError(kErrorDomainGCD, "device_not_registered")) { |
| 85 | return device_id; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 86 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 87 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 88 | error->reset(); |
| 89 | |
| 90 | if (registered) |
| 91 | device_id = device_info_->GetDeviceId(error); |
| 92 | |
| 93 | return device_id; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 96 | std::string Manager::HandleGetDeviceInfo(chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 97 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
| 98 | |
| 99 | std::string device_info_str; |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 100 | auto device_info = device_info_->GetDeviceInfo(error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 101 | if (!device_info) |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 102 | return device_info_str; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 103 | |
| 104 | base::JSONWriter::Write(device_info.get(), &device_info_str); |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 105 | return device_info_str; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 108 | std::string Manager::HandleRegisterDevice( |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 109 | chromeos::ErrorPtr* error, |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 110 | const std::map<std::string, std::string>& params) { |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 111 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 112 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 113 | return device_info_->RegisterDevice(params, error); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 116 | void Manager::HandleUpdateState( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 117 | chromeos::ErrorPtr* error, |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 118 | const chromeos::VariantDictionary& property_set) { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 119 | state_manager_->UpdateProperties(property_set, error); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 122 | void Manager::HandleAddCommand( |
| 123 | chromeos::ErrorPtr* error, const std::string& json_command) { |
| 124 | std::string error_message; |
| 125 | std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 126 | json_command, base::JSON_PARSE_RFC, nullptr, &error_message)); |
| 127 | if (!value) { |
| 128 | chromeos::Error::AddTo(error, chromeos::errors::json::kDomain, |
| 129 | chromeos::errors::json::kParseError, error_message); |
| 130 | return; |
| 131 | } |
| 132 | auto command_instance = buffet::CommandInstance::FromJson( |
| 133 | value.get(), command_manager_->GetCommandDictionary(), error); |
| 134 | if (command_instance) |
| 135 | command_manager_->AddCommand(std::move(command_instance)); |
| 136 | } |
| 137 | |
Alex Vakulenko | 7a1dc0b | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 138 | std::string Manager::HandleTestMethod(chromeos::ErrorPtr* error, |
| 139 | const std::string& message) { |
| 140 | LOG(INFO) << "Received call to test method: " << message; |
| 141 | return message; |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 144 | } // namespace buffet |