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.
24 #include <systemd/sd-event.h>
25 #include <json-c/json.h>
26 #include "policy_manager.hpp"
27 #include "hmi-debug.h"
31 #include "dummy_stm.h"
37 typedef struct AreaState {
43 typedef std::vector<AreaState> AreaList;
44 typedef struct LayoutState {
46 std::map<std::string, int> category_num;
48 std::map<std::string, std::vector<std::string>> role_history;
51 typedef struct LayerState {
53 LayoutState layout_state;
56 typedef struct EventInfo {
62 struct sd_event* event_loop;
63 std::map<int, struct sd_event_source*> event_source_list;
64 std::map<int, std::string> g_req_role_list;
65 PolicyManager::CallbackTable callback;
66 std::queue<EventInfo> g_event_info_queue;
68 std::unordered_map<std::string, LayerState> g_prv_layers;
69 std::unordered_map<std::string, LayerState> g_crr_layers;
70 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
71 std::unordered_map<std::string, LayoutState> g_default_layouts;
75 PolicyManager::PolicyManager() :
83 HMI_DEBUG("wm:pm", "Call");
86 int PolicyManager::initialize() {
87 HMI_DEBUG("wm:pm", "Call");
92 for (int i = stm::gStmEventNoMin; i <= stm::gStmEventNoMax; i++) {
93 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
94 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
97 for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
98 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
99 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
102 for (int i = stm::gStmAreaNoMin; i <= stm::gStmAreaNoMax; i++) {
103 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
104 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
108 ret = this->loadRoleDb();
110 HMI_ERROR("wm:pm", "Load role.db Error!!");
115 ret = this->loadLayoutDb();
117 HMI_ERROR("wm:pm", "Load layout.db Error!!");
121 // Initialize current/previous state of layers
122 pm::AreaState init_area;
123 pm::LayoutState init_layout;
124 init_area.name = "none";
125 init_area.category = "none";
126 init_area.role = "none";
127 init_layout.area_list.push_back(init_area);
129 for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
130 const char* layer_name = stm::gStmLayerName[i];
131 pm::g_crr_layers[layer_name].name = layer_name;
132 pm::g_crr_layers[layer_name].layout_state = init_layout;
135 pm::g_prv_layers = pm::g_crr_layers;
137 // Initialize StateTransitioner
138 stm::stmInitialize();
140 // Initialize sd_event loop
141 ret = this->initializeSdEventLoop();
143 HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
150 int PolicyManager::initializeSdEventLoop() {
151 // Get default event loop object
152 int ret = sd_event_new(&(pm::event_loop));
154 HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
158 // Create thread for sd_event and detach
159 std::thread sd_event_loop([this]() {
161 sd_event_run(pm::event_loop, 1000);
164 sd_event_loop.detach();
169 static void addStateToJson(
170 const char* key, int is_changed, const char* state, json_object** json_out) {
171 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
172 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
176 json_object* json_obj = json_object_new_object();
177 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
179 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
180 json_object_object_add(json_obj, "state", json_object_new_string(state));
182 json_object_object_add(*json_out, key, json_obj);
185 static void addStateToJson(const char* layer_name, unsigned int changed,
186 pm::AreaList area_list, json_object** json_out) {
187 if ((nullptr == layer_name) || (1 < changed) || (nullptr == json_out)) {
188 HMI_ERROR("wm:pm", "Invalid argument!!!");
192 json_object* json_areas = json_object_new_array();
193 json_object* json_tmp;
194 for (pm::AreaState as : area_list) {
195 json_tmp = json_object_new_object();
196 json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
197 json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
198 json_object_array_add(json_areas, json_tmp);
201 json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
202 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
203 json_object_object_add(*json_out, "areas", json_areas);
206 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role);
207 static int checkPolicy(sd_event_source *source, void *data) {
208 HMI_DEBUG("wm:pm", "Call");
209 HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
211 int event_data = *((int*)data);
213 int event_no, category_no, area_no;
214 event_no = (event_data & STM_MSK_EVT_NO) - 1;
215 category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
216 area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
217 HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
218 stm::gStmEventName[event_no],
219 stm::gStmCategoryName[category_no],
220 stm::gStmAreaName[area_no]);
223 stm::stm_state_t crr_state;
224 int ret = stm::stmTransitionState(event_data, &crr_state);
226 HMI_ERROR("wm:pm", "Error!!");
230 HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
231 crr_state.parking_brake.is_changed,
232 crr_state.parking_brake.state,
233 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
234 HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
235 crr_state.accel_pedal.is_changed,
236 crr_state.accel_pedal.state,
237 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
238 HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
239 crr_state.lightstatus_brake.is_changed,
240 crr_state.lightstatus_brake.state,
241 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
242 HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
243 crr_state.car.is_changed,
245 stm::gStmCarStateNo2Name[crr_state.car.state]);
246 HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
247 crr_state.lamp.is_changed,
248 crr_state.lamp.state,
249 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
250 HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
251 crr_state.restriction_mode.is_changed,
252 crr_state.restriction_mode.state,
253 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
254 HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
255 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
256 crr_state.layer[stm::gStmLayerNoHomescreen].state,
257 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
258 HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
259 crr_state.layer[stm::gStmLayerNoApps].is_changed,
260 crr_state.layer[stm::gStmLayerNoApps].state,
261 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
262 HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
263 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
264 crr_state.layer[stm::gStmLayerNoRestriction].state,
265 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
266 HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
267 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
268 crr_state.layer[stm::gStmLayerNoOnScreen].state,
269 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
271 // Store previous layers
272 pm::g_prv_layers = pm::g_crr_layers;
274 std::string req_role = pm::g_req_role_list[event_data];
275 std::string req_evt = std::string(stm::gStmEventName[event_no]);
276 std::string req_ctg = std::string(stm::gStmCategoryName[category_no]);
277 std::string req_area = std::string(stm::gStmAreaName[area_no]);
278 HMI_DEBUG("wm:pm", "REQ: event:%s role%s category:%s area:%s",
279 req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
282 for (int layer_no = stm::gStmLayerNoMin;
283 layer_no <= stm::gStmLayerNoMax; layer_no++) {
284 const char* layer_name = stm::gStmLayerName[layer_no];
285 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
288 // If restriction mode is changed off -> on,
289 // store current state for state of restriction mode off
290 if ((crr_state.restriction_mode.is_changed)
291 && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
292 HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
293 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
296 // If car state is changed car_stop -> car_run,
297 // store current state for state of car stop
298 if ((crr_state.car.is_changed)
299 && (stm::gStmCarStateNoRun == crr_state.car.state)) {
300 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
301 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
306 // This layer is changed?
307 if (crr_state.layer[layer_no].is_changed) {
308 // Get previous layout name of this layer
309 pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
310 std::string prv_layout_name = prv_layout_state.name;
312 // Get current layout name of this layer
313 int crr_layout_state_no = crr_state.layer[layer_no].state;
314 std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
316 pm::LayoutState crr_layout_state;
318 if ((crr_state.restriction_mode.is_changed)
319 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
320 // If restriction mode is changed on -> off,
321 // restore state of restriction mode off
322 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
323 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
325 if ((crr_state.car.is_changed)
326 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
327 // If car state is changed car_run -> car_stop,
328 // restore state of car stop
329 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
330 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
334 // Copy previous layout state for current
335 crr_layout_state = prv_layout_state;
337 if (prv_layout_name == crr_layout_name) {
338 HMI_DEBUG("wm:lm", "Previous layout is same with current");
341 // If previous layout is NOT same with current,
342 // current areas is set with default value
343 HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
344 crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
345 crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
346 crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
349 // Create candidate list
350 std::map<std::string, pm::AreaList> cand_list;
351 for (int ctg_no=stm::gStmCategoryNoMin;
352 ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
353 const char* ctg = stm::gStmCategoryName[ctg_no];
354 HMI_DEBUG("wm:pm", "ctg:%s", ctg);
356 // Create candidate list for category from the previous displayed categories
357 pm::AreaList tmp_cand_list;
358 for (pm::AreaState area_state : prv_layout_state.area_list) {
359 if (std::string(ctg) == area_state.category) {
360 // If there is the category which is same with new category in previous layout,
362 HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
363 area_state.category.c_str(), area_state.role.c_str());
364 tmp_cand_list.push_back(area_state);
368 int candidate_num = prv_layout_state.category_num[ctg];
369 int blank_num = crr_layout_state.category_num[ctg];
370 HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
372 // If requested event is "activate"
373 // and there are requested category and area,
374 // update area with requested role in current layout.
375 bool request_for_this_layer = false;
376 bool updated = false;
377 if ((ctg == req_ctg) && ("activate" == req_evt) ) {
378 HMI_DEBUG("wm:pm", "requested event is activate");
379 for (pm::AreaState &as : crr_layout_state.area_list) {
380 if (as.category == req_ctg) {
381 request_for_this_layer = true;
383 if (as.name == req_area) {
384 HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
385 as.name.c_str(), as.category.c_str(), as.role.c_str());
394 // If NOT updated: there is not requested area in new layout,
395 // so push requested role to candidate list
396 if (request_for_this_layer && (!updated)) {
397 HMI_DEBUG("wm:pm", "Push request to candidate list");
398 pm::AreaState area_state;
399 area_state.name = req_area;
400 area_state.category = req_ctg;
401 area_state.role = req_role;
402 tmp_cand_list.push_back(area_state);
406 // Compare number of candidate/blank,
407 // And remove role in order of the oldest as necessary
408 if (candidate_num < blank_num) {
409 // Refer history stack
410 // and add to the top of tmp_cand_list in order to the newest
411 while (candidate_num != blank_num) {
412 pm::AreaState area_state;
413 area_state.name = "";
414 area_state.category = ctg;
415 if (0 != crr_layout_state.role_history[ctg].size()) {
416 HMI_ERROR("wm:pm", "Use role in history stack:%s",
417 crr_layout_state.role_history[ctg].back().c_str());
418 area_state.role = crr_layout_state.role_history[ctg].back();
419 crr_layout_state.role_history[ctg].pop_back();
422 HMI_ERROR("wm:pm", "There is no role in history stack!!");
423 area_state.role = "";
425 tmp_cand_list.push_back(area_state);
429 else if (candidate_num > blank_num) {
430 HMI_DEBUG("wm:pm", "candidate_num > blank_num");
432 // Remove the oldest role from candidate list
433 while (candidate_num != blank_num) {
434 std::string removed_role = tmp_cand_list.begin()->role;
435 HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
436 removed_role.c_str());
437 tmp_cand_list.erase(tmp_cand_list.begin());
440 // Push removed data to history stack
441 crr_layout_state.role_history[ctg].push_back(removed_role);
444 else { // (candidate_num == blank_num)
448 cand_list[ctg] = tmp_cand_list;
452 for (pm::AreaState &as : crr_layout_state.area_list) {
453 HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
454 as.name.c_str(), as.category.c_str());
456 HMI_DEBUG("wm:pm", "Update this area with role:%s",
457 cand_list[as.category].begin()->role.c_str());
458 as.role = cand_list[as.category].begin()->role;
459 cand_list[as.category].erase(cand_list[as.category].begin());
463 // Update current layout of this layer
464 pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
469 for (auto itr : pm::g_crr_layers) {
470 pm::LayerState ls = itr.second;
471 HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
472 HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
474 for (pm::AreaState as : ls.layout_state.area_list) {
475 HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
476 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
477 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
481 json_object* json_out = json_object_new_object();
485 // "parking_brake": {
486 // "is_changed": <bool>,
487 // "state": <const char*>
489 addStateToJson("parking_brake",
490 crr_state.parking_brake.is_changed,
491 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
495 // "is_changed": <bool>,
496 // "state": <const char*>
498 addStateToJson("accel_pedal",
499 crr_state.accel_pedal.is_changed,
500 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
503 // "lightstatus_brake": {
504 // "is_changed": <bool>,
505 // "state": <const char*>
507 addStateToJson("lightstatus_brake",
508 crr_state.lightstatus_brake.is_changed,
509 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
513 // "is_changed": <bool>,
514 // "state": <const char*>
516 addStateToJson("car",
517 crr_state.car.is_changed,
518 stm::gStmCarStateNo2Name[crr_state.car.state],
522 // "is_changed": <bool>,
523 // "state": <const char*>
525 addStateToJson("lamp",
526 crr_state.lamp.is_changed,
527 stm::gStmLampStateNo2Name[crr_state.lamp.state],
530 // "restriction_mode": {
531 // "is_changed": <bool>,
532 // "state": <const char*>
534 addStateToJson("restriction_mode",
535 crr_state.restriction_mode.is_changed,
536 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
539 // Create layout information
544 // "changed": <bool>,
547 // "name":<const char*>,
548 // "role":<const char*>
555 json_object* json_layer = json_object_new_array();
556 json_object* json_tmp;
557 for (int layer_no = stm::gStmLayerNoMin;
558 layer_no <= stm::gStmLayerNoMax; layer_no++) {
559 const char* layer_name = stm::gStmLayerName[layer_no];
560 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
562 json_tmp = json_object_new_object();
563 addStateToJson(layer_name,
564 crr_state.layer[layer_no].is_changed,
565 pm::g_crr_layers[layer_name].layout_state.area_list,
567 json_object_array_add(json_layer, json_tmp);
570 // Add json array of layer
571 json_object_object_add(json_out, "layers", json_layer);
573 // Notify state is changed
574 if (nullptr != pm::callback.onStateTransitioned) {
575 pm::callback.onStateTransitioned(json_out);
578 if (crr_state.car.is_changed) {
579 if (stm::gStmCarStateNoRun == crr_state.car.state) {
580 // Set delay event(restriction mode on)
581 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, "");
583 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
584 // Set event(restriction mode off)
585 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, "");
587 // Stop timer for restriction on event
588 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
589 != pm::event_source_list.end()) {
590 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
591 sd_event_source *event_source
592 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
593 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
595 HMI_ERROR("wm:pm", "Failed to stop timer");
601 // Release json_object
602 json_object_put(json_out);
607 // Destroy sd_event_source object
608 sd_event_source_unref(source);
610 // Remove event source from list
611 if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
612 pm::event_source_list.erase(event_data);
615 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
619 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
620 HMI_DEBUG("wm:pm", "Call");
622 int ret = checkPolicy(source, data);
626 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
628 HMI_DEBUG("wm:pm", "Call");
629 HMI_DEBUG("wm:pm", "event:0x%x delay:%d role:%s", event, delay_ms, role.c_str());
631 // Store requested role
632 pm::g_req_role_list[event] = role;
635 int ret = sd_event_add_defer(pm::event_loop, NULL,
636 &checkPolicy, new int(event));
638 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
639 pm::g_req_role_list.erase(event);
645 struct timespec time_spec;
646 clock_gettime(CLOCK_MONOTONIC, &time_spec);
648 // Calculate timer fired time
649 uint64_t usec = (time_spec.tv_sec * 1000000)
650 + (time_spec.tv_nsec / 1000)
654 struct sd_event_source* event_source;
655 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
656 &timerEvent, new int(event));
658 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
659 pm::g_req_role_list.erase(event);
663 // Store event source
664 pm::event_source_list[event] = event_source;
670 void PolicyManager::registerCallback(CallbackTable callback) {
671 HMI_DEBUG("wm:pm", "Call");
673 pm::callback.onStateTransitioned = callback.onStateTransitioned;
674 pm::callback.onError = callback.onError;
677 int PolicyManager::setInputEventData(json_object* json_in) {
678 HMI_DEBUG("wm:pm", "Call");
681 if (nullptr == json_in) {
682 HMI_ERROR("wm:pm", "Argument is NULL!!");
686 // Get event from json_object
687 const char* event = this->getStringFromJson(json_in, "event");
689 if (nullptr != event) {
690 // Convert name to number
691 event_no = this->eventname2no_[event];
692 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
695 // Get role from json_object
696 const char* role = this->getStringFromJson(json_in, "role");
698 if (nullptr != role) {
699 HMI_DEBUG("wm:pm", "role(%s)", role);
701 // Convert role to category
702 const char* category = this->role2category_[role].c_str();
703 if (0 == strcmp("", category)) {
704 HMI_ERROR("wm:pm", "Error!!");
707 HMI_DEBUG("wm:pm", "category(%s)", category);
709 // Convert name to number
710 category_no = categoryname2no_[category];
711 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
714 // Get areat from json_object
715 const char* area = this->getStringFromJson(json_in, "area");
717 if (nullptr != area) {
718 // Convert name to number
719 area_no = areaname2no_[area];
720 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
723 // Set event info to the queue
724 pm::EventInfo event_info;
725 event_info.event = (event_no | category_no | area_no);
726 if (nullptr == role) {
727 event_info.role = std::string("");
730 event_info.role = std::string(role);
732 event_info.delay = 0;
733 pm::g_event_info_queue.push(event_info);
738 int PolicyManager::executeStateTransition() {
739 HMI_DEBUG("wm:pm", "Call");
742 pm::EventInfo event_info;
744 while (!pm::g_event_info_queue.empty()) {
745 // Get event info from queue and delete
746 event_info = pm::g_event_info_queue.front();
747 pm::g_event_info_queue.pop();
749 // Set event info for checking policy
750 ret = checkPolicyEntry(event_info.event, event_info.delay, event_info.role);
755 void PolicyManager::undoState() {
756 HMI_DEBUG("wm:pm", "Call");
761 pm::g_crr_layers = pm::g_prv_layers;
764 extern const char* kDefaultRoleDb;
765 int PolicyManager::loadRoleDb() {
766 HMI_DEBUG("wm:pm", "Call");
768 std::string file_name;
770 // Get afm application installed dir
771 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
772 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
774 if (!afm_app_install_dir) {
775 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
778 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
782 json_object* json_obj;
783 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
785 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
786 json_obj = json_tokener_parse(kDefaultRoleDb);
788 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
790 json_object* json_roles;
791 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
792 HMI_ERROR("wm:pm", "Parse Error!!");
796 int len = json_object_array_length(json_roles);
797 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
798 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
800 json_object* json_tmp;
801 const char* category;
804 for (int i=0; i<len; i++) {
805 json_tmp = json_object_array_get_idx(json_roles, i);
807 category = this->getStringFromJson(json_tmp, "category");
808 roles = this->getStringFromJson(json_tmp, "role");
809 areas = this->getStringFromJson(json_tmp, "area");
811 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
812 HMI_ERROR("wm:pm", "Parse Error!!");
816 // Parse roles by '|'
817 std::vector<std::string> vct_roles;
818 vct_roles = this->parseString(std::string(roles), '|');
820 // Parse areas by '|'
821 std::vector<std::string> vct_areas;
822 vct_areas = this->parseString(std::string(areas), '|');
824 // Set role, category, default area
825 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
826 // Delete space from role and area name
827 std::string role = this->deleteSpace(*itr);
828 std::string area = this->deleteSpace(vct_areas[0]);
830 this->role2category_[role] = std::string(category);
831 this->role2defaultarea_[role] = area;
834 this->category2role_[std::string(category)] = std::string(roles);
838 HMI_DEBUG("wm:pm", "Check role2category_");
839 for (auto& x:this->role2category_){
840 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
843 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
844 for (auto& x:this->role2defaultarea_){
845 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
848 HMI_DEBUG("wm:pm", "Check category2role_");
849 for (auto& x:this->category2role_){
850 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
856 extern const char* kDefaultLayoutDb;
857 int PolicyManager::loadLayoutDb() {
858 HMI_DEBUG("wm:lm", "Call");
860 // Get afm application installed dir
861 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
862 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
864 std::string file_name;
865 if (!afm_app_install_dir) {
866 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
869 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
873 json_object* json_obj;
874 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
876 HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
877 json_obj = json_tokener_parse(kDefaultLayoutDb);
879 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
882 HMI_DEBUG("wm:pm", "Perse layouts");
883 json_object* json_cfg;
884 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
885 HMI_ERROR("wm:pm", "Parse Error!!");
889 int len = json_object_array_length(json_cfg);
890 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
891 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
895 const char* category;
896 for (int i=0; i<len; i++) {
897 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
899 layout = this->getStringFromJson(json_tmp, "name");
900 if (nullptr == layout) {
901 HMI_ERROR("wm:pm", "Parse Error!!");
904 HMI_DEBUG("wm:pm", "> layout:%s", layout);
906 json_object* json_area_array;
907 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
908 HMI_ERROR("wm:pm", "Parse Error!!");
912 int len_area = json_object_array_length(json_area_array);
913 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
914 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
916 pm::LayoutState layout_state;
917 pm::AreaState area_state;
918 std::map<std::string, int> category_num;
919 for (int ctg_no = stm::gStmCategoryNoMin;
920 ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
921 const char* ctg_name = stm::gStmCategoryName[ctg_no];
922 category_num[ctg_name] = 0;
925 for (int j=0; j<len_area; j++) {
926 json_object* json_area = json_object_array_get_idx(json_area_array, j);
929 const char* area = this->getStringFromJson(json_area, "name");
930 if (nullptr == area) {
931 HMI_ERROR("wm:pm", "Parse Error!!");
934 area_state.name = std::string(area);
935 HMI_DEBUG("wm:pm", ">> area:%s", area);
937 // Get app attribute of the area
938 category = this->getStringFromJson(json_area, "category");
939 if (nullptr == category) {
940 HMI_ERROR("wm:pm", "Parse Error!!");
943 area_state.category = std::string(category);
944 category_num[category]++;
945 HMI_DEBUG("wm:pm", ">>> category:%s", category);
947 role = this->getStringFromJson(json_area, "role");
948 if (nullptr != role) {
949 // Role is NOT essential here
950 area_state.role = std::string(role);
953 area_state.role = std::string("");
956 HMI_DEBUG("wm:pm", ">>> role:%s", role);
958 layout_state.area_list.push_back(area_state);
962 layout_state.name = layout;
963 layout_state.category_num = category_num;
964 pm::g_default_layouts[layout] = layout_state;
967 // initialize for none layout
968 pm::LayoutState none_layout_state;
969 memset(&none_layout_state, 0, sizeof(none_layout_state));
970 none_layout_state.name = "none";
971 pm::g_default_layouts["none"] = none_layout_state;
974 for(auto itr_layout = pm::g_default_layouts.begin();
975 itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
976 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
978 for (auto itr_area = itr_layout->second.area_list.begin();
979 itr_area != itr_layout->second.area_list.end(); ++itr_area) {
980 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
981 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
982 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
984 for (auto itr_role = itr_area->second.begin();
985 itr_role != itr_area->second.end(); ++itr_role) {
986 HMI_DEBUG("wm:pm", ">>> >>> >>> attribute:%s, name:%s",
987 itr_role->first.c_str(), itr_role->second.c_str());
993 // Release json_object
994 json_object_put(json_obj);
1000 // This function will be removed because json_helper has same function.
1001 // json_helper should be library.
1002 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
1003 if ((nullptr == obj) || (nullptr == key)) {
1004 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
1009 if (!json_object_object_get_ex(obj, key, &tmp)) {
1010 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1014 return json_object_get_string(tmp);
1018 // This function will be removed because json_helper has same function.
1019 // json_helper should be library.
1020 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
1021 const int input_size = 128;
1024 if ((nullptr == file) || (nullptr == obj)) {
1025 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
1029 HMI_DEBUG("wm:jh", "Input file: %s", file);
1032 FILE *fp = fopen(file, "rb");
1033 if (nullptr == fp) {
1034 HMI_ERROR("wm:jh", "Could not open file");
1039 struct json_tokener *tokener = json_tokener_new();
1040 enum json_tokener_error json_error;
1041 char buffer[input_size];
1044 size_t len = fread(buffer, sizeof(char), input_size, fp);
1045 *obj = json_tokener_parse_ex(tokener, buffer, len);
1046 if (nullptr != *obj) {
1047 HMI_DEBUG("wm:jh", "File input is success");
1052 json_error = json_tokener_get_error(tokener);
1053 if ((json_tokener_continue != json_error)
1054 || (input_size > len)) {
1055 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1056 (input_size * block_cnt), json_tokener_error_desc(json_error));
1057 HMI_ERROR("wm:jh", "\n%s", buffer);
1067 // Free json_tokener
1068 json_tokener_free(tokener);
1073 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1074 // Parse string by delimiter
1075 std::vector<std::string> vct;
1076 std::stringstream ss{str};
1078 while (std::getline(ss, buf, delimiter)) {
1086 std::string PolicyManager::deleteSpace(std::string str) {
1087 std::string ret = str;
1089 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1095 const char* kDefaultRoleDb = "{ \
1098 \"category\": \"homescreen\", \
1099 \"role\": \"homescreen\", \
1100 \"area\": \"full\", \
1103 \"category\": \"map\", \
1104 \"role\": \"map\", \
1105 \"area\": \"full | normal | split.main\", \
1108 \"category\": \"general\", \
1109 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1110 \"area\": \"normal\", \
1113 \"category\": \"phone\", \
1114 \"role\": \"phone\", \
1115 \"area\": \"normal\", \
1118 \"category\": \"splitable\", \
1119 \"role\": \"splitable1 | splitable2\", \
1120 \"area\": \"normal | split.main | split.sub\", \
1123 \"category\": \"popup\", \
1124 \"role\": \"popup\", \
1125 \"area\": \"on_screen\", \
1128 \"category\": \"system_alert\", \
1129 \"role\": \"system_alert\", \
1130 \"area\": \"on_screen\", \
1133 \"category\": \"tbt\", \
1134 \"role\": \"tbt\", \
1135 \"area\": \"hud\", \
1141 const char* kDefaultLayoutDb = "{ \
1145 \"layer\": \"on_screen\", \
1148 \"name\": \"pop_up\", \
1149 \"role\": \"incomming_call\" \
1155 \"layer\": \"on_screen\", \
1158 \"name\": \"system_alert\", \
1159 \"role\": \"system_alert\" \
1165 \"layer\": \"apps\", \
1168 \"name\": \"normal\", \
1175 \"layer\": \"apps\", \
1178 \"name\": \"split.main\", \
1182 \"name\": \"split.sub\", \
1183 \"category\": \"hvac\" \
1189 \"layer\": \"apps\", \
1192 \"name\": \"full\", \
1199 \"layer\": \"apps\", \
1202 \"name\": \"normal\", \
1203 \"category\": \"splitable\" \
1209 \"layer\": \"apps\", \
1212 \"name\": \"split.main\", \
1213 \"category\": \"splitable\" \
1216 \"name\": \"split.sub\", \
1217 \"category\": \"splitable\" \
1223 \"layer\": \"apps\", \
1226 \"name\": \"normal\", \
1227 \"category\": \"general\" \
1233 \"layer\": \"homescreen\", \
1236 \"name\": \"full\", \
1237 \"role\": \"homescreen\" \
1244 \"name\": \"normal\", \
1253 \"name\": \"split.main\", \
1262 \"name\": \"split.sub\", \
1271 \"name\": \"full\", \
1280 \"name\": \"pop_up\", \
1289 \"name\": \"system_alert\", \