add hs-config
authorwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>
Sat, 13 Apr 2019 08:21:46 +0000 (16:21 +0800)
committerwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>
Sat, 13 Apr 2019 08:21:46 +0000 (16:21 +0800)
Change-Id: Id007f025ad647faf502c1bee6f5de8de4298df7e

conf/hs-conf.json
src/CMakeLists.txt
src/homescreen.cpp
src/hs-config.cpp [new file with mode: 0644]
src/hs-config.h [new file with mode: 0644]

index 84a8386..e50ad8a 100644 (file)
@@ -3,32 +3,36 @@
                "times": 20000,
                "sleep": 50
        },
-       "hs-apps": [
-               {
-                       "appid": "homescreen",
-                       "visibility": "visible"
-               },
-               {
-                       "appid": "launcher",
-                       "visibility": "invisible"
-               },
-               {
-                       "appid": "onscreenapp",
-                       "visibility": "invisible"
-               },
-               {
-                       "appid": "restriction",
-                       "visibility": "invisible"
-               }
-       ],
-       "default-lastmode": [
-               {
-                       "appid": "launcher",
-                       "area": "normal.full"
-               }
-       ],
-       "normal-apps": {
-                       "appid": "warehouse",
-                       "visibility": "invisible"
+       "recover" {
+               "hs-apps": [
+                       {
+                               "appid": "homescreen",
+                               "visibility": "visible"
+                       },
+                       {
+                               "appid": "launcher",
+                               "visibility": "invisible"
+                       },
+                       {
+                               "appid": "onscreenapp",
+                               "visibility": "invisible"
+                       },
+                       {
+                               "appid": "restriction",
+                               "visibility": "invisible"
+                       }
+               ],
+               "default-lastmode": [
+                       {
+                               "appid": "launcher",
+                               "area": "normal.full"
+                       }
+               ],
+               "normal-apps": [
+                       {
+                               "appid": "warehouse",
+                               "visibility": "invisible"
+                       }
+               ]
        }
 }
\ No newline at end of file
index 53055b4..7085bac 100644 (file)
@@ -28,7 +28,8 @@ set(binding_hs_sources
   hs-clientmanager.cpp
   hs-client.cpp
   hs-proxy.cpp
-  hs-appinfo.cpp)
+  hs-appinfo.cpp
+  hs-config.cpp)
 
 link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined)
 include_directories(${PROJECT_SOURCE_DIR}/include)
index c9ad6ef..7b8e653 100644 (file)
@@ -24,6 +24,7 @@
 #include "hs-helper.h"
 #include "hs-clientmanager.h"
 #include "hs-appinfo.h"
+#include "hs-config.h"
 
 
 const char _error[] = "error";
@@ -32,23 +33,6 @@ const char _display_message[] = "display_message";
 const char _reply_message[] = "reply_message";
 const char _keyData[] = "data";
 const char _keyId[] = "id";
