aboutsummaryrefslogtreecommitdiffstats
path: root/src/hs-config.cpp
blob: c0d6942a622d016afc99d92ffe3e7840c1ffd43f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * 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<std::string, 3> 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";
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_ERROR("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(), &times_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<recover_app_info> v_lastmode = std::move(getRecoverAppInfo(m_lastmode));
        if(!v_lastmode.empty()) {   // got saving lastmode isn't null, instead of default lastmode
            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<struct recover_app_info> HS_Config::getRecoverAppInfo(struct json_object *obj)
{
    int array_len = json_object_array_length(obj);
    std::vector<struct recover_app_info> 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;
        }
        v_app_info.push_back(info);
    }

    return v_app_info;
}