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"
29 #define AFB_BINDING_VERSION 2
30 #include <afb/afb-binding.h>
31 #include <systemd/sd-event.h>
37 static const int kInvisibleRoleHistoryNum = 5;
39 static PolicyManager *g_context;
41 static int transitionStateWrapper(sd_event_source *source, void *data)
43 int ret = g_context->transitionState(source, data);
47 static int timerEventWrapper(sd_event_source *source, uint64_t usec, void *data)
49 int ret = g_context->timerEvent(source, usec, data);
55 PolicyManager::PolicyManager()
64 int PolicyManager::initialize()
69 for (int i = StmEvtNoMin; i <= StmEvtNoMax; i++)
71 HMI_DEBUG("event name:%s no:%d", kStmEventName[i], i);
72 this->eventname2no[kStmEventName[i]] = i;
75 for (int i = StmCtgNoMin; i <= StmCtgNoMax; i++)
77 HMI_DEBUG("category name:%s no:%d", kStmCategoryName[i], i);
78 this->categoryname2no[kStmCategoryName[i]] = i;
81 for (int i = StmAreaNoMin; i <= StmAreaNoMax; i++)
83 HMI_DEBUG("area name:%s no:%d", kStmAreaName[i], i);
84 this->areaname2no[kStmAreaName[i]] = i;
88 ret = this->loadRoleDb();
91 HMI_ERROR("Load roles.db Error!!");
96 ret = this->loadStateDb();
99 HMI_ERROR("Load states.db Error!!");
103 // Initialize state which is managed by PolicyManager
104 this->initializeState();
106 // Initialize StateTransitioner
110 pm::g_context = this;
115 void PolicyManager::registerCallback(CallbackTable callback)
117 this->callback.onStateTransitioned = callback.onStateTransitioned;
118 this->callback.onError = callback.onError;
121 int PolicyManager::setInputEventData(json_object *json_in)
124 if (nullptr == json_in)
126 HMI_ERROR("Argument is NULL!!");
130 // Get event from json_object
131 const char *event = this->getStringFromJson(json_in, "event");
132 int event_no = StmEvtNoNone;
133 if (nullptr != event)
135 // Convert name to number
136 auto itr = this->eventname2no.find(event);
137 if (this->eventname2no.end() != itr)
139 event_no = this->eventname2no[event];
140 HMI_DEBUG("event(%s:%d)", event, event_no);
144 HMI_ERROR("Invalid event name!!");
150 HMI_ERROR("Event is not set!!");
154 // Get role from json_object
155 const char *role = this->getStringFromJson(json_in, "role");
156 std::string category = "";
157 int category_no = StmCtgNoNone;
160 HMI_DEBUG("role(%s)", role);
162 // Convert role to category
163 auto itr = this->role2category.find(role);
164 if (this->role2category.end() != itr)
166 category = this->role2category[role];
170 itr = this->role2category.find("fallback");
171 if (this->role2category.end() != itr)
173 HMI_DEBUG("Role:%s is not registered in roles.db, fallback as normal app", role);
174 category = this->role2category["fallback"];
180 // Convert name to number
181 category_no = categoryname2no[category];
182 HMI_DEBUG("category(%s:%d)", category.c_str(), category_no);
185 if (StmCtgNoNone == category_no)
190 // Get area from json_object
191 const char *area = this->getStringFromJson(json_in, "area");
192 int area_no = StmAreaNoNone;
193 if ((nullptr != area) && (StmCtgNoNone != category_no))
195 for (const auto &x : this->category2areas[category])
197 if (x == std::string(area))
199 area_no = this->areaname2no[area];
203 if (StmAreaNoNone == area_no)
205 area = this->category2areas[category].front().c_str();
206 area_no = this->areaname2no[area];
208 HMI_DEBUG("area(%s:%d)", area, area_no);
211 // Set event info to the queue
212 EventInfo event_info;
213 int event_id = STM_CREATE_EVENT_ID(event_no, category_no, area_no);
214 event_info.event = event_id;
215 event_info.role = std::string(role);
216 event_info.delay = 0;
217 this->event_info_queue.push(event_info);
222 int PolicyManager::executeStateTransition()
225 EventInfo event_info;
227 // Get event info from queue and delete
228 event_info = this->event_info_queue.front();
229 this->event_info_queue.pop();
231 // Set event info to check policy
232 ret = this->setStateTransitionProcessToSystemd(event_info.event,
238 void PolicyManager::undoState()
240 HMI_DEBUG("Undo State !!!");
245 HMI_DEBUG(">>>>>>>>>> BEFORE UNDO");
246 this->dumpLayerState(this->crr_layers);
248 this->crr_layers = this->prv_layers;
250 HMI_DEBUG(">>>>>>>>>> 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("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("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->updateLayer(event_id, crr_state);
323 void PolicyManager::updateLayer(int event_id, StmState crr_state)
325 for (int layer_no = StmLayerNoMin;
326 layer_no <= StmLayerNoMax; layer_no++)
328 HMI_DEBUG(">>> LAYER:%s CHANGED:%d LAYOUT:%s",
329 kStmLayerName[layer_no], crr_state.layer[layer_no].changed,
330 kStmLayoutName[crr_state.layer[layer_no].state]);
333 // Store previous layers
334 this->prv_layers = this->crr_layers;
337 for (int layer_no = StmLayerNoMin;
338 layer_no <= StmLayerNoMax; layer_no++)
340 const char *layer_name = kStmLayerName[layer_no];
342 // This layer is changed?
343 int changed = crr_state.layer[layer_no].changed;
346 HMI_DEBUG(">>>>>>>>>> Update layout of layer:%s", layer_name);
348 // Get current layout name of this layer
349 int crr_layout_state_no = crr_state.layer[layer_no].state;
350 std::string crr_layout_name = std::string(kStmLayoutName[crr_layout_state_no]);
352 LayoutState crr_layout_state;
353 this->updateLayout(event_id, layer_no,
354 crr_layout_name, crr_layout_state);
356 // Update current layout of this layer
357 this->crr_layers[layer_name].layout_state = crr_layout_state;
359 // Update changed flag
360 this->crr_layers[layer_name].changed = (changed) ? true : false;
363 // Erase role for the event_id from list
364 this->req_role_list.erase(event_id);
366 HMI_DEBUG(">>>>>>>>>> DUMP LAYERS (BEFORE)");
367 this->dumpLayerState(this->prv_layers);
369 HMI_DEBUG(">>>>>>>>>> DUMP LAYERS (AFTER)");
370 this->dumpLayerState(this->crr_layers);
372 this->dumpInvisibleRoleHistory();
375 int PolicyManager::updateLayout(int event_id, int layer_no,
376 std::string crr_layout_name, LayoutState &crr_layout_state)
380 int event_no = STM_GET_EVENT_FROM_ID(event_id);
381 int category_no = STM_GET_CATEGORY_FROM_ID(event_id);
382 int area_no = STM_GET_AREA_FROM_ID(event_id);
384 std::string req_evt = kStmEventName[event_no];
385 std::string req_ctg = kStmCategoryName[category_no];
386 std::string req_area = kStmAreaName[area_no];
387 std::string req_role = this->req_role_list[event_id];
389 const char *layer_name = kStmLayerName[layer_no];
391 // Get previous layout name of this layer
392 LayoutState prv_layout_state = this->prv_layers[layer_name].layout_state;
393 std::string prv_layout_name = prv_layout_state.name;
395 if ((prv_layout_name == crr_layout_name) &&
396 (kStmLayoutName[StmLayoutNoNone] == crr_layout_name))
398 // If previous and current layout are none
399 // Copy previous layout state for current
400 crr_layout_state = prv_layout_state;
405 crr_layout_state = prv_layout_state;
408 HMI_DEBUG("-- layout name previous:%s current:%s",
409 prv_layout_name.c_str(), crr_layout_name.c_str());
410 if (prv_layout_name == crr_layout_name)
412 HMI_DEBUG("---- Previous layout is same with current");
416 // If previous layout is NOT same with current,
417 // current areas is set with default value
418 HMI_DEBUG("---- Previous layout is NOT same with current");
419 crr_layout_state.name = this->default_layouts[crr_layout_name].name;
420 crr_layout_state.category_num = this->default_layouts[crr_layout_name].category_num;
421 crr_layout_state.area_list = this->default_layouts[crr_layout_name].area_list;
424 // Create candidate list
425 std::map<std::string, AreaList> cand_list;
426 // for (int ctg_no = StmCtgNoMin;
427 // ctg_no <= StmCtgNoMax; ctg_no++)
429 for (const auto &ctg : this->layer2categories[layer_name])
431 // if (ctg_no == StmCtgNoNone)
436 // const char *ctg = kStmCategoryName[ctg_no];
437 HMI_DEBUG("-- Create candidate list for ctg:%s", ctg.c_str());
439 AreaList tmp_cand_list;
440 int candidate_num = 0;
441 int blank_num = crr_layout_state.category_num[ctg];
443 // If requested event is "activate"
444 // and there are requested category and area,
445 // update area with requested role in current layout.
446 bool request_for_this_layer = false;
447 std::string used_role = "";
448 if ((ctg == req_ctg) && ("activate" == req_evt))
450 HMI_DEBUG("---- Requested event is activate");
451 for (AreaState &as : crr_layout_state.area_list)
453 if (as.category == req_ctg)
455 request_for_this_layer = true;
457 if (as.name == req_area)
460 used_role = req_role;
462 HMI_DEBUG("------ Update current layout: area:%s category:%s role:%s",
463 as.name.c_str(), as.category.c_str(), as.role.c_str());
470 // Create candidate list for category from the previous displayed categories
471 for (AreaState area_state : prv_layout_state.area_list)
473 if ((ctg == area_state.category) &&
474 (used_role != area_state.role))
476 // If there is the category
477 // which is same with new category and not used for updating yet,
479 HMI_DEBUG("---- Push previous(category:%s role:%s) to candidate list",
480 area_state.category.c_str(), area_state.role.c_str());
481 tmp_cand_list.push_back(area_state);
486 // If NOT updated by requested area:
487 // there is not requested area in new layout,
488 // so push requested role to candidate list
489 if (request_for_this_layer && ("" == used_role))
491 HMI_DEBUG("---- Push request(area:%s category:%s role:%s) to candidate list",
492 req_area.c_str(), req_ctg.c_str(), req_role.c_str());
493 AreaState area_state;
494 area_state.name = req_area;
495 area_state.category = req_ctg;
496 area_state.role = req_role;
497 tmp_cand_list.push_back(area_state);
501 HMI_DEBUG("---- blank_num:%d candidate_num:%d", blank_num, candidate_num);
503 // Compare number of candidate/blank,
504 // And remove role in order of the oldest as necessary
505 if (candidate_num < blank_num)
507 // Refer history stack
508 // and add to the top of tmp_cand_list in order to the newest
509 while (candidate_num != blank_num)
511 AreaState area_state;
512 area_state.name = kStmAreaName[StmAreaNoNone];
513 area_state.category = ctg;
514 area_state.role = this->popInvisibleRoleHistory(ctg);
515 if ("" == area_state.role)
517 HMI_ERROR("There is no role in history stack!!");
519 tmp_cand_list.push_back(area_state);
520 HMI_DEBUG("------ Add role:%s to candidate list",
521 area_state.role.c_str());
525 else if (candidate_num > blank_num)
527 // Remove the oldest role from candidate list
528 while (candidate_num != blank_num)
530 std::string removed_role = tmp_cand_list.begin()->role;
531 HMI_DEBUG("------ Remove the oldest role:%s from candidate list",
532 removed_role.c_str());
533 tmp_cand_list.erase(tmp_cand_list.begin());
536 // Push removed data to history stack
537 this->pushInvisibleRoleHistory(ctg, removed_role);
539 // Remove from current layout
540 for (AreaState &as : crr_layout_state.area_list)
542 if (as.role == removed_role)
550 { // (candidate_num == blank_num)
554 cand_list[ctg] = tmp_cand_list;
558 HMI_DEBUG("-- Update areas by using candidate list");
559 for (AreaState &as : crr_layout_state.area_list)
561 HMI_DEBUG("---- Check area:%s category:%s role:%s",
562 as.name.c_str(), as.category.c_str(), as.role.c_str());
565 HMI_DEBUG("------ Update this area with role:%s",
566 cand_list[as.category].begin()->role.c_str());
567 as.role = cand_list[as.category].begin()->role;
568 cand_list[as.category].erase(cand_list[as.category].begin());
575 void PolicyManager::createOutputInformation(StmState crr_state, json_object **json_out)
577 json_object *json_tmp;
579 // Create layout information
584 // "changed": <bool>,
587 // "name":<const char*>,
588 // "role":<const char*>
595 json_object *json_layer = json_object_new_array();
596 const char *layer_name;
597 for (int layer_no = StmLayerNoMin;
598 layer_no <= StmLayerNoMax; layer_no++)
600 layer_name = kStmLayerName[layer_no];
601 json_tmp = json_object_new_object();
602 this->addStateToJson(layer_name,
603 this->crr_layers[layer_name].changed,
604 this->crr_layers[layer_name].layout_state.area_list,
606 json_object_array_add(json_layer, json_tmp);
608 json_object_object_add(*json_out, "layers", json_layer);
611 int PolicyManager::transitionState(sd_event_source *source, void *data)
613 HMI_DEBUG(">>>>>>>>>> START STATE TRANSITION");
615 int event_id = *((int *)data);
617 int event_no, category_no, area_no;
618 event_no = STM_GET_EVENT_FROM_ID(event_id);
619 category_no = STM_GET_CATEGORY_FROM_ID(event_id);
620 area_no = STM_GET_AREA_FROM_ID(event_id);
621 HMI_DEBUG(">>>>>>>>>> EVENT:%s CATEGORY:%s AREA:%s",
622 kStmEventName[event_no],
623 kStmCategoryName[category_no],
624 kStmAreaName[area_no]);
628 int ret = stmTransitionState(event_id, &crr_state);
631 HMI_ERROR("Failed transition state");
632 if (nullptr != this->callback.onError)
634 json_object *json_out = json_object_new_object();
635 json_object_object_add(json_out, "message",
636 json_object_new_string("Failed to transition state"));
637 json_object_object_add(json_out, "event",
638 json_object_new_string(kStmEventName[event_no]));
639 json_object_object_add(json_out, "role",
640 json_object_new_string(this->req_role_list[event_id].c_str()));
641 json_object_object_add(json_out, "area",
642 json_object_new_string(kStmAreaName[area_no]));
643 this->callback.onError(json_out);
644 json_object_put(json_out);
649 // Update state which is managed by PolicyManager
650 this->updateState(event_id, crr_state);
652 // Create output information for ResourceManager
653 json_object *json_out = json_object_new_object();
654 this->createOutputInformation(crr_state, &json_out);
656 // Notify changed state
657 if (nullptr != this->callback.onStateTransitioned)
659 this->callback.onStateTransitioned(json_out);
662 // Release json_object
663 json_object_put(json_out);
668 // Destroy sd_event_source object
669 sd_event_source_unref(source);
671 // Remove event source from list
672 if (this->event_source_list.find(event_id) != this->event_source_list.end())
674 this->event_source_list.erase(event_id);
677 HMI_DEBUG(">>>>>>>>>> FINISH STATE TRANSITION");
681 int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data)
685 int ret = this->transitionState(source, data);
689 int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t delay_ms, std::string role)
691 HMI_DEBUG("event_id:0x%x delay:%d role:%s", event_id, delay_ms, role.c_str());
693 // Store requested role
694 this->req_role_list[event_id] = role;
698 int ret = sd_event_add_defer(afb_daemon_get_event_loop(), NULL,
699 &pm::transitionStateWrapper, new int(event_id));
702 HMI_ERROR("Faild to sd_event_add_defer: errno:%d", ret);
703 this->req_role_list.erase(event_id);
710 struct timespec time_spec;
711 clock_gettime(CLOCK_MONOTONIC, &time_spec);
713 // Calculate timer fired time
714 uint64_t usec = (time_spec.tv_sec * 1000000) + (time_spec.tv_nsec / 1000) + (delay_ms * 1000);
717 struct sd_event_source *event_source;
718 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &event_source,
719 CLOCK_MONOTONIC, usec, 1,
720 &pm::timerEventWrapper, new int(event_id));
723 HMI_ERROR("Faild to sd_event_add_time: errno:%d", ret);
724 this->req_role_list.erase(event_id);
728 // Store event source
729 this->event_source_list[event_id] = event_source;
735 int PolicyManager::loadRoleDb()
737 std::string file_name;
739 // Get afm application installed dir
740 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
741 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
743 if (!afm_app_install_dir)
745 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
749 file_name = std::string(afm_app_install_dir) + std::string("/etc/roles.db");
753 json_object *json_obj;
754 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
757 HMI_ERROR("Could not open roles.db, so use default role information");
758 json_obj = json_tokener_parse(kDefaultRoleDb);
760 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
762 json_object *json_roles;
763 if (!json_object_object_get_ex(json_obj, "roles", &json_roles))
765 HMI_ERROR("Parse Error!!");
769 int len = json_object_array_length(json_roles);
770 HMI_DEBUG("json_cfg len:%d", len);
771 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_roles));
773 json_object *json_tmp;
774 const char *category;
778 for (int i = 0; i < len; i++)
780 json_tmp = json_object_array_get_idx(json_roles, i);
782 category = this->getStringFromJson(json_tmp, "category");
783 roles = this->getStringFromJson(json_tmp, "role");
784 areas = this->getStringFromJson(json_tmp, "area");
785 layer = this->getStringFromJson(json_tmp, "layer");
787 if ((nullptr == category) || (nullptr == roles) ||
788 (nullptr == areas) || (nullptr == layer))
790 HMI_ERROR("Parse Error!!");
794 // Parse roles by '|'
795 std::vector<std::string> vct_roles;
796 vct_roles = this->parseString(std::string(roles), '|');
798 // Parse areas by '|'
800 vct_areas = this->parseString(std::string(areas), '|');
802 // Set role, category, areas
803 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr)
805 this->role2category[*itr] = std::string(category);
807 this->category2role[category] = std::string(roles);
808 this->category2areas[category] = vct_areas;
809 this->layer2categories[layer].push_back(category);
813 HMI_DEBUG("Check role2category");
814 for (const auto &x : this->role2category)
816 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), x.second.c_str());
819 HMI_DEBUG("Check category2role");
820 for (const auto &x : this->category2role)
822 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), x.second.c_str());
825 HMI_DEBUG("Check category2areas");
826 for (const auto &x : this->category2areas)
828 for (const auto &y : x.second)
830 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), y.c_str());
836 int PolicyManager::loadStateDb()
840 // Get afm application installed dir
841 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
842 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
844 std::string file_name;
845 if (!afm_app_install_dir)
847 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
851 file_name = std::string(afm_app_install_dir) + std::string("/etc/states.db");
855 json_object *json_obj;
856 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
859 HMI_DEBUG("Could not open states.db, so use default layout information");
860 json_obj = json_tokener_parse(kDefaultStateDb);
862 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
865 HMI_DEBUG("Perse states");
866 json_object *json_cfg;
867 if (!json_object_object_get_ex(json_obj, "states", &json_cfg))
869 HMI_ERROR("Parse Error!!");
873 int len = json_object_array_length(json_cfg);
874 HMI_DEBUG("json_cfg len:%d", len);
875 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_cfg));
879 const char *category;
880 for (int i = 0; i < len; i++)
882 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
884 layout = this->getStringFromJson(json_tmp, "name");
885 if (nullptr == layout)
887 HMI_ERROR("Parse Error!!");
890 HMI_DEBUG("> layout:%s", layout);
892 json_object *json_area_array;
893 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array))
895 HMI_ERROR("Parse Error!!");
899 int len_area = json_object_array_length(json_area_array);
900 HMI_DEBUG("json_area_array len:%d", len_area);
901 HMI_DEBUG("json_area_array dump:%s", json_object_get_string(json_area_array));
903 LayoutState layout_state;
904 AreaState area_state;
905 std::map<std::string, int> category_num;
906 for (int ctg_no = StmCtgNoMin;
907 ctg_no <= StmCtgNoMax; ctg_no++)
909 const char *ctg_name = kStmCategoryName[ctg_no];
910 category_num[ctg_name] = 0;
913 for (int j = 0; j < len_area; j++)
915 json_object *json_area = json_object_array_get_idx(json_area_array, j);
918 const char *area = this->getStringFromJson(json_area, "name");
921 HMI_ERROR("Parse Error!!");
924 area_state.name = std::string(area);
925 HMI_DEBUG(">> area:%s", area);
927 // Get app attribute of the area
928 category = this->getStringFromJson(json_area, "category");
929 if (nullptr == category)
931 HMI_ERROR("Parse Error!!");
934 area_state.category = std::string(category);
935 category_num[category]++;
936 HMI_DEBUG(">>> category:%s", category);
938 role = this->getStringFromJson(json_area, "role");
941 // Role is NOT essential here
942 area_state.role = std::string(role);
946 area_state.role = std::string("");
948 HMI_DEBUG(">>> role:%s", role);
950 layout_state.area_list.push_back(area_state);
953 layout_state.name = layout;
954 layout_state.category_num = category_num;
955 this->default_layouts[layout] = layout_state;
958 // initialize for none layout
959 LayoutState none_layout_state;
960 memset(&none_layout_state, 0, sizeof(none_layout_state));
961 none_layout_state.name = "none";
962 this->default_layouts["none"] = none_layout_state;
965 for (auto itr_layout = this->default_layouts.begin();
966 itr_layout != this->default_layouts.end(); ++itr_layout)
968 HMI_DEBUG(">>> layout:%s", itr_layout->first.c_str());
970 for (auto itr_area = itr_layout->second.area_list.begin();
971 itr_area != itr_layout->second.area_list.end(); ++itr_area)
973 HMI_DEBUG(">>> >>> area :%s", itr_area->name.c_str());
974 HMI_DEBUG(">>> >>> category:%s", itr_area->category.c_str());
975 HMI_DEBUG(">>> >>> role :%s", itr_area->role.c_str());
979 // Release json_object
980 json_object_put(json_obj);
985 void PolicyManager::pushInvisibleRoleHistory(std::string category, std::string role)
987 auto i = std::remove_if(this->invisible_role_history[category].begin(),
988 this->invisible_role_history[category].end(),
989 [role](std::string x) { return (role == x); });
991 if (this->invisible_role_history[category].end() != i)
993 this->invisible_role_history[category].erase(i);
996 this->invisible_role_history[category].push_back(role);
998 if (pm::kInvisibleRoleHistoryNum < invisible_role_history[category].size())
1000 this->invisible_role_history[category].erase(
1001 this->invisible_role_history[category].begin());
1005 std::string PolicyManager::popInvisibleRoleHistory(std::string category)
1008 if (invisible_role_history[category].empty())
1014 role = this->invisible_role_history[category].back();
1015 this->invisible_role_history[category].pop_back();
1020 const char *PolicyManager::getStringFromJson(json_object *obj, const char *key)
1023 if (!json_object_object_get_ex(obj, key, &tmp))
1025 HMI_DEBUG("Not found key \"%s\"", key);
1029 return json_object_get_string(tmp);
1032 int PolicyManager::inputJsonFilie(const char *file, json_object **obj)
1034 const int input_size = 128;
1037 HMI_DEBUG("Input file: %s", file);
1040 FILE *fp = fopen(file, "rb");
1043 HMI_ERROR("Could not open file");
1048 struct json_tokener *tokener = json_tokener_new();
1049 enum json_tokener_error json_error;
1050 char buffer[input_size];
1054 size_t len = fread(buffer, sizeof(char), input_size, fp);
1055 *obj = json_tokener_parse_ex(tokener, buffer, len);
1056 if (nullptr != *obj)
1058 HMI_DEBUG("File input is success");
1063 json_error = json_tokener_get_error(tokener);
1064 if ((json_tokener_continue != json_error) || (input_size > len))
1066 HMI_ERROR("Failed to parse file (byte:%d err:%s)",
1067 (input_size * block_cnt), json_tokener_error_desc(json_error));
1068 HMI_ERROR("\n%s", buffer);
1078 // Free json_tokener
1079 json_tokener_free(tokener);
1084 void PolicyManager::dumpLayerState(std::unordered_map<std::string, LayerState> &layers)
1086 HMI_DEBUG("-------------------------------------------------------------------------------------------------------");
1087 HMI_DEBUG("|%-15s|%s|%-20s|%-20s|%-20s|%-20s|",
1088 "LAYER", "C", "LAYOUT", "AREA", "CATEGORY", "ROLE");
1089 for (const auto &itr : layers)
1091 LayerState ls = itr.second;
1092 const char* layer = ls.name.c_str();
1093 const char* changed = (ls.changed) ? "T" : "f";
1094 const char* layout = ls.layout_state.name.c_str();
1096 for (const auto &as : ls.layout_state.area_list)
1101 HMI_DEBUG("|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1102 layer, changed, layout,
1103 as.name.c_str(), as.category.c_str(), as.role.c_str());
1106 HMI_DEBUG("|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1107 "", "", "", as.name.c_str(), as.category.c_str(), as.role.c_str());
1110 HMI_DEBUG("-------------------------------------------------------------------------------------------------------");
1113 void PolicyManager::dumpInvisibleRoleHistory()
1115 HMI_DEBUG(">>>>>>>>>> DUMP INVISIBLE ROLE HISTORY ( category [older > newer] )");
1116 for (int ctg_no = StmCtgNoMin; ctg_no <= StmCtgNoMax; ctg_no++)
1118 if (ctg_no == StmCtgNoNone)
1121 std::string category = std::string(kStmCategoryName[ctg_no]);
1123 std::string str = category + " [ ";
1124 for (const auto &i : this->invisible_role_history[category])
1128 HMI_DEBUG("%s", str.c_str());
1132 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter)
1134 // Parse string by delimiter
1135 std::vector<std::string> vct;
1136 std::stringstream ss{str};
1138 while (std::getline(ss, buf, delimiter))
1142 // Delete space and push back to vector
1143 vct.push_back(this->deleteSpace(buf));
1149 std::string PolicyManager::deleteSpace(std::string str)
1151 std::string ret = str;
1153 while ((pos = ret.find_first_of(" ")) != std::string::npos)
1160 const char *PolicyManager::kDefaultRoleDb = "{ \
1163 \"category\": \"homescreen\", \
1164 \"role\": \"homescreen\", \
1165 \"area\": \"fullscreen\", \
1168 \"category\": \"map\", \
1169 \"role\": \"map\", \
1170 \"area\": \"normal.full | split.main\", \
1173 \"category\": \"general\", \
1174 \"role\": \"launcher | poi | browser | sdl | mixer | radio | hvac | debug | phone | video | music\", \
1175 \"area\": \"normal.full\", \
1178 \"category\": \"system\", \
1179 \"role\": \"settings | dashboard\", \
1180 \"area\": \"normal.full\", \
1183 \"category\": \"software_keyboard\", \
1184 \"role\": \"software_keyboard\", \
1185 \"area\": \"software_keyboard\", \
1188 \"category\": \"restriction\", \
1189 \"role\": \"restriction\", \
1190 \"area\": \"restriction.normal | restriction.split.main | restriction.split.sub\", \
1193 \"category\": \"pop_up\", \
1194 \"role\": \"pop_up\", \
1195 \"area\": \"on_screen\", \
1198 \"category\": \"system_alert\", \
1199 \"role\": \"system_alert\", \
1200 \"area\": \"on_screen\", \
1205 const char *PolicyManager::kDefaultStateDb = "{ \
1208 \"name\": \"homescreen\", \
1209 \"layer\": \"far_homescreen\", \
1212 \"name\": \"fullscreen\", \
1213 \"category\": \"homescreen\" \
1218 \"name\": \"map.normal\", \
1219 \"layer\": \"apps\", \
1222 \"name\": \"normal.full\", \
1223 \"category\": \"map\" \
1228 \"name\": \"map.split\", \
1229 \"layer\": \"apps\", \
1232 \"name\": \"split.main\", \
1233 \"category\": \"map\" \
1236 \"name\": \"split.sub\", \
1237 \"category\": \"splitable\" \
1242 \"name\": \"map.fullscreen\", \
1243 \"layer\": \"apps\", \
1246 \"name\": \"fullscreen\", \
1247 \"category\": \"map\" \
1252 \"name\": \"splitable.normal\", \
1253 \"layer\": \"apps\", \
1256 \"name\": \"normal.full\", \
1257 \"category\": \"splitable\" \
1262 \"name\": \"splitable.split\", \
1263 \"layer\": \"apps\", \
1266 \"name\": \"split.main\", \
1267 \"category\": \"splitable\" \
1270 \"name\": \"split.sub\", \
1271 \"category\": \"splitable\" \
1276 \"name\": \"general.normal\", \
1277 \"layer\": \"apps\", \
1280 \"name\": \"normal.full\", \
1281 \"category\": \"general\" \
1286 \"name\": \"system.normal\", \
1287 \"layer\": \"apps\", \
1290 \"name\": \"normal.full\", \
1291 \"category\": \"system\" \
1296 \"name\": \"software_keyboard\", \
1297 \"layer\": \"near_homescreen\", \
1300 \"name\": \"software_keyboard\", \
1301 \"category\": \"software_keyboard\" \
1306 \"name\": \"restriction.normal\", \
1307 \"layer\": \"restriction\", \
1310 \"name\": \"restriction.normal\", \
1311 \"category\": \"restriction\" \
1316 \"name\": \"restriction.split.main\", \
1317 \"layer\": \"restriction\", \
1320 \"name\": \"restriction.split.main\", \
1321 \"category\": \"restriction\" \
1326 \"name\": \"restriction.split.sub\", \
1327 \"layer\": \"restriction\", \
1330 \"name\": \"restriction.split.sub\", \
1331 \"category\": \"restriction\" \
1336 \"name\": \"pop_up\", \
1337 \"layer\": \"on_screen\", \
1340 \"name\": \"on_screen\", \
1341 \"category\": \"pop_up\" \
1346 \"name\": \"system_alert\", \
1347 \"layer\": \"on_screen\", \
1350 \"name\": \"on_screen\", \
1351 \"category\": \"system_alert\" \