Fix place of connection.json
[apps/agl-service-windowmanager.git] / policy_manager / policy_manager.cpp
index 1c6995b..bf70614 100644 (file)
@@ -26,7 +26,7 @@
 
 extern "C"
 {
-#define AFB_BINDING_VERSION 3
+#define AFB_BINDING_VERSION 2
 #include <afb/afb-binding.h>
 #include <systemd/sd-event.h>
 #include "stm.h"
@@ -34,6 +34,9 @@ extern "C"
 
 namespace pm
 {
+static const char kPathRolesConfigFile[] = "/etc/roles.json";
+static const char kPathLayoutsConfigFile[] = "/etc/layouts.json";
+
 static const int kInvisibleRoleHistoryNum = 5;
 
 static PolicyManager *g_context;
@@ -59,12 +62,24 @@ PolicyManager::PolicyManager()
       role2category(),
       category2role(),
       category2areas()
-{}
+{
+    this->p_crr_state = new (StmState);
+    this->p_prv_state = new (StmState);
+}
+
+PolicyManager::~PolicyManager()
+{
+    delete this->p_crr_state;
+    delete this->p_prv_state;
+}
 
-int PolicyManager::initialize()
+int PolicyManager::initialize(std::string ecu_name)
 {
     int ret = 0;
 
+    // Set ECU name
+    this->ecu_name = ecu_name;
+
     // Create convert map
     for (int i = StmEvtNoMin; i <= StmEvtNoMax; i++)
     {
@@ -84,19 +99,19 @@ int PolicyManager::initialize()
         this->areaname2no[kStmAreaName[i]] = i;
     }
 
-    // Load roles.db
-    ret = this->loadRoleDb();
+    // Load roles config
+    ret = this->loadRolesConfigFile();
     if (0 > ret)
     {
-        HMI_ERROR("Load roles.db Error!!");
+        HMI_ERROR("Load roles config file Error!!");
         return ret;
     }
 
-    // Load states.db
-    ret = this->loadStateDb();
+    // Load layouts config
+    ret = this->loadLayoutsConfigFile();
     if (0 > ret)
     {
-        HMI_ERROR("Load states.db Error!!");
+        HMI_ERROR("Load layouts config file Error!!");
         return ret;
     }
 
@@ -104,7 +119,7 @@ int PolicyManager::initialize()
     this->initializeState();
 
     // Initialize StateTransitioner
-    stmInitialize();
+    stmInitialize(ecu_name.c_str());
 
     // Store instance
     pm::g_context = this;
@@ -170,7 +185,7 @@ int PolicyManager::setInputEventData(json_object *json_in)
             itr = this->role2category.find("fallback");
             if (this->role2category.end() != itr)
             {
-                HMI_DEBUG("Role:%s is not registered in roles.db, fallback as normal app", role);
+                HMI_DEBUG("Role:%s is not registered in roles config file, fallback as normal app", role);
                 category = this->role2category["fallback"];
             }
         }
@@ -246,6 +261,7 @@ void PolicyManager::undoState()
     this->dumpLayerState(this->crr_layers);
 
     this->crr_layers = this->prv_layers;
+    this->crr_invisible_role_history = this->prv_invisible_role_history;
 
     HMI_DEBUG(">>>>>>>>>> AFTER UNDO");
     this->dumpLayerState(this->crr_layers);
@@ -253,9 +269,25 @@ void PolicyManager::undoState()
 
 void PolicyManager::initializeState()
 {
+    this->initializeModeState();
     this->initializeLayerState();
 }
 
+void PolicyManager::initializeModeState()
+{
+    Mode init_car_ele;
+    init_car_ele.state = "none";
+    init_car_ele.changed = false;
+
+    for (int i = StmCarElementNoMin; i <= StmCarElementNoMax; i++)
+    {
+        const char *car_ele_name = kStmCarElementName[i];
+        this->crr_car_elements[car_ele_name] = init_car_ele;
+    }
+
+    this->prv_car_elements = this->crr_car_elements;
+}
+
 void PolicyManager::initializeLayerState()
 {
     AreaState init_area;
@@ -315,47 +347,109 @@ void PolicyManager::addStateToJson(const char *layer_name, bool changed,
     json_object_object_add(*json_out, "areas", json_areas);
 }
 
-void PolicyManager::updateState(int event_id, StmState crr_state)
+void PolicyManager::updateState(int event_id)
 {
-    this->updateLayer(event_id, crr_state);
+    this->updateModeState();
+    this->updateLayer(event_id);
+}
+
+void PolicyManager::updateModeState()
+{
+    int car_state_no;
+    std::string car_state;
+    bool changed;
+
+    // Store previous layers
+    this->prv_car_elements = this->crr_car_elements;
+
+    // Update car elements
+    HMI_DEBUG(">>> CAR ELEMENTS");
+    for (int car_ele_no = StmCarElementNoMin;
+         car_ele_no <= StmCarElementNoMax; car_ele_no++)
+    {
+        const char *car_ele_name = kStmCarElementName[car_ele_no];
+
+        car_state_no = this->p_crr_state->car_element[car_ele_no].state;
+        car_state = kStmCarElementStateNameList[car_ele_no][car_state_no];
+        changed = (this->p_crr_state->car_element[car_ele_no].changed) ? true : false;
+
+        this->crr_car_elements[car_ele_name].state = car_state;
+        this->crr_car_elements[car_ele_name].changed = changed;
+
+        HMI_DEBUG(">>> >>> NAME: %s", car_ele_name);
+        HMI_DEBUG(">>> >>> >>> STATE:%s", car_state.c_str());
+        HMI_DEBUG(">>> >>> >>> CHANGED:%s", (changed) ? "true" : "false");
+    }
 }
 
-void PolicyManager::updateLayer(int event_id, StmState crr_state)
+void PolicyManager::updateLayer(int event_id)
 {
     for (int layer_no = StmLayerNoMin;
          layer_no <= StmLayerNoMax; layer_no++)
     {
         HMI_DEBUG(">>> LAYER:%s CHANGED:%d LAYOUT:%s",
-                  kStmLayerName[layer_no], crr_state.layer[layer_no].changed,
-                  kStmLayoutName[crr_state.layer[layer_no].state]);
+                  kStmLayerName[layer_no], this->p_crr_state->layer[layer_no].changed,
+                  kStmLayoutName[this->p_crr_state->layer[layer_no].state]);
     }
 
     // Store previous layers
     this->prv_layers = this->crr_layers;
 
+    // Store previous role history
+    this->prv_invisible_role_history = this->crr_invisible_role_history;
+
     // Update layers
     for (int layer_no = StmLayerNoMin;
          layer_no <= StmLayerNoMax; layer_no++)
     {
         const char *layer_name = kStmLayerName[layer_no];
 
+        // If restriction mode is changed to mode2 on,
+        // store current state for state of restriction mode off
+        if (this->changedRestrictionModeTo2On() ||
+            this->changedAccelPedalOffToOn())   // Control by Accel
+            // this->changedLightstatusBrakeOnToOff())   // Control by Brake
+        {
+            HMI_DEBUG("Store current state for state of restriction mode off");
+            this->prv_layers_car_stop[layer_name] = this->crr_layers[layer_name];
+        }
+
         // This layer is changed?
-        int changed = crr_state.layer[layer_no].changed;
+        int changed = this->p_crr_state->layer[layer_no].changed;
         if (changed)
         {
             HMI_DEBUG(">>>>>>>>>> Update layout of layer:%s", layer_name);
 
             // Get current layout name of this layer
-            int crr_layout_state_no = crr_state.layer[layer_no].state;
+            int crr_layout_state_no = this->p_crr_state->layer[layer_no].state;
             std::string crr_layout_name = std::string(kStmLayoutName[crr_layout_state_no]);
 
             LayoutState crr_layout_state;
-            this->updateLayout(event_id, layer_no,
-                               crr_layout_name, crr_layout_state);
+            changed = this->updateLayout(event_id, layer_no,
+                                         crr_layout_name, crr_layout_state);
 
             // Update current layout of this layer
             this->crr_layers[layer_name].layout_state = crr_layout_state;
         }
+        else
+        {
+            int category_no = STM_GET_CATEGORY_FROM_ID(event_id);
+            std::string req_ctg = kStmCategoryName[category_no];
+            std::string req_role = this->req_role_list[event_id];
+            for (const auto &ctg : this->layer2categories[layer_name])
+            {
+                if (ctg == req_ctg)
+                {
+                    // If layer is not changed and requested role is in this layer,
+                    // push requested role to history stack
+                    // because the application which has this role have been started
+                    HMI_DEBUG("Add requested role to history "
+                              "because the application which has this role have been started");
+                    this->pushInvisibleRoleHistory(req_ctg, req_role);
+                }
+            }
+        }
+
         // Update changed flag
         this->crr_layers[layer_name].changed = (changed) ? true : false;
     }
@@ -375,7 +469,7 @@ void PolicyManager::updateLayer(int event_id, StmState crr_state)
 int PolicyManager::updateLayout(int event_id, int layer_no,
                                 std::string crr_layout_name, LayoutState &crr_layout_state)
 {
-    int changed;
+    int changed = 1;
 
     int event_no = STM_GET_EVENT_FROM_ID(event_id);
     int category_no = STM_GET_CATEGORY_FROM_ID(event_id);
@@ -392,7 +486,37 @@ int PolicyManager::updateLayout(int event_id, int layer_no,
     LayoutState prv_layout_state = this->prv_layers[layer_name].layout_state;
     std::string prv_layout_name = prv_layout_state.name;
 
-    if ((prv_layout_name == crr_layout_name) &&
+    if (this->changedRestrictionMode2OnToOther() ||
+        this->changedAccelPedalOnToOff())   // Control by Accel
+        // this->changedLightstatusBrakeOffToOn())   // Control by Brake
+    {
+        // If restriction mode is changed from mode2 -> mode1,
+        // restore state of restriction mode off
+        HMI_DEBUG("Restriction mode is changed from mode2 -> mode1, so restore state of restriction mode off");
+        crr_layout_state = this->prv_layers_car_stop[layer_name].layout_state;
+        crr_layout_name = crr_layout_state.name;
+        if ((prv_layout_name == crr_layout_name) &&
+            (kStmAreaName[StmAreaNoNone] == crr_layout_name))
+        {
+            changed = 0;
+        }
+        else
+        {
+            // If the roles which is exist in previous layout is not in current,
+            // push to role history
+            for (const auto &prv_as : prv_layout_state.area_list)
+            {
+                for (const auto &crr_as : crr_layout_state.area_list)
+                {
+                    if (prv_as.role == crr_as.role)
+                        break;
+                }
+
+                this->pushInvisibleRoleHistory(prv_as.category, prv_as.role);
+            }
+        }
+    }
+    else if ((prv_layout_name == crr_layout_name) &&
         (kStmLayoutName[StmLayoutNoNone] == crr_layout_name))
     {
         // If previous and current layout are none
@@ -572,10 +696,35 @@ int PolicyManager::updateLayout(int event_id, int layer_no,
     return changed;
 }
 
-void PolicyManager::createOutputInformation(StmState crr_state, json_object **json_out)
+void PolicyManager::createOutputInformation(json_object **json_out)
 {
     json_object *json_tmp;
 
+    // Create car element information
+    // {
+    //     "car_elements": [
+    //     {
+    //         "parking_brake": {
+    //             "changed": <bool>,
+    //             "state": <const char*>
+    //         },
+    //         ...
+    //     },
+    json_object *json_car_ele = json_object_new_array();
+    const char *car_ele_name;
+    for (int car_ele_no = StmCarElementNoMin;
+         car_ele_no <= StmCarElementNoMax; car_ele_no++)
+    {
+        car_ele_name = kStmCarElementName[car_ele_no];
+        json_tmp = json_object_new_object();
+        this->addStateToJson(car_ele_name,
+                             this->crr_car_elements[car_ele_name].changed,
+                             this->crr_car_elements[car_ele_name].state,
+                             &json_tmp);
+        json_object_array_add(json_car_ele, json_tmp);
+    }
+    json_object_object_add(*json_out, "car_elements", json_car_ele);
+
     // Create layout information
     //
     //     "layers": [
@@ -608,6 +757,39 @@ void PolicyManager::createOutputInformation(StmState crr_state, json_object **js
     json_object_object_add(*json_out, "layers", json_layer);
 }
 
+void PolicyManager::controlTimerEvent()
+{
+    // for ALS2018 (RestrictionMode = Running and after 3sec)
+    // if (this->p_crr_state->car_element[StmCarElementNoRunning].changed)
+    // {
+    //     if (StmRunningNoRun == this->p_crr_state->car_element[StmCarElementNoRunning].state)
+    //     {
+    //         // Set delay event(restriction mode on)
+    //         this->setStateTransitionProcessToSystemd(StmEvtNoRestrictionModeOn,
+    //                                                  3000, "");
+    //     }
+    //     else if (StmRunningNoStop ==
+    //              this->p_crr_state->car_element[StmCarElementNoRunning].state)
+    //     {
+    //         // Stop timer for restriction on event
+    //         if (this->event_source_list.find(StmEvtNoRestrictionModeOn) !=
+    //             this->event_source_list.end())
+    //         {
+    //             HMI_DEBUG("Stop timer for restriction on");
+    //             sd_event_source *event_source = this->event_source_list[StmEvtNoRestrictionModeOn];
+    //             int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
+    //             if (0 > ret)
+    //             {
+    //                 HMI_ERROR("Failed to stop timer");
+    //             }
+    //         }
+
+    //         // Set event(restriction mode off)
+    //         this->setStateTransitionProcessToSystemd(StmEvtNoRestrictionModeOff, 0, "");
+    //     }
+    // }
+}
+
 int PolicyManager::transitionState(sd_event_source *source, void *data)
 {
     HMI_DEBUG(">>>>>>>>>> START STATE TRANSITION");
@@ -623,9 +805,11 @@ int PolicyManager::transitionState(sd_event_source *source, void *data)
               kStmCategoryName[category_no],
               kStmAreaName[area_no]);
 
+    // Store current state
+    *(this->p_prv_state) = *(this->p_crr_state);
+
     // Transition state
-    StmState crr_state;
-    int ret = stmTransitionState(event_id, &crr_state);
+    int ret = stmTransitionState(event_id, this->p_crr_state);
     if (0 > ret)
     {
         HMI_ERROR("Failed transition state");
@@ -647,11 +831,11 @@ int PolicyManager::transitionState(sd_event_source *source, void *data)
     }
 
     // Update state which is managed by PolicyManager
-    this->updateState(event_id, crr_state);
+    this->updateState(event_id);
 
     // Create output information for ResourceManager
     json_object *json_out = json_object_new_object();
-    this->createOutputInformation(crr_state, &json_out);
+    this->createOutputInformation(&json_out);
 
     // Notify changed state
     if (nullptr != this->callback.onStateTransitioned)
@@ -659,6 +843,9 @@ int PolicyManager::transitionState(sd_event_source *source, void *data)
         this->callback.onStateTransitioned(json_out);
     }
 
+    // Start/Stop timer events
+    this->controlTimerEvent();
+
     // Release json_object
     json_object_put(json_out);
 
@@ -689,15 +876,15 @@ int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data
 int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t delay_ms, std::string role)
 {
     struct sd_event_source *event_source;
-    HMI_DEBUG("wm:pm event_id:0x%x delay:%d role:%s", event_id, delay_ms, role.c_str());
+    HMI_DEBUG("event_id:0x%x delay:%d role:%s", event_id, delay_ms, role.c_str());
 
     if (0 == delay_ms)
     {
-        int ret = sd_event_add_defer(afb_api_get_event_loop(afbBindingV3root), &event_source,
+        int ret = sd_event_add_defer(afb_daemon_get_event_loop(), &event_source,
                                      &pm::transitionStateWrapper, new int(event_id));
         if (0 > ret)
         {
-            HMI_ERROR("wm:pm Failed to sd_event_add_defer: errno:%d", ret);
+            HMI_ERROR("Faild to sd_event_add_defer: errno:%d", ret);
             return -1;
         }
     }
@@ -711,12 +898,12 @@ int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t del
         uint64_t usec = (time_spec.tv_sec * 1000000) + (time_spec.tv_nsec / 1000) + (delay_ms * 1000);
 
         // Set timer
-        int ret = sd_event_add_time(afb_api_get_event_loop(afbBindingV3root), &event_source,
+        int ret = sd_event_add_time(afb_daemon_get_event_loop(), &event_source,
                                     CLOCK_BOOTTIME, usec, 1,
                                     &pm::timerEventWrapper, new int(event_id));
         if (0 > ret)
         {
-            HMI_ERROR("wm:pm Failed to sd_event_add_time: errno:%d", ret);
+            HMI_ERROR("Faild to sd_event_add_time: errno:%d", ret);
             return -1;
         }
     }
@@ -727,7 +914,87 @@ int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t del
     return 0;
 }
 
-int PolicyManager::loadRoleDb()
+bool PolicyManager::changedRestrictionModeTo2On()
+{
+    // TODO: If possible thie process should be include in zipc stm in the future
+    if (this->p_crr_state->car_element[StmCarElementNoRestrictionMode].changed &&
+        (StmRestrictionModeSttNoOn != this->p_prv_state->car_element[StmCarElementNoRestrictionMode].state) &&
+        (StmRestrictionModeSttNoOn == this->p_crr_state->car_element[StmCarElementNoRestrictionMode].state))
+    {
+        return true;
+    }
+    return false;
+}
+
+bool PolicyManager::changedRestrictionMode2OnToOther()
+{
+    // TODO: If possible thie process should be include in zipc stm in the future
+    if (this->p_crr_state->car_element[StmCarElementNoRestrictionMode].changed &&
+        (StmRestrictionModeSttNoOn == this->p_prv_state->car_element[StmCarElementNoRestrictionMode].state) &&
+        (StmRestrictionModeSttNoOn != this->p_crr_state->car_element[StmCarElementNoRestrictionMode].state))
+    {
+        return true;
+    }
+    return false;
+}
+
+// bool PolicyManager::changedLightstatusBrakeOffToOn()
+// {
+//     // TODO: For master
+//     //       If possible thie process should be include in zipc stm in the future
+//     if (("master" == this->ecu_name) &&
+//         this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].changed &&
+//         (StmLightstatusBrakeSttNoOff == this->p_prv_state->car_element[StmCarElementNoLightstatusBrake].state) &&
+//         (StmLightstatusBrakeSttNoOn  == this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].state))
+//     {
+//         return true;
+//     }
+//     return false;
+// }
+
+// bool PolicyManager::changedLightstatusBrakeOnToOff()
+// {
+//     // TODO: For master
+//     //       If possible thie process should be include in zipc stm in the future
+//     if (("master" == this->ecu_name) &&
+//         this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].changed &&
+//         (StmLightstatusBrakeSttNoOn == this->p_prv_state->car_element[StmCarElementNoLightstatusBrake].state) &&
+//         (StmLightstatusBrakeSttNoOff  == this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].state))
+//     {
+//         return true;
+//     }
+//     return false;
+// }
+
+bool PolicyManager::changedAccelPedalOffToOn()
+{
+    // TODO: For master
+    //       If possible thie process should be include in zipc stm in the future
+    if (("master" == this->ecu_name) &&
+        this->p_crr_state->car_element[StmCarElementNoAccelPedal].changed &&
+        (StmAccelPedalSttNoOff == this->p_prv_state->car_element[StmCarElementNoAccelPedal].state) &&
+        (StmAccelPedalSttNoOn  == this->p_crr_state->car_element[StmCarElementNoAccelPedal].state))
+    {
+        return true;
+    }
+    return false;
+}
+
+bool PolicyManager::changedAccelPedalOnToOff()
+{
+    // TODO: For master
+    //       If possible thie process should be include in zipc stm in the future
+    if (("master" == this->ecu_name) &&
+        this->p_crr_state->car_element[StmCarElementNoAccelPedal].changed &&
+        (StmAccelPedalSttNoOn == this->p_prv_state->car_element[StmCarElementNoAccelPedal].state) &&
+        (StmAccelPedalSttNoOff  == this->p_crr_state->car_element[StmCarElementNoAccelPedal].state))
+    {
+        return true;
+    }
+    return false;
+}
+
+int PolicyManager::loadRolesConfigFile()
 {
     std::string file_name;
 
@@ -741,21 +1008,62 @@ int PolicyManager::loadRoleDb()
     }
     else
     {
-        file_name = std::string(afm_app_install_dir) + std::string("/etc/roles.db");
+        file_name = std::string(afm_app_install_dir) + std::string(pm::kPathRolesConfigFile);
     }
 
-    // Load roles.db
+    // Load roles config file
     json_object *json_obj;
     int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
     if (0 > ret)
     {
-        HMI_ERROR("Could not open roles.db, so use default role information");
-        json_obj = json_tokener_parse(kDefaultRoleDb);
+        HMI_ERROR("Could not open %s, so use default role information", pm::kPathRolesConfigFile);
+        json_obj = json_tokener_parse(kDefaultRolesConfig);
     }
     HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
 
+    // Parse ecus
+    json_object *json_cfg;
+    if (!json_object_object_get_ex(json_obj, "ecus", &json_cfg))
+    {
+        HMI_ERROR("Parse Error!!");
+        return -1;
+    }
+
+    int num_ecu = json_object_array_length(json_cfg);
+    HMI_DEBUG("json_cfg(ecus) len:%d", num_ecu);
+
+    const char* c_ecu_name;
+    json_object *json_ecu;
+    for (int i = 0; i < num_ecu; i++)
+    {
+        json_ecu= json_object_array_get_idx(json_cfg, i);
+
+        c_ecu_name = this->getStringFromJson(json_ecu, "name");
+        if (nullptr == c_ecu_name)
+        {
+            HMI_ERROR("Parse Error!!");
+            return -1;
+        }
+
+        if (std::string(c_ecu_name) == this->ecu_name)
+        {
+            break;
+        }
+        else
+        {
+            json_ecu = nullptr;
+        }
+    }
+
+    if (!json_ecu)
+    {
+        HMI_ERROR("Areas for ecu:%s is NOT exist!!", this->ecu_name.c_str());
+        return -1;
+    }
+
+    // Parse roles
     json_object *json_roles;
-    if (!json_object_object_get_ex(json_obj, "roles", &json_roles))
+    if (!json_object_object_get_ex(json_ecu, "roles", &json_roles))
     {
         HMI_ERROR("Parse Error!!");
         return -1;
@@ -802,6 +1110,22 @@ int PolicyManager::loadRoleDb()
         this->category2role[category] = std::string(roles);
         this->category2areas[category] = vct_areas;
         this->layer2categories[layer].push_back(category);
+
+        // TODO: For run-by-default applications, set history in advance
+        const char *auto_started_roles;
+        auto_started_roles = this->getStringFromJson(json_tmp, "auto_started_roles");
+        if (nullptr != auto_started_roles)
+        {
+            std::vector<std::string> vct_auto_started_roles;
+            vct_auto_started_roles =
+                this->parseString(std::string(auto_started_roles), '|');
+
+            for (auto itr = vct_auto_started_roles.end() - 1;
+                 itr >= vct_auto_started_roles.begin(); --itr)
+            {
+                this->pushInvisibleRoleHistory(category, *itr);
+            }
+        }
     }
 
     // Check
@@ -825,10 +1149,19 @@ int PolicyManager::loadRoleDb()
             HMI_DEBUG("key:%s, val:%s", x.first.c_str(), y.c_str());
         }
     }
+
+    HMI_DEBUG("Check layer2categories");
+    for (const auto &x : this->layer2categories)
+    {
+        for (const auto &y : x.second)
+        {
+            HMI_DEBUG("key:%s, val:%s", x.first.c_str(), y.c_str());
+        }
+    }
     return 0;
 }
 
-int PolicyManager::loadStateDb()
+int PolicyManager::loadLayoutsConfigFile()
 {
     HMI_DEBUG("Call");
 
@@ -843,38 +1176,78 @@ int PolicyManager::loadStateDb()
     }
     else
     {
-        file_name = std::string(afm_app_install_dir) + std::string("/etc/states.db");
+        file_name = std::string(afm_app_install_dir) + std::string(pm::kPathLayoutsConfigFile);
     }
 
-    // Load states.db
+    // Load states config file
     json_object *json_obj;
     int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
     if (0 > ret)
     {
-        HMI_DEBUG("Could not open states.db, so use default layout information");
-        json_obj = json_tokener_parse(kDefaultStateDb);
+        HMI_DEBUG("Could not open %s, so use default layout information", pm::kPathLayoutsConfigFile);
+        json_obj = json_tokener_parse(kDefaultLayoutsConfig);
     }
     HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
 
-    // Perse states
-    HMI_DEBUG("Perse states");
+    // Parse ecus
     json_object *json_cfg;
-    if (!json_object_object_get_ex(json_obj, "states", &json_cfg))
+    if (!json_object_object_get_ex(json_obj, "ecus", &json_cfg))
     {
         HMI_ERROR("Parse Error!!");
         return -1;
     }
 
-    int len = json_object_array_length(json_cfg);
-    HMI_DEBUG("json_cfg len:%d", len);
-    HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
+    int num_ecu = json_object_array_length(json_cfg);
+    HMI_DEBUG("json_cfg(ecus) len:%d", num_ecu);
+
+    const char* c_ecu_name;
+    json_object *json_ecu;
+    for (int i = 0; i < num_ecu; i++)
+    {
+        json_ecu= json_object_array_get_idx(json_cfg, i);
+
+        c_ecu_name = this->getStringFromJson(json_ecu, "name");
+        if (nullptr == c_ecu_name)
+        {
+            HMI_ERROR("Parse Error!!");
+            return -1;
+        }
+
+        if (std::string(c_ecu_name) == this->ecu_name)
+        {
+            break;
+        }
+        else
+        {
+            json_ecu = nullptr;
+        }
+    }
+
+    if (!json_ecu)
+    {
+        HMI_ERROR("Areas for ecu:%s is NOT exist!!", this->ecu_name.c_str());
+        return -1;
+    }
+
+    // Perse layouts
+    HMI_DEBUG("Perse layouts");
+    json_object *json_layouts;
+    if (!json_object_object_get_ex(json_ecu, "layouts", &json_layouts))
+    {
+        HMI_ERROR("Parse Error!!");
+        return -1;
+    }
+
+    int len = json_object_array_length(json_layouts);
+    HMI_DEBUG("json_layouts len:%d", len);
+    HMI_DEBUG("json_layouts dump:%s", json_object_get_string(json_layouts));
 
     const char *layout;
     const char *role;
     const char *category;
     for (int i = 0; i < len; i++)
     {
-        json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
+        json_object *json_tmp = json_object_array_get_idx(json_layouts, i);
 
         layout = this->getStringFromJson(json_tmp, "name");
         if (nullptr == layout)
@@ -979,35 +1352,35 @@ int PolicyManager::loadStateDb()
 
 void PolicyManager::pushInvisibleRoleHistory(std::string category, std::string role)
 {
-    auto i = std::remove_if(this->invisible_role_history[category].begin(),
-                            this->invisible_role_history[category].end(),
+    auto i = std::remove_if(this->crr_invisible_role_history[category].begin(),
+                            this->crr_invisible_role_history[category].end(),
                             [role](std::string x) { return (role == x); });
 
-    if (this->invisible_role_history[category].end() != i)
+    if (this->crr_invisible_role_history[category].end() != i)
     {
-        this->invisible_role_history[category].erase(i);
+        this->crr_invisible_role_history[category].erase(i);
     }
 
-    this->invisible_role_history[category].push_back(role);
+    this->crr_invisible_role_history[category].push_back(role);
 
-    if (pm::kInvisibleRoleHistoryNum < invisible_role_history[category].size())
+    if (pm::kInvisibleRoleHistoryNum < crr_invisible_role_history[category].size())
     {
-        this->invisible_role_history[category].erase(
-            this->invisible_role_history[category].begin());
+        this->crr_invisible_role_history[category].erase(
+            this->crr_invisible_role_history[category].begin());
     }
 }
 
 std::string PolicyManager::popInvisibleRoleHistory(std::string category)
 {
     std::string role;
-    if (invisible_role_history[category].empty())
+    if (crr_invisible_role_history[category].empty())
     {
         role = "";
     }
     else
     {
-        role = this->invisible_role_history[category].back();
-        this->invisible_role_history[category].pop_back();
+        role = this->crr_invisible_role_history[category].back();
+        this->crr_invisible_role_history[category].pop_back();
     }
     return role;
 }
@@ -1116,7 +1489,7 @@ void PolicyManager::dumpInvisibleRoleHistory()
         std::string category = std::string(kStmCategoryName[ctg_no]);
 
         std::string str = category + " [ ";
-        for (const auto &i : this->invisible_role_history[category])
+        for (const auto &i : this->crr_invisible_role_history[category])
             str += (i + " > ");
 
         str += "]";
@@ -1152,7 +1525,7 @@ std::string PolicyManager::deleteSpace(std::string str)
     return ret;
 }
 
-const char *PolicyManager::kDefaultRoleDb = "{ \
+const char *PolicyManager::kDefaultRolesConfig = "{ \
     \"roles\":[ \
     { \
         \"category\": \"homescreen\", \
@@ -1197,8 +1570,8 @@ const char *PolicyManager::kDefaultRoleDb = "{ \
     ] \
 }";
 
-const char *PolicyManager::kDefaultStateDb = "{ \
-    \"states\": [ \
+const char *PolicyManager::kDefaultLayoutsConfig = "{ \
+    \"layouts\": [ \
         { \
             \"name\": \"homescreen\", \
             \"layer\": \"far_homescreen\", \