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 char kPathRolesConfigFile[] = "/etc/roles.json";
38 static const char kPathLayoutsConfigFile[] = "/etc/layouts.json";
40 static const int kInvisibleRoleHistoryNum = 5;
42 static PolicyManager *g_context;
44 static int transitionStateWrapper(sd_event_source *source, void *data)
46 int ret = g_context->transitionState(source, data);
50 static int timerEventWrapper(sd_event_source *source, uint64_t usec, void *data)
52 int ret = g_context->timerEvent(source, usec, data);
58 PolicyManager::PolicyManager()
66 this->p_crr_state = new (StmState);
67 this->p_prv_state = new (StmState);
70 PolicyManager::~PolicyManager()
72 delete this->p_crr_state;
73 delete this->p_prv_state;
76 int PolicyManager::initialize(std::string ecu_name)
81 this->ecu_name = ecu_name;
84 for (int i = StmEvtNoMin; i <= StmEvtNoMax; i++)
86 HMI_DEBUG("event name:%s no:%d", kStmEventName[i], i);
87 this->eventname2no[kStmEventName[i]] = i;
90 for (int i = StmCtgNoMin; i <= StmCtgNoMax; i++)
92 HMI_DEBUG("category name:%s no:%d", kStmCategoryName[i], i);
93 this->categoryname2no[kStmCategoryName[i]] = i;
96 for (int i = StmAreaNoMin; i <= StmAreaNoMax; i++)
98 HMI_DEBUG("area name:%s no:%d", kStmAreaName[i], i);
99 this->areaname2no[kStmAreaName[i]] = i;
103 ret = this->loadRolesConfigFile();
106 HMI_ERROR("Load roles config file Error!!");
110 // Load layouts config
111 ret = this->loadLayoutsConfigFile();
114 HMI_ERROR("Load layouts config file Error!!");
118 // Initialize state which is managed by PolicyManager
119 this->initializeState();
121 // Initialize StateTransitioner
122 stmInitialize(ecu_name.c_str());
125 pm::g_context = this;
130 void PolicyManager::registerCallback(CallbackTable callback)
132 this->callback.onStateTransitioned = callback.onStateTransitioned;
133 this->callback.onError = callback.onError;
136 int PolicyManager::setInputEventData(json_object *json_in)
139 if (nullptr == json_in)
141 HMI_ERROR("Argument is NULL!!");
145 // Get event from json_object
146 const char *event = this->getStringFromJson(json_in, "event");
147 int event_no = StmEvtNoNone;
148 if (nullptr != event)
150 // Convert name to number
151 auto itr = this->eventname2no.find(event);
152 if (this->eventname2no.end() != itr)
154 event_no = this->eventname2no[event];
155 HMI_DEBUG("event(%s:%d)", event, event_no);
159 HMI_ERROR("Invalid event name!!");
165 HMI_ERROR("Event is not set!!");
169 // Get role from json_object
170 const char *role = this->getStringFromJson(json_in, "role");
171 std::string category = "";
172 int category_no = StmCtgNoNone;
175 HMI_DEBUG("role(%s)", role);
177 // Convert role to category
178 auto itr = this->role2category.find(role);
179 if (this->role2category.end() != itr)
181 category = this->role2category[role];
185 itr = this->role2category.find("fallback");
186 if (this->role2category.end() != itr)
188 HMI_DEBUG("Role:%s is not registered in roles config file, fallback as normal app", role);
189 category = this->role2category["fallback"];
195 // Convert name to number
196 category_no = categoryname2no[category];
197 HMI_DEBUG("category(%s:%d)", category.c_str(), category_no);
200 if (StmCtgNoNone == category_no)
205 // Get area from json_object
206 const char *area = this->getStringFromJson(json_in, "area");
207 int area_no = StmAreaNoNone;
208 if ((nullptr != area) && (StmCtgNoNone != category_no))
210 for (const auto &x : this->category2areas[category])
212 if (x == std::string(area))
214 area_no = this->areaname2no[area];
218 if (StmAreaNoNone == area_no)
220 area = this->category2areas[category].front().c_str();
221 area_no = this->areaname2no[area];
223 HMI_DEBUG("area(%s:%d)", area, area_no);
226 // Set event info to the queue
227 EventInfo event_info;
228 int event_id = STM_CREATE_EVENT_ID(event_no, category_no, area_no);
229 event_info.event = event_id;
230 event_info.role = std::string(role);
231 event_info.delay = 0;
232 this->event_info_queue.push(event_info);
237 int PolicyManager::executeStateTransition()
240 EventInfo event_info;
242 // Get event info from queue and delete
243 event_info = this->event_info_queue.front();
244 this->event_info_queue.pop();
246 // Set event info to check policy
247 ret = this->setStateTransitionProcessToSystemd(event_info.event,
253 void PolicyManager::undoState()
255 HMI_DEBUG("Undo State !!!");
260 HMI_DEBUG(">>>>>>>>>> BEFORE UNDO");
261 this->dumpLayerState(this->crr_layers);
263 this->crr_layers = this->prv_layers;
264 this->crr_invisible_role_history = this->prv_invisible_role_history;
266 HMI_DEBUG(">>>>>>>>>> AFTER UNDO");
267 this->dumpLayerState(this->crr_layers);
270 void PolicyManager::initializeState()
272 this->initializeModeState();
273 this->initializeLayerState();
276 void PolicyManager::initializeModeState()
279 init_car_ele.state = "none";
280 init_car_ele.changed = false;
282 for (int i = StmCarElementNoMin; i <= StmCarElementNoMax; i++)
284 const char *car_ele_name = kStmCarElementName[i];
285 this->crr_car_elements[car_ele_name] = init_car_ele;
288 this->prv_car_elements = this->crr_car_elements;
291 void PolicyManager::initializeLayerState()
294 LayoutState init_layout;
295 init_area.name = kStmAreaName[StmAreaNoNone];
296 init_area.category = kStmCategoryName[StmCtgNoNone];
298 init_layout.name = kStmLayoutName[StmLayoutNoNone];
299 init_layout.area_list.push_back(init_area);
301 for (int i = StmLayerNoMin; i <= StmLayerNoMax; i++)
303 const char *layer_name = kStmLayerName[i];
304 this->crr_layers[layer_name].name = layer_name;
305 this->crr_layers[layer_name].layout_state = init_layout;
306 this->crr_layers[layer_name].changed = false;
309 this->prv_layers = this->crr_layers;
312 void PolicyManager::addStateToJson(const char *name, bool changed,
313 std::string state, json_object **json_out)
315 if ((nullptr == name) || (nullptr == json_out))
317 HMI_ERROR("Invalid argument!!!");
321 json_object_object_add(*json_out, "name", json_object_new_string(name));
322 json_object_object_add(*json_out, "state", json_object_new_string(state.c_str()));
323 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
326 void PolicyManager::addStateToJson(const char *layer_name, bool changed,
327 AreaList area_list, json_object **json_out)
329 if ((nullptr == layer_name) || (nullptr == json_out))
331 HMI_ERROR("Invalid argument!!!");
335 json_object *json_areas = json_object_new_array();
336 json_object *json_tmp;
337 for (const auto &as : area_list)
339 json_tmp = json_object_new_object();
340 json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
341 json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
342 json_object_array_add(json_areas, json_tmp);
345 json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
346 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
347 json_object_object_add(*json_out, "areas", json_areas);
350 void PolicyManager::updateState(int event_id)
352 this->updateModeState();
353 this->updateLayer(event_id);
356 void PolicyManager::updateModeState()
359 std::string car_state;
362 // Store previous layers
363 this->prv_car_elements = this->crr_car_elements;
365 // Update car elements
366 HMI_DEBUG(">>> CAR ELEMENTS");
367 for (int car_ele_no = StmCarElementNoMin;
368 car_ele_no <= StmCarElementNoMax; car_ele_no++)
370 const char *car_ele_name = kStmCarElementName[car_ele_no];
372 car_state_no = this->p_crr_state->car_element[car_ele_no].state;
373 car_state = kStmCarElementStateNameList[car_ele_no][car_state_no];
374 changed = (this->p_crr_state->car_element[car_ele_no].changed) ? true : false;
376 this->crr_car_elements[car_ele_name].state = car_state;
377 this->crr_car_elements[car_ele_name].changed = changed;
379 HMI_DEBUG(">>> >>> NAME: %s", car_ele_name);
380 HMI_DEBUG(">>> >>> >>> STATE:%s", car_state.c_str());
381 HMI_DEBUG(">>> >>> >>> CHANGED:%s", (changed) ? "true" : "false");
385 void PolicyManager::updateLayer(int event_id)
387 for (int layer_no = StmLayerNoMin;
388 layer_no <= StmLayerNoMax; layer_no++)
390 HMI_DEBUG(">>> LAYER:%s CHANGED:%d LAYOUT:%s",
391 kStmLayerName[layer_no], this->p_crr_state->layer[layer_no].changed,
392 kStmLayoutName[this->p_crr_state->layer[layer_no].state]);
395 // Store previous layers
396 this->prv_layers = this->crr_layers;
398 // Store previous role history
399 this->prv_invisible_role_history = this->crr_invisible_role_history;
402 for (int layer_no = StmLayerNoMin;
403 layer_no <= StmLayerNoMax; layer_no++)
405 const char *layer_name = kStmLayerName[layer_no];
407 // If restriction mode is changed to mode2 on,
408 // store current state for state of restriction mode off
409 if (this->changedRestrictionModeTo2On() ||
410 this->changedLightstatusBrakeOnToOff())
412 HMI_DEBUG("Store current state for state of restriction mode off");
413 this->prv_layers_car_stop[layer_name] = this->crr_layers[layer_name];
416 // This layer is changed?
417 int changed = this->p_crr_state->layer[layer_no].changed;
420 HMI_DEBUG(">>>>>>>>>> Update layout of layer:%s", layer_name);
422 // Get current layout name of this layer
423 int crr_layout_state_no = this->p_crr_state->layer[layer_no].state;
424 std::string crr_layout_name = std::string(kStmLayoutName[crr_layout_state_no]);
426 LayoutState crr_layout_state;
427 changed = this->updateLayout(event_id, layer_no,
428 crr_layout_name, crr_layout_state);
430 // Update current layout of this layer
431 this->crr_layers[layer_name].layout_state = crr_layout_state;
435 int category_no = STM_GET_CATEGORY_FROM_ID(event_id);
436 std::string req_ctg = kStmCategoryName[category_no];
437 std::string req_role = this->req_role_list[event_id];
438 for (const auto &ctg : this->layer2categories[layer_name])
442 // If layer is not changed and requested role is in this layer,
443 // push requested role to history stack
444 // because the application which has this role have been started
445 HMI_DEBUG("Add requested role to history "
446 "because the application which has this role have been started");
447 this->pushInvisibleRoleHistory(req_ctg, req_role);
452 // Update changed flag
453 this->crr_layers[layer_name].changed = (changed) ? true : false;
456 // Erase role for the event_id from list
457 this->req_role_list.erase(event_id);
459 HMI_DEBUG(">>>>>>>>>> DUMP LAYERS (BEFORE)");
460 this->dumpLayerState(this->prv_layers);
462 HMI_DEBUG(">>>>>>>>>> DUMP LAYERS (AFTER)");
463 this->dumpLayerState(this->crr_layers);
465 this->dumpInvisibleRoleHistory();
468 int PolicyManager::updateLayout(int event_id, int layer_no,
469 std::string crr_layout_name, LayoutState &crr_layout_state)
473 int event_no = STM_GET_EVENT_FROM_ID(event_id);
474 int category_no = STM_GET_CATEGORY_FROM_ID(event_id);
475 int area_no = STM_GET_AREA_FROM_ID(event_id);
477 std::string req_evt = kStmEventName[event_no];
478 std::string req_ctg = kStmCategoryName[category_no];
479 std::string req_area = kStmAreaName[area_no];
480 std::string req_role = this->req_role_list[event_id];
482 const char *layer_name = kStmLayerName[layer_no];
484 // Get previous layout name of this layer
485 LayoutState prv_layout_state = this->prv_layers[layer_name].layout_state;
486 std::string prv_layout_name = prv_layout_state.name;
488 if (this->changedRestrictionMode2OnToOther() ||
489 this->changedLightstatusBrakeOffToOn())
491 // If restriction mode is changed from mode2 -> mode1,
492 // restore state of restriction mode off
493 HMI_DEBUG("Restriction mode is changed from mode2 -> mode1, so restore state of restriction mode off");
494 crr_layout_state = this->prv_layers_car_stop[layer_name].layout_state;
495 crr_layout_name = crr_layout_state.name;
496 if ((prv_layout_name == crr_layout_name) &&
497 (kStmAreaName[StmAreaNoNone] == crr_layout_name))
503 // If the roles which is exist in previous layout is not in current,
504 // push to role history
505 for (const auto &prv_as : prv_layout_state.area_list)
507 for (const auto &crr_as : crr_layout_state.area_list)
509 if (prv_as.role == crr_as.role)
513 this->pushInvisibleRoleHistory(prv_as.category, prv_as.role);
517 else if ((prv_layout_name == crr_layout_name) &&
518 (kStmLayoutName[StmLayoutNoNone] == crr_layout_name))
520 // If previous and current layout are none
521 // Copy previous layout state for current
522 crr_layout_state = prv_layout_state;
527 crr_layout_state = prv_layout_state;
530 HMI_DEBUG("-- layout name previous:%s current:%s",
531 prv_layout_name.c_str(), crr_layout_name.c_str());
532 if (prv_layout_name == crr_layout_name)
534 HMI_DEBUG("---- Previous layout is same with current");
538 // If previous layout is NOT same with current,
539 // current areas is set with default value
540 HMI_DEBUG("---- Previous layout is NOT same with current");
541 crr_layout_state.name = this->default_layouts[crr_layout_name].name;
542 crr_layout_state.category_num = this->default_layouts[crr_layout_name].category_num;
543 crr_layout_state.area_list = this->default_layouts[crr_layout_name].area_list;
546 // Create candidate list
547 std::map<std::string, AreaList> cand_list;
548 // for (int ctg_no = StmCtgNoMin;
549 // ctg_no <= StmCtgNoMax; ctg_no++)
551 for (const auto &ctg : this->layer2categories[layer_name])
553 // if (ctg_no == StmCtgNoNone)
558 // const char *ctg = kStmCategoryName[ctg_no];
559 HMI_DEBUG("-- Create candidate list for ctg:%s", ctg.c_str());
561 AreaList tmp_cand_list;
562 int candidate_num = 0;
563 int blank_num = crr_layout_state.category_num[ctg];
565 // If requested event is "activate"
566 // and there are requested category and area,
567 // update area with requested role in current layout.
568 bool request_for_this_layer = false;
569 std::string used_role = "";
570 if ((ctg == req_ctg) && ("activate" == req_evt))
572 HMI_DEBUG("---- Requested event is activate");
573 for (AreaState &as : crr_layout_state.area_list)
575 if (as.category == req_ctg)
577 request_for_this_layer = true;
579 if (as.name == req_area)
582 used_role = req_role;
584 HMI_DEBUG("------ Update current layout: area:%s category:%s role:%s",
585 as.name.c_str(), as.category.c_str(), as.role.c_str());
592 // Create candidate list for category from the previous displayed categories
593 for (AreaState area_state : prv_layout_state.area_list)
595 if ((ctg == area_state.category) &&
596 (used_role != area_state.role))
598 // If there is the category
599 // which is same with new category and not used for updating yet,
601 HMI_DEBUG("---- Push previous(category:%s role:%s) to candidate list",
602 area_state.category.c_str(), area_state.role.c_str());
603 tmp_cand_list.push_back(area_state);
608 // If NOT updated by requested area:
609 // there is not requested area in new layout,
610 // so push requested role to candidate list
611 if (request_for_this_layer && ("" == used_role))
613 HMI_DEBUG("---- Push request(area:%s category:%s role:%s) to candidate list",
614 req_area.c_str(), req_ctg.c_str(), req_role.c_str());
615 AreaState area_state;
616 area_state.name = req_area;
617 area_state.category = req_ctg;
618 area_state.role = req_role;
619 tmp_cand_list.push_back(area_state);
623 HMI_DEBUG("---- blank_num:%d candidate_num:%d", blank_num, candidate_num);
625 // Compare number of candidate/blank,
626 // And remove role in order of the oldest as necessary
627 if (candidate_num < blank_num)
629 // Refer history stack
630 // and add to the top of tmp_cand_list in order to the newest
631 while (candidate_num != blank_num)
633 AreaState area_state;
634 area_state.name = kStmAreaName[StmAreaNoNone];
635 area_state.category = ctg;
636 area_state.role = this->popInvisibleRoleHistory(ctg);
637 if ("" == area_state.role)
639 HMI_ERROR("There is no role in history stack!!");
641 tmp_cand_list.push_back(area_state);
642 HMI_DEBUG("------ Add role:%s to candidate list",
643 area_state.role.c_str());
647 else if (candidate_num > blank_num)
649 // Remove the oldest role from candidate list
650 while (candidate_num != blank_num)
652 std::string removed_role = tmp_cand_list.begin()->role;
653 HMI_DEBUG("------ Remove the oldest role:%s from candidate list",
654 removed_role.c_str());
655 tmp_cand_list.erase(tmp_cand_list.begin());
658 // Push removed data to history stack
659 this->pushInvisibleRoleHistory(ctg, removed_role);
661 // Remove from current layout
662 for (AreaState &as : crr_layout_state.area_list)
664 if (as.role == removed_role)
672 { // (candidate_num == blank_num)
676 cand_list[ctg] = tmp_cand_list;
680 HMI_DEBUG("-- Update areas by using candidate list");
681 for (AreaState &as : crr_layout_state.area_list)
683 HMI_DEBUG("---- Check area:%s category:%s role:%s",
684 as.name.c_str(), as.category.c_str(), as.role.c_str());
687 HMI_DEBUG("------ Update this area with role:%s",
688 cand_list[as.category].begin()->role.c_str());
689 as.role = cand_list[as.category].begin()->role;
690 cand_list[as.category].erase(cand_list[as.category].begin());
697 void PolicyManager::createOutputInformation(json_object **json_out)
699 json_object *json_tmp;
701 // Create car element information
705 // "parking_brake": {
706 // "changed": <bool>,
707 // "state": <const char*>
711 json_object *json_car_ele = json_object_new_array();
712 const char *car_ele_name;
713 for (int car_ele_no = StmCarElementNoMin;
714 car_ele_no <= StmCarElementNoMax; car_ele_no++)
716 car_ele_name = kStmCarElementName[car_ele_no];
717 json_tmp = json_object_new_object();
718 this->addStateToJson(car_ele_name,
719 this->crr_car_elements[car_ele_name].changed,
720 this->crr_car_elements[car_ele_name].state,
722 json_object_array_add(json_car_ele, json_tmp);
724 json_object_object_add(*json_out, "car_elements", json_car_ele);
726 // Create layout information
731 // "changed": <bool>,
734 // "name":<const char*>,
735 // "role":<const char*>
742 json_object *json_layer = json_object_new_array();
743 const char *layer_name;
744 for (int layer_no = StmLayerNoMin;
745 layer_no <= StmLayerNoMax; layer_no++)
747 layer_name = kStmLayerName[layer_no];
748 json_tmp = json_object_new_object();
749 this->addStateToJson(layer_name,
750 this->crr_layers[layer_name].changed,
751 this->crr_layers[layer_name].layout_state.area_list,
753 json_object_array_add(json_layer, json_tmp);
755 json_object_object_add(*json_out, "layers", json_layer);
758 void PolicyManager::controlTimerEvent()
760 if (this->p_crr_state->car_element[StmCarElementNoRunning].changed)
762 if (StmRunningNoRun == this->p_crr_state->car_element[StmCarElementNoRunning].state)
764 // Set delay event(restriction mode on)
765 this->setStateTransitionProcessToSystemd(StmEvtNoRestrictionModeOn,
768 else if (StmRunningNoStop ==
769 this->p_crr_state->car_element[StmCarElementNoRunning].state)
771 // Stop timer for restriction on event
772 if (this->event_source_list.find(StmEvtNoRestrictionModeOn) !=
773 this->event_source_list.end())
775 HMI_DEBUG("Stop timer for restriction on");
776 sd_event_source *event_source = this->event_source_list[StmEvtNoRestrictionModeOn];
777 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
780 HMI_ERROR("Failed to stop timer");
784 // Set event(restriction mode off)
785 this->setStateTransitionProcessToSystemd(StmEvtNoRestrictionModeOff, 0, "");
790 int PolicyManager::transitionState(sd_event_source *source, void *data)
792 HMI_DEBUG(">>>>>>>>>> START STATE TRANSITION");
794 int event_id = *((int *)data);
796 int event_no, category_no, area_no;
797 event_no = STM_GET_EVENT_FROM_ID(event_id);
798 category_no = STM_GET_CATEGORY_FROM_ID(event_id);
799 area_no = STM_GET_AREA_FROM_ID(event_id);
800 HMI_DEBUG(">>>>>>>>>> EVENT:%s CATEGORY:%s AREA:%s",
801 kStmEventName[event_no],
802 kStmCategoryName[category_no],
803 kStmAreaName[area_no]);
805 // Store current state
806 *(this->p_prv_state) = *(this->p_crr_state);
809 int ret = stmTransitionState(event_id, this->p_crr_state);
812 HMI_ERROR("Failed transition state");
813 if (nullptr != this->callback.onError)
815 json_object *json_out = json_object_new_object();
816 json_object_object_add(json_out, "message",
817 json_object_new_string("Failed to transition state"));
818 json_object_object_add(json_out, "event",
819 json_object_new_string(kStmEventName[event_no]));
820 json_object_object_add(json_out, "role",
821 json_object_new_string(this->req_role_list[event_id].c_str()));
822 json_object_object_add(json_out, "area",
823 json_object_new_string(kStmAreaName[area_no]));
824 this->callback.onError(json_out);
825 json_object_put(json_out);
830 // Update state which is managed by PolicyManager
831 this->updateState(event_id);
833 // Create output information for ResourceManager
834 json_object *json_out = json_object_new_object();
835 this->createOutputInformation(&json_out);
837 // Notify changed state
838 if (nullptr != this->callback.onStateTransitioned)
840 this->callback.onStateTransitioned(json_out);
843 // Start/Stop timer events
844 this->controlTimerEvent();
846 // Release json_object
847 json_object_put(json_out);
852 // Destroy sd_event_source object
853 sd_event_source_unref(source);
855 // Remove event source from list
856 if (this->event_source_list.find(event_id) != this->event_source_list.end())
858 this->event_source_list.erase(event_id);
861 HMI_DEBUG(">>>>>>>>>> FINISH STATE TRANSITION");
865 int PolicyManager::timerEvent(sd_event_source *source, uint64_t usec, void *data)
869 int ret = this->transitionState(source, data);
873 int PolicyManager::setStateTransitionProcessToSystemd(int event_id, uint64_t delay_ms, std::string role)
875 struct sd_event_source *event_source;
876 HMI_DEBUG("event_id:0x%x delay:%d role:%s", event_id, delay_ms, role.c_str());
880 int ret = sd_event_add_defer(afb_daemon_get_event_loop(), &event_source,
881 &pm::transitionStateWrapper, new int(event_id));
884 HMI_ERROR("Faild to sd_event_add_defer: errno:%d", ret);
891 struct timespec time_spec;
892 clock_gettime(CLOCK_BOOTTIME, &time_spec);
894 // Calculate timer fired time
895 uint64_t usec = (time_spec.tv_sec * 1000000) + (time_spec.tv_nsec / 1000) + (delay_ms * 1000);
898 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &event_source,
899 CLOCK_BOOTTIME, usec, 1,
900 &pm::timerEventWrapper, new int(event_id));
903 HMI_ERROR("Faild to sd_event_add_time: errno:%d", ret);
907 // Store event source
908 this->event_source_list[event_id] = event_source;
909 // Store requested role
910 this->req_role_list[event_id] = role;
914 bool PolicyManager::changedRestrictionModeTo2On()
916 // TODO: If possible thie process should be include in zipc stm in the future
917 if (this->p_crr_state->car_element[StmCarElementNoRestrictionMode].changed &&
918 (StmRestrictionModeSttNoOn != this->p_prv_state->car_element[StmCarElementNoRestrictionMode].state) &&
919 (StmRestrictionModeSttNoOn == this->p_crr_state->car_element[StmCarElementNoRestrictionMode].state))
926 bool PolicyManager::changedRestrictionMode2OnToOther()
928 // TODO: If possible thie process should be include in zipc stm in the future
929 if (this->p_crr_state->car_element[StmCarElementNoRestrictionMode].changed &&
930 (StmRestrictionModeSttNoOn == this->p_prv_state->car_element[StmCarElementNoRestrictionMode].state) &&
931 (StmRestrictionModeSttNoOn != this->p_crr_state->car_element[StmCarElementNoRestrictionMode].state))
938 bool PolicyManager::changedLightstatusBrakeOffToOn()
941 // If possible thie process should be include in zipc stm in the future
942 if (("master" == this->ecu_name) &&
943 this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].changed &&
944 (StmLightstatusBrakeSttNoOff == this->p_prv_state->car_element[StmCarElementNoLightstatusBrake].state) &&
945 (StmLightstatusBrakeSttNoOn == this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].state))
952 bool PolicyManager::changedLightstatusBrakeOnToOff()
955 // If possible thie process should be include in zipc stm in the future
956 if (("master" == this->ecu_name) &&
957 this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].changed &&
958 (StmLightstatusBrakeSttNoOn == this->p_prv_state->car_element[StmCarElementNoLightstatusBrake].state) &&
959 (StmLightstatusBrakeSttNoOff == this->p_crr_state->car_element[StmCarElementNoLightstatusBrake].state))
966 int PolicyManager::loadRolesConfigFile()
968 std::string file_name;
970 // Get afm application installed dir
971 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
972 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
974 if (!afm_app_install_dir)
976 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
980 file_name = std::string(afm_app_install_dir) + std::string(pm::kPathRolesConfigFile);
983 // Load roles config file
984 json_object *json_obj;
985 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
988 HMI_ERROR("Could not open %s, so use default role information", pm::kPathRolesConfigFile);
989 json_obj = json_tokener_parse(kDefaultRolesConfig);
991 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
994 json_object *json_cfg;
995 if (!json_object_object_get_ex(json_obj, "ecus", &json_cfg))
997 HMI_ERROR("Parse Error!!");
1001 int num_ecu = json_object_array_length(json_cfg);
1002 HMI_DEBUG("json_cfg(ecus) len:%d", num_ecu);
1004 const char* c_ecu_name;
1005 json_object *json_ecu;
1006 for (int i = 0; i < num_ecu; i++)
1008 json_ecu= json_object_array_get_idx(json_cfg, i);
1010 c_ecu_name = this->getStringFromJson(json_ecu, "name");
1011 if (nullptr == c_ecu_name)
1013 HMI_ERROR("Parse Error!!");
1017 if (std::string(c_ecu_name) == this->ecu_name)
1029 HMI_ERROR("Areas for ecu:%s is NOT exist!!", this->ecu_name.c_str());
1034 json_object *json_roles;
1035 if (!json_object_object_get_ex(json_ecu, "roles", &json_roles))
1037 HMI_ERROR("Parse Error!!");
1041 int len = json_object_array_length(json_roles);
1042 HMI_DEBUG("json_cfg len:%d", len);
1043 HMI_DEBUG("json_cfg dump:%s", json_object_get_string(json_roles));
1045 json_object *json_tmp;
1046 const char *category;
1050 for (int i = 0; i < len; i++)
1052 json_tmp = json_object_array_get_idx(json_roles, i);
1054 category = this->getStringFromJson(json_tmp, "category");
1055 roles = this->getStringFromJson(json_tmp, "role");
1056 areas = this->getStringFromJson(json_tmp, "area");
1057 layer = this->getStringFromJson(json_tmp, "layer");
1059 if ((nullptr == category) || (nullptr == roles) ||
1060 (nullptr == areas) || (nullptr == layer))
1062 HMI_ERROR("Parse Error!!");
1066 // Parse roles by '|'
1067 std::vector<std::string> vct_roles;
1068 vct_roles = this->parseString(std::string(roles), '|');
1070 // Parse areas by '|'
1072 vct_areas = this->parseString(std::string(areas), '|');
1074 // Set role, category, areas
1075 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr)
1077 this->role2category[*itr] = std::string(category);
1079 this->category2role[category] = std::string(roles);
1080 this->category2areas[category] = vct_areas;
1081 this->layer2categories[layer].push_back(category);
1085 HMI_DEBUG("Check role2category");
1086 for (const auto &x : this->role2category)
1088 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), x.second.c_str());
1091 HMI_DEBUG("Check category2role");
1092 for (const auto &x : this->category2role)
1094 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), x.second.c_str());
1097 HMI_DEBUG("Check category2areas");
1098 for (const auto &x : this->category2areas)
1100 for (const auto &y : x.second)
1102 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), y.c_str());
1106 HMI_DEBUG("Check layer2categories");
1107 for (const auto &x : this->layer2categories)
1109 for (const auto &y : x.second)
1111 HMI_DEBUG("key:%s, val:%s", x.first.c_str(), y.c_str());
1117 int PolicyManager::loadLayoutsConfigFile()
1121 // Get afm application installed dir
1122 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1123 HMI_DEBUG("afm_app_install_dir:%s", afm_app_install_dir);
1125 std::string file_name;
1126 if (!afm_app_install_dir)
1128 HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
1132 file_name = std::string(afm_app_install_dir) + std::string(pm::kPathLayoutsConfigFile);
1135 // Load states config file
1136 json_object *json_obj;
1137 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
1140 HMI_DEBUG("Could not open %s, so use default layout information", pm::kPathLayoutsConfigFile);
1141 json_obj = json_tokener_parse(kDefaultLayoutsConfig);
1143 HMI_DEBUG("json_obj dump:%s", json_object_get_string(json_obj));
1146 json_object *json_cfg;
1147 if (!json_object_object_get_ex(json_obj, "ecus", &json_cfg))
1149 HMI_ERROR("Parse Error!!");
1153 int num_ecu = json_object_array_length(json_cfg);
1154 HMI_DEBUG("json_cfg(ecus) len:%d", num_ecu);
1156 const char* c_ecu_name;
1157 json_object *json_ecu;
1158 for (int i = 0; i < num_ecu; i++)
1160 json_ecu= json_object_array_get_idx(json_cfg, i);
1162 c_ecu_name = this->getStringFromJson(json_ecu, "name");
1163 if (nullptr == c_ecu_name)
1165 HMI_ERROR("Parse Error!!");
1169 if (std::string(c_ecu_name) == this->ecu_name)
1181 HMI_ERROR("Areas for ecu:%s is NOT exist!!", this->ecu_name.c_str());
1186 HMI_DEBUG("Perse layouts");
1187 json_object *json_layouts;
1188 if (!json_object_object_get_ex(json_ecu, "layouts", &json_layouts))
1190 HMI_ERROR("Parse Error!!");
1194 int len = json_object_array_length(json_layouts);
1195 HMI_DEBUG("json_layouts len:%d", len);
1196 HMI_DEBUG("json_layouts dump:%s", json_object_get_string(json_layouts));
1200 const char *category;
1201 for (int i = 0; i < len; i++)
1203 json_object *json_tmp = json_object_array_get_idx(json_layouts, i);
1205 layout = this->getStringFromJson(json_tmp, "name");
1206 if (nullptr == layout)
1208 HMI_ERROR("Parse Error!!");
1211 HMI_DEBUG("> layout:%s", layout);
1213 json_object *json_area_array;
1214 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array))
1216 HMI_ERROR("Parse Error!!");
1220 int len_area = json_object_array_length(json_area_array);
1221 HMI_DEBUG("json_area_array len:%d", len_area);
1222 HMI_DEBUG("json_area_array dump:%s", json_object_get_string(json_area_array));
1224 LayoutState layout_state;
1225 AreaState area_state;
1226 std::map<std::string, int> category_num;
1227 for (int ctg_no = StmCtgNoMin;
1228 ctg_no <= StmCtgNoMax; ctg_no++)
1230 const char *ctg_name = kStmCategoryName[ctg_no];
1231 category_num[ctg_name] = 0;
1234 for (int j = 0; j < len_area; j++)
1236 json_object *json_area = json_object_array_get_idx(json_area_array, j);
1239 const char *area = this->getStringFromJson(json_area, "name");
1240 if (nullptr == area)
1242 HMI_ERROR("Parse Error!!");
1245 area_state.name = std::string(area);
1246 HMI_DEBUG(">> area:%s", area);
1248 // Get app attribute of the area
1249 category = this->getStringFromJson(json_area, "category");
1250 if (nullptr == category)
1252 HMI_ERROR("Parse Error!!");
1255 area_state.category = std::string(category);
1256 category_num[category]++;
1257 HMI_DEBUG(">>> category:%s", category);
1259 role = this->getStringFromJson(json_area, "role");
1260 if (nullptr != role)
1262 // Role is NOT essential here
1263 area_state.role = std::string(role);
1267 area_state.role = std::string("");
1269 HMI_DEBUG(">>> role:%s", role);
1271 layout_state.area_list.push_back(area_state);
1274 layout_state.name = layout;
1275 layout_state.category_num = category_num;
1276 this->default_layouts[layout] = layout_state;
1279 // initialize for none layout
1280 LayoutState none_layout_state;
1281 memset(&none_layout_state, 0, sizeof(none_layout_state));
1282 none_layout_state.name = "none";
1283 this->default_layouts["none"] = none_layout_state;
1286 for (auto itr_layout = this->default_layouts.begin();
1287 itr_layout != this->default_layouts.end(); ++itr_layout)
1289 HMI_DEBUG(">>> layout:%s", itr_layout->first.c_str());
1291 for (auto itr_area = itr_layout->second.area_list.begin();
1292 itr_area != itr_layout->second.area_list.end(); ++itr_area)
1294 HMI_DEBUG(">>> >>> area :%s", itr_area->name.c_str());
1295 HMI_DEBUG(">>> >>> category:%s", itr_area->category.c_str());
1296 HMI_DEBUG(">>> >>> role :%s", itr_area->role.c_str());
1300 // Release json_object
1301 json_object_put(json_obj);
1306 void PolicyManager::pushInvisibleRoleHistory(std::string category, std::string role)
1308 auto i = std::remove_if(this->crr_invisible_role_history[category].begin(),
1309 this->crr_invisible_role_history[category].end(),
1310 [role](std::string x) { return (role == x); });
1312 if (this->crr_invisible_role_history[category].end() != i)
1314 this->crr_invisible_role_history[category].erase(i);
1317 this->crr_invisible_role_history[category].push_back(role);
1319 if (pm::kInvisibleRoleHistoryNum < crr_invisible_role_history[category].size())
1321 this->crr_invisible_role_history[category].erase(
1322 this->crr_invisible_role_history[category].begin());
1326 std::string PolicyManager::popInvisibleRoleHistory(std::string category)
1329 if (crr_invisible_role_history[category].empty())
1335 role = this->crr_invisible_role_history[category].back();
1336 this->crr_invisible_role_history[category].pop_back();
1341 const char *PolicyManager::getStringFromJson(json_object *obj, const char *key)
1344 if (!json_object_object_get_ex(obj, key, &tmp))
1346 HMI_DEBUG("Not found key \"%s\"", key);
1350 return json_object_get_string(tmp);
1353 int PolicyManager::inputJsonFilie(const char *file, json_object **obj)
1355 const int input_size = 128;
1358 HMI_DEBUG("Input file: %s", file);
1361 FILE *fp = fopen(file, "rb");
1364 HMI_ERROR("Could not open file");
1369 struct json_tokener *tokener = json_tokener_new();
1370 enum json_tokener_error json_error;
1371 char buffer[input_size];
1375 size_t len = fread(buffer, sizeof(char), input_size, fp);
1376 *obj = json_tokener_parse_ex(tokener, buffer, len);
1377 if (nullptr != *obj)
1379 HMI_DEBUG("File input is success");
1384 json_error = json_tokener_get_error(tokener);
1385 if ((json_tokener_continue != json_error) || (input_size > len))
1387 HMI_ERROR("Failed to parse file (byte:%d err:%s)",
1388 (input_size * block_cnt), json_tokener_error_desc(json_error));
1389 HMI_ERROR("\n%s", buffer);
1399 // Free json_tokener
1400 json_tokener_free(tokener);
1405 void PolicyManager::dumpLayerState(std::unordered_map<std::string, LayerState> &layers)
1407 HMI_DEBUG("-------------------------------------------------------------------------------------------------------");
1408 HMI_DEBUG("|%-15s|%s|%-20s|%-20s|%-20s|%-20s|",
1409 "LAYER", "C", "LAYOUT", "AREA", "CATEGORY", "ROLE");
1410 for (const auto &itr : layers)
1412 LayerState ls = itr.second;
1413 const char* layer = ls.name.c_str();
1414 const char* changed = (ls.changed) ? "T" : "f";
1415 const char* layout = ls.layout_state.name.c_str();
1417 for (const auto &as : ls.layout_state.area_list)
1422 HMI_DEBUG("|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1423 layer, changed, layout,
1424 as.name.c_str(), as.category.c_str(), as.role.c_str());
1427 HMI_DEBUG("|%-15s|%1s|%-20s|%-20s|%-20s|%-20s|",
1428 "", "", "", as.name.c_str(), as.category.c_str(), as.role.c_str());
1431 HMI_DEBUG("-------------------------------------------------------------------------------------------------------");
1434 void PolicyManager::dumpInvisibleRoleHistory()
1436 HMI_DEBUG(">>>>>>>>>> DUMP INVISIBLE ROLE HISTORY ( category [older > newer] )");
1437 for (int ctg_no = StmCtgNoMin; ctg_no <= StmCtgNoMax; ctg_no++)
1439 if (ctg_no == StmCtgNoNone)
1442 std::string category = std::string(kStmCategoryName[ctg_no]);
1444 std::string str = category + " [ ";
1445 for (const auto &i : this->crr_invisible_role_history[category])
1449 HMI_DEBUG("%s", str.c_str());
1453 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter)
1455 // Parse string by delimiter
1456 std::vector<std::string> vct;
1457 std::stringstream ss{str};
1459 while (std::getline(ss, buf, delimiter))
1463 // Delete space and push back to vector
1464 vct.push_back(this->deleteSpace(buf));
1470 std::string PolicyManager::deleteSpace(std::string str)
1472 std::string ret = str;
1474 while ((pos = ret.find_first_of(" ")) != std::string::npos)
1481 const char *PolicyManager::kDefaultRolesConfig = "{ \
1484 \"category\": \"homescreen\", \
1485 \"role\": \"homescreen\", \
1486 \"area\": \"fullscreen\", \
1489 \"category\": \"map\", \
1490 \"role\": \"map\", \
1491 \"area\": \"normal.full | split.main\", \
1494 \"category\": \"general\", \
1495 \"role\": \"launcher | poi | browser | sdl | mixer | radio | hvac | debug | phone | video | music\", \
1496 \"area\": \"normal.full\", \
1499 \"category\": \"system\", \
1500 \"role\": \"settings | dashboard\", \
1501 \"area\": \"normal.full\", \
1504 \"category\": \"software_keyboard\", \
1505 \"role\": \"software_keyboard\", \
1506 \"area\": \"software_keyboard\", \
1509 \"category\": \"restriction\", \
1510 \"role\": \"restriction\", \
1511 \"area\": \"restriction.normal | restriction.split.main | restriction.split.sub\", \
1514 \"category\": \"pop_up\", \
1515 \"role\": \"pop_up\", \
1516 \"area\": \"on_screen\", \
1519 \"category\": \"system_alert\", \
1520 \"role\": \"system_alert\", \
1521 \"area\": \"on_screen\", \
1526 const char *PolicyManager::kDefaultLayoutsConfig = "{ \
1529 \"name\": \"homescreen\", \
1530 \"layer\": \"far_homescreen\", \
1533 \"name\": \"fullscreen\", \
1534 \"category\": \"homescreen\" \
1539 \"name\": \"map.normal\", \
1540 \"layer\": \"apps\", \
1543 \"name\": \"normal.full\", \
1544 \"category\": \"map\" \
1549 \"name\": \"map.split\", \
1550 \"layer\": \"apps\", \
1553 \"name\": \"split.main\", \
1554 \"category\": \"map\" \
1557 \"name\": \"split.sub\", \
1558 \"category\": \"splitable\" \
1563 \"name\": \"map.fullscreen\", \
1564 \"layer\": \"apps\", \
1567 \"name\": \"fullscreen\", \
1568 \"category\": \"map\" \
1573 \"name\": \"splitable.normal\", \
1574 \"layer\": \"apps\", \
1577 \"name\": \"normal.full\", \
1578 \"category\": \"splitable\" \
1583 \"name\": \"splitable.split\", \
1584 \"layer\": \"apps\", \
1587 \"name\": \"split.main\", \
1588 \"category\": \"splitable\" \
1591 \"name\": \"split.sub\", \
1592 \"category\": \"splitable\" \
1597 \"name\": \"general.normal\", \
1598 \"layer\": \"apps\", \
1601 \"name\": \"normal.full\", \
1602 \"category\": \"general\" \
1607 \"name\": \"system.normal\", \
1608 \"layer\": \"apps\", \
1611 \"name\": \"normal.full\", \
1612 \"category\": \"system\" \
1617 \"name\": \"software_keyboard\", \
1618 \"layer\": \"near_homescreen\", \
1621 \"name\": \"software_keyboard\", \
1622 \"category\": \"software_keyboard\" \
1627 \"name\": \"restriction.normal\", \
1628 \"layer\": \"restriction\", \
1631 \"name\": \"restriction.normal\", \
1632 \"category\": \"restriction\" \
1637 \"name\": \"restriction.split.main\", \
1638 \"layer\": \"restriction\", \
1641 \"name\": \"restriction.split.main\", \
1642 \"category\": \"restriction\" \
1647 \"name\": \"restriction.split.sub\", \
1648 \"layer\": \"restriction\", \
1651 \"name\": \"restriction.split.sub\", \
1652 \"category\": \"restriction\" \
1657 \"name\": \"pop_up\", \
1658 \"layer\": \"on_screen\", \
1661 \"name\": \"on_screen\", \
1662 \"category\": \"pop_up\" \
1667 \"name\": \"system_alert\", \
1668 \"layer\": \"on_screen\", \
1671 \"name\": \"on_screen\", \
1672 \"category\": \"system_alert\" \