1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker *
4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker *
8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker *
10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker */
16*84e33947SAndroid Build Coastguard Worker
17*84e33947SAndroid Build Coastguard Worker #include "chre_host/config_util.h"
18*84e33947SAndroid Build Coastguard Worker #include "chre_host/log.h"
19*84e33947SAndroid Build Coastguard Worker
20*84e33947SAndroid Build Coastguard Worker #include <dirent.h>
21*84e33947SAndroid Build Coastguard Worker #include <json/json.h>
22*84e33947SAndroid Build Coastguard Worker #include <filesystem>
23*84e33947SAndroid Build Coastguard Worker #include <fstream>
24*84e33947SAndroid Build Coastguard Worker #include <regex>
25*84e33947SAndroid Build Coastguard Worker
26*84e33947SAndroid Build Coastguard Worker namespace android {
27*84e33947SAndroid Build Coastguard Worker namespace chre {
28*84e33947SAndroid Build Coastguard Worker
findAllNanoappsInFolder(const std::string & path,std::vector<std::string> & outNanoapps)29*84e33947SAndroid Build Coastguard Worker bool findAllNanoappsInFolder(const std::string &path,
30*84e33947SAndroid Build Coastguard Worker std::vector<std::string> &outNanoapps) {
31*84e33947SAndroid Build Coastguard Worker DIR *dir = opendir(path.c_str());
32*84e33947SAndroid Build Coastguard Worker if (dir == nullptr) {
33*84e33947SAndroid Build Coastguard Worker LOGE("Failed to open nanoapp folder %s", path.c_str());
34*84e33947SAndroid Build Coastguard Worker return false;
35*84e33947SAndroid Build Coastguard Worker }
36*84e33947SAndroid Build Coastguard Worker std::regex regex("(\\w+)\\.napp_header");
37*84e33947SAndroid Build Coastguard Worker std::cmatch match;
38*84e33947SAndroid Build Coastguard Worker for (struct dirent *entry; (entry = readdir(dir)) != nullptr;) {
39*84e33947SAndroid Build Coastguard Worker if (!std::regex_match(entry->d_name, match, regex)) {
40*84e33947SAndroid Build Coastguard Worker continue;
41*84e33947SAndroid Build Coastguard Worker }
42*84e33947SAndroid Build Coastguard Worker std::string nanoapp_name = match[1];
43*84e33947SAndroid Build Coastguard Worker LOGD("Found nanoapp: %s", nanoapp_name.c_str());
44*84e33947SAndroid Build Coastguard Worker outNanoapps.push_back(nanoapp_name);
45*84e33947SAndroid Build Coastguard Worker }
46*84e33947SAndroid Build Coastguard Worker closedir(dir);
47*84e33947SAndroid Build Coastguard Worker return true;
48*84e33947SAndroid Build Coastguard Worker }
49*84e33947SAndroid Build Coastguard Worker
getPreloadedNanoappsFromConfigFile(const std::string & configFilePath,std::string & outDirectory,std::vector<std::string> & outNanoapps)50*84e33947SAndroid Build Coastguard Worker bool getPreloadedNanoappsFromConfigFile(const std::string &configFilePath,
51*84e33947SAndroid Build Coastguard Worker std::string &outDirectory,
52*84e33947SAndroid Build Coastguard Worker std::vector<std::string> &outNanoapps) {
53*84e33947SAndroid Build Coastguard Worker std::ifstream configFileStream(configFilePath);
54*84e33947SAndroid Build Coastguard Worker
55*84e33947SAndroid Build Coastguard Worker Json::CharReaderBuilder builder;
56*84e33947SAndroid Build Coastguard Worker Json::Value config;
57*84e33947SAndroid Build Coastguard Worker if (!configFileStream) {
58*84e33947SAndroid Build Coastguard Worker // TODO(b/350102369) to deprecate preloaded_nanoapps.json
59*84e33947SAndroid Build Coastguard Worker // During the transition, fall back to the old behavior if the json
60*84e33947SAndroid Build Coastguard Worker // file exists. But if the json file does not exist, do the new behavior
61*84e33947SAndroid Build Coastguard Worker // to load all nanoapps in /vendor/etc/chre or where ever the location.
62*84e33947SAndroid Build Coastguard Worker LOGI("Failed to open config file '%s' load all nanoapps in folder ",
63*84e33947SAndroid Build Coastguard Worker configFilePath.c_str());
64*84e33947SAndroid Build Coastguard Worker std::filesystem::path path(configFilePath);
65*84e33947SAndroid Build Coastguard Worker outDirectory = path.parent_path().string();
66*84e33947SAndroid Build Coastguard Worker return findAllNanoappsInFolder(outDirectory, outNanoapps);
67*84e33947SAndroid Build Coastguard Worker } else if (!Json::parseFromStream(builder, configFileStream, &config,
68*84e33947SAndroid Build Coastguard Worker /* errs = */ nullptr)) {
69*84e33947SAndroid Build Coastguard Worker LOGE("Failed to parse nanoapp config file");
70*84e33947SAndroid Build Coastguard Worker return false;
71*84e33947SAndroid Build Coastguard Worker } else if (!config.isMember("nanoapps") || !config.isMember("source_dir")) {
72*84e33947SAndroid Build Coastguard Worker LOGE("Malformed preloaded nanoapps config");
73*84e33947SAndroid Build Coastguard Worker return false;
74*84e33947SAndroid Build Coastguard Worker }
75*84e33947SAndroid Build Coastguard Worker
76*84e33947SAndroid Build Coastguard Worker outDirectory = config["source_dir"].asString();
77*84e33947SAndroid Build Coastguard Worker for (Json::ArrayIndex i = 0; i < config["nanoapps"].size(); ++i) {
78*84e33947SAndroid Build Coastguard Worker const std::string &nanoappName = config["nanoapps"][i].asString();
79*84e33947SAndroid Build Coastguard Worker outNanoapps.push_back(nanoappName);
80*84e33947SAndroid Build Coastguard Worker }
81*84e33947SAndroid Build Coastguard Worker return true;
82*84e33947SAndroid Build Coastguard Worker }
83*84e33947SAndroid Build Coastguard Worker
84*84e33947SAndroid Build Coastguard Worker } // namespace chre
85*84e33947SAndroid Build Coastguard Worker } // namespace android
86