aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuta Doi <yuta-d@witz-inc.co.jp>2018-06-21 13:20:31 +0900
committerYuta Doi <yuta-d@witz-inc.co.jp>2018-06-25 17:17:09 +0900
commit68a3537de96da4601756b3ab580b1071cb36547e (patch)
treebe3eaed4c8da518a2f9d58cf85db725f459f9954
parent90c66dd7d8ae2dae35cf4e68360f08d2a586651e (diff)
Move global variable to PolicyManager member
Signed-off-by: Yuta Doi <yuta-d@witz-inc.co.jp>
-rw-r--r--src/policy_manager/policy_manager.cpp251
-rw-r--r--src/policy_manager/policy_manager.hpp65
-rw-r--r--src/policy_manager/zipc/dummy_stm.h9
3 files changed, 182 insertions, 143 deletions
diff --git a/src/policy_manager/policy_manager.cpp b/src/policy_manager/policy_manager.cpp
index d4aab12..347f810 100644
--- a/src/policy_manager/policy_manager.cpp
+++ b/src/policy_manager/policy_manager.cpp
@@ -31,47 +31,19 @@ extern "C" {
}
} // namespace stm
-
namespace pm {
-typedef struct AreaState {
- std::string name;
- std::string category;
- std::string role;
-} AreaState;
-
-typedef std::vector<AreaState> AreaList;
-typedef struct LayoutState {
- std::string name;
- std::map<std::string, int> category_num;
- AreaList area_list;
- std::map<std::string, std::vector<std::string>> role_history;
-} LayoutState;
-
-typedef struct LayerState {
- std::string name;
- LayoutState layout_state;
- bool changed;
-} LayerState;
-
-typedef struct CarElement {
- std::string state;
- bool changed;
-} CarElement;
-
-struct sd_event* event_loop;
-std::map<int, struct sd_event_source*> event_source_list;
-std::map<int, std::string> g_req_role_list;
-PolicyManager::CallbackTable callback;
-
-std::unordered_map<std::string, CarElement> g_prv_car_elements;
-std::unordered_map<std::string, CarElement> g_crr_car_elements;
+static PolicyManager *g_context;
-std::unordered_map<std::string, LayerState> g_prv_layers;
-std::unordered_map<std::string, LayerState> g_crr_layers;
+static int transitionStateWrapper(sd_event_source *source, void *data) {
+ int ret = pm::g_context->transitionState(source, data);
+ return ret;
+}
-std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
+static int timerEventWrapper(sd_event_source *source, uint64_t usec, void *data) {
+ int ret = pm::g_context->timerEvent(source, usec, data);
+ return ret;
+}
-std::unordered_map<std::string, LayoutState> g_default_layouts;
} // namespace pm
@@ -122,7 +94,7 @@ int PolicyManager::initialize() {
}
// Initialize state which is managed by PolicyManager
- this->initializeLocalState();
+ this->initializeState();
// Initialize StateTransitioner
stm::stmInitialize();
@@ -134,25 +106,27 @@ int PolicyManager::initialize() {
return ret;
}
+ pm::g_context = this;
+
return ret;
}
-static void initializeLocalCarElementState() {
- pm::CarElement init_car_ele;
+void PolicyManager::initializeCarElementState() {
+ CarElement init_car_ele;
init_car_ele.state = "none";
init_car_ele.changed = false;
for (int i = stm::StmCarElementNoMin; i <= stm::StmCarElementNoMax; i++) {
const char* car_ele_name = stm::kStmCarElementName[i];
- pm::g_crr_car_elements[car_ele_name] = init_car_ele;
+ this->crr_car_elements[car_ele_name] = init_car_ele;
}
- pm::g_prv_car_elements = pm::g_crr_car_elements;
+ this->prv_car_elements = this->crr_car_elements;
}
-static void initializeLocalLayerState() {
- pm::AreaState init_area;
- pm::LayoutState init_layout;
+void PolicyManager::initializeLayerState() {
+ AreaState init_area;
+ LayoutState init_layout;
init_area.name = stm::kStmAreaName[stm::StmAreaNoNone];
init_area.category = "";
init_area.role = "";
@@ -161,22 +135,22 @@ static void initializeLocalLayerState() {
for (int i = stm::StmLayerNoMin; i <= stm::StmLayerNoMax; i++) {
const char* layer_name = stm::kStmLayerName[i];
- pm::g_crr_layers[layer_name].name = layer_name;
- pm::g_crr_layers[layer_name].layout_state = init_layout;
- pm::g_crr_layers[layer_name].changed = false;
+ this->crr_layers[layer_name].name = layer_name;
+ this->crr_layers[layer_name].layout_state = init_layout;
+ this->crr_layers[layer_name].changed = false;
}
- pm::g_prv_layers = pm::g_crr_layers;
+ this->prv_layers = this->crr_layers;
}
-void PolicyManager::initializeLocalState() {
- initializeLocalCarElementState();
- initializeLocalLayerState();
+void PolicyManager::initializeState() {
+ this->initializeCarElementState();
+ this->initializeLayerState();
}
int PolicyManager::initializeSdEventLoop() {
// Get default event loop object
- int ret = sd_event_new(&(pm::event_loop));
+ int ret = sd_event_new(&(this->event_loop));
if (0 > ret) {
HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
return -1;
@@ -185,7 +159,7 @@ int PolicyManager::initializeSdEventLoop() {
// Create thread for sd_event and detach
std::thread sd_event_loop([this]() {
while (1) {
- sd_event_run(pm::event_loop, 1000);
+ sd_event_run(this->event_loop, 1000);
}
});
sd_event_loop.detach();
@@ -193,8 +167,8 @@ int PolicyManager::initializeSdEventLoop() {
return 0;
}
-static void addStateToJson(
- const char* name, bool changed, std::string state, json_object** json_out) {
+void PolicyManager::addStateToJson(const char* name, bool changed,
+ std::string state, json_object** json_out) {
if ((nullptr == name) || (nullptr == json_out)) {
HMI_ERROR("wm:pm", "Invalid argument!!!");
return;
@@ -205,8 +179,8 @@ static void addStateToJson(
json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
}
-static void addStateToJson(const char* layer_name, bool changed,
- pm::AreaList area_list, json_object** json_out) {
+void PolicyManager::addStateToJson(const char* layer_name, bool changed,
+ AreaList area_list, json_object** json_out) {
if ((nullptr == layer_name) || (nullptr == json_out)) {
HMI_ERROR("wm:pm", "Invalid argument!!!");
return;
@@ -214,7 +188,7 @@ static void addStateToJson(const char* layer_name, bool changed,
json_object* json_areas = json_object_new_array();
json_object* json_tmp;
- for (pm::AreaState as : area_list) {
+ for (AreaState as : area_list) {
json_tmp = json_object_new_object();
json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
@@ -226,13 +200,13 @@ static void addStateToJson(const char* layer_name, bool changed,
json_object_object_add(*json_out, "areas", json_areas);
}
-static void updateLocalCarElementState(stm::StmState crr_state) {
+void PolicyManager::updateCarElementState(stm::StmState crr_state) {
int car_state_no;
std::string car_state;
bool changed;
// Store previous layers
- pm::g_prv_car_elements = pm::g_crr_car_elements;
+ this->prv_car_elements = this->crr_car_elements;
// Update car elements
HMI_DEBUG("wm:pm", ">>> CAR ELEMENT");
@@ -244,8 +218,8 @@ static void updateLocalCarElementState(stm::StmState crr_state) {
car_state = stm::kStmCarStateNameList[car_ele_no][car_state_no];
changed = (crr_state.car_element[car_ele_no].changed) ? true : false;
- pm::g_crr_car_elements[car_ele_name].state = car_state;
- pm::g_crr_car_elements[car_ele_name].changed = changed;
+ this->crr_car_elements[car_ele_name].state = car_state;
+ this->crr_car_elements[car_ele_name].changed = changed;
HMI_DEBUG("wm:pm", ">>> >>> NAME: %s", car_ele_name);
HMI_DEBUG("wm:pm", ">>> >>> >>> STATE:%s", car_state.c_str());
@@ -253,7 +227,7 @@ static void updateLocalCarElementState(stm::StmState crr_state) {
}
}
-static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
+void PolicyManager::updateLayerState(int event_data, stm::StmState crr_state) {
int event_no, category_no, area_no;
event_no = STM_GET_EVENT_FROM_ID(event_data);
@@ -263,12 +237,12 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
std::string req_evt = stm::kStmEventName[event_no];
std::string req_ctg = stm::kStmCategoryName[category_no];
std::string req_area = stm::kStmAreaName[area_no];
- std::string req_role = pm::g_req_role_list[event_data];
+ std::string req_role = this->req_role_list[event_data];
HMI_DEBUG("wm:pm", "REQ: event:%s role:%s category:%s area:%s",
req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
// Store previous layers
- pm::g_prv_layers = pm::g_crr_layers;
+ this->prv_layers = this->crr_layers;
// Update layers
for (int layer_no = stm::StmLayerNoMin;
@@ -282,7 +256,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
if ((crr_state.car_element[stm::StmCarElementNoRestrictionMode].changed)
&& (stm::StmRestrictionModeSttNoOn == crr_state.car_element[stm::StmCarElementNoRestrictionMode].state)) {
HMI_DEBUG("wm:pm", "Store current state for state of restriction mode off");
- pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
+ this->prv_layers_car_stop[layer_name] = this->crr_layers[layer_name];
}
#else
// If car state is changed car_stop -> car_run,
@@ -290,7 +264,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
if ((crr_state.car.changed)
&& (stm::StmRunningNoRun == crr_state.car.state)) {
HMI_DEBUG("wm:pm", "Store current state for state of car stop");
- pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
+ this->prv_layers_car_stop[layer_name] = this->crr_layers[layer_name];
}
#endif
@@ -300,14 +274,14 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
HMI_DEBUG("wm:pm", "This layer is changed");
// Get previous layout name of this layer
- pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
+ LayoutState prv_layout_state = this->prv_layers[layer_name].layout_state;
std::string prv_layout_name = prv_layout_state.name;
// Get current layout name of this layer
int crr_layout_state_no = crr_state.layer[layer_no].state;
std::string crr_layout_name = std::string(stm::kStmLayoutName[crr_layout_state_no]);
- pm::LayoutState crr_layout_state;
+ LayoutState crr_layout_state;
#if 1
if ((crr_state.car_element[stm::StmCarElementNoRestrictionMode].changed)
&& (stm::StmRestrictionModeSttNoOff == crr_state.car_element[stm::StmCarElementNoRestrictionMode].state)) {
@@ -321,7 +295,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
// restore state of car stop
HMI_DEBUG("wm:pm", "Car state is changed car_run -> car_stop, so restore state of car stop");
#endif
- crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
+ 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)
&& (stm::kStmAreaName[stm::StmAreaNoNone] == crr_layout_name)) {
@@ -347,21 +321,21 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
// 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");
- crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
- crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
- crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
+ 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;
}
// Create candidate list
- std::map<std::string, pm::AreaList> cand_list;
+ std::map<std::string, AreaList> cand_list;
for (int ctg_no=stm::StmCtgNoMin;
ctg_no<=stm::StmCtgNoMax; ctg_no++) {
const char* ctg = stm::kStmCategoryName[ctg_no];
HMI_DEBUG("wm:pm", "ctg:%s", ctg);
// Create candidate list for category from the previous displayed categories
- pm::AreaList tmp_cand_list;
- for (pm::AreaState area_state : prv_layout_state.area_list) {
+ AreaList tmp_cand_list;
+ for (AreaState area_state : prv_layout_state.area_list) {
if (std::string(ctg) == area_state.category) {
// If there is the category which is same with new category in previous layout,
// push it to list
@@ -382,7 +356,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
bool updated = false;
if ((ctg == req_ctg) && ("activate" == req_evt) ) {
HMI_DEBUG("wm:pm", "requested event is activate");
- for (pm::AreaState &as : crr_layout_state.area_list) {
+ for (AreaState &as : crr_layout_state.area_list) {
if (as.category == req_ctg) {
request_for_this_layer = true;
@@ -401,7 +375,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
// so push requested role to candidate list
if (request_for_this_layer && (!updated)) {
HMI_DEBUG("wm:pm", "Push request to candidate list");
- pm::AreaState area_state;
+ AreaState area_state;
area_state.name = req_area;
area_state.category = req_ctg;
area_state.role = req_role;
@@ -415,7 +389,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
// Refer history stack
// and add to the top of tmp_cand_list in order to the newest
while (candidate_num != blank_num) {
- pm::AreaState area_state;
+ AreaState area_state;
area_state.name = "";
area_state.category = ctg;
if (0 != crr_layout_state.role_history[ctg].size()) {
@@ -455,7 +429,7 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
}
// Update areas
- for (pm::AreaState &as : crr_layout_state.area_list) {
+ for (AreaState &as : crr_layout_state.area_list) {
HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
as.name.c_str(), as.category.c_str());
if ("" == as.role) {
@@ -467,22 +441,22 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
}
}
// Update current layout of this layer
- pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
+ this->crr_layers[layer_name].layout_state = crr_layout_state;
}
// Update changed flag
- pm::g_crr_layers[layer_name].changed = (changed) ? true : false;
+ this->crr_layers[layer_name].changed = (changed) ? true : false;
}
// Erase role for the event_data from list
- pm::g_req_role_list.erase(event_data);
+ this->req_role_list.erase(event_data);
// Check
- for (auto itr : pm::g_crr_layers) {
- pm::LayerState ls = itr.second;
+ for (auto itr : this->crr_layers) {
+ LayerState ls = itr.second;
HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
- for (pm::AreaState as : ls.layout_state.area_list) {
+ for (AreaState as : ls.layout_state.area_list) {
HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
@@ -491,12 +465,12 @@ static void updateLocalLayerState(int event_data, stm::StmState crr_state) {
}
}
-static void updateLocalState(int event_data, stm::StmState crr_state) {
- updateLocalCarElementState(crr_state);
- updateLocalLayerState(event_data, crr_state);
+void PolicyManager::updateState(int event_data, stm::StmState crr_state) {
+ this->updateCarElementState(crr_state);
+ this->updateLayerState(event_data, crr_state);
}
-static void createOutputInformation(stm::StmState crr_state, json_object **json_out) {
+void PolicyManager::createOutputInformation(stm::StmState crr_state, json_object **json_out) {
json_object* json_tmp;
// Create car element information
@@ -515,10 +489,10 @@ static void createOutputInformation(stm::StmState crr_state, json_object **json_
car_ele_no <= stm::StmCarElementNoMax; car_ele_no++) {
car_ele_name = stm::kStmCarElementName[car_ele_no];
json_tmp = json_object_new_object();
- addStateToJson(car_ele_name,
- pm::g_crr_car_elements[car_ele_name].changed,
- pm::g_crr_car_elements[car_ele_name].state,
- &json_tmp);
+ 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);
@@ -545,29 +519,28 @@ static void createOutputInformation(stm::StmState crr_state, json_object **json_
layer_no <= stm::StmLayerNoMax; layer_no++) {
layer_name = stm::kStmLayerName[layer_no];
json_tmp = json_object_new_object();
- addStateToJson(layer_name,
- pm::g_crr_layers[layer_name].changed,
- pm::g_crr_layers[layer_name].layout_state.area_list,
- &json_tmp);
+ this->addStateToJson(layer_name,
+ this->crr_layers[layer_name].changed,
+ this->crr_layers[layer_name].layout_state.area_list,
+ &json_tmp);
json_object_array_add(json_layer, json_tmp);
}
json_object_object_add(*json_out, "layers", json_layer);
}
-static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role);
-static void controlTimerEvent(stm::StmState crr_state) {
+void PolicyManager::controlTimerEvent(stm::StmState crr_state) {
if (crr_state.car_element[stm::StmCarElementNoRunning].changed) {
if (stm::StmRunningNoRun == crr_state.car_element[stm::StmCarElementNoRunning].state) {
// Set delay event(restriction mode on)
- checkPolicyEntry(stm::StmEvtNoRestrictionModeOn, 3000, "");
+ this->setStateTransitionProcessToSystemd(stm::StmEvtNoRestrictionModeOn, 3000, "");
}
else if (stm::StmRunningNoStop == crr_state.car_element[stm::StmCarElementNoRunning].state) {
// Stop timer for restriction on event
- if (pm::event_source_list.find(stm::StmEvtNoRestrictionModeOn)
- != pm::event_source_list.end()) {
+ if (this->event_source_list.find(stm::StmEvtNoRestrictionModeOn)
+ != this->event_source_list.end()) {
HMI_DEBUG("wm:pm", "Stop timer for restriction on");
sd_event_source *event_source
- = pm::event_source_list[stm::StmEvtNoRestrictionModeOn];
+ = this->event_source_list[stm::StmEvtNoRestrictionModeOn];
int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
if (0 > ret) {
HMI_ERROR("wm:pm", "Failed to stop timer");
@@ -575,12 +548,12 @@ static void controlTimerEvent(stm::StmState crr_state) {
}
// Set event(restriction mode off)
- checkPolicyEntry(stm::StmEvtNoRestrictionModeOff, 0, "");
+ this->setStateTransitionProcessToSystemd(stm::StmEvtNoRestrictionModeOff, 0, "");
}
}
}
-static int checkPolicy(sd_event_source *source, void *data) {
+int PolicyManager::transitionState(sd_event_source *source, void *data) {
HMI_DEBUG("wm:pm", "Call");
HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
@@ -600,36 +573,36 @@ static int checkPolicy(sd_event_source *source, void *data) {
int ret = stm::stmTransitionState(event_data, &crr_state);
if (0 > ret) {
HMI_ERROR("wm:pm", "Failed transition state");
- if (nullptr != pm::callback.onError) {
+ if (nullptr != this->callback.onError) {
json_object* json_out = json_object_new_object();
json_object_object_add(json_out, "message",
json_object_new_string("Failed to transition state"));
json_object_object_add(json_out, "event",
json_object_new_string(stm::kStmEventName[event_no]));
json_object_object_add(json_out, "role",
- json_object_new_string(pm::g_req_role_list[event_data].c_str()));
+ json_object_new_string(this->req_role_list[event_data].c_str()));
json_object_object_add(json_out, "area",
json_object_new_string(stm::kStmAreaName[area_no]));
- pm::callback.onError(json_out);
+ this->callback.onError(json_out);
json_object_put(json_out);
}
return -1;
}
// Update state which is managed by PolicyManager
- updateLocalState(event_data, crr_state);
+ this->updateState(event_data, crr_state);
// Create output information for ResourceManager
json_object* json_out = json_object_new_object();
- createOutputInformation(crr_state, &json_out);
+ this->createOutputInformation(crr_state, &json_out);
// Notify changed state
- if (nullptr != pm::callback.onStateTransitioned) {
- pm::callback.onStateTransitioned(json_out);
+ if (nullptr != this->callback.onStateTransitioned) {
+ this->callback.onStateTransitioned(json_out);
}
// Start/Stop timer events
- controlTimerEvent(crr_state);
+ this->controlTimerEvent(crr_state);
// Release json_object
json_object_put(json_out);
@@ -641,35 +614,35 @@ static int checkPolicy(sd_event_source *source, void *data) {
sd_event_source_unref(source);
// Remove event source from list
- if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
- pm::event_source_list.erase(event_data);
+ if (this->event_source_list.find(event_data) != this->event_source_list.end()) {
+ this->event_source_list.erase(event_data);
}
HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
return 0;
}
-static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
+int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data) {
HMI_DEBUG("wm:pm", "Call");
- int ret = checkPolicy(source, data);
+ int ret = this->transitionState(source, data);
return ret;
}
-static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
+int PolicyManager::setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role)
{
HMI_DEBUG("wm:pm", "Call");
HMI_DEBUG("wm:pm", "event:0x%x delay:%d role:%s", event, delay_ms, role.c_str());
// Store requested role
- pm::g_req_role_list[event] = role;
+ this->req_role_list[event] = role;
if (0 == delay_ms) {
- int ret = sd_event_add_defer(pm::event_loop, NULL,
- &checkPolicy, new int(event));
+ int ret = sd_event_add_defer(this->event_loop, NULL,
+ &pm::transitionStateWrapper, new int(event));
if (0 > ret) {
HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
- pm::g_req_role_list.erase(event);
+ this->req_role_list.erase(event);
return -1;
}
}
@@ -685,16 +658,16 @@ static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
// Set timer
struct sd_event_source* event_source;
- int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
- &timerEvent, new int(event));
+ int ret = sd_event_add_time(this->event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
+ &pm::timerEventWrapper, new int(event));
if (0 > ret) {
HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
- pm::g_req_role_list.erase(event);
+ this->req_role_list.erase(event);
return -1;
}
// Store event source
- pm::event_source_list[event] = event_source;
+ this->event_source_list[event] = event_source;
}
return 0;
@@ -703,8 +676,8 @@ static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
void PolicyManager::registerCallback(CallbackTable callback) {
HMI_DEBUG("wm:pm", "Call");
- pm::callback.onStateTransitioned = callback.onStateTransitioned;
- pm::callback.onError = callback.onError;
+ this->callback.onStateTransitioned = callback.onStateTransitioned;
+ this->callback.onError = callback.onError;
}
int PolicyManager::setInputEventData(json_object* json_in) {
@@ -800,7 +773,7 @@ int PolicyManager::executeStateTransition() {
this->event_info_queue.pop();
// Set event info for checking policy
- ret = checkPolicyEntry(event_info.event, event_info.delay, event_info.role);
+ ret = this->setStateTransitionProcessToSystemd(event_info.event, event_info.delay, event_info.role);
}
return ret;
}
@@ -811,7 +784,7 @@ void PolicyManager::undoState() {
// Undo state of STM
stm::stmUndoState();
- pm::g_crr_layers = pm::g_prv_layers;
+ this->crr_layers = this->prv_layers;
}
extern const char* kDefaultRoleDb;
@@ -966,8 +939,8 @@ int PolicyManager::loadLayoutDb() {
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));
- pm::LayoutState layout_state;
- pm::AreaState area_state;
+ LayoutState layout_state;
+ AreaState area_state;
std::map<std::string, int> category_num;
for (int ctg_no = stm::StmCtgNoMin;
ctg_no <= stm::StmCtgNoMax; ctg_no++) {
@@ -1014,18 +987,18 @@ int PolicyManager::loadLayoutDb() {
layout_state.name = layout;
layout_state.category_num = category_num;
- pm::g_default_layouts[layout] = layout_state;
+ this->default_layouts[layout] = layout_state;
}
// initialize for none layout
- pm::LayoutState none_layout_state;
+ LayoutState none_layout_state;
memset(&none_layout_state, 0, sizeof(none_layout_state));
none_layout_state.name = "none";
- pm::g_default_layouts["none"] = none_layout_state;
+ this->default_layouts["none"] = none_layout_state;
// Check
- for(auto itr_layout = pm::g_default_layouts.begin();
- itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
+ 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());
for (auto itr_area = itr_layout->second.area_list.begin();
diff --git a/src/policy_manager/policy_manager.hpp b/src/policy_manager/policy_manager.hpp
index fd7314c..47f7d26 100644
--- a/src/policy_manager/policy_manager.hpp
+++ b/src/policy_manager/policy_manager.hpp
@@ -22,10 +22,13 @@
#include <vector>
#include <queue>
-
struct json_object;
struct sd_event;
+struct sd_event_source;
+namespace stm {
+struct StmState;
+}
class PolicyManager {
@@ -46,6 +49,9 @@ public:
int executeStateTransition();
void undoState();
+ int transitionState(sd_event_source *source, void *data);
+ int timerEvent(sd_event_source *source, uint64_t usec, void *data);
+
private:
// Disable copy and move
PolicyManager(PolicyManager const &) = delete;
@@ -59,6 +65,31 @@ private:
uint64_t delay;
} EventInfo;
+ typedef struct AreaState {
+ std::string name;
+ std::string category;
+ std::string role;
+ } AreaState;
+
+ typedef std::vector<AreaState> AreaList;
+ typedef struct LayoutState {
+ std::string name;
+ std::map<std::string, int> category_num;
+ AreaList area_list;
+ std::map<std::string, std::vector<std::string>> role_history;
+ } LayoutState;
+
+ typedef struct LayerState {
+ std::string name;
+ LayoutState layout_state;
+ bool changed;
+ } LayerState;
+
+ typedef struct CarElement {
+ std::string state;
+ bool changed;
+ } CarElement;
+
// Convert map
std::unordered_map<std::string, int> eventname2no;
std::unordered_map<std::string, int> categoryname2no;
@@ -70,8 +101,32 @@ private:
std::queue<EventInfo> event_info_queue;
- void initializeLocalState();
+ struct sd_event* event_loop;
+ CallbackTable callback;
+
+ std::map<int, struct sd_event_source*> event_source_list;
+ std::map<int, std::string> req_role_list;
+
+ std::unordered_map<std::string, CarElement> prv_car_elements;
+ std::unordered_map<std::string, CarElement> crr_car_elements;
+
+ std::unordered_map<std::string, LayerState> prv_layers;
+ std::unordered_map<std::string, LayerState> crr_layers;
+
+ std::unordered_map<std::string, LayerState> prv_layers_car_stop;
+
+ std::unordered_map<std::string, LayoutState> default_layouts;
+
+ void initializeState();
+ void initializeCarElementState();
+ void initializeLayerState();
int initializeSdEventLoop();
+ void updateState(int event_data, stm::StmState crr_state);
+ void updateCarElementState(stm::StmState crr_state);
+ void updateLayerState(int event_data, stm::StmState crr_state);
+ void createOutputInformation(stm::StmState crr_state, json_object **json_out);
+ void controlTimerEvent(stm::StmState crr_state);
+ int setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role);
// Load role.db
int loadRoleDb();
@@ -79,11 +134,15 @@ private:
// Load layout.db
int loadLayoutDb();
+ void addStateToJson(const char* name, bool changed,
+ std::string state, json_object** json_out);
+ void addStateToJson(const char* layer_name, bool changed,
+ AreaList area_list, json_object** json_out);
const char* getStringFromJson(json_object* obj, const char* key);
int inputJsonFilie(const char* file, json_object** obj);
+
std::vector<std::string> parseString(std::string str, char delimiter);
std::string deleteSpace(std::string str);
-
};
diff --git a/src/policy_manager/zipc/dummy_stm.h b/src/policy_manager/zipc/dummy_stm.h
index 9e64918..9a1df3c 100644
--- a/src/policy_manager/zipc/dummy_stm.h
+++ b/src/policy_manager/zipc/dummy_stm.h
@@ -196,11 +196,18 @@ typedef struct StmBaseState {
int state;
} StmBaseState;
-typedef struct {
+typedef struct StmState {
StmBaseState car_element[StmCarElementNoNum];
StmBaseState layer[StmLayerNoNum];
} StmState;
+#if 0
+struct stm_state_t {
+ stm_base_state car_element[gStmCarElementNoNum];
+ stm_base_state layer[gStmLayerNoNum];
+};
+typedef struct stm_state_t stm_state_t;
+#endif
// API
void stmInitialize();