add hs-config
[apps/agl-service-homescreen.git] / src / hs-config.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
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
18 #include "hs-config.h"
19
20
21 /**
22  * read configuration file to memory
23  *
24  * #### Parameters
25  *  - Nothing
26  *
27  * #### Return
28  * None
29  *
30  */
31 int HS_Config::readConfig(void)
32 {
33     auto rootdir = std::string(getenv("AFM_APP_INSTALL_DIR"));
34     auto path = rootdir + "/etc/" + hs_conf_json;
35     if(readJsonFile(path.c_str(), &m_hs_conf) < 0) {
36         AFB_ERROR("read %s failed.", hs_conf_json.c_str());
37         return -1;
38     }
39
40     path = rootdir + "/etc/" + lastmode_json;
41     if(readJsonFile(path.c_str(), &m_lastmode) < 0) {
42         AFB_ERROR("read %s failed.", lastmode_json.c_str());
43         m_lastmode = nullptr;
44     }
45
46     return parseConfig();
47 }
48
49 /**
50  * parse configuration file contents
51  *
52  * #### Parameters
53  *  - Nothing
54  *
55  * #### Return
56  * 0 : success
57  * 1 : fail
58  *
59  */
60 int HS_Config::parseConfig(void)
61 {
62     struct json_object *handshake_obj, *times_obj, *sleep_obj;
63     if(json_object_object_get_ex(m_hs_conf, key_handshake.c_str(), &handshake_obj)
64     && json_object_object_get_ex(handshake_obj, key_times.c_str(), &times_obj)
65     && json_object_object_get_ex(handshake_obj, key_sleep.c_str(), &sleep_obj)) {
66         m_handshake_info.times = json_object_get_int(times_obj);
67         m_handshake_info.sleep = json_object_get_int(sleep_obj);
68     }
69     else {
70         AFB_WARNING("get handshake info error, use default value.");
71     }
72
73     struct json_object *recover_obj;
74     if(json_object_object_get_ex(m_hs_conf, key_recover.c_str(), &recover_obj)) {
75         for(auto &m : keys_recover_type) {
76             struct json_object *obj;
77             if(json_object_object_get_ex(recover_obj, m.c_str(), &obj)) {
78                 if(json_object_get_type(obj) != json_type_array ) {
79                     continue;
80                 }
81                 m_recover_map[m] = std::move(getRecoverAppInfo(obj));
82             }
83         }
84
85         if(m_recover_map.empty()) {
86             AFB_ERROR("get homescreen recover list failed.");
87             return -1;
88         }
89     }
90     else {
91         AFB_ERROR("get homescreen recover object failed.");
92         return -1;
93     }
94
95     if(json_object_get_type(m_lastmode) == json_type_array ) {
96         struct std::vector<recover_app_info> v_lastmode = std::move(getRecoverAppInfo(m_lastmode));
97         if(!v_lastmode.empty()) {   // got saving lastmode isn't null, instead of default lastmode
98             m_recover_map[keys_recover_type[1]] = std::move(v_lastmode);
99         }
100     }
101
102     return 0;
103 }
104
105 /**
106  * get recover application information
107  * appid, visibility, display area
108  *
109  * #### Parameters
110  *  - obj : application information
111  *
112  * #### Return
113  * recover_app_info vector
114  *
115  */
116 std::vector<struct recover_app_info> HS_Config::getRecoverAppInfo(struct json_object *obj)
117 {
118     int array_len = json_object_array_length(obj);
119     std::vector<struct recover_app_info> v_app_info;
120     for (int i = 0; i < array_len; ++i) {
121         struct json_object *info_obj = json_object_array_get_idx(obj, i);
122         struct recover_app_info info;
123         struct json_object *value_obj;
124         if(json_object_object_get_ex(info_obj, key_appid.c_str(), &value_obj)) {
125             info.appid = json_object_get_string(value_obj);
126         }
127         else {
128             AFB_ERROR("recover infomation doesn't include appid.");
129             v_app_info.clear();
130             return v_app_info;
131         }
132         if(json_object_object_get_ex(info_obj, key_area.c_str(), &value_obj)) {
133             info.area = json_object_get_string(value_obj);
134         }
135         else {
136             info.area = "normal.full";
137         }
138         if(json_object_object_get_ex(info_obj, key_visibility.c_str(), &value_obj)) {
139             std::string visibility = json_object_get_string(value_obj);
140             info.visibility = (visibility == "visible") ? true:false;
141         }
142         else {
143             info.visibility = false;
144         }
145         v_app_info.push_back(info);
146     }
147
148     return v_app_info;
149 }