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 <dirent.h>
21 #include <json/json.h>
22 #include <filesystem>
23 #include <fstream>
24 #include <regex>
25
26 namespace android {
27 namespace chre {
28
findAllNanoappsInFolder(const std::string & path,std::vector<std::string> & outNanoapps)29 bool findAllNanoappsInFolder(const std::string &path,
30 std::vector<std::string> &outNanoapps) {
31 DIR *dir = opendir(path.c_str());
32 if (dir == nullptr) {
33 LOGE("Failed to open nanoapp folder %s", path.c_str());
34 return false;
35 }
36 std::regex regex("(\\w+)\\.napp_header");
37 std::cmatch match;
38 for (struct dirent *entry; (entry = readdir(dir)) != nullptr;) {
39 if (!std::regex_match(entry->d_name, match, regex)) {
40 continue;
41 }
42 std::string nanoapp_name = match[1];
43 LOGD("Found nanoapp: %s", nanoapp_name.c_str());
44 outNanoapps.push_back(nanoapp_name);
45 }
46 closedir(dir);
47 return true;
48 }
49
getPreloadedNanoappsFromConfigFile(const std::string & configFilePath,std::string & outDirectory,std::vector<std::string> & outNanoapps)50 bool getPreloadedNanoappsFromConfigFile(const std::string &configFilePath,
51 std::string &outDirectory,
52 std::vector<std::string> &outNanoapps) {
53 std::ifstream configFileStream(configFilePath);
54
55 Json::CharReaderBuilder builder;
56 Json::Value config;
57 if (!configFileStream) {
58 // TODO(b/350102369) to deprecate preloaded_nanoapps.json
59 // During the transition, fall back to the old behavior if the json
60 // file exists. But if the json file does not exist, do the new behavior
61 // to load all nanoapps in /vendor/etc/chre or where ever the location.
62 LOGI("Failed to open config file '%s' load all nanoapps in folder ",
63 configFilePath.c_str());
64 std::filesystem::path path(configFilePath);
65 outDirectory = path.parent_path().string();
66 return findAllNanoappsInFolder(outDirectory, outNanoapps);
67 } else if (!Json::parseFromStream(builder, configFileStream, &config,
68 /* errs = */ nullptr)) {
69 LOGE("Failed to parse nanoapp config file");
70 return false;
71 } else if (!config.isMember("nanoapps") || !config.isMember("source_dir")) {
72 LOGE("Malformed preloaded nanoapps config");
73 return false;
74 }
75
76 outDirectory = config["source_dir"].asString();
77 for (Json::ArrayIndex i = 0; i < config["nanoapps"].size(); ++i) {
78 const std::string &nanoappName = config["nanoapps"][i].asString();
79 outNanoapps.push_back(nanoappName);
80 }
81 return true;
82 }
83
84 } // namespace chre
85 } // namespace android
86