blob: 4d746a1191aeea92a7deba9b3d25b5d1b2e4aa9f [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/developer/feedback/feedback_data/annotations/time_provider.h"
#include <lib/syslog/cpp/macros.h>
#include <lib/zx/time.h>
#include <string>
#include "src/developer/feedback/feedback_data/annotations/utils.h"
#include "src/developer/feedback/feedback_data/constants.h"
#include "src/developer/feedback/utils/errors.h"
#include "src/developer/feedback/utils/time.h"
#include "src/lib/timekeeper/clock.h"
namespace feedback {
namespace {
using timekeeper::Clock;
const AnnotationKeys kSupportedAnnotations = {
kAnnotationDeviceUptime,
kAnnotationDeviceUTCTime,
};
AnnotationOr GetUptime() {
const auto uptime = FormatDuration(zx::nsec(zx_clock_get_monotonic()));
if (!uptime) {
FX_LOGS(ERROR) << "got negative uptime from zx_clock_get_monotonic()";
return AnnotationOr(Error::kBadValue);
}
return AnnotationOr(*uptime);
}
AnnotationOr GetUTCTime(const Clock& clock) {
const auto time = CurrentUTCTime(clock);
if (!time) {
FX_LOGS(ERROR) << "error getting UTC time from timekeeper::Clock::Now()";
return AnnotationOr(Error::kBadValue);
}
return AnnotationOr(*time);
}
} // namespace
TimeProvider::TimeProvider(std::unique_ptr<Clock> clock) : clock_(std::move(clock)) {}
::fit::promise<Annotations> TimeProvider::GetAnnotations(zx::duration timeout,
const AnnotationKeys& allowlist) {
const AnnotationKeys annotations_to_get = RestrictAllowlist(allowlist, kSupportedAnnotations);
if (annotations_to_get.empty()) {
return ::fit::make_result_promise<Annotations>(::fit::ok<Annotations>({}));
}
Annotations annotations;
for (const auto& key : annotations_to_get) {
if (key == kAnnotationDeviceUptime) {
annotations.insert({key, GetUptime()});
} else if (key == kAnnotationDeviceUTCTime) {
annotations.insert({key, GetUTCTime(*clock_)});
}
}
return ::fit::make_ok_promise(annotations);
}
} // namespace feedback