blob: 367359aa5565d57d378b3fa71e3bdcd6c09ab09a [file] [log] [blame]
Alex Deymof7ead812015-10-23 17:37:27 -07001//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/weave_service.h"
18
19#include <cmath>
20#include <string>
21
22#include <base/bind.h>
23#include <base/values.h>
24#include <brillo/errors/error.h>
25
26#include "update_engine/update_status_utils.h"
27
28using std::string;
29
30namespace {
31
32const char kWeaveComponent[] = "updater";
33
34} // namespace
35
36namespace chromeos_update_engine {
37
38bool WeaveService::Init(scoped_refptr<dbus::Bus> bus,
39 DelegateInterface* delegate) {
40 delegate_ = delegate;
41 device_ = weaved::Device::CreateInstance(
42 bus, base::Bind(&WeaveService::UpdateWeaveState, base::Unretained(this)));
43 device_->AddComponent(kWeaveComponent, {"_updater"});
44 device_->AddCommandHandler(
45 kWeaveComponent,
46 "_updater.checkForUpdates",
47 base::Bind(&WeaveService::OnCheckForUpdates, base::Unretained(this)));
48 device_->AddCommandHandler(
49 kWeaveComponent,
50 "_updater.trackChannel",
51 base::Bind(&WeaveService::OnTrackChannel, base::Unretained(this)));
52
53 return true;
54}
55
56void WeaveService::UpdateWeaveState() {
57 if (!device_ || !delegate_)
58 return;
59
60 int64_t last_checked_time;
61 double progress;
62 update_engine::UpdateStatus update_status;
63 string current_channel;
64 string tracking_channel;
65
66 if (!delegate_->GetWeaveState(&last_checked_time,
67 &progress,
68 &update_status,
69 &current_channel,
70 &tracking_channel))
71 return;
72
73 // Round to progress to 1% (0.01) to avoid excessive and meaningless state
74 // changes.
75 progress = std::floor(progress * 100.) / 100.;
76
77 brillo::VariantDictionary state{
78 {"_updater.currentChannel", current_channel},
79 {"_updater.trackingChannel", tracking_channel},
80 {"_updater.status", UpdateStatusToWeaveStatus(update_status)},
81 {"_updater.progress", progress},
82 {"_updater.lastUpdateCheckTimestamp",
83 static_cast<double>(last_checked_time)},
84 };
85
86 if (!device_->SetStateProperties(kWeaveComponent, state, nullptr)) {
87 LOG(ERROR) << "Failed to update _updater state.";
88 }
89}
90
91void WeaveService::OnCheckForUpdates(
92 const std::weak_ptr<weaved::Command>& cmd) {
93 auto command = cmd.lock();
94 if (!command)
95 return;
96
97 brillo::ErrorPtr error;
98 if (!delegate_->OnCheckForUpdates(&error)) {
99 command->Abort(error->GetCode(), error->GetMessage(), nullptr);
100 return;
101 }
102 command->Complete({}, nullptr);
103}
104
105void WeaveService::OnTrackChannel(const std::weak_ptr<weaved::Command>& cmd) {
106 auto command = cmd.lock();
107 if (!command)
108 return;
109
110 string channel = command->GetParameter<string>("channel");
111 brillo::ErrorPtr error;
112 if (!delegate_->OnTrackChannel(channel, &error)) {
113 command->Abort(error->GetCode(), error->GetMessage(), nullptr);
114 return;
115 }
116 command->Complete({}, nullptr);
117}
118
119} // namespace chromeos_update_engine