Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #include "libweave/src/base_api_handler.h" |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 6 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 7 | #include "libweave/src/commands/command_instance.h" |
| 8 | #include "libweave/src/commands/command_manager.h" |
| 9 | #include "libweave/src/device_registration_info.h" |
| 10 | #include "libweave/src/states/state_manager.h" |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 11 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 12 | namespace weave { |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 13 | |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame^] | 14 | namespace { |
| 15 | const char kBaseStateFirmwareVersion[] = "base.firmwareVersion"; |
| 16 | const char kBaseStateAnonymousAccessRole[] = "base.localAnonymousAccessMaxRole"; |
| 17 | const char kBaseStateDiscoveryEnabled[] = "base.localDiscoveryEnabled"; |
| 18 | const char kBaseStatePairingEnabled[] = "base.localPairingEnabled"; |
| 19 | } // namespace |
| 20 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 21 | BaseApiHandler::BaseApiHandler( |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame^] | 22 | DeviceRegistrationInfo* device_info, |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 23 | const std::shared_ptr<StateManager>& state_manager, |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 24 | const std::shared_ptr<CommandManager>& command_manager) |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 25 | : device_info_{device_info}, state_manager_{state_manager} { |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame^] | 26 | device_info_->AddOnConfigChangedCallback(base::Bind( |
| 27 | &BaseApiHandler::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); |
| 28 | |
| 29 | // Populate state fields that belong to the system. |
| 30 | base::FilePath lsb_release_path("/etc/lsb-release"); |
| 31 | chromeos::KeyValueStore lsb_release_store; |
| 32 | std::string firmware_version; |
| 33 | if (lsb_release_store.Load(lsb_release_path) && |
| 34 | lsb_release_store.GetString("CHROMEOS_RELEASE_VERSION", |
| 35 | &firmware_version)) { |
| 36 | base::DictionaryValue state; |
| 37 | state.SetStringWithoutPathExpansion(kBaseStateFirmwareVersion, |
| 38 | firmware_version); |
| 39 | CHECK(state_manager_->SetProperties(state, nullptr)); |
| 40 | } else { |
| 41 | LOG(ERROR) << "Failed to get CHROMEOS_RELEASE_VERSION from " |
| 42 | << lsb_release_path.value(); |
| 43 | } |
| 44 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 45 | command_manager->AddOnCommandAddedCallback(base::Bind( |
| 46 | &BaseApiHandler::OnCommandAdded, weak_ptr_factory_.GetWeakPtr())); |
| 47 | } |
| 48 | |
Vitaly Buka | 12affd8 | 2015-07-23 18:45:35 -0700 | [diff] [blame] | 49 | void BaseApiHandler::OnCommandAdded(Command* command) { |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 50 | if (command->GetStatus() != CommandStatus::kQueued) |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 51 | return; |
| 52 | |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 53 | if (command->GetName() == "base.updateBaseConfiguration") |
| 54 | return UpdateBaseConfiguration(command); |
| 55 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 56 | if (command->GetName() == "base.updateDeviceInfo") |
| 57 | return UpdateDeviceInfo(command); |
| 58 | } |
| 59 | |
Vitaly Buka | 12affd8 | 2015-07-23 18:45:35 -0700 | [diff] [blame] | 60 | void BaseApiHandler::UpdateBaseConfiguration(Command* command) { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 61 | command->SetProgress(base::DictionaryValue{}, nullptr); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 62 | |
| 63 | const BuffetConfig& config{device_info_->GetConfig()}; |
| 64 | std::string anonymous_access_role{config.local_anonymous_access_role()}; |
| 65 | bool discovery_enabled{config.local_discovery_enabled()}; |
| 66 | bool pairing_enabled{config.local_pairing_enabled()}; |
| 67 | |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 68 | auto parameters = command->GetParameters(); |
| 69 | parameters->GetString("localAnonymousAccessMaxRole", &anonymous_access_role); |
| 70 | parameters->GetBoolean("localDiscoveryEnabled", &discovery_enabled); |
| 71 | parameters->GetBoolean("localPairingEnabled", &pairing_enabled); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 72 | |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 73 | if (!device_info_->UpdateBaseConfig(anonymous_access_role, discovery_enabled, |
| 74 | pairing_enabled, nullptr)) { |
| 75 | return command->Abort(); |
| 76 | } |
| 77 | |
| 78 | command->Done(); |
| 79 | } |
| 80 | |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame^] | 81 | void BaseApiHandler::OnConfigChanged(const Settings& settings) { |
| 82 | base::DictionaryValue state; |
| 83 | state.SetStringWithoutPathExpansion(kBaseStateAnonymousAccessRole, |
| 84 | settings.local_anonymous_access_role); |
| 85 | state.SetBooleanWithoutPathExpansion(kBaseStateDiscoveryEnabled, |
| 86 | settings.local_discovery_enabled); |
| 87 | state.SetBooleanWithoutPathExpansion(kBaseStatePairingEnabled, |
| 88 | settings.local_pairing_enabled); |
| 89 | state_manager_->SetProperties(state, nullptr); |
| 90 | } |
| 91 | |
Vitaly Buka | 12affd8 | 2015-07-23 18:45:35 -0700 | [diff] [blame] | 92 | void BaseApiHandler::UpdateDeviceInfo(Command* command) { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 93 | command->SetProgress(base::DictionaryValue{}, nullptr); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 94 | |
| 95 | const BuffetConfig& config{device_info_->GetConfig()}; |
| 96 | std::string name{config.name()}; |
| 97 | std::string description{config.description()}; |
| 98 | std::string location{config.location()}; |
| 99 | |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 100 | auto parameters = command->GetParameters(); |
| 101 | parameters->GetString("name", &name); |
| 102 | parameters->GetString("description", &description); |
| 103 | parameters->GetString("location", &location); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 104 | |
| 105 | if (!device_info_->UpdateDeviceInfo(name, description, location, nullptr)) { |
| 106 | return command->Abort(); |
| 107 | } |
| 108 | |
| 109 | command->Done(); |
| 110 | } |
| 111 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 112 | } // namespace weave |