2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018 Konsulko Group
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
24 #include <json-c/json.h>
25 #include "policy_manager.hpp"
27 #include "hmi-debug.h"
31 #define AFB_BINDING_VERSION 2
32 #include <afb/afb-binding.h>
33 #include <systemd/sd-event.h>
39 static const int kInvisibleRoleHistoryNum = 5;
41 static PolicyManager *g_context;
43 static int transitionStateWrapper(sd_event_source *source, void *data)
45 int ret = g_context->transitionState(source, data);
49 static int timerEventWrapper(sd_event_source *source, uint64_t usec, void *data)
51 int ret = g_context->timerEvent(source, usec, data);
57 PolicyManager::PolicyManager()
66 int PolicyManager::initialize()
71 for (int i = StmEvtNoMin; i <= StmEvtNoMax; i++)
73 HMI_DEBUG("wm:pm", "event name:%s no:%d", kStmEventName[i], i);
74 this->eventname2no[kStmEventName[i]] = i;
77 for (int i = StmCtgNoMin; i <= StmCtgNoMax; i++)
79 HMI_DEBUG("wm:pm", "category name:%s no:%d", kStmCategoryName[i], i);
80 this->categoryname2no[kStmCategoryName[i]] = i;
83 for (int i = StmAreaNoMin; i <= StmAreaNoMax; i++)
85 HMI_DEBUG("wm:pm", "area name:%s no:%d", kStmAreaName[i], i);
86 this->areaname2no[kStmAreaName[i]] = i;
90 ret = this->loadRoleDb();
93 HMI_ERROR("wm:pm", "Load roles.db Error!!");
98 ret = this->loadStateDb();
101 HMI_ERROR("wm:pm", "Load states.db Error!!");
105 // Initialize state which is managed by PolicyManager
106 this->initializeState();
108 // Initialize StateTransitioner
112 pm::g_context = this;
117 void PolicyManager::registerCallback(CallbackTable callback)
119 this->callback.onStateTransitioned = callback.onStateTransitioned;
120 this->callback.onError = callback.onError;
123 int PolicyManager::setInputEventData(json_object *json_in)
126 if (nullptr == json_in)
128 HMI_ERROR("wm:pm", "Argument is NULL!!");
132 // Get event from json_object
133 const char *event = this->getStringFromJson(json_in, "event");
134 int event_no = StmEvtNoNone;
135 if (nullptr != event)
137 // Convert name to number
138 auto itr = this->eventname2no.find(event);
139 if (this->eventname2no.end() != itr)
141 event_no = this->eventname2no[event];
142 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
146 HMI_ERROR("wm:pm", "Invalid event name!!");
152 HMI_ERROR("wm:pm", "Event is not set!!");
156 // Get role from json_object
157 const char *role = this->getStringFromJson(json_in, "role");
158 std::string category = "";
159 int category_no = StmCtgNoNone;
162 HMI_DEBUG("wm:pm", "role(%s)", role);
164 // Convert role to category
165 auto itr = this->role2category.find(role);
166 if (this->role2category.end() != itr)
168 category = this->role2category[role];
172 itr = this->role2category.find("fallback");
173 if (this->role2category.end() != itr)
175 HMI_DEBUG("wm:pm", "Role:%s is not registered in roles.db, fallback as normal app", role);
176 category = this->role2category["fallback"];
182 // Convert name to number
183 category_no = categoryname2no[category];
184 HMI_DEBUG("wm:pm", "category(%s:%d)", category.c_str(), category_no);
187 if (StmCtgNoNone == category_no)
192 // Get area from json_object
193 const char *area = this->getStringFromJson(json_in, "area");
194 int area_no = StmAreaNoNone;
195 if ((nullptr != area) && (StmCtgNoNone != category_no))
197 for (const auto &x : this->category2areas[category])
199 if (x == std::string(area))
201 area_no = this->areaname2no[area];
205 if (StmAreaNoNone == area_no)
207 area = this->category2areas[category].front().c_str();
208 area_no = this->areaname2no[area];
210 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
213 // Set event info to the queue
214 EventInfo event_info;
215 int event_id = STM_CREATE_EVENT_ID(event_no, category_no, area_no);
216 event_info.event = event_id;
217 event_info.role = std::string(role);
218 event_info.delay = 0;
219 this->event_info_queue.push(event_info);
224 int PolicyManager::executeStateTransition()
227 EventInfo event_info;
229 // Get event info from queue and delete
230 event_info = this->event_info_queue.front();
231 this->event_info_queue.pop();
233 // Set event info to check policy
234 ret = this->setStateTransitionProcessToSystemd(event_info.event,
240 void PolicyManager::undoState()
242 HMI_DEBUG("wm:pm", "Undo State !!!");
247 HMI_DEBUG("wm:pm", ">>>>>>>>>> BEFORE UNDO");
248 this->dumpLayerState(this->crr_layers);
250 this->crr_layers = this->prv_layers;
252 HMI_DEBUG("wm:pm", ">>>>>>>>>> AFTER UNDO");
253 this->dumpLayerState(this->crr_layers);
256 void PolicyManager::initializeState()
258 this->initializeLayerState();
261 void PolicyManager::initializeLayerState()
264 LayoutState init_layout;
265 init_area.name = kStmAreaName[StmAreaNoNone];
266 init_area.category = kStmCategoryName[StmCtgNoNone];
268 init_layout.name = kStmLayoutName[StmLayoutNoNone];
269 init_layout.area_list.push_back(init_area);
271 for (int i = StmLayerNoMin; i <= StmLayerNoMax; i++)
273 const char *layer_name = kStmLayerName[i];
274 this->crr_layers[layer_name].name = layer_name;
275 this->crr_layers[layer_name].layout_state = init_layout;
276 this->crr_layers[layer_name].changed = false;
279 this->prv_layers = this->crr_layers;
282 void PolicyManager::addStateToJson(const char *name, bool changed,
283 std::string state, json_object **json_out)
285 if ((nullptr == name) || (nullptr == json_out))
287 HMI_ERROR("wm:pm", "Invalid argument!!!");
291 json_object_object_add(*json_out, "name", json_object_new_string(name));
292 json_object_object_add(*json_out, "state", json_object_new_string(state.c_str()));
293 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
296 void PolicyManager::addStateToJson(const char *layer_name, bool changed,
297 AreaList area_list, json_object **json_out)
299 if ((nullptr == layer_name) || (nullptr == json_out))
301 HMI_ERROR("wm:pm", "Invalid argument!!!");
305 json_object *json_areas = json_object_new_array();
306 json_object *json_tmp;
307 for (const auto &as : area_list)
309 json_tmp = json_object_new_object();
310 json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
311 json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
312 json_object_array_add(json_areas, json_tmp);
315 json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
316 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
317 json_object_object_add(*json_out, "areas", json_areas);
320 void PolicyManager::updateState(int event_id, StmState crr_state)
322 this->updateLayer(event_id, crr_state);
325 void PolicyManager::updateLayer(int event_id, StmState crr_state)
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 // Store previous layers
336 this->prv_layers = this->crr_layers;
339 for (int layer_no = StmLayerNoMin;
340 layer_no <= StmLayerNoMax; layer_no++)
342 const char *layer_name = kStmLayerName[layer_no];
344 // This layer is changed?
345 int changed = crr_state.layer[layer_no].changed;
348 HMI_DEBUG("wm:pm", ">>>>>>>>>> Update layout of layer:%s", layer_name);
350 // Get current layout name of this layer
351 int crr_layout_state_no = crr_state.layer[layer_no].state;
352 std::string crr_layout_name = std::string(kStmLayoutName[crr_layout_state_no]);
354 LayoutState crr_layout_state;
355 this->updateLayout(event_id, layer_no,
356 crr_layout_name, crr_layout_state);
358 // Update current layout of this layer
359 this->crr_layers[layer_name].layout_state = crr_layout_state;
361 // Update changed flag
362 this->crr_layers[layer_name].changed = (changed) ? true : false;
365 // Erase role for the event_id from list
366 this->req_role_list.erase(event_id);
368 HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP LAYERS (BEFORE)");
369 this->dumpLayerState(this->prv_layers);
371 HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP LAYERS (AFTER)");
372 this->dumpLayerState(this->crr_layers);
374 this->dumpInvisibleRoleHistory();
377 int PolicyManager::updateLayout(int event_id, int layer_no,
378 std::string crr_layout_name, LayoutState &crr_layout_state)
382 int event_no = STM_GET_EVENT_FROM_ID(event_id);
383 int category_no = STM_GET_CATEGORY_FROM_ID(event_id);
384 int area_no = STM_GET_AREA_FROM_ID(event_id);
386 std::string req_evt = kStmEventName[event_no];
387 std::string req_ctg = kStmCategoryName[category_no];
388 std::string req_area = kStmAreaName[area_no];
389 std::string req_role = this->req_role_list[event_id];
391 const char *layer_name = kStmLayerName[layer_no];
393 // Get previous layout name of this layer
394 LayoutState prv_layout_state = this->prv_layers[layer_name].layout_state;
395 std::string prv_layout_name = prv_layout_state.name;
397 if ((prv_layout_name == crr_layout_name) &&
398 (kStmLayoutName[StmLayoutNoNone] == crr_layout_name))
400 // If previous and current layout are none
401 // Copy previous layout state for current
402 crr_layout_state = prv_layout_state;
407 crr_layout_state = prv_layout_state;
410 HMI_DEBUG("wm:pm", "-- layout name previous:%s current:%s",
411 prv_layout_name.c_str(), crr_layout_name.c_str());
412 if (prv_layout_name == crr_layout_name)
414 HMI_DEBUG("wm:pm", "---- Previous layout is same with current");
418 // If previous layout is NOT same with current,
419 // current areas is set with default value
420 HMI_DEBUG("wm:pm", "---- Previous layout is NOT same with current");
421 crr_layout_state.name = this->default_layouts[crr_layout_name].name;
422 crr_layout_state.category_num = this->default_layouts[crr_layout_name].category_num;
423 crr_layout_state.area_list = this->default_layouts[crr_layout_name].area_list;
426 // Create candidate list
427 std::map<std::string, AreaList> cand_list;
428 // for (int ctg_no = StmCtgNoMin;
429 // ctg_no <= StmCtgNoMax; ctg_no++)
431 for (const auto &ctg : this->layer2categories[layer_name])
433 // if (ctg_no == StmCtgNoNone)
438 // const char *ctg = kStmCategoryName[ctg_no];
439 HMI_DEBUG("wm:pm", "-- Create candidate list for ctg:%s", ctg.c_str());
441 AreaList tmp_cand_list;
442 int candidate_num = 0;
443 int blank_num = crr_layout_state.category_num[ctg];
445 // If requested event is "activate"
446 // and there are requested category and area,
447 // update area with requested role in current layout.
448 bool request_for_this_layer = false;
449 std::string used_role = "";
450 if ((ctg == req_ctg) && ("activate" == req_evt))
452 HMI_DEBUG("wm:pm", "---- Requested event is activate");
453 for (AreaState &as : crr_layout_state.area_list)
455 if (as.category == req_ctg)
457 request_for_this_layer = true;
459 if (as.name == req_area)
462 used_role = req_role;
464 HMI_DEBUG("wm:pm", "------ Update current layout: area:%s category:%s role:%s",
465 as.name.c_str(), as.category.c_str(), as.role.c_str());
472 // Create candidate list for category from the previous displayed categories
473 for (AreaState area_state : prv_layout_state.area_list)
475 if ((ctg == area_state.category) &&
476 (used_role != area_state.role))
478 // If there is the category
479 // which is same with new category and not used for updating yet,
481 HMI_DEBUG("wm:pm", "---- Push previous(category:%s role:%s) to candidate list",
482 area_state.category.c_str(), area_state.role.c_str());
483 tmp_cand_list.push_back(area_state);
488 // If NOT updated by requested area:
489 // there is not requested area in new layout,
490 // so push requested role to candidate list
491 if (request_for_this_layer && ("" == used_role))
493 HMI_DEBUG("wm:pm", "---- Push request(area:%s category:%s role:%s) to candidate list",
494 req_area.c_str(), req_ctg.c_str(), req_role.c_str());
495 AreaState area_state;
496 area_state.name = req_area;
497 area_state.category = req_ctg;
498 area_state.role = req_role;
499 tmp_cand_list.push_back(area_state);
503 HMI_DEBUG("wm:pm", "---- blank_num:%d candidate_num:%d", blank_num, candidate_num);
505 // Compare number of candidate/blank,
506 // And remove role in order of the oldest as necessary
507 if (candidate_num < blank_num)
509 // Refer history stack
510 // and add to the top of tmp_cand_list in order to the newest
511 while (candidate_num != blank_num)
513 AreaState area_state;
514 area_state.name = kStmAreaName[StmAreaNoNone];
515 area_state.category = ctg;
516 area_state.role = this->popInvisibleRoleHistory(ctg);
517 if ("" == area_state.role)
519 HMI_ERROR("wm:pm", "There is no role in history stack!!");
521 tmp_cand_list.push_back(area_state);
522 HMI_DEBUG("wm:pm", "------ Add role:%s to candidate list",
523 area_state.role.c_str());
527 else if (candidate_num > blank_num)
529 // Remove the oldest role from candidate list
530 while (candidate_num != blank_num)
532 std::string removed_role = tmp_cand_list.begin()->role;
533 HMI_DEBUG("wm:pm", "------ Remove the oldest role:%s from candidate list",
534 removed_role.c_str());
535 tmp_cand_list.erase(tmp_cand_list.begin());
538 // Push removed data to history stack
539 this->pushInvisibleRoleHistory(ctg, removed_role);
541 // Remove from current layout
542 for (AreaState &as : crr_layout_state.area_list)
544 if (as.role == removed_role)
552 { // (candidate_num == blank_num)
556 cand_list[ctg] = tmp_cand_list;
560 HMI_DEBUG("wm:pm", "-- Update areas by using candidate list");
561 for (AreaState &as : crr_layout_state.area_list)
563 HMI_DEBUG("wm:pm", "---- Check area:%s category:%s role:%s",
564 as.name.c_str(), as.category.c_str(), as.role.c_str());
567 HMI_DEBUG("wm:pm", "------ Update this area with role:%s",
568 cand_list[as.category].begin()->role.c_str());
569 as.role = cand_list[as.category].begin()->role;
570 cand_list[as.category].erase(cand_list[as.category].begin());
577 void PolicyManager::createOutputInformation(StmState crr_state, json_object **json_out)
579 json_object *json_tmp;
581 // Create layout information
586 // "changed": <bool>,
589 // "name":<const char*>,
590 // "role":<const char*>
597 json_object *json_layer = json_object_new_array();
598 const char *layer_name;
599 for (int layer_no = StmLayerNoMin;
600 layer_no <= StmLayerNoMax; layer_no++)
602 layer_name = kStmLayerName[layer_no];
603 json_tmp = json_object_new_object();
604 this->addStateToJson(layer_name,
605 this->crr_layers[layer_name].changed,
606 this->crr_layers[layer_name].layout_state.area_list,
608 json_object_array_add(json_layer, json_tmp);
610 json_object_object_add(*json_out, "layers", json_layer);
613 int PolicyManager::transitionState(sd_event_source *source, void *data)
615 HMI_DEBUG("wm:pm", ">>>>>>>>>> START STATE TRANSITION");
617 int event_id = *((int *)data);
619 int event_no, category_no, area_no;
620 event_no = STM_GET_EVENT_FROM_ID(event_id);
621 category_no = STM_GET_CATEGORY_FROM_ID(event_id);
622 area_no = STM_GET_AREA_FROM_ID(event_id);
623 HMI_DEBUG("wm:pm", ">>>>>>>>>> EVENT:%s CATEGORY:%s AREA:%s",
624 kStmEventName[event_no],
625 kStmCategoryName[category_no],
626 kStmAreaName[area_no]);
630 int ret = stmTransitionState(event_id, &crr_state);
633 HMI_ERROR("wm:pm", "Failed transition state");
634 if (nullptr != this->callback.onError)
636 json_object *json_out = json_object_new_object();
637 json_object_object_add(json_out, "message",
638 json_object_new_string("Failed to transition state"));
639 json_object_object_add(json_out, "event",
640 json_object_new_string(kStmEventName[event_no]));
641 json_object_object_add(json_out, "role",
642 json_object_new_string(this->req_role_list[event_id].c_str()));
643 json_object_object_add(json_out, "area",
644 json_object_new_string(kStmAreaName[area_no]));
645 this->callback.onError(json_out);
646 json_object_put(json_out);
651 // Update state which is managed by PolicyManager
652 this->updateState(event_id, crr_state);
654 // Create output information for ResourceManager
655 json_object *json_out = json_object_new_object();
656 this->createOutputInformation(crr_state, &json_out);
658 // Notify changed state
659 if (nullptr != this->callback.onStateTransitioned)
661 this->callback.onStateTransitioned(json_out);
664 // Release json_object
665 json_object_put(json_out);
670 // Destroy sd_event_source object
671 sd_event_source_unref(source);
673 // Remove event source from list
674 if (this->event_source_list.find(event_id) != this->event_source_list.end())
676 this->event_source_list.erase(event_id);
679 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH STATE TRANSITION");
683 int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data)
685 HMI_DEBUG("wm:pm", "Call");
687 int ret = this->transitionState(source, data);
691 int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t delay_ms, std::string role)
693 struct sd_event_source *event_source;
694 HMI_DEBUG("wm:pm", "event_id:0x%x delay:%d role:%s", event_id, delay_ms, role.c_str());
698 int ret = sd_event_add_defer(afb_daemon_get_event_loop(), &event_source,
699 &pm::transitionStateWrapper, new int(event_id));
702 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
709 struct timespec time_spec;
710 clock_gettime(CLOCK_BOOTTIME, &time_spec);
712 // Calculate timer fired time
713 uint64_t usec = (time_spec.tv_sec * 1000000) + (time_spec.tv_nsec / 1000) + (delay_ms * 1000);
716 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &event_source,
717 CLOCK_BOOTTIME, usec, 1,
718 &pm::timerEventWrapper, new int(event_id));
721 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
725 // Store event source
726 this->event_source_list[event_id] = event_source;
727 // Store requested role
728 this->req_role_list[event_id] = role;
732 int PolicyManager::loadRoleDb()
734 std::string file_name(get_file_path("roles.db", "wm:pm"));
737 json_object *json_obj;
738 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
741 HMI_ERROR("wm:pm", "Could not open roles.db, so use default role information");
742 json_obj = json_tokener_parse(kDefaultRoleDb);
744 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
746 json_object *json_roles;
747 if (!json_object_object_get_ex(json_obj, "roles", &json_roles))
749 HMI_ERROR("wm:pm", "Parse Error!!");
753 int len = json_object_array_length(json_roles);
754 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
755 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
757 json_object *json_tmp;
758 const char *category;
762 for (int i = 0; i < len; i++)
764 json_tmp = json_object_array_get_idx(json_roles, i);
766 category = this->getStringFromJson(json_tmp, "category");
767 roles = this->getStringFromJson(json_tmp, "role");
768 areas = this->getStringFromJson(json_tmp, "area");
769 layer = this->getStringFromJson(json_tmp, "layer");
771 if ((nullptr == category) || (nullptr == roles) ||
772 (nullptr == areas) || (nullptr == layer))
774 HMI_ERROR("wm:pm", "Parse Error!!");
778 // Parse roles by '|'
779 std::vector<std::string> vct_roles;
780 vct_roles = this->parseString(std::string(roles), '|');
782 // Parse areas by '|'
784 vct_areas = this->parseString(std::string(areas), '|');
786 // Set role, category, areas
787 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr)
789 this->role2category[*itr] = std::string(category);
791 this->category2role[category] = std::string(roles);
792 this->category2areas[category] = vct_areas;
793 this->layer2categories[layer].push_back(category);
797 HMI_DEBUG("wm:pm", "Check role2category");
798 for (const auto &x : this->role2category)
800 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
803 HMI_DEBUG("wm:pm", "Check category2role");
804 for (const auto &x : this->category2role)
806 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
809 HMI_DEBUG("wm:pm", "Check category2areas");
810 for (const auto &x : this->category2areas)
812 for (const auto &y : x.second)
814 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), y.c_str());
820 int PolicyManager::loadStateDb()
822 HMI_DEBUG("wm:pm", "Call");
824 std::string file_name(get_file_path("states.db", "wm:pm"));
827 json_object *json_obj;
828 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
831 HMI_DEBUG("wm:pm", "Could not open states.db, so use default layout information");
832 json_obj = json_tokener_parse(kDefaultStateDb);
834 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
837 HMI_DEBUG("wm:pm", "Perse states");
838 json_object *json_cfg;
839 if (!json_object_object_get_ex(json_obj, "states", &json_cfg))
841 HMI_ERROR("wm:pm", "Parse Error!!");
845 int len = json_object_array_length(json_cfg);
846 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
847 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
851 const char *category;
852 for (int i = 0; i < len; i++)
854 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
856 layout = this->getStringFromJson(json_tmp, "name");
857 if (nullptr == layout)
859 HMI_ERROR("wm:pm", "Parse Error!!");
862 HMI_DEBUG("wm:pm", "> layout:%s", layout);
864 json_object *json_area_array;
865 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array))
867 HMI_ERROR("wm:pm", "Parse Error!!");
871 int len_area = json_object_array_length(json_area_array);
872 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
873 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
875 LayoutState layout_state;
876 AreaState area_state;
877 std::map<std::string, int> category_num;
878 for (int ctg_no = StmCtgNoMin;
879 ctg_no <= StmCtgNoMax; ctg_no++)
881 const char *ctg_name = kStmCategoryName[ctg_no];
882 category_num[ctg_name] = 0;
885 for (int j = 0; j < len_area; j++)
887 json_object *json_area = json_object_array_get_idx(json_area_array, j);
890 const char *area = this->getStringFromJson(json_area, "name");
893 HMI_ERROR("wm:pm", "Parse Error!!");
896 area_state.name = std::string(area);
897 HMI_DEBUG("wm:pm", ">> area:%s", area);
899 // Get app attribute of the area
900 category = this->getStringFromJson(json_area, "category");
901 if (nullptr == category)
903 HMI_ERROR("wm:pm", "Parse Error!!");
906 area_state.category = std::string(category);
907 category_num[category]++;
908 HMI_DEBUG("wm:pm", ">>> category:%s", category);
910 role = this->getStringFromJson(json_area, "role");
913 // Role is NOT essential here
914 area_state.role = std::string(role);
918 area_state.role = std::string("");
920 HMI_DEBUG("wm:pm", ">>> role:%s", role);
922 layout_state.area_list.push_back(area_state);
925 layout_state.name = layout;
926 layout_state.category_num = category_num;
927 this->default_layouts[layout] = layout_state;
930 // initialize for none layout
931 LayoutState none_layout_state;
932 memset(&none_layout_state, 0, sizeof(none_layout_state));
933 none_layout_state.name = "none";
934 this->default_layouts["none"] = none_layout_state;
937 for (auto itr_layout = this->default_layouts.begin();
938 itr_layout != this->default_layouts.end(); ++itr_layout)
940 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
942 for (auto itr_area = itr_layout->second.area_list.begin();
943 itr_area != itr_layout->second.area_list.end(); ++itr_area)
945 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
946 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
947 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
951 // Release json_object
952 json_object_put(json_obj);
957 void PolicyManager::pushInvisibleRoleHistory(std::string category, std::string role)
959 auto i = std::remove_if(this->invisible_role_history[category].begin(),
960 this->invisible_role_history[category].end(),
961 [role](std::string x) { return (role == x); });
963 if (this->invisible_role_history[category].end() != i)
965 this->invisible_role_history[category].erase(i);
968 this->invisible_role_history[category].push_back(role);
970 if (pm::kInvisibleRoleHistoryNum < invisible_role_history[category].size())
972 this->invisible_role_history[category].erase(
973 this->invisible_role_history[category].begin());
977 std::string PolicyManager::popInvisibleRoleHistory(std::string category)
980 if (invisible_role_history[category].empty())
986 role = this->invisible_role_history[category].back();
987 this->invisible_role_history[category].pop_back();
992 const char *PolicyManager::getStringFromJson(json_object *obj, const char *key)
995 if (!json_object_object_get_ex(obj, key, &tmp))
997 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1001 return json_object_get_string(tmp);
1004 int PolicyManager::inputJsonFilie(const char *file, json_object **obj)
1006 const int input_size = 128;
1009 HMI_DEBUG("wm:pm", "Input file: %s", file);
1012 FILE *fp = fopen(file, "rb");
1015 HMI_ERROR("wm:pm", "Could not open file");
1020 struct json_tokener *tokener = json_tokener_new();
1021 enum json_tokener_error json_error;
1022 char buffer[input_size];
1026 size_t len = fread(buffer, sizeof(char), input_size, fp);
1027 *obj = json_tokener_parse_ex(tokener, buffer, len);
1028 if (nullptr != *obj)
1030 HMI_DEBUG("wm:pm", "File input is success");
1035 json_error = json_tokener_get_error(tokener);
1036 if ((json_tokener_continue != json_error) || (input_size > len))
1038 HMI_ERROR("wm:pm", "Failed to parse file (byte:%d err:%s)",
1039 (input_size * block_cnt), json_tokener_error_desc(json_error));
1040 HMI_ERROR("wm:pm", "\n%s", buffer);
1050 // Free json_tokener
1051 json_tokener_free(tokener);
1056 void PolicyManager::dumpLayerState(std::unordered_map<std::string, LayerState> &layers)
1058 HMI_DEBUG("wm:pm", "-------------------------------------------------------------------------------------------------------");
1059 HMI_DEBUG("wm:pm", "|%-15s|%s|%-20s|%-20s|%-20s|%-20s|",
1060 "LAYER", "C", "LAYOUT", "AREA", "CATEGORY", "ROLE");
1061 for (const auto &itr : layers)
1063 LayerState ls = itr.second;
1064 const char* layer = ls.name.c_str();
1065 const char* changed = (ls.changed) ? "T" : "f";
1066 const char* layout = ls.layout_state.name.c_str();
1068 for (const auto &as : ls.layout_state.area_list)
1073 HMI_DEBUG("wm:pm", "|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1074 layer, changed, layout,
1075 as.name.c_str(), as.category.c_str(), as.role.c_str());
1078 HMI_DEBUG("wm:pm", "|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1079 "", "", "", as.name.c_str(), as.category.c_str(), as.role.c_str());
1082 HMI_DEBUG("wm:pm", "-------------------------------------------------------------------------------------------------------");
1085 void PolicyManager::dumpInvisibleRoleHistory()
1087 HMI_DEBUG("wm:pm", ">>>>>>>>>> DUMP INVISIBLE ROLE HISTORY ( category [older > newer] )");
1088 for (int ctg_no = StmCtgNoMin; ctg_no <= StmCtgNoMax; ctg_no++)
1090 if (ctg_no == StmCtgNoNone)
1093 std::string category = std::string(kStmCategoryName[ctg_no]);
1095 std::string str = category + " [ ";
1096 for (const auto &i : this->invisible_role_history[category])
1100 HMI_DEBUG("wm:pm", "%s", str.c_str());
1104 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter)
1106 // Parse string by delimiter
1107 std::vector<std::string> vct;
1108 std::stringstream ss{str};
1110 while (std::getline(ss, buf, delimiter))
1114 // Delete space and push back to vector
1115 vct.push_back(this->deleteSpace(buf));
1121 std::string PolicyManager::deleteSpace(std::string str)
1123 std::string ret = str;
1125 while ((pos = ret.find_first_of(" ")) != std::string::npos)
1132 const char *PolicyManager::kDefaultRoleDb = "{ \
1135 \"category\": \"homescreen\", \
1136 \"role\": \"homescreen\", \
1137 \"area\": \"fullscreen\", \
1140 \"category\": \"map\", \
1141 \"role\": \"map\", \
1142 \"area\": \"normal.full | split.main\", \
1145 \"category\": \"general\", \
1146 \"role\": \"launcher | poi | browser | sdl | mixer | radio | hvac | debug | phone | video | music\", \
1147 \"area\": \"normal.full\", \
1150 \"category\": \"system\", \
1151 \"role\": \"settings | dashboard\", \
1152 \"area\": \"normal.full\", \
1155 \"category\": \"software_keyboard\", \
1156 \"role\": \"software_keyboard\", \
1157 \"area\": \"software_keyboard\", \
1160 \"category\": \"restriction\", \
1161 \"role\": \"restriction\", \
1162 \"area\": \"restriction.normal | restriction.split.main | restriction.split.sub\", \
1165 \"category\": \"pop_up\", \
1166 \"role\": \"pop_up\", \
1167 \"area\": \"on_screen\", \
1170 \"category\": \"system_alert\", \
1171 \"role\": \"system_alert\", \
1172 \"area\": \"on_screen\", \
1177 const char *PolicyManager::kDefaultStateDb = "{ \
1180 \"name\": \"homescreen\", \
1181 \"layer\": \"far_homescreen\", \
1184 \"name\": \"fullscreen\", \
1185 \"category\": \"homescreen\" \
1190 \"name\": \"map.normal\", \
1191 \"layer\": \"apps\", \
1194 \"name\": \"normal.full\", \
1195 \"category\": \"map\" \
1200 \"name\": \"map.split\", \
1201 \"layer\": \"apps\", \
1204 \"name\": \"split.main\", \
1205 \"category\": \"map\" \
1208 \"name\": \"split.sub\", \
1209 \"category\": \"splitable\" \
1214 \"name\": \"map.fullscreen\", \
1215 \"layer\": \"apps\", \
1218 \"name\": \"fullscreen\", \
1219 \"category\": \"map\" \
1224 \"name\": \"splitable.normal\", \
1225 \"layer\": \"apps\", \
1228 \"name\": \"normal.full\", \
1229 \"category\": \"splitable\" \
1234 \"name\": \"splitable.split\", \
1235 \"layer\": \"apps\", \
1238 \"name\": \"split.main\", \
1239 \"category\": \"splitable\" \
1242 \"name\": \"split.sub\", \
1243 \"category\": \"splitable\" \
1248 \"name\": \"general.normal\", \
1249 \"layer\": \"apps\", \
1252 \"name\": \"normal.full\", \
1253 \"category\": \"general\" \
1258 \"name\": \"system.normal\", \
1259 \"layer\": \"apps\", \
1262 \"name\": \"normal.full\", \
1263 \"category\": \"system\" \
1268 \"name\": \"software_keyboard\", \
1269 \"layer\": \"near_homescreen\", \
1272 \"name\": \"software_keyboard\", \
1273 \"category\": \"software_keyboard\" \
1278 \"name\": \"restriction.normal\", \
1279 \"layer\": \"restriction\", \
1282 \"name\": \"restriction.normal\", \
1283 \"category\": \"restriction\" \
1288 \"name\": \"restriction.split.main\", \
1289 \"layer\": \"restriction\", \
1292 \"name\": \"restriction.split.main\", \
1293 \"category\": \"restriction\" \
1298 \"name\": \"restriction.split.sub\", \
1299 \"layer\": \"restriction\", \
1302 \"name\": \"restriction.split.sub\", \
1303 \"category\": \"restriction\" \
1308 \"name\": \"pop_up\", \
1309 \"layer\": \"on_screen\", \
1312 \"name\": \"on_screen\", \
1313 \"category\": \"pop_up\" \
1318 \"name\": \"system_alert\", \
1319 \"layer\": \"on_screen\", \
1322 \"name\": \"on_screen\", \
1323 \"category\": \"system_alert\" \