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