2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <json-c/json.h>
24 #include "policy_manager.hpp"
25 #include "hmi-debug.h"
29 #define AFB_BINDING_VERSION 2
30 #include <afb/afb-binding.h>
31 #include <systemd/sd-event.h>
37 static PolicyManager *g_context;
39 static int transitionStateWrapper(sd_event_source *source, void *data)
41 int ret = g_context->transitionState(source, data);
45 static int timerEventWrapper(sd_event_source *source, uint64_t usec, void *data)
47 int ret = g_context->timerEvent(source, usec, data);
53 PolicyManager::PolicyManager()
62 int PolicyManager::initialize()
67 for (int i = StmEvtNoMin; i <= StmEvtNoMax; i++)
69 HMI_DEBUG("wm:pm", "event name:%s no:%d", kStmEventName[i], i);
70 this->eventname2no[kStmEventName[i]] = i;
73 for (int i = StmCtgNoMin; i <= StmCtgNoMax; i++)
75 HMI_DEBUG("wm:pm", "category name:%s no:%d", kStmCategoryName[i], i);
76 this->categoryname2no[kStmCategoryName[i]] = i;
79 for (int i = StmAreaNoMin; i <= StmAreaNoMax; i++)
81 HMI_DEBUG("wm:pm", "area name:%s no:%d", kStmAreaName[i], i);
82 this->areaname2no[kStmAreaName[i]] = i;
86 ret = this->loadRoleDb();
89 HMI_ERROR("wm:pm", "Load roles.db Error!!");
94 ret = this->loadStateDb();
97 HMI_ERROR("wm:pm", "Load states.db Error!!");
101 // Initialize state which is managed by PolicyManager
102 this->initializeState();
104 // Initialize StateTransitioner
108 pm::g_context = this;
113 void PolicyManager::registerCallback(CallbackTable callback)
115 this->callback.onStateTransitioned = callback.onStateTransitioned;
116 this->callback.onError = callback.onError;
119 int PolicyManager::setInputEventData(json_object *json_in)
122 if (nullptr == json_in)
124 HMI_ERROR("wm:pm", "Argument is NULL!!");
128 // Get event from json_object
129 const char *event = this->getStringFromJson(json_in, "event");
130 int event_no = StmEvtNoNone;
131 if (nullptr != event)
133 // Convert name to number
134 auto itr = this->eventname2no.find(event);
135 if (this->eventname2no.end() != itr)
137 event_no = this->eventname2no[event];
138 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
142 HMI_ERROR("wm:pm", "Invalid event name!!");
148 HMI_ERROR("wm:pm", "Event is not set!!");
152 // Get role from json_object
153 const char *role = this->getStringFromJson(json_in, "role");
154 int category_no = StmCtgNoNone;
157 HMI_DEBUG("wm:pm", "role(%s)", role);
159 // Convert role to category
160 std::string category = "";
161 auto itr = this->role2category.find(role);
162 if (this->role2category.end() != itr)
164 category = this->role2category[role];
168 itr = this->role2category.find("fallback");
169 if (this->role2category.end() != itr)
171 HMI_DEBUG("wm:pm", "Role:%s is not registered in roles.db, fallback as normal app", role);
172 category = this->role2category["fallback"];
178 // Convert name to number
179 category_no = categoryname2no[category];
180 HMI_DEBUG("wm:pm", "category(%s:%d)", category.c_str(), category_no);
184 if (StmCtgNoNone == category_no)
189 // Get areat from json_object
190 const char *area = this->getStringFromJson(json_in, "area");
191 int area_no = StmAreaNoNone;
194 auto itr = this->areaname2no.find(area);
195 if (this->areaname2no.end() != itr)
197 area_no = this->areaname2no[area];
198 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
202 // If role is set and area is not set, use default area
203 if ((StmAreaNoNone == area_no) && (StmCtgNoNone != category_no))
205 std::string def_area = this->role2defaultarea[role];
206 area_no = this->areaname2no[def_area];
207 HMI_DEBUG("wm:pm", "area(%s:%d)", def_area.c_str(), area_no);
210 // Set event info to the queue
211 EventInfo event_info;
212 int event_id = STM_CREATE_EVENT_ID(event_no, category_no, area_no);
213 event_info.event = event_id;
214 event_info.role = std::string(role);
215 event_info.delay = 0;
216 this->event_info_queue.push(event_info);
221 int PolicyManager::executeStateTransition()
224 EventInfo event_info;
226 while (!this->event_info_queue.empty())
228 // Get event info from queue and delete
229 event_info = this->event_info_queue.front();
230 this->event_info_queue.pop();
232 // Set event info for checking policy
233 ret = this->setStateTransitionProcessToSystemd(event_info.event, event_info.delay, event_info.role);
238 void PolicyManager::undoState()
240 HMI_DEBUG("wm:pm", "Undo State !!!");
245 HMI_DEBUG("wm:pm", ">>>>>>>>>> BEFORE UNDO");
246 this->dumpLayerState(this->crr_layers);
248 this->crr_layers = this->prv_layers;
250 HMI_DEBUG("wm:pm", ">>>>>>>>>> AFTER UNDO");
251 this->dumpLayerState(this->crr_layers);
254 void PolicyManager::initializeState()
256 this->initializeLayerState();
259 void PolicyManager::initializeLayerState()
262 LayoutState init_layout;
263 init_area.name = kStmAreaName[StmAreaNoNone];
264 init_area.category = kStmCategoryName[StmCtgNoNone];
266 init_layout.name = kStmLayoutName[StmLayoutNoNone];
267 init_layout.area_list.push_back(init_area);
269 for (int i = StmLayerNoMin; i <= StmLayerNoMax; i++)
271 const char *layer_name = kStmLayerName[i];
272 this->crr_layers[layer_name].name = layer_name;
273 this->crr_layers[layer_name].layout_state = init_layout;
274 this->crr_layers[layer_name].changed = false;
277 this->prv_layers = this->crr_layers;
280 void PolicyManager::addStateToJson(const char *name, bool changed,
281 std::string state, json_object **json_out)
283 if ((nullptr == name) || (nullptr == json_out))
285 HMI_ERROR("wm:pm", "Invalid argument!!!");
289 json_object_object_add(*json_out, "name", json_object_new_string(name));
290 json_object_object_add(*json_out, "state", json_object_new_string(state.c_str()));
291 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
294 void PolicyManager::addStateToJson(const char *layer_name, bool changed,
295 AreaList area_list, json_object **json_out)
297 if ((nullptr == layer_name) || (nullptr == json_out))
299 HMI_ERROR("wm:pm", "Invalid argument!!!");
303 json_object *json_areas = json_object_new_array();
304 json_object *json_tmp;
305 for (const auto &as : area_list)
307 json_tmp = json_object_new_object();
308 json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
309 json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
310 json_object_array_add(json_areas, json_tmp);
313 json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
314 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
315 json_object_object_add(*json_out, "areas", json_areas);
318 void PolicyManager::updateState(int event_id, StmState crr_state)
320 this->updateLayerState(event_id, crr_state);
323 void PolicyManager::updateLayerState(int event_id, StmState crr_state)
325 int event_no, category_no, area_no;
327 for (int layer_no = StmLayerNoMin;
328 layer_no <= StmLayerNoMax; layer_no++)
330 HMI_DEBUG("wm:pm", ">>> LAYER:%s CHANGED:%d LAYOUT:%s",
331 kStmLayerName[layer_no], crr_state.layer[layer_no].changed,
332 kStmLayoutName[crr_state.layer[layer_no].state]);
335 event_no = STM_GET_EVENT_FROM_ID(event_id);
336 category_no = STM_GET_CATEGORY_FROM_ID(event_id);
337 area_no = STM_GET_AREA_FROM_ID(event_id);
339 std::string req_evt = kStmEventName[event_no];
340 std::string req_ctg = kStmCategoryName[category_no];
341 std::string req_area = kStmAreaName[area_no];
342 std::string req_role = this->req_role_list[event_id];
344 // Store previous layers
345 this->prv_layers = this->crr_layers;
348 for (int layer_no = StmLayerNoMin;
349 layer_no <= StmLayerNoMax; layer_no++)
351 const char *layer_name = kStmLayerName[layer_no];
353 // This layer is changed?
354 int changed = crr_state.layer[layer_no].changed;
357 HMI_DEBUG("wm:pm", ">>>>>>>>>> Update layer:%s", layer_name);
359 // Get previous layout name of this layer
360 LayoutState prv_layout_state = this->prv_layers[layer_name].layout_state;
361 std::string prv_layout_name = prv_layout_state.name;
363 // Get current layout name of this layer
364 int crr_layout_state_no = crr_state.layer[layer_no].state;
365 std::string crr_layout_name = std::string(kStmLayoutName[crr_layout_state_no]);
367 LayoutState crr_layout_state;
368 if ((prv_layout_name == crr_layout_name) &&
369 (kStmAreaName[StmAreaNoNone] == crr_layout_name))
371 // Copy previous layout state for current
372 crr_layout_state = prv_layout_state;
377 // Copy previous layout state for current
378 crr_layout_state = prv_layout_state;
380 HMI_DEBUG("wm:pm", "-- layout name previous:%s current:%s",
381 prv_layout_name.c_str(), crr_layout_name.c_str());
382 if (prv_layout_name == crr_layout_name)
384 HMI_DEBUG("wm:pm", "---- Previous layout is same with current");
388 // If previous layout is NOT same with current,
389 // current areas is set with default value
390 HMI_DEBUG("wm:pm", "---- Previous layout is NOT same with current");
391 crr_layout_state.name = this->default_layouts[crr_layout_name].name;
392 crr_layout_state.category_num = this->default_layouts[crr_layout_name].category_num;
393 crr_layout_state.area_list = this->default_layouts[crr_layout_name].area_list;
396 // Create candidate list
397 std::map<std::string, AreaList> cand_list;
398 for (int ctg_no = StmCtgNoMin;
399 ctg_no <= StmCtgNoMax; ctg_no++)
401 if (ctg_no == StmCtgNoNone)
406 const char *ctg = kStmCategoryName[ctg_no];
407 HMI_DEBUG("wm:pm", "-- Create candidate list for ctg:%s", ctg);
409 AreaList tmp_cand_list;
410 int candidate_num = 0;
411 int blank_num = crr_layout_state.category_num[ctg];
413 // If requested event is "activate"
414 // and there are requested category and area,
415 // update area with requested role in current layout.
416 bool request_for_this_layer = false;
417 std::string used_role = "";
418 if ((ctg == req_ctg) && ("activate" == req_evt))
420 HMI_DEBUG("wm:pm", "---- Requested event is activate");
421 for (AreaState &as : crr_layout_state.area_list)
423 if (as.category == req_ctg)
425 request_for_this_layer = true;
427 if (as.name == req_area)
430 used_role = req_role;
432 HMI_DEBUG("wm:pm", "------ Update current layout: area:%s category:%s role:%s",
433 as.name.c_str(), as.category.c_str(), as.role.c_str());
440 // Create candidate list for category from the previous displayed categories
441 for (AreaState area_state : prv_layout_state.area_list)
443 if ((std::string(ctg) == area_state.category) &&
444 (used_role != area_state.role))
446 // If there is the category
447 // which is same with new category and not used for updating yet,
449 HMI_DEBUG("wm:pm", "---- Push previous(category:%s role:%s) to candidate list",
450 area_state.category.c_str(), area_state.role.c_str());
451 tmp_cand_list.push_back(area_state);
456 // If NOT updated by requested area:
457 // there is not requested area in new layout,
458 // so push requested role to candidate list
459 if (request_for_this_layer && ("" == used_role))
461 HMI_DEBUG("wm:pm", "---- Push request(area:%s category:%s role:%s) to candidate list",
462 req_area.c_str(), req_ctg.c_str(), req_role.c_str());
463 AreaState area_state;
464 area_state.name = req_area;
465 area_state.category = req_ctg;
466 area_state.role = req_role;
467 tmp_cand_list.push_back(area_state);
471 HMI_DEBUG("wm:pm", "---- blank_num:%d candidate_num:%d", blank_num, candidate_num);
473 // Compare number of candidate/blank,
474 // And remove role in order of the oldest as necessary
475 if (candidate_num < blank_num)
477 // Refer history stack
478 // and add to the top of tmp_cand_list in order to the newest
479 while (candidate_num != blank_num)
481 AreaState area_state;
482 area_state.name = kStmAreaName[StmAreaNoNone];
483 area_state.category = ctg;
484 area_state.role = this->popRoleHistory(ctg);
485 if ("" == area_state.role)
487 HMI_ERROR("wm:pm", "There is no role in history stack!!");
489 tmp_cand_list.push_back(area_state);
490 HMI_DEBUG("wm:pm", "------ Add role:%s to candidate list",
491 area_state.role.c_str());
495 else if (candidate_num > blank_num)
497 // Remove the oldest role from candidate list
498 while (candidate_num != blank_num)
500 std::string removed_role = tmp_cand_list.begin()->role;
501 HMI_DEBUG("wm:pm", "------ Remove the oldest role:%s from candidate list",
502 removed_role.c_str());
503 tmp_cand_list.erase(tmp_cand_list.begin());
506 // Push removed data to history stack
507 this->pushRoleHistory(ctg, removed_role);
509 // Remove from current layout
510 for (AreaState &as : crr_layout_state.area_list)
512 if (as.role == removed_role)
520 { // (candidate_num == blank_num)
524 cand_list[ctg] = tmp_cand_list;
528 HMI_DEBUG("wm:pm", "-- Update areas by using candidate list");
529 for (AreaState &as : crr_layout_state.area_list)
531 HMI_DEBUG("wm:pm", "---- Check area:%s category:%s role:%s",
532 as.name.c_str(), as.category.c_str(), as.role.c_str());
535 HMI_DEBUG("wm:pm", "------ Update this area with role:%s",
536 cand_list[as.category].begin()->role.c_str());
537 as.role = cand_list[as.category].begin()->role;
538 cand_list[as.category].erase(cand_list[as.category].begin());
542 // Update current layout of this layer
543 this->crr_layers[layer_name].layout_state = crr_layout_state;
545 // Update changed flag
546 this->crr_layers[layer_name].changed = (changed) ? true : false;
549 // Erase role for the event_id from list
550 this->req_role_list.erase(event_id);
552 HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP LAYERS (BEFORE)");
553 this->dumpLayerState(this->prv_layers);
555 HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP LAYERS (AFTER)");
556 this->dumpLayerState(this->crr_layers);
558 this->dumpRoleHistory();
561 void PolicyManager::createOutputInformation(StmState crr_state, json_object **json_out)
563 json_object *json_tmp;
565 // Create layout information
570 // "changed": <bool>,
573 // "name":<const char*>,
574 // "role":<const char*>
581 json_object *json_layer = json_object_new_array();
582 const char *layer_name;
583 for (int layer_no = StmLayerNoMin;
584 layer_no <= StmLayerNoMax; layer_no++)
586 layer_name = kStmLayerName[layer_no];
587 json_tmp = json_object_new_object();
588 this->addStateToJson(layer_name,
589 this->crr_layers[layer_name].changed,
590 this->crr_layers[layer_name].layout_state.area_list,
592 json_object_array_add(json_layer, json_tmp);
594 json_object_object_add(*json_out, "layers", json_layer);
597 int PolicyManager::transitionState(sd_event_source *source, void *data)
599 HMI_DEBUG("wm:pm", ">>>>>>>>>> START STATE TRANSITION");
601 int event_id = *((int *)data);
603 int event_no, category_no, area_no;
604 event_no = STM_GET_EVENT_FROM_ID(event_id);
605 category_no = STM_GET_CATEGORY_FROM_ID(event_id);
606 area_no = STM_GET_AREA_FROM_ID(event_id);
607 HMI_DEBUG("wm:pm", ">>>>>>>>>> EVENT:%s CATEGORY:%s AREA:%s",
608 kStmEventName[event_no],
609 kStmCategoryName[category_no],
610 kStmAreaName[area_no]);
614 int ret = stmTransitionState(event_id, &crr_state);
617 HMI_ERROR("wm:pm", "Failed transition state");
618 if (nullptr != this->callback.onError)
620 json_object *json_out = json_object_new_object();
621 json_object_object_add(json_out, "message",
622 json_object_new_string("Failed to transition state"));
623 json_object_object_add(json_out, "event",
624 json_object_new_string(kStmEventName[event_no]));
625 json_object_object_add(json_out, "role",
626 json_object_new_string(this->req_role_list[event_id].c_str()));
627 json_object_object_add(json_out, "area",
628 json_object_new_string(kStmAreaName[area_no]));
629 this->callback.onError(json_out);
630 json_object_put(json_out);
635 // Update state which is managed by PolicyManager
636 this->updateState(event_id, crr_state);
638 // Create output information for ResourceManager
639 json_object *json_out = json_object_new_object();
640 this->createOutputInformation(crr_state, &json_out);
642 // Notify changed state
643 if (nullptr != this->callback.onStateTransitioned)
645 this->callback.onStateTransitioned(json_out);
648 // Release json_object
649 json_object_put(json_out);
654 // Destroy sd_event_source object
655 sd_event_source_unref(source);
657 // Remove event source from list
658 if (this->event_source_list.find(event_id) != this->event_source_list.end())
660 this->event_source_list.erase(event_id);
663 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH STATE TRANSITION");
667 int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data)
669 HMI_DEBUG("wm:pm", "Call");
671 int ret = this->transitionState(source, data);
675 int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t delay_ms, std::string role)
677 HMI_DEBUG("wm:pm", "event_id:0x%x delay:%d role:%s", event_id, delay_ms, role.c_str());
679 // Store requested role
680 this->req_role_list[event_id] = role;
684 int ret = sd_event_add_defer(afb_daemon_get_event_loop(), NULL,
685 &pm::transitionStateWrapper, new int(event_id));
688 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
689 this->req_role_list.erase(event_id);
696 struct timespec time_spec;
697 clock_gettime(CLOCK_MONOTONIC, &time_spec);
699 // Calculate timer fired time
700 uint64_t usec = (time_spec.tv_sec * 1000000) + (time_spec.tv_nsec / 1000) + (delay_ms * 1000);
703 struct sd_event_source *event_source;
704 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &event_source,
705 CLOCK_MONOTONIC, usec, 1,
706 &pm::timerEventWrapper, new int(event_id));
709 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
710 this->req_role_list.erase(event_id);
714 // Store event source
715 this->event_source_list[event_id] = event_source;
721 int PolicyManager::loadRoleDb()
723 std::string file_name;
725 // Get afm application installed dir
726 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
727 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
729 if (!afm_app_install_dir)
731 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
735 file_name = std::string(afm_app_install_dir) + std::string("/etc/roles.db");
739 json_object *json_obj;
740 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
743 HMI_ERROR("wm:pm", "Could not open roles.db, so use default role information");
744 json_obj = json_tokener_parse(kDefaultRoleDb);
746 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
748 json_object *json_roles;
749 if (!json_object_object_get_ex(json_obj, "roles", &json_roles))
751 HMI_ERROR("wm:pm", "Parse Error!!");
755 int len = json_object_array_length(json_roles);
756 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
757 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
759 json_object *json_tmp;
760 const char *category;
763 for (int i = 0; i < len; i++)
765 json_tmp = json_object_array_get_idx(json_roles, i);
767 category = this->getStringFromJson(json_tmp, "category");
768 roles = this->getStringFromJson(json_tmp, "role");
769 areas = this->getStringFromJson(json_tmp, "area");
771 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas))
773 HMI_ERROR("wm:pm", "Parse Error!!");
777 // Parse roles by '|'
778 std::vector<std::string> vct_roles;
779 vct_roles = this->parseString(std::string(roles), '|');
781 // Parse areas by '|'
782 std::vector<std::string> vct_areas;
783 vct_areas = this->parseString(std::string(areas), '|');
785 // Set role, category, default area
786 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr)
788 // Delete space from role and area name
789 std::string role = this->deleteSpace(*itr);
790 std::string area = this->deleteSpace(vct_areas[0]);
792 this->role2category[role] = std::string(category);
793 this->role2defaultarea[role] = area;
796 this->category2role[std::string(category)] = std::string(roles);
800 HMI_DEBUG("wm:pm", "Check role2category");
801 for (const auto &x : this->role2category)
803 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
806 HMI_DEBUG("wm:pm", "Check role2defaultarea");
807 for (const auto &x : this->role2defaultarea)
809 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
812 HMI_DEBUG("wm:pm", "Check category2role");
813 for (const auto &x : this->category2role)
815 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
821 int PolicyManager::loadStateDb()
823 HMI_DEBUG("wm:pm", "Call");
825 // Get afm application installed dir
826 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
827 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
829 std::string file_name;
830 if (!afm_app_install_dir)
832 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
836 file_name = std::string(afm_app_install_dir) + std::string("/etc/states.db");
840 json_object *json_obj;
841 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
844 HMI_DEBUG("wm:pm", "Could not open states.db, so use default layout information");
845 json_obj = json_tokener_parse(kDefaultStateDb);
847 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
850 HMI_DEBUG("wm:pm", "Perse states");
851 json_object *json_cfg;
852 if (!json_object_object_get_ex(json_obj, "states", &json_cfg))
854 HMI_ERROR("wm:pm", "Parse Error!!");
858 int len = json_object_array_length(json_cfg);
859 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
860 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
864 const char *category;
865 for (int i = 0; i < len; i++)
867 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
869 layout = this->getStringFromJson(json_tmp, "name");
870 if (nullptr == layout)
872 HMI_ERROR("wm:pm", "Parse Error!!");
875 HMI_DEBUG("wm:pm", "> layout:%s", layout);
877 json_object *json_area_array;
878 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array))
880 HMI_ERROR("wm:pm", "Parse Error!!");
884 int len_area = json_object_array_length(json_area_array);
885 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
886 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
888 LayoutState layout_state;
889 AreaState area_state;
890 std::map<std::string, int> category_num;
891 for (int ctg_no = StmCtgNoMin;
892 ctg_no <= StmCtgNoMax; ctg_no++)
894 const char *ctg_name = kStmCategoryName[ctg_no];
895 category_num[ctg_name] = 0;
898 for (int j = 0; j < len_area; j++)
900 json_object *json_area = json_object_array_get_idx(json_area_array, j);
903 const char *area = this->getStringFromJson(json_area, "name");
906 HMI_ERROR("wm:pm", "Parse Error!!");
909 area_state.name = std::string(area);
910 HMI_DEBUG("wm:pm", ">> area:%s", area);
912 // Get app attribute of the area
913 category = this->getStringFromJson(json_area, "category");
914 if (nullptr == category)
916 HMI_ERROR("wm:pm", "Parse Error!!");
919 area_state.category = std::string(category);
920 category_num[category]++;
921 HMI_DEBUG("wm:pm", ">>> category:%s", category);
923 role = this->getStringFromJson(json_area, "role");
926 // Role is NOT essential here
927 area_state.role = std::string(role);
931 area_state.role = std::string("");
933 HMI_DEBUG("wm:pm", ">>> role:%s", role);
935 layout_state.area_list.push_back(area_state);
938 layout_state.name = layout;
939 layout_state.category_num = category_num;
940 this->default_layouts[layout] = layout_state;
943 // initialize for none layout
944 LayoutState none_layout_state;
945 memset(&none_layout_state, 0, sizeof(none_layout_state));
946 none_layout_state.name = "none";
947 this->default_layouts["none"] = none_layout_state;
950 for (auto itr_layout = this->default_layouts.begin();
951 itr_layout != this->default_layouts.end(); ++itr_layout)
953 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
955 for (auto itr_area = itr_layout->second.area_list.begin();
956 itr_area != itr_layout->second.area_list.end(); ++itr_area)
958 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
959 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
960 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
964 // Release json_object
965 json_object_put(json_obj);
970 static const int kHistoryNum = 5;
971 void PolicyManager::pushRoleHistory(std::string category, std::string role)
973 auto i = std::remove_if(this->role_history[category].begin(),
974 this->role_history[category].end(),
975 [role](std::string x) { return (role == x); });
977 if (this->role_history[category].end() != i)
979 this->role_history[category].erase(i);
982 this->role_history[category].push_back(role);
984 if (kHistoryNum < role_history[category].size())
986 this->role_history[category].erase(
987 this->role_history[category].begin());
991 std::string PolicyManager::popRoleHistory(std::string category)
994 if (role_history[category].empty())
1000 role = this->role_history[category].back();
1001 this->role_history[category].pop_back();
1006 const char *PolicyManager::getStringFromJson(json_object *obj, const char *key)
1009 if (!json_object_object_get_ex(obj, key, &tmp))
1011 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1015 return json_object_get_string(tmp);
1018 int PolicyManager::inputJsonFilie(const char *file, json_object **obj)
1020 const int input_size = 128;
1023 HMI_DEBUG("wm:pm", "Input file: %s", file);
1026 FILE *fp = fopen(file, "rb");
1029 HMI_ERROR("wm:pm", "Could not open file");
1034 struct json_tokener *tokener = json_tokener_new();
1035 enum json_tokener_error json_error;
1036 char buffer[input_size];
1040 size_t len = fread(buffer, sizeof(char), input_size, fp);
1041 *obj = json_tokener_parse_ex(tokener, buffer, len);
1042 if (nullptr != *obj)
1044 HMI_DEBUG("wm:pm", "File input is success");
1049 json_error = json_tokener_get_error(tokener);
1050 if ((json_tokener_continue != json_error) || (input_size > len))
1052 HMI_ERROR("wm:pm", "Failed to parse file (byte:%d err:%s)",
1053 (input_size * block_cnt), json_tokener_error_desc(json_error));
1054 HMI_ERROR("wm:pm", "\n%s", buffer);
1064 // Free json_tokener
1065 json_tokener_free(tokener);
1070 void PolicyManager::dumpLayerState(std::unordered_map<std::string, LayerState> &layers)
1072 HMI_DEBUG("wm:pm", "-------------------------------------------------------------------------------------------------------");
1073 HMI_DEBUG("wm:pm", "|%-15s|%s|%-20s|%-20s|%-20s|%-20s|",
1074 "LAYER", "C", "LAYOUT", "AREA", "CATEGORY", "ROLE");
1075 for (const auto &itr : layers)
1077 LayerState ls = itr.second;
1078 const char* layer = ls.name.c_str();
1079 const char* changed = (ls.changed) ? "T" : "f";
1080 const char* layout = ls.layout_state.name.c_str();
1082 for (const auto &as : ls.layout_state.area_list)
1087 HMI_DEBUG("wm:pm", "|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1088 layer, changed, layout,
1089 as.name.c_str(), as.category.c_str(), as.role.c_str());
1092 HMI_DEBUG("wm:pm", "|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1093 "", "", "", as.name.c_str(), as.category.c_str(), as.role.c_str());
1096 HMI_DEBUG("wm:pm", "-------------------------------------------------------------------------------------------------------");
1099 void PolicyManager::dumpRoleHistory()
1101 HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP ROLE HISTORY ( category [older > newer] )");
1102 for (int ctg_no = StmCtgNoMin; ctg_no <= StmCtgNoMax; ctg_no++)
1104 if (ctg_no == StmCtgNoNone)
1107 std::string category = std::string(kStmCategoryName[ctg_no]);
1109 std::string str = category + " [ ";
1110 for (const auto &i : this->role_history[category])
1114 HMI_DEBUG("wm:pm", "%s", str.c_str());
1118 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter)
1120 // Parse string by delimiter
1121 std::vector<std::string> vct;
1122 std::stringstream ss{str};
1124 while (std::getline(ss, buf, delimiter))
1134 std::string PolicyManager::deleteSpace(std::string str)
1136 std::string ret = str;
1138 while ((pos = ret.find_first_of(" ")) != std::string::npos)
1145 const char *PolicyManager::kDefaultRoleDb = "{ \
1148 \"category\": \"homescreen\", \
1149 \"role\": \"homescreen\", \
1150 \"area\": \"fullscreen\", \
1153 \"category\": \"map\", \
1154 \"role\": \"map\", \
1155 \"area\": \"normal.full | split.main\", \
1158 \"category\": \"general\", \
1159 \"role\": \"launcher | poi | browser | sdl | mixer | radio | hvac | debug | phone | video | music\", \
1160 \"area\": \"normal.full\", \
1163 \"category\": \"system\", \
1164 \"role\": \"settings | dashboard\", \
1165 \"area\": \"normal.full\", \
1168 \"category\": \"software_keyboard\", \
1169 \"role\": \"software_keyboard\", \
1170 \"area\": \"software_keyboard\", \
1173 \"category\": \"restriction\", \
1174 \"role\": \"restriction\", \
1175 \"area\": \"restriction.normal | restriction.split.main | restriction.split.sub\", \
1178 \"category\": \"pop_up\", \
1179 \"role\": \"pop_up\", \
1180 \"area\": \"on_screen\", \
1183 \"category\": \"system_alert\", \
1184 \"role\": \"system_alert\", \
1185 \"area\": \"on_screen\", \
1190 const char *PolicyManager::kDefaultStateDb = "{ \
1193 \"name\": \"homescreen\", \
1194 \"layer\": \"far_homescreen\", \
1197 \"name\": \"fullscreen\", \
1198 \"category\": \"homescreen\" \
1203 \"name\": \"map.normal\", \
1204 \"layer\": \"apps\", \
1207 \"name\": \"normal.full\", \
1208 \"category\": \"map\" \
1213 \"name\": \"map.split\", \
1214 \"layer\": \"apps\", \
1217 \"name\": \"split.main\", \
1218 \"category\": \"map\" \
1221 \"name\": \"split.sub\", \
1222 \"category\": \"splitable\" \
1227 \"name\": \"map.fullscreen\", \
1228 \"layer\": \"apps\", \
1231 \"name\": \"fullscreen\", \
1232 \"category\": \"map\" \
1237 \"name\": \"splitable.normal\", \
1238 \"layer\": \"apps\", \
1241 \"name\": \"normal.full\", \
1242 \"category\": \"splitable\" \
1247 \"name\": \"splitable.split\", \
1248 \"layer\": \"apps\", \
1251 \"name\": \"split.main\", \
1252 \"category\": \"splitable\" \
1255 \"name\": \"split.sub\", \
1256 \"category\": \"splitable\" \
1261 \"name\": \"general.normal\", \
1262 \"layer\": \"apps\", \
1265 \"name\": \"normal.full\", \
1266 \"category\": \"general\" \
1271 \"name\": \"system.normal\", \
1272 \"layer\": \"apps\", \
1275 \"name\": \"normal.full\", \
1276 \"category\": \"system\" \
1281 \"name\": \"software_keyboard\", \
1282 \"layer\": \"near_homescreen\", \
1285 \"name\": \"software_keyboard\", \
1286 \"category\": \"software_keyboard\" \
1291 \"name\": \"restriction.normal\", \
1292 \"layer\": \"restriction\", \
1295 \"name\": \"restriction.normal\", \
1296 \"category\": \"restriction\" \
1301 \"name\": \"restriction.split.main\", \
1302 \"layer\": \"restriction\", \
1305 \"name\": \"restriction.split.main\", \
1306 \"category\": \"restriction\" \
1311 \"name\": \"restriction.split.sub\", \
1312 \"layer\": \"restriction\", \
1315 \"name\": \"restriction.split.sub\", \
1316 \"category\": \"restriction\" \
1321 \"name\": \"pop_up\", \
1322 \"layer\": \"on_screen\", \
1325 \"name\": \"on_screen\", \
1326 \"category\": \"pop_up\" \
1331 \"name\": \"system_alert\", \
1332 \"layer\": \"on_screen\", \
1335 \"name\": \"on_screen\", \
1336 \"category\": \"system_alert\" \