-const char _keyHandshake[] = "handshake";
-const char _keyTimes[] = "times";
-const char _keySleep[] = "sleep";
-const char _hs_conf_json[] = "hs-conf.json";
-const char _lastmode_json[] = "lastmode.json";
-
-struct hs_config {
-    struct json_object *hs_conf;
-    struct json_object *lastmode;
-};
-static struct hs_config g_hs_config;
-
-const char _wm_event[] = "windowmanager/screenUpdated";
-static void screenUpdateCb(void *closure, const char *event, struct json_object* obj, afb_api_t api)
-{
-    AFB_WARNING("windowmanager/screenUpdated callback. obj=%s.", json_object_to_json_string(obj));
-}
 
 struct hs_instance {
   HS_ClientManager *client_manager;   // the connection session manager
@@ -76,28 +60,24 @@ private:
 int hs_instance::init(afb_api_t api)
 {
     if(client_manager == nullptr) {
-        AFB_ERROR("FATAL ERROR: client_manager is nullptr.");
+        AFB_ERROR("client_manager is nullptr.");
         return -1;
     }
     client_manager->init();
 
     if(app_info == nullptr) {
-        AFB_ERROR("FATAL ERROR: app_info is nullptr.");
+        AFB_ERROR("app_info is nullptr.");
         return -1;
     }
     app_info->init(api);
 
-    struct json_object *handshake_obj;
-    if(json_object_object_get_ex(g_hs_config.hs_conf, _keyHandshake, &handshake_obj) == 0) {
-        AFB_ERROR("get handshake failed.");
+    HS_Config hs_config;
+    if(hs_config.readConfig() < 0) {
+        AFB_ERROR("read config file failed.");
         return -1;
     }
-    else {
-        struct json_object *times_obj, *sleep_obj;
-        json_object_object_get_ex(handshake_obj, _keyTimes, &times_obj);
-        json_object_object_get_ex(handshake_obj, _keySleep, &sleep_obj);
-        AFB_WARNING("get handshake times=%d, sleep=%d", json_object_get_int(times_obj), json_object_get_int(sleep_obj));
-    }
+
+    // handshake
 
     return 0;
 }
@@ -563,18 +543,6 @@ static const afb_verb_t verbs[]= {
 static int preinit(afb_api_t api)
 {
     AFB_DEBUG("binding preinit (was register)");
-    auto rootdir = std::string(getenv("AFM_APP_INSTALL_DIR"));
-    auto path = rootdir + "/etc/" + _hs_conf_json;
-    if(readJsonFile(path.c_str(), &g_hs_config.hs_conf) < 0) {
-        AFB_ERROR("read %s failed.", _hs_conf_json);
-        return -1;
-    }
-
-    path = rootdir + "/etc/" + _lastmode_json;
-    if(readJsonFile(path.c_str(), &g_hs_config.lastmode) < 0) {
-        AFB_ERROR("read %s failed.", _lastmode_json);
-        return -1;
-    }
     return 0;
 }
 
diff --git a/src/hs-config.cpp b/src/hs-config.cpp
new file mode 100644 (file)
index 0000000..dad3a7e
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * 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"
+
+
+/**
+ * read configuration file to memory
+ *
+ * #### Parameters
+ *  - Nothing
+ *
+ * #### Return
+ * None
+ *
+ */
+int HS_Config::readConfig(void)
+{
+    auto rootdir = std::string(getenv("AFM_APP_INSTALL_DIR"));
+    auto path = rootdir + "/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 = rootdir + "/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, display area
+ *
+ * #### 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_area.c_str(), &value_obj)) {
+            info.area = json_object_get_string(value_obj);
+        }
+        else {
+            info.area = "normal.full";
+        }
+        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;
+}
\ No newline at end of file
diff --git a/src/hs-config.h b/src/hs-config.h
new file mode 100644 (file)
index 0000000..7cc5dd3
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+#ifndef HOMESCREEN_CONFIG_H
+#define HOMESCREEN_CONFIG_H
+
+#include <array>
+#include <vector>
+#include <unordered_map>
+#include "hs-helper.h"
+
+struct handshake_info {
+    int times;    // sleep times
+    int sleep;    // sleep x ms
+};
+
+struct recover_app_info {
+    std::string appid;  // application id like "dashboard"
+    std::string area;   // application default display area
+    bool visibility;    // the visibility when system starting
+};
+
+using recover_map = std::unordered_map<std::string, std::vector<struct recover_app_info>>;
+
+class HS_Config {
+public:
+    HS_Config() :  m_hs_conf(nullptr), m_lastmode(nullptr), m_handshake_info({20000, 50}) {}
+    ~HS_Config() {json_object_put(m_hs_conf);json_object_put(m_lastmode);}
+    HS_Config(HS_Config const &) = delete;
+    HS_Config &operator=(HS_Config const &) = delete;
+    HS_Config(HS_Config &&) = delete;
+    HS_Config &operator=(HS_Config &&) = delete;
+
+    int readConfig(void);
+    struct handshake_info getHandshakeInfo(void) {return m_handshake_info;}
+    recover_map getRecoverMap(void) {return m_recover_map;}
+
+private:
+    int parseConfig(void);
+    std::vector<struct recover_app_info> getRecoverAppInfo(struct json_object *obj);
+
+    const std::string hs_conf_json = "hs-conf.json";
+    const std::string lastmode_json = "lastmode.json";
+    const std::string key_handshake = "handshake";
+    const std::string key_times = "times";
+    const std::string key_sleep = "sleep";
+    const std::string key_recover = "recover";
+    const std::string key_appid = "appid";
+    const std::string key_visibility = "visibility";
+    const std::string key_area = "area";
+    const std::array<std::string, 3> keys_recover_type = {   // based on hs-conf.json
+        "hs-apps",
+        "default-lastmode",
+        "normal-apps"
+    };
+
+    struct json_object *m_hs_conf;
+    struct json_object *m_lastmode;
+    recover_map m_recover_map;
+    struct handshake_info m_handshake_info;
+};
+#endif // HOMESCREEN_CONFIG_H
\ No newline at end of file