1 /*
2 * Copyright (C) 2022 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 "chre_host/config_util.h"
18 #include "chre_host/log.h"
19
20 #include <json/json.h>
21 #include <fstream>
22
23 namespace android {
24 namespace chre {
25
getPreloadedNanoappsFromConfigFile(const std::string & configFilePath,std::string & outDirectory,std::vector<std::string> & outNanoapps)26 bool getPreloadedNanoappsFromConfigFile(const std::string &configFilePath,
27 std::string &outDirectory,
28 std::vector<std::string> &outNanoapps) {
29 std::ifstream configFileStream(configFilePath);
30
31 Json::CharReaderBuilder builder;
32 Json::Value config;
33 if (!configFileStream) {
34 LOGE("Failed to open config file '%s'", configFilePath.c_str());
35 return false;
36 } else if (!Json::parseFromStream(builder, configFileStream, &config,
37 /* errs = */ nullptr)) {
38 LOGE("Failed to parse nanoapp config file");
39 return false;
40 } else if (!config.isMember("nanoapps") || !config.isMember("source_dir")) {
41 LOGE("Malformed preloaded nanoapps config");
42 return false;
43 }
44
45 outDirectory = config["source_dir"].asString();
46 for (Json::ArrayIndex i = 0; i < config["nanoapps"].size(); ++i) {
47 const std::string &nanoappName = config["nanoapps"][i].asString();
48 outNanoapps.push_back(nanoappName);
49 }
50 return true;
51 }
52
53 } // namespace chre
54 } // namespace android
55