/* * Copyright (c) 2019 TOYOTA MOTOR CORPORATION * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "hs-config.h" const std::array HS_Config::keys_recover_type = { // based on hs-conf.json "hs-apps", "default-lastmode", "normal-apps" }; const std::string HS_Config::lastmode_json = "lastmode.json"; const std::string HS_Config::key_appid = "appid"; const std::string HS_Config::key_visibility = "visibility"; const std::string HS_Config::key_after = "after"; std::string HS_Config::root_dir = ""; /** * read configuration file to memory * * #### Parameters * - Nothing * * #### Return * None * */ int HS_Config::readConfig(void) { root_dir = std::string(getenv("AFM_APP_INSTALL_DIR")); auto path = root_dir + "/etc/" + hs_conf_json; if(readJsonFile(path.c_str(), &m_hs_conf) < 0) { AFB_ERROR("read %s failed.", hs_conf_json.c_str()); return -1; } path = root_dir + "/etc/" + lastmode_json; if(readJsonFile(path.c_str(), &m_lastmode) < 0) { AFB_WARNING("read %s failed.", lastmode_json.c_str()); m_lastmode = nullptr; } return parseConfig(); } /** * parse configuration file contents * * #### Parameters * - Nothing * * #### Return * 0 : success * 1 : fail * */ int HS_Config::parseConfig(void) { struct json_object *handshake_obj, *times_obj, *sleep_obj; if(json_object_object_get_ex(m_hs_conf, key_handshake.c_str(), &handshake_obj) && json_object_object_get_ex(handshake_obj, key_times.c_str(), ×_obj) && json_object_object_get_ex(handshake_obj, key_sleep.c_str(), &sleep_obj)) { m_handshake_info.times = json_object_get_int(times_obj); m_handshake_info.sleep = json_object_get_int(sleep_obj); } else { AFB_WARNING("get handshake info error, use default value."); } struct json_object *recover_obj; if(json_object_object_get_ex(m_hs_conf, key_recover.c_str(), &recover_obj)) { for(auto &m : keys_recover_type) { struct json_object *obj; if(json_object_object_get_ex(recover_obj, m.c_str(), &obj)) { if(json_object_get_type(obj) != json_type_array ) { continue; } m_recover_map[m] = std::move(getRecoverAppInfo(obj)); } } if(m_recover_map.empty()) { AFB_ERROR("get homescreen recover list failed."); return -1; } } else { AFB_ERROR("get homescreen recover object failed."); return -1; } if(json_object_get_type(m_lastmode) == json_type_array ) { struct std::vector v_lastmode = std::move(getRecoverAppInfo(m_lastmode)); if(!v_lastmode.empty()) { // got saving lastmode isn't null, instead of default lastmode for(auto &it : v_lastmode) { if(it.after.empty() && !m_recover_map[keys_recover_type[1]].empty()) { it.after = m_recover_map[keys_recover_type[1]][0].after; } } m_recover_map[keys_recover_type[1]] = std::move(v_lastmode); } } return 0; } /** * get recover application information * appid, visibility * * #### Parameters * - obj : application information * * #### Return * recover_app_info vector * */ std::vector HS_Config::getRecoverAppInfo(struct json_object *obj) { int array_len = json_object_array_length(obj); std::vector v_app_info; for (int i = 0; i < array_len; ++i) { struct json_object *info_obj = json_object_array_get_idx(obj, i); struct recover_app_info info; struct json_object *value_obj; if(json_object_object_get_ex(info_obj, key_appid.c_str(), &value_obj)) { info.appid = json_object_get_string(value_obj); } else { AFB_ERROR("recover infomation doesn't include appid."); v_app_info.clear(); return v_app_info; } if(json_object_object_get_ex(info_obj, key_visibility.c_str(), &value_obj)) { std::string visibility = json_object_get_string(value_obj); info.visibility = (visibility == "visible") ? true:false; } else { info.visibility = false; } if(json_object_object_get_ex(info_obj, key_after.c_str(), &value_obj)) { info.after = json_object_get_string(value_obj); } else { info.after.clear(); } v_app_info.push_back(info); } return v_app_info; }