blob: 995a3aafae1c302e68aecd19b17f7ddade4e18dc [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
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070021#include <errno.h>
David Pursell54a8fe42017-09-29 16:05:26 -070022#include <fcntl.h>
23
David Pursell54a8fe42017-09-29 16:05:26 -070024#include <android-base/file.h>
David Pursell54a8fe42017-09-29 16:05:26 -070025#include <utils/Log.h>
26
27using android::base::RemoveFileIfExists;
28using android::base::ReadFileToString;
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070029using Json::Reader;
30using Json::Value;
David Pursell54a8fe42017-09-29 16:05:26 -070031
32namespace android {
33
34namespace {
35
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070036// Keys for volume, brightness, and user-defined parameters.
37constexpr const char* kKeyVolume = "volume";
38constexpr const char* kKeyBrightness = "brightness";
39constexpr const char* kKeyParams = "params";
David Pursell54a8fe42017-09-29 16:05:26 -070040
41constexpr const char* kNextBootFile = "/data/misc/bootanimation/next_boot.json";
42constexpr const char* kLastBootFile = "/data/misc/bootanimation/last_boot.json";
43
44void swapBootConfigs() {
45 // rename() will fail if next_boot.json doesn't exist, so delete
46 // last_boot.json manually first.
47 std::string err;
48 if (!RemoveFileIfExists(kLastBootFile, &err))
49 ALOGE("Unable to delete last boot file: %s", err.c_str());
50
51 if (rename(kNextBootFile, kLastBootFile) && errno != ENOENT)
52 ALOGE("Unable to swap boot files: %s", strerror(errno));
53
54 int fd = open(kNextBootFile, O_CREAT, DEFFILEMODE);
55 if (fd == -1) {
56 ALOGE("Unable to create next boot file: %s", strerror(errno));
57 } else {
58 // Make next_boot.json writable to everyone so DeviceManagementService
59 // can save saved_parameters there.
60 if (fchmod(fd, DEFFILEMODE))
61 ALOGE("Unable to set next boot file permissions: %s", strerror(errno));
62 close(fd);
63 }
64}
65
66} // namespace
67
David Pursell54a8fe42017-09-29 16:05:26 -070068BootParameters::BootParameters() {
69 swapBootConfigs();
70 loadParameters();
71}
72
73void BootParameters::loadParameters() {
74 std::string contents;
75 if (!ReadFileToString(kLastBootFile, &contents)) {
76 if (errno != ENOENT)
77 ALOGE("Unable to read from %s: %s", kLastBootFile, strerror(errno));
78
79 return;
80 }
81
Mickey Keeley953f1092018-04-26 11:06:06 -070082 loadParameters(contents);
83}
84
85void BootParameters::loadParameters(const std::string& raw_json) {
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070086 if (!Reader().parse(raw_json, mJson)) {
87 return;
88 }
David Pursell54a8fe42017-09-29 16:05:26 -070089
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070090 // A missing key returns a safe, missing value.
91 // Ignore invalid or missing JSON parameters.
92 Value& jsonValue = mJson[kKeyVolume];
93 if (jsonValue.isDouble()) {
94 mVolume = jsonValue.asFloat();
95 }
David Pursell54a8fe42017-09-29 16:05:26 -070096
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070097 jsonValue = mJson[kKeyBrightness];
98 if (jsonValue.isDouble()) {
99 mBrightness = jsonValue.asFloat();
100 }
101
102 jsonValue = mJson[kKeyParams];
103 if (jsonValue.isObject()) {
104 for (auto &key : jsonValue.getMemberNames()) {
105 Value& value = jsonValue[key];
106 if (value.isString()) {
107 mParameters.push_back({
108 .key = key.c_str(),
109 .value = value.asCString()
110 });
111 }
David Pursell54a8fe42017-09-29 16:05:26 -0700112 }
Mickey Keeleydfaa9c52018-04-26 15:05:25 -0700113 }
David Pursell54a8fe42017-09-29 16:05:26 -0700114}
115
116} // namespace android