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 <systemd/sd-event.h>
24 #include <json-c/json.h>
25 #include "policy_manager.hpp"
26 #include "hmi-debug.h"
30 #include "dummy_stm.h"
36 typedef struct AreaState {
42 typedef struct LayoutState {
44 std::map<std::string, int> category_num;
45 // int category_num[stm::gStmCategoryNoNum];
46 std::vector<AreaState> area_list;
49 typedef struct LayerState {
51 LayoutState layout_state;
54 struct sd_event* event_loop;
55 std::map<int, struct sd_event_source*> event_source_list;
56 std::map<int, std::string> g_event_info_list;
57 PolicyManager::CallbackTable callback;
59 std::unordered_map<std::string, LayerState> g_prv_layers;
60 std::unordered_map<std::string, LayerState> g_crr_layers;
61 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
62 std::unordered_map<std::string, LayoutState> g_default_layouts;
66 PolicyManager::PolicyManager() :
74 HMI_DEBUG("wm:pm", "Call");
77 int PolicyManager::initialize() {
78 HMI_DEBUG("wm:pm", "Call");
83 for (unsigned int i=0; i<STM_NUM_EVT; i++) {
84 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
85 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
88 for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
89 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
90 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
93 for (unsigned int i=0; i<STM_NUM_ARA; i++) {
94 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
95 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
99 ret = this->loadRoleDb();
101 HMI_ERROR("wm:pm", "Load role.db Error!!");
106 ret = this->loadLayoutDb();
108 HMI_ERROR("wm:pm", "Load layout.db Error!!");
112 // Initialize current/previous state of layers
113 pm::AreaState init_area;
114 pm::LayoutState init_layout;
115 init_area.name = "none";
116 init_area.category = "none";
117 init_area.role = "none";
118 init_layout.area_list.push_back(init_area);
120 for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
121 const char* layer_name = stm::gStmLayerName[i];
122 pm::g_crr_layers[layer_name].name = layer_name;
123 pm::g_crr_layers[layer_name].layout_state = init_layout;
126 pm::g_prv_layers = pm::g_crr_layers;
128 // Initialize StateTransitioner
129 stm::stmInitialize();
131 // Initialize sd_event loop
132 ret = this->initializeSdEventLoop();
134 HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
141 int PolicyManager::initializeSdEventLoop() {
142 // Get default event loop object
143 int ret = sd_event_new(&(pm::event_loop));
145 HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
149 // Create thread for sd_event and detach
150 std::thread sd_event_loop([this]() {
152 sd_event_run(pm::event_loop, 1000);
155 sd_event_loop.detach();
160 static void addStateToJson(
161 const char* key, int is_changed, const char* state, json_object** json_out) {
162 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
163 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
167 json_object* json_obj = json_object_new_object();
168 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
170 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
171 json_object_object_add(json_obj, "state", json_object_new_string(state));
173 json_object_object_add(*json_out, key, json_obj);
176 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role);
177 static int checkPolicy(sd_event_source *source, void *data) {
178 HMI_DEBUG("wm:pm", "Call");
179 HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
181 int event_data = *((int*)data);
183 int event_no, category_no, area_no;
184 event_no = (event_data & STM_MSK_EVT_NO) - 1;
185 category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
186 area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
187 HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
188 stm::gStmEventName[event_no],
189 stm::gStmCategoryName[category_no],
190 stm::gStmAreaName[area_no]);
193 stm::stm_state_t crr_state;
194 int ret = stm::stmTransitionState(event_data, &crr_state);
196 HMI_ERROR("wm:pm", "Error!!");
200 HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
201 crr_state.parking_brake.is_changed,
202 crr_state.parking_brake.state,
203 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
204 HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
205 crr_state.accel_pedal.is_changed,
206 crr_state.accel_pedal.state,
207 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
208 HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
209 crr_state.lightstatus_brake.is_changed,
210 crr_state.lightstatus_brake.state,
211 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
212 HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
213 crr_state.car.is_changed,
215 stm::gStmCarStateNo2Name[crr_state.car.state]);
216 HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
217 crr_state.lamp.is_changed,
218 crr_state.lamp.state,
219 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
220 HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
221 crr_state.restriction_mode.is_changed,
222 crr_state.restriction_mode.state,
223 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
224 HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
225 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
226 crr_state.layer[stm::gStmLayerNoHomescreen].state,
227 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
228 HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
229 crr_state.layer[stm::gStmLayerNoApps].is_changed,
230 crr_state.layer[stm::gStmLayerNoApps].state,
231 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
232 HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
233 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
234 crr_state.layer[stm::gStmLayerNoRestriction].state,
235 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
236 HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
237 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
238 crr_state.layer[stm::gStmLayerNoOnScreen].state,
239 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
241 #if 1 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
242 // Store previous layers
243 pm::g_prv_layers = pm::g_crr_layers;
245 std::string role = pm::g_event_info_list[event_data];
246 std::string event = std::string(stm::gStmEventName[event_no]);
247 std::string category = std::string(stm::gStmCategoryName[category_no]);
248 std::string area = std::string(stm::gStmAreaName[area_no]);
250 for (int layer_no = stm::gStmLayerNoMin;
251 layer_no <= stm::gStmLayerNoMax; layer_no++) {
252 const char* layer_name = stm::gStmLayerName[layer_no];
253 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
256 // If restriction mode is changed off -> on,
257 // store current state for state of restriction mode off
258 if ((crr_state.restriction_mode.is_changed)
259 && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
260 HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
261 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
264 // If car state is changed car_stop -> car_run,
265 // store current state for state of car stop
266 if ((crr_state.car.is_changed)
267 && (stm::gStmCarStateNoRun == crr_state.car.state)) {
268 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
269 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
274 // This layer is changed?
275 if (crr_state.layer[layer_no].is_changed) {
276 // Get previous layout name of this layer
277 pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
278 std::string prv_layout_name = prv_layout_state.name;
280 // Get current layout name of this layer
281 int crr_layout_state_no = crr_state.layer[layer_no].state;
282 std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
284 pm::LayoutState crr_layout_state;
286 if ((crr_state.restriction_mode.is_changed)
287 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
288 // If restriction mode is changed on -> off,
289 // restore state of restriction mode off
290 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
291 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
293 if ((crr_state.car.is_changed)
294 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
295 // If car state is changed car_run -> car_stop,
296 // restore state of car stop
297 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
298 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
301 else if ("none" == crr_layout_name) {
302 // If current layout is "none",
303 // current areas is set with "none"
304 HMI_DEBUG("wm:pm", "Current layout is \"none\"");
305 pm::AreaState crr_area_state;
306 crr_area_state.name = "none";
307 crr_area_state.category = "none";
308 crr_area_state.role = "none";
309 crr_layout_state.name = "none";
310 crr_layout_state.area_list.push_back(crr_area_state);
313 if (prv_layout_name == crr_layout_name) {
314 // If previous layout is same with current,
315 // previous areas are copied to current
316 HMI_DEBUG("wm:lm", "Previous layout is same with current");
317 crr_layout_state = prv_layout_state;
320 // If previous layout is NOT same with current,
321 // current areas is set with default value
322 HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
323 crr_layout_state = pm::g_default_layouts[crr_layout_name];
326 // Update role in new area
328 if (crr_state.restriction_mode.is_changed) {
329 // Updating role is not necessary
330 // because new_role is not specified
331 // when restriction mode is changed
332 HMI_DEBUG("wm:lm", "Updating role is not necessary because new_role is not specified when restriction mode is changed");
334 if (crr_state.car.is_changed) {
335 // Updating role is not necessary
336 // because new_role is not specified
337 // when car state is changed
338 HMI_DEBUG("wm:lm", "Updating role is not necessary because new_role is not specified when car state is changed");
343 std::map<std::string, std::vector<pm::AreaState>> ctg_list;
344 for (int ctg_no=stm::gStmCategoryNoMin;
345 ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
346 const char* ctg = stm::gStmCategoryName[ctg_no];
347 HMI_DEBUG("wm:pm", "ctg:%s", ctg);
349 // Create area list of previous/current app
350 std::vector<pm::AreaState> area_list;
351 for (pm::AreaState area_state : prv_layout_state.area_list) {
352 if (std::string(ctg) == area_state.category) {
353 HMI_DEBUG("wm:pm", "push area_state to prv list");
354 area_list.push_back(area_state);
358 int prv_ctg_num = prv_layout_state.category_num[ctg];
359 int crr_ctg_num = crr_layout_state.category_num[ctg];
360 HMI_DEBUG("wm:pm", "crr_ctg_num:%d prv_ctg_num:%d", crr_ctg_num, prv_ctg_num);
362 if (crr_ctg_num < prv_ctg_num) {
363 HMI_DEBUG("wm:pm", "crr_ctg_num < prv_ctg_num");
364 if (0 == strcmp(ctg, category.c_str())) {
365 HMI_DEBUG("wm:pm", "category of requested role is same");
366 for (auto i = area_list.begin(); i != area_list.end(); ++i) {
367 if ((role == i->role) && (area == i->name)) {
368 HMI_DEBUG("wm:pm", "remove requested role data");
374 HMI_DEBUG("wm:pm", "remove the oldest area data");
375 area_list.erase(area_list.begin());
378 else if (crr_ctg_num >= prv_ctg_num) {
379 HMI_DEBUG("wm:pm", "crr_ctg_num >= prv_ctg_num");
380 if (0 == strcmp(ctg, category.c_str())) {
381 HMI_DEBUG("wm:pm", "category of requested role is same");
382 if ((crr_ctg_num == prv_ctg_num) && (0 != prv_ctg_num)) {
383 HMI_DEBUG("wm:pm", "remove the oldest area data for adding requested role");
384 area_list.erase(area_list.begin());
390 for (pm::AreaState area_state : prv_layout_state.area_list) {
391 HMI_DEBUG("wm:pm", "area_list name:%s category:%s role:%s",
392 area_state.name.c_str(),
393 area_state.category.c_str(),
394 area_state.role.c_str());
397 ctg_list[ctg] = area_list;
400 if (event == "activate") {
401 // First, update area for requested role
402 bool updated = false;
403 for (pm::AreaState &as : crr_layout_state.area_list) {
404 if ((as.category == category) && (as.name == area)) {
411 // If NOT updated: there is not requested area in new layout,
412 // requested role is used later.
414 pm::AreaState area_state;
415 area_state.name = area;
416 area_state.category = category;
417 area_state.role = role;
418 ctg_list[category].push_back(area_state);
423 for (pm::AreaState &as : crr_layout_state.area_list) {
425 // This conditional expression is useful in only activate
426 if (as.role != role) {
427 as.role = ctg_list[as.category].begin()->role;
428 ctg_list[as.category].erase(ctg_list[as.category].begin());
433 // Update current layout of this layer
434 pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
439 for (auto itr : pm::g_crr_layers) {
440 pm::LayerState ls = itr.second;
441 HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
442 HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
444 for (pm::AreaState as : ls.layout_state.area_list) {
445 HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
446 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
447 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
450 #endif // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
452 json_object* json_out = json_object_new_object();
456 // "parking_brake": {
457 // "is_changed": <bool>,
458 // "state": <const char*>
460 addStateToJson("parking_brake",
461 crr_state.parking_brake.is_changed,
462 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
466 // "is_changed": <bool>,
467 // "state": <const char*>
469 addStateToJson("accel_pedal",
470 crr_state.accel_pedal.is_changed,
471 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
474 // "lightstatus_brake": {
475 // "is_changed": <bool>,
476 // "state": <const char*>
478 addStateToJson("lightstatus_brake",
479 crr_state.lightstatus_brake.is_changed,
480 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
484 // "is_changed": <bool>,
485 // "state": <const char*>
487 addStateToJson("car",
488 crr_state.car.is_changed,
489 stm::gStmCarStateNo2Name[crr_state.car.state],
493 // "is_changed": <bool>,
494 // "state": <const char*>
496 addStateToJson("lamp",
497 crr_state.lamp.is_changed,
498 stm::gStmLampStateNo2Name[crr_state.lamp.state],
501 // "restriction_mode": {
502 // "is_changed": <bool>,
503 // "state": <const char*>
505 addStateToJson("restriction_mode",
506 crr_state.restriction_mode.is_changed,
507 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
511 json_object* json_layer = json_object_new_array();
512 json_object* json_tmp;
516 // "is_changed": <bool>,
517 // "state": <const char*>
522 json_tmp = json_object_new_object();
523 addStateToJson("homescreen",
524 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
525 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state],
527 json_object_array_add(json_layer, json_tmp);
531 // "is_changed": <bool>,
532 // "state": <const char*>
535 json_tmp = json_object_new_object();
536 addStateToJson("apps",
537 crr_state.layer[stm::gStmLayerNoApps].is_changed,
538 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state],
540 json_object_array_add(json_layer, json_tmp);
544 // "is_changed": <bool>,
545 // "state": <const char*>
548 json_tmp = json_object_new_object();
549 addStateToJson("restriction",
550 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
551 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state],
553 json_object_array_add(json_layer, json_tmp);
557 // "is_changed": <bool>,
558 // "state": <const char*>
561 json_tmp = json_object_new_object();
562 addStateToJson("on_screen",
563 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
564 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state],
566 json_object_array_add(json_layer, json_tmp);
568 // Add json array of layer
569 json_object_object_add(json_out, "layers", json_layer);
571 // Notify state is changed
572 if (nullptr != pm::callback.onStateTransitioned) {
573 pm::callback.onStateTransitioned(json_out);
576 if (crr_state.car.is_changed) {
577 if (stm::gStmCarStateNoRun == crr_state.car.state) {
578 // Set delay event(restriction mode on)
579 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, nullptr);
581 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
582 // Set event(restriction mode off)
583 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, nullptr);
585 // Stop timer for restriction on event
586 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
587 != pm::event_source_list.end()) {
588 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
589 sd_event_source *event_source
590 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
591 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
593 HMI_ERROR("wm:pm", "Failed to stop timer");
599 // Release json_object
600 json_object_put(json_out);
605 // Destroy sd_event_source object
606 sd_event_source_unref(source);
608 // Remove event source from list
609 if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
610 pm::event_source_list.erase(event_data);
613 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
617 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
618 int ret = checkPolicy(source, data);
622 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role)
624 HMI_DEBUG("wm:pm", "Call");
625 HMI_DEBUG("wm:pm", "event:0x%x", event);
628 if (nullptr == role) {
629 pm::g_event_info_list[event] = std::string("");
632 pm::g_event_info_list[event] = std::string(role);
636 int ret = sd_event_add_defer(pm::event_loop, NULL,
637 &checkPolicy, new int(event));
639 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
640 pm::g_event_info_list.erase(event);
646 struct timespec time_spec;
647 clock_gettime(CLOCK_MONOTONIC, &time_spec);
649 // Calculate timer fired time
650 uint64_t usec = (time_spec.tv_sec * 1000000)
651 + (time_spec.tv_nsec / 1000)
655 struct sd_event_source* event_source;
656 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
657 &timerEvent, new int(event));
659 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
660 pm::g_event_info_list.erase(event);
664 // Store event source
665 pm::event_source_list[event] = event_source;
671 void PolicyManager::registerCallback(CallbackTable callback) {
672 pm::callback.onStateTransitioned = callback.onStateTransitioned;
673 pm::callback.onError = callback.onError;
676 int PolicyManager::inputEvent(json_object* json_in) {
677 HMI_DEBUG("wm:pm", "Call");
680 if (nullptr == json_in) {
681 HMI_ERROR("wm:pm", "Argument is NULL!!");
685 // Get event from json_object
686 const char* event = this->getStringFromJson(json_in, "event");
688 if (nullptr != event) {
689 // Convert name to number
690 event_no = this->eventname2no_[event];
691 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
694 // Get role from json_object
695 const char* role = this->getStringFromJson(json_in, "role");
697 if (nullptr != role) {
698 HMI_DEBUG("wm:pm", "role(%s)", role);
700 // Convert role to category
701 const char* category = this->role2category_[role].c_str();
702 if (0 == strcmp("", category)) {
703 HMI_ERROR("wm:pm", "Error!!");
706 HMI_DEBUG("wm:pm", "category(%s)", category);
708 // Convert name to number
709 category_no = categoryname2no_[category];
710 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
713 // Get areat from json_object
714 const char* area = this->getStringFromJson(json_in, "area");
716 if (nullptr != area) {
717 // Convert name to number
718 area_no = areaname2no_[area];
719 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
723 checkPolicyEntry((event_no | category_no | area_no), 0, role);
728 std::string PolicyManager::roleToCategory(const char* role) {
729 return this->role2category_[role];
732 extern const char* kDefaultRoleDb;
733 int PolicyManager::loadRoleDb() {
734 HMI_DEBUG("wm:pm", "Call");
736 std::string file_name;
738 // Get afm application installed dir
739 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
740 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
742 if (!afm_app_install_dir) {
743 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
746 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
750 json_object* json_obj;
751 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
753 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
754 json_obj = json_tokener_parse(kDefaultRoleDb);
756 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
758 json_object* json_roles;
759 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
760 HMI_ERROR("wm:pm", "Parse Error!!");
764 int len = json_object_array_length(json_roles);
765 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
766 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
768 json_object* json_tmp;
769 const char* category;
772 for (int i=0; i<len; i++) {
773 json_tmp = json_object_array_get_idx(json_roles, i);
775 category = this->getStringFromJson(json_tmp, "category");
776 roles = this->getStringFromJson(json_tmp, "role");
777 areas = this->getStringFromJson(json_tmp, "area");
779 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
780 HMI_ERROR("wm:pm", "Parse Error!!");
784 // Parse roles by '|'
785 std::vector<std::string> vct_roles;
786 vct_roles = this->parseString(std::string(roles), '|');
788 // Parse areas by '|'
789 std::vector<std::string> vct_areas;
790 vct_areas = this->parseString(std::string(areas), '|');
792 // Set role, category, default area
793 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
794 // Delete space from role and area name
795 std::string role = this->deleteSpace(*itr);
796 std::string area = this->deleteSpace(vct_areas[0]);
798 this->role2category_[role] = std::string(category);
799 this->role2defaultarea_[role] = area;
802 this->category2role_[std::string(category)] = std::string(roles);
806 HMI_DEBUG("wm:pm", "Check role2category_");
807 for (auto& x:this->role2category_){
808 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
811 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
812 for (auto& x:this->role2defaultarea_){
813 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
816 HMI_DEBUG("wm:pm", "Check category2role_");
817 for (auto& x:this->category2role_){
818 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
824 extern const char* kDefaultLayoutDb;
825 int PolicyManager::loadLayoutDb() {
826 HMI_DEBUG("wm:lm", "Call");
828 // Get afm application installed dir
829 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
830 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
832 std::string file_name;
833 if (!afm_app_install_dir) {
834 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
837 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
841 json_object* json_obj;
842 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
844 HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
845 json_obj = json_tokener_parse(kDefaultLayoutDb);
847 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
850 HMI_DEBUG("wm:pm", "Perse layouts");
851 json_object* json_cfg;
852 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
853 HMI_ERROR("wm:pm", "Parse Error!!");
857 int len = json_object_array_length(json_cfg);
858 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
859 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
863 const char* category;
864 for (int i=0; i<len; i++) {
865 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
867 layout = this->getStringFromJson(json_tmp, "name");
868 if (nullptr == layout) {
869 HMI_ERROR("wm:pm", "Parse Error!!");
872 HMI_DEBUG("wm:pm", "> layout:%s", layout);
874 json_object* json_area_array;
875 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
876 HMI_ERROR("wm:pm", "Parse Error!!");
880 int len_area = json_object_array_length(json_area_array);
881 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
882 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
884 pm::LayoutState layout_state;
885 pm::AreaState area_state;
886 std::map<std::string, int> category_num;
887 for (int ctg_no = stm::gStmCategoryNoMin;
888 ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
889 const char* ctg_name = stm::gStmCategoryName[ctg_no];
890 category_num[ctg_name] = 0;
893 for (int j=0; j<len_area; j++) {
894 json_object* json_area = json_object_array_get_idx(json_area_array, j);
897 const char* area = this->getStringFromJson(json_area, "name");
898 if (nullptr == area) {
899 HMI_ERROR("wm:pm", "Parse Error!!");
902 area_state.name = std::string(area);
903 HMI_DEBUG("wm:pm", ">> area:%s", area);
905 // Get app attribute of the area
906 category = this->getStringFromJson(json_area, "category");
907 if (nullptr == category) {
908 HMI_ERROR("wm:pm", "Parse Error!!");
911 area_state.category = std::string(category);
912 category_num[category]++;
913 HMI_DEBUG("wm:pm", ">>> category:%s", category);
915 role = this->getStringFromJson(json_area, "role");
916 if (nullptr != role) {
917 // Role is NOT essential here
918 area_state.role = std::string(role);
919 HMI_DEBUG("wm:pm", ">>> role:%s", role);
922 layout_state.area_list.push_back(area_state);
926 layout_state.name = layout;
927 layout_state.category_num = category_num;
928 pm::g_default_layouts[layout] = layout_state;
932 for(auto itr_layout = pm::g_default_layouts.begin();
933 itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
934 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
936 for (auto itr_area = itr_layout->second.area_list.begin();
937 itr_area != itr_layout->second.area_list.end(); ++itr_area) {
938 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
939 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
940 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
942 for (auto itr_role = itr_area->second.begin();
943 itr_role != itr_area->second.end(); ++itr_role) {
944 HMI_DEBUG("wm:pm", ">>> >>> >>> attribute:%s, name:%s",
945 itr_role->first.c_str(), itr_role->second.c_str());
951 // Release json_object
952 json_object_put(json_obj);
958 // This function will be removed because json_helper has same function.
959 // json_helper should be library.
960 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
961 if ((nullptr == obj) || (nullptr == key)) {
962 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
967 if (!json_object_object_get_ex(obj, key, &tmp)) {
968 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
972 return json_object_get_string(tmp);
976 // This function will be removed because json_helper has same function.
977 // json_helper should be library.
978 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
979 const int input_size = 128;
982 if ((nullptr == file) || (nullptr == obj)) {
983 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
987 HMI_DEBUG("wm:jh", "Input file: %s", file);
990 FILE *fp = fopen(file, "rb");
992 HMI_ERROR("wm:jh", "Could not open file");
997 struct json_tokener *tokener = json_tokener_new();
998 enum json_tokener_error json_error;
999 char buffer[input_size];
1002 size_t len = fread(buffer, sizeof(char), input_size, fp);
1003 *obj = json_tokener_parse_ex(tokener, buffer, len);
1004 if (nullptr != *obj) {
1005 HMI_DEBUG("wm:jh", "File input is success");
1010 json_error = json_tokener_get_error(tokener);
1011 if ((json_tokener_continue != json_error)
1012 || (input_size > len)) {
1013 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1014 (input_size * block_cnt), json_tokener_error_desc(json_error));
1015 HMI_ERROR("wm:jh", "\n%s", buffer);
1025 // Free json_tokener
1026 json_tokener_free(tokener);
1031 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1032 // Parse string by delimiter
1033 std::vector<std::string> vct;
1034 std::stringstream ss{str};
1036 while (std::getline(ss, buf, delimiter)) {
1044 std::string PolicyManager::deleteSpace(std::string str) {
1045 std::string ret = str;
1047 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1053 const char* kDefaultRoleDb = "{ \
1056 \"category\": \"homescreen\", \
1057 \"role\": \"homescreen\", \
1058 \"area\": \"full\", \
1061 \"category\": \"map\", \
1062 \"role\": \"map\", \
1063 \"area\": \"full | normal | split.main\", \
1066 \"category\": \"general\", \
1067 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1068 \"area\": \"normal\", \
1071 \"category\": \"phone\", \
1072 \"role\": \"phone\", \
1073 \"area\": \"normal\", \
1076 \"category\": \"splitable\", \
1077 \"role\": \"splitable1 | splitable2\", \
1078 \"area\": \"normal | split.main | split.sub\", \
1081 \"category\": \"popup\", \
1082 \"role\": \"popup\", \
1083 \"area\": \"on_screen\", \
1086 \"category\": \"system_alert\", \
1087 \"role\": \"system_alert\", \
1088 \"area\": \"on_screen\", \
1091 \"category\": \"tbt\", \
1092 \"role\": \"tbt\", \
1093 \"area\": \"hud\", \
1099 const char* kDefaultLayoutDb = "{ \
1103 \"layer\": \"on_screen\", \
1106 \"name\": \"pop_up\", \
1107 \"role\": \"incomming_call\" \
1113 \"layer\": \"on_screen\", \
1116 \"name\": \"system_alert\", \
1117 \"role\": \"system_alert\" \
1123 \"layer\": \"apps\", \
1126 \"name\": \"normal\", \
1133 \"layer\": \"apps\", \
1136 \"name\": \"split.main\", \
1140 \"name\": \"split.sub\", \
1141 \"category\": \"hvac\" \
1147 \"layer\": \"apps\", \
1150 \"name\": \"full\", \
1157 \"layer\": \"apps\", \
1160 \"name\": \"normal\", \
1161 \"category\": \"splitable\" \
1167 \"layer\": \"apps\", \
1170 \"name\": \"split.main\", \
1171 \"category\": \"splitable\" \
1174 \"name\": \"split.sub\", \
1175 \"category\": \"splitable\" \
1181 \"layer\": \"apps\", \
1184 \"name\": \"normal\", \
1185 \"category\": \"general\" \
1191 \"layer\": \"homescreen\", \
1194 \"name\": \"full\", \
1195 \"role\": \"homescreen\" \
1202 \"name\": \"normal\", \
1211 \"name\": \"split.main\", \
1220 \"name\": \"split.sub\", \
1229 \"name\": \"full\", \
1238 \"name\": \"pop_up\", \
1247 \"name\": \"system_alert\", \