blob: 06cdbf8c33bb25b18228b634822c918118448f01 [file] [log] [blame]
David Pursell54a8fe42017-09-29 16:05:26 -07001/*
2 * Copyright (C) 2017 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 "BootParameters.h"
18
19#define LOG_TAG "BootParameters"
20
21#include <fcntl.h>
22
David Pursell54a8fe42017-09-29 16:05:26 -070023#include <android-base/file.h>
24#include <base/json/json_parser.h>
25#include <base/json/json_reader.h>
26#include <base/json/json_value_converter.h>
27#include <utils/Log.h>
28
29using android::base::RemoveFileIfExists;
30using android::base::ReadFileToString;
31using base::JSONReader;
32using base::JSONValueConverter;
33using base::Value;
34
35namespace android {
36
37namespace {
38
39// Brightness and volume are stored as integer strings in next_boot.json.
40// They are divided by this constant to produce the actual float values in
41// range [0.0, 1.0]. This constant must match its counterpart in
42// DeviceManager.
43constexpr const float kFloatScaleFactor = 1000.0f;
44
45constexpr const char* kNextBootFile = "/data/misc/bootanimation/next_boot.json";
46constexpr const char* kLastBootFile = "/data/misc/bootanimation/last_boot.json";
47
48void swapBootConfigs() {
49 // rename() will fail if next_boot.json doesn't exist, so delete
50 // last_boot.json manually first.
51 std::string err;
52 if (!RemoveFileIfExists(kLastBootFile, &err))
53 ALOGE("Unable to delete last boot file: %s", err.c_str());
54
55 if (rename(kNextBootFile, kLastBootFile) && errno != ENOENT)
56 ALOGE("Unable to swap boot files: %s", strerror(errno));
57
58 int fd = open(kNextBootFile, O_CREAT, DEFFILEMODE);
59 if (fd == -1) {
60 ALOGE("Unable to create next boot file: %s", strerror(errno));
61 } else {
62 // Make next_boot.json writable to everyone so DeviceManagementService
63 // can save saved_parameters there.
64 if (fchmod(fd, DEFFILEMODE))
65 ALOGE("Unable to set next boot file permissions: %s", strerror(errno));
66 close(fd);
67 }
68}
69
70} // namespace
71
72BootParameters::SavedBootParameters::SavedBootParameters()
73 : brightness(-kFloatScaleFactor), volume(-kFloatScaleFactor) {}
74
75void BootParameters::SavedBootParameters::RegisterJSONConverter(
76 JSONValueConverter<SavedBootParameters>* converter) {
77 converter->RegisterIntField("brightness", &SavedBootParameters::brightness);
78 converter->RegisterIntField("volume", &SavedBootParameters::volume);
79 converter->RegisterRepeatedString("param_names",
80 &SavedBootParameters::param_names);
81 converter->RegisterRepeatedString("param_values",
82 &SavedBootParameters::param_values);
83}
84
85BootParameters::BootParameters() {
86 swapBootConfigs();
87 loadParameters();
88}
89
90void BootParameters::loadParameters() {
91 std::string contents;
92 if (!ReadFileToString(kLastBootFile, &contents)) {
93 if (errno != ENOENT)
94 ALOGE("Unable to read from %s: %s", kLastBootFile, strerror(errno));
95
96 return;
97 }
98
Mickey Keeley953f1092018-04-26 11:06:06 -070099 loadParameters(contents);
100}
101
102void BootParameters::loadParameters(const std::string& raw_json) {
103 std::unique_ptr<Value> json = JSONReader::Read(raw_json);
David Pursell54a8fe42017-09-29 16:05:26 -0700104 if (json.get() == nullptr) {
105 return;
106 }
107
108 JSONValueConverter<SavedBootParameters> converter;
109 if (converter.Convert(*(json.get()), &mRawParameters)) {
110 mBrightness = mRawParameters.brightness / kFloatScaleFactor;
111 mVolume = mRawParameters.volume / kFloatScaleFactor;
112
113 if (mRawParameters.param_names.size() == mRawParameters.param_values.size()) {
114 for (size_t i = 0; i < mRawParameters.param_names.size(); i++) {
115 mParameters.push_back({
116 .key = mRawParameters.param_names[i]->c_str(),
117 .value = mRawParameters.param_values[i]->c_str()
118 });
119 }
120 } else {
121 ALOGW("Parameter names and values size mismatch");
122 }
123 }
124}
125
126} // namespace android