diff options
author | zheng_wenlong <wenlong_zheng@nexty-ele.com> | 2018-10-30 14:36:16 +0900 |
---|---|---|
committer | zheng_wenlong <wenlong_zheng@nexty-ele.com> | 2018-10-30 14:36:16 +0900 |
commit | de492ca32c0cc4eb7e090ba33945eb75a21cc14a (patch) | |
tree | c81e48f9fa7199afa87a25781599ed6d963989b1 /policy_manager/policy_manager.cpp | |
parent | 3ae8e9b6a7947b91409b112e1a699385627c5945 (diff) |
add source for ces2019
Diffstat (limited to 'policy_manager/policy_manager.cpp')
-rw-r--r-- | policy_manager/policy_manager.cpp | 620 |
1 files changed, 473 insertions, 147 deletions
diff --git a/policy_manager/policy_manager.cpp b/policy_manager/policy_manager.cpp index c7bb007..05f2ccb 100644 --- a/policy_manager/policy_manager.cpp +++ b/policy_manager/policy_manager.cpp @@ -22,7 +22,7 @@ #include <algorithm> #include <json-c/json.h> #include "policy_manager.hpp" -#include "hmi-debug.h" +#include "util.hpp" extern "C" { @@ -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,44 +62,56 @@ 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++) { - HMI_DEBUG("wm:pm", "event name:%s no:%d", kStmEventName[i], i); + HMI_DEBUG("event name:%s no:%d", kStmEventName[i], i); this->eventname2no[kStmEventName[i]] = i; } for (int i = StmCtgNoMin; i <= StmCtgNoMax; i++) { - HMI_DEBUG("wm:pm", "category name:%s no:%d", kStmCategoryName[i], i); + HMI_DEBUG("category name:%s no:%d", kStmCategoryName[i], i); this->categoryname2no[kStmCategoryName[i]] = i; } for (int i = StmAreaNoMin; i <= StmAreaNoMax; i++) { - HMI_DEBUG("wm:pm", "area name:%s no:%d", kStmAreaName[i], i); + HMI_DEBUG("area name:%s no:%d", kStmAreaName[i], i); this->areaname2no[kStmAreaName[i]] = i; } - // Load roles.db - ret = this->loadRoleDb(); + // Load roles config + ret = this->loadRolesConfigFile(); if (0 > ret) { - HMI_ERROR("wm:pm", "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("wm:pm", "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; @@ -123,7 +138,7 @@ int PolicyManager::setInputEventData(json_object *json_in) // Check arguments if (nullptr == json_in) { - HMI_ERROR("wm:pm", "Argument is NULL!!"); + HMI_ERROR("Argument is NULL!!"); return -1; } @@ -137,17 +152,17 @@ int PolicyManager::setInputEventData(json_object *json_in) if (this->eventname2no.end() != itr) { event_no = this->eventname2no[event]; - HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no); + HMI_DEBUG("event(%s:%d)", event, event_no); } else { - HMI_ERROR("wm:pm", "Invalid event name!!"); + HMI_ERROR("Invalid event name!!"); return -1; } } else { - HMI_ERROR("wm:pm", "Event is not set!!"); + HMI_ERROR("Event is not set!!"); return -1; } @@ -157,7 +172,7 @@ int PolicyManager::setInputEventData(json_object *json_in) int category_no = StmCtgNoNone; if (nullptr != role) { - HMI_DEBUG("wm:pm", "role(%s)", role); + HMI_DEBUG("role(%s)", role); // Convert role to category auto itr = this->role2category.find(role); @@ -170,7 +185,7 @@ int PolicyManager::setInputEventData(json_object *json_in) itr = this->role2category.find("fallback"); if (this->role2category.end() != itr) { - HMI_DEBUG("wm:pm", "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"]; } } @@ -179,7 +194,7 @@ int PolicyManager::setInputEventData(json_object *json_in) { // Convert name to number category_no = categoryname2no[category]; - HMI_DEBUG("wm:pm", "category(%s:%d)", category.c_str(), category_no); + HMI_DEBUG("category(%s:%d)", category.c_str(), category_no); } } if (StmCtgNoNone == category_no) @@ -205,7 +220,7 @@ int PolicyManager::setInputEventData(json_object *json_in) area = this->category2areas[category].front().c_str(); area_no = this->areaname2no[area]; } - HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no); + HMI_DEBUG("area(%s:%d)", area, area_no); } // Set event info to the queue @@ -237,25 +252,42 @@ int PolicyManager::executeStateTransition() void PolicyManager::undoState() { - HMI_DEBUG("wm:pm", "Undo State !!!"); + HMI_DEBUG("Undo State !!!"); // Undo state of STM stmUndoState(); - HMI_DEBUG("wm:pm", ">>>>>>>>>> BEFORE UNDO"); + HMI_DEBUG(">>>>>>>>>> BEFORE UNDO"); this->dumpLayerState(this->crr_layers); this->crr_layers = this->prv_layers; + this->crr_invisible_role_history = this->prv_invisible_role_history; - HMI_DEBUG("wm:pm", ">>>>>>>>>> AFTER UNDO"); + HMI_DEBUG(">>>>>>>>>> AFTER UNDO"); this->dumpLayerState(this->crr_layers); } 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; @@ -282,7 +314,7 @@ void PolicyManager::addStateToJson(const char *name, bool changed, { if ((nullptr == name) || (nullptr == json_out)) { - HMI_ERROR("wm:pm", "Invalid argument!!!"); + HMI_ERROR("Invalid argument!!!"); return; } @@ -296,7 +328,7 @@ void PolicyManager::addStateToJson(const char *layer_name, bool changed, { if ((nullptr == layer_name) || (nullptr == json_out)) { - HMI_ERROR("wm:pm", "Invalid argument!!!"); + HMI_ERROR("Invalid argument!!!"); return; } @@ -315,47 +347,108 @@ 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::updateLayer(int event_id, StmState crr_state) +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) { for (int layer_no = StmLayerNoMin; layer_no <= StmLayerNoMax; layer_no++) { - HMI_DEBUG("wm:pm", ">>> LAYER:%s CHANGED:%d LAYOUT:%s", - kStmLayerName[layer_no], crr_state.layer[layer_no].changed, - kStmLayoutName[crr_state.layer[layer_no].state]); + HMI_DEBUG(">>> LAYER:%s CHANGED:%d LAYOUT:%s", + 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->changedLightstatusBrakeOnToOff()) + { + 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("wm:pm", ">>>>>>>>>> Update layout of layer:%s", layer_name); + 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; } @@ -363,10 +456,10 @@ void PolicyManager::updateLayer(int event_id, StmState crr_state) // Erase role for the event_id from list this->req_role_list.erase(event_id); - HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP LAYERS (BEFORE)"); + HMI_DEBUG(">>>>>>>>>> DUMP LAYERS (BEFORE)"); this->dumpLayerState(this->prv_layers); - HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP LAYERS (AFTER)"); + HMI_DEBUG(">>>>>>>>>> DUMP LAYERS (AFTER)"); this->dumpLayerState(this->crr_layers); this->dumpInvisibleRoleHistory(); @@ -375,7 +468,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 +485,36 @@ 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->changedLightstatusBrakeOffToOn()) + { + // 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 @@ -405,17 +527,17 @@ int PolicyManager::updateLayout(int event_id, int layer_no, crr_layout_state = prv_layout_state; changed = 1; - HMI_DEBUG("wm:pm", "-- layout name previous:%s current:%s", + HMI_DEBUG("-- layout name previous:%s current:%s", prv_layout_name.c_str(), crr_layout_name.c_str()); if (prv_layout_name == crr_layout_name) { - HMI_DEBUG("wm:pm", "---- Previous layout is same with current"); + HMI_DEBUG("---- Previous layout is same with current"); } else { // If previous layout is NOT same with current, // current areas is set with default value - HMI_DEBUG("wm:pm", "---- Previous layout is NOT same with current"); + HMI_DEBUG("---- Previous layout is NOT same with current"); crr_layout_state.name = this->default_layouts[crr_layout_name].name; crr_layout_state.category_num = this->default_layouts[crr_layout_name].category_num; crr_layout_state.area_list = this->default_layouts[crr_layout_name].area_list; @@ -434,7 +556,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, // } // const char *ctg = kStmCategoryName[ctg_no]; - HMI_DEBUG("wm:pm", "-- Create candidate list for ctg:%s", ctg.c_str()); + HMI_DEBUG("-- Create candidate list for ctg:%s", ctg.c_str()); AreaList tmp_cand_list; int candidate_num = 0; @@ -447,7 +569,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, std::string used_role = ""; if ((ctg == req_ctg) && ("activate" == req_evt)) { - HMI_DEBUG("wm:pm", "---- Requested event is activate"); + HMI_DEBUG("---- Requested event is activate"); for (AreaState &as : crr_layout_state.area_list) { if (as.category == req_ctg) @@ -459,7 +581,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, as.role = req_role; used_role = req_role; blank_num--; - HMI_DEBUG("wm:pm", "------ Update current layout: area:%s category:%s role:%s", + HMI_DEBUG("------ Update current layout: area:%s category:%s role:%s", as.name.c_str(), as.category.c_str(), as.role.c_str()); break; } @@ -476,7 +598,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, // If there is the category // which is same with new category and not used for updating yet, // push it to list - HMI_DEBUG("wm:pm", "---- Push previous(category:%s role:%s) to candidate list", + HMI_DEBUG("---- Push previous(category:%s role:%s) to candidate list", area_state.category.c_str(), area_state.role.c_str()); tmp_cand_list.push_back(area_state); candidate_num++; @@ -488,7 +610,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, // so push requested role to candidate list if (request_for_this_layer && ("" == used_role)) { - HMI_DEBUG("wm:pm", "---- Push request(area:%s category:%s role:%s) to candidate list", + HMI_DEBUG("---- Push request(area:%s category:%s role:%s) to candidate list", req_area.c_str(), req_ctg.c_str(), req_role.c_str()); AreaState area_state; area_state.name = req_area; @@ -498,7 +620,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, candidate_num++; } - HMI_DEBUG("wm:pm", "---- blank_num:%d candidate_num:%d", blank_num, candidate_num); + HMI_DEBUG("---- blank_num:%d candidate_num:%d", blank_num, candidate_num); // Compare number of candidate/blank, // And remove role in order of the oldest as necessary @@ -514,10 +636,10 @@ int PolicyManager::updateLayout(int event_id, int layer_no, area_state.role = this->popInvisibleRoleHistory(ctg); if ("" == area_state.role) { - HMI_ERROR("wm:pm", "There is no role in history stack!!"); + HMI_ERROR("There is no role in history stack!!"); } tmp_cand_list.push_back(area_state); - HMI_DEBUG("wm:pm", "------ Add role:%s to candidate list", + HMI_DEBUG("------ Add role:%s to candidate list", area_state.role.c_str()); candidate_num++; } @@ -528,7 +650,7 @@ int PolicyManager::updateLayout(int event_id, int layer_no, while (candidate_num != blank_num) { std::string removed_role = tmp_cand_list.begin()->role; - HMI_DEBUG("wm:pm", "------ Remove the oldest role:%s from candidate list", + HMI_DEBUG("------ Remove the oldest role:%s from candidate list", removed_role.c_str()); tmp_cand_list.erase(tmp_cand_list.begin()); candidate_num--; @@ -555,14 +677,14 @@ int PolicyManager::updateLayout(int event_id, int layer_no, } // Update areas - HMI_DEBUG("wm:pm", "-- Update areas by using candidate list"); + HMI_DEBUG("-- Update areas by using candidate list"); for (AreaState &as : crr_layout_state.area_list) { - HMI_DEBUG("wm:pm", "---- Check area:%s category:%s role:%s", + HMI_DEBUG("---- Check area:%s category:%s role:%s", as.name.c_str(), as.category.c_str(), as.role.c_str()); if ("" == as.role) { - HMI_DEBUG("wm:pm", "------ Update this area with role:%s", + HMI_DEBUG("------ Update this area with role:%s", cand_list[as.category].begin()->role.c_str()); as.role = cand_list[as.category].begin()->role; cand_list[as.category].erase(cand_list[as.category].begin()); @@ -572,10 +694,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,9 +755,41 @@ void PolicyManager::createOutputInformation(StmState crr_state, json_object **js json_object_object_add(*json_out, "layers", json_layer); } +void PolicyManager::controlTimerEvent() +{ + 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("wm:pm", ">>>>>>>>>> START STATE TRANSITION"); + HMI_DEBUG(">>>>>>>>>> START STATE TRANSITION"); int event_id = *((int *)data); @@ -618,17 +797,19 @@ int PolicyManager::transitionState(sd_event_source *source, void *data) event_no = STM_GET_EVENT_FROM_ID(event_id); category_no = STM_GET_CATEGORY_FROM_ID(event_id); area_no = STM_GET_AREA_FROM_ID(event_id); - HMI_DEBUG("wm:pm", ">>>>>>>>>> EVENT:%s CATEGORY:%s AREA:%s", + HMI_DEBUG(">>>>>>>>>> EVENT:%s CATEGORY:%s AREA:%s", kStmEventName[event_no], 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("wm:pm", "Failed transition state"); + HMI_ERROR("Failed transition state"); if (nullptr != this->callback.onError) { json_object *json_out = json_object_new_object(); @@ -647,11 +828,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 +840,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); @@ -674,13 +858,13 @@ int PolicyManager::transitionState(sd_event_source *source, void *data) this->event_source_list.erase(event_id); } - HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH STATE TRANSITION"); + HMI_DEBUG(">>>>>>>>>> FINISH STATE TRANSITION"); return 0; } int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data) { - HMI_DEBUG("wm:pm", "Call"); + HMI_DEBUG("Call"); int ret = this->transitionState(source, data); return ret; @@ -689,7 +873,7 @@ 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) { @@ -697,7 +881,7 @@ int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t del &pm::transitionStateWrapper, new int(event_id)); if (0 > ret) { - HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret); + HMI_ERROR("Faild to sd_event_add_defer: errno:%d", ret); return -1; } } @@ -716,7 +900,7 @@ int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t del &pm::timerEventWrapper, new int(event_id)); if (0 > ret) { - HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret); + HMI_ERROR("Faild to sd_event_add_time: errno:%d", ret); return -1; } } @@ -727,43 +911,136 @@ 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; +} + +int PolicyManager::loadRolesConfigFile() { std::string file_name; // Get afm application installed dir char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR"); - HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir); + HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir); if (!afm_app_install_dir) { - HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined"); + HMI_ERROR("AFM_APP_INSTALL_DIR is not defined"); } 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("wm:pm", "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("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj)); + 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("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } int len = json_object_array_length(json_roles); - HMI_DEBUG("wm:pm", "json_cfg len:%d", len); - HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles)); + HMI_DEBUG("json_cfg len:%d", len); + HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_roles)); json_object *json_tmp; const char *category; @@ -782,7 +1059,7 @@ int PolicyManager::loadRoleDb() if ((nullptr == category) || (nullptr == roles) || (nullptr == areas) || (nullptr == layer)) { - HMI_ERROR("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } @@ -805,95 +1082,144 @@ int PolicyManager::loadRoleDb() } // Check - HMI_DEBUG("wm:pm", "Check role2category"); + HMI_DEBUG("Check role2category"); for (const auto &x : this->role2category) { - HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str()); + HMI_DEBUG("key:%s, val:%s", x.first.c_str(), x.second.c_str()); } - HMI_DEBUG("wm:pm", "Check category2role"); + HMI_DEBUG("Check category2role"); for (const auto &x : this->category2role) { - HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str()); + HMI_DEBUG("key:%s, val:%s", x.first.c_str(), x.second.c_str()); } - HMI_DEBUG("wm:pm", "Check category2areas"); + HMI_DEBUG("Check category2areas"); for (const auto &x : this->category2areas) { for (const auto &y : x.second) { - HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), y.c_str()); + 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("wm:pm", "Call"); + HMI_DEBUG("Call"); // Get afm application installed dir char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR"); - HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir); + HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir); std::string file_name; if (!afm_app_install_dir) { - HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined"); + HMI_ERROR("AFM_APP_INSTALL_DIR is not defined"); } 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("wm:pm", "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("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj)); + HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj)); - // Perse states - HMI_DEBUG("wm:pm", "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 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("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } - int len = json_object_array_length(json_cfg); - HMI_DEBUG("wm:pm", "json_cfg len:%d", len); - HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg)); + 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) { - HMI_ERROR("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } - HMI_DEBUG("wm:pm", "> layout:%s", layout); + HMI_DEBUG("> layout:%s", layout); json_object *json_area_array; if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) { - HMI_ERROR("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } int len_area = json_object_array_length(json_area_array); - HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area); - HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array)); + HMI_DEBUG("json_area_array len:%d", len_area); + HMI_DEBUG("json_area_array dump:%s", json_object_get_string(json_area_array)); LayoutState layout_state; AreaState area_state; @@ -913,22 +1239,22 @@ int PolicyManager::loadStateDb() const char *area = this->getStringFromJson(json_area, "name"); if (nullptr == area) { - HMI_ERROR("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } area_state.name = std::string(area); - HMI_DEBUG("wm:pm", ">> area:%s", area); + HMI_DEBUG(">> area:%s", area); // Get app attribute of the area category = this->getStringFromJson(json_area, "category"); if (nullptr == category) { - HMI_ERROR("wm:pm", "Parse Error!!"); + HMI_ERROR("Parse Error!!"); return -1; } area_state.category = std::string(category); category_num[category]++; - HMI_DEBUG("wm:pm", ">>> category:%s", category); + HMI_DEBUG(">>> category:%s", category); role = this->getStringFromJson(json_area, "role"); if (nullptr != role) @@ -940,7 +1266,7 @@ int PolicyManager::loadStateDb() { area_state.role = std::string(""); } - HMI_DEBUG("wm:pm", ">>> role:%s", role); + HMI_DEBUG(">>> role:%s", role); layout_state.area_list.push_back(area_state); } @@ -960,14 +1286,14 @@ int PolicyManager::loadStateDb() for (auto itr_layout = this->default_layouts.begin(); itr_layout != this->default_layouts.end(); ++itr_layout) { - HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str()); + HMI_DEBUG(">>> layout:%s", itr_layout->first.c_str()); for (auto itr_area = itr_layout->second.area_list.begin(); itr_area != itr_layout->second.area_list.end(); ++itr_area) { - HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str()); - HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str()); - HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str()); + HMI_DEBUG(">>> >>> area :%s", itr_area->name.c_str()); + HMI_DEBUG(">>> >>> category:%s", itr_area->category.c_str()); + HMI_DEBUG(">>> >>> role :%s", itr_area->role.c_str()); } } @@ -979,35 +1305,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; } @@ -1017,7 +1343,7 @@ const char *PolicyManager::getStringFromJson(json_object *obj, const char *key) json_object *tmp; if (!json_object_object_get_ex(obj, key, &tmp)) { - HMI_DEBUG("wm:pm", "Not found key \"%s\"", key); + HMI_DEBUG("Not found key \"%s\"", key); return nullptr; } @@ -1029,13 +1355,13 @@ int PolicyManager::inputJsonFilie(const char *file, json_object **obj) const int input_size = 128; int ret = -1; - HMI_DEBUG("wm:pm", "Input file: %s", file); + HMI_DEBUG("Input file: %s", file); // Open json file FILE *fp = fopen(file, "rb"); if (nullptr == fp) { - HMI_ERROR("wm:pm", "Could not open file"); + HMI_ERROR("Could not open file"); return ret; } @@ -1050,7 +1376,7 @@ int PolicyManager::inputJsonFilie(const char *file, json_object **obj) *obj = json_tokener_parse_ex(tokener, buffer, len); if (nullptr != *obj) { - HMI_DEBUG("wm:pm", "File input is success"); + HMI_DEBUG("File input is success"); ret = 0; break; } @@ -1058,9 +1384,9 @@ int PolicyManager::inputJsonFilie(const char *file, json_object **obj) json_error = json_tokener_get_error(tokener); if ((json_tokener_continue != json_error) || (input_size > len)) { - HMI_ERROR("wm:pm", "Failed to parse file (byte:%d err:%s)", + HMI_ERROR("Failed to parse file (byte:%d err:%s)", (input_size * block_cnt), json_tokener_error_desc(json_error)); - HMI_ERROR("wm:pm", "\n%s", buffer); + HMI_ERROR("\n%s", buffer); *obj = nullptr; break; } @@ -1078,8 +1404,8 @@ int PolicyManager::inputJsonFilie(const char *file, json_object **obj) void PolicyManager::dumpLayerState(std::unordered_map<std::string, LayerState> &layers) { - HMI_DEBUG("wm:pm", "-------------------------------------------------------------------------------------------------------"); - HMI_DEBUG("wm:pm", "|%-15s|%s|%-20s|%-20s|%-20s|%-20s|", + HMI_DEBUG("-------------------------------------------------------------------------------------------------------"); + HMI_DEBUG("|%-15s|%s|%-20s|%-20s|%-20s|%-20s|", "LAYER", "C", "LAYOUT", "AREA", "CATEGORY", "ROLE"); for (const auto &itr : layers) { @@ -1093,21 +1419,21 @@ void PolicyManager::dumpLayerState(std::unordered_map<std::string, LayerState> & if (first) { first = false; - HMI_DEBUG("wm:pm", "|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|", + HMI_DEBUG("|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|", layer, changed, layout, as.name.c_str(), as.category.c_str(), as.role.c_str()); } else - HMI_DEBUG("wm:pm", "|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|", + HMI_DEBUG("|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|", "", "", "", as.name.c_str(), as.category.c_str(), as.role.c_str()); } } - HMI_DEBUG("wm:pm", "-------------------------------------------------------------------------------------------------------"); + HMI_DEBUG("-------------------------------------------------------------------------------------------------------"); } void PolicyManager::dumpInvisibleRoleHistory() { - HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP INVISIBLE ROLE HISTORY ( category [older > newer] )"); + HMI_DEBUG(">>>>>>>>>> DUMP INVISIBLE ROLE HISTORY ( category [older > newer] )"); for (int ctg_no = StmCtgNoMin; ctg_no <= StmCtgNoMax; ctg_no++) { if (ctg_no == StmCtgNoNone) @@ -1116,11 +1442,11 @@ 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 += "]"; - HMI_DEBUG("wm:pm", "%s", str.c_str()); + HMI_DEBUG("%s", str.c_str()); } } @@ -1152,7 +1478,7 @@ std::string PolicyManager::deleteSpace(std::string str) return ret; } -const char *PolicyManager::kDefaultRoleDb = "{ \ +const char *PolicyManager::kDefaultRolesConfig = "{ \ \"roles\":[ \ { \ \"category\": \"homescreen\", \ @@ -1197,8 +1523,8 @@ const char *PolicyManager::kDefaultRoleDb = "{ \ ] \ }"; -const char *PolicyManager::kDefaultStateDb = "{ \ - \"states\": [ \ +const char *PolicyManager::kDefaultLayoutsConfig = "{ \ + \"layouts\": [ \ { \ \"name\": \"homescreen\", \ \"layer\": \"far_homescreen\", \ |