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 std::vector<AreaState> AreaList;
43 typedef struct LayoutState {
45 std::map<std::string, int> category_num;
46 // int category_num[stm::gStmCategoryNoNum];
48 std::map<std::string, std::vector<std::string>> role_history;
51 typedef struct LayerState {
53 LayoutState layout_state;
56 struct sd_event* event_loop;
57 std::map<int, struct sd_event_source*> event_source_list;
58 std::map<int, std::string> g_event_info_list;
59 PolicyManager::CallbackTable callback;
61 std::unordered_map<std::string, LayerState> g_prv_layers;
62 std::unordered_map<std::string, LayerState> g_crr_layers;
63 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
64 std::unordered_map<std::string, LayoutState> g_default_layouts;
68 PolicyManager::PolicyManager() :
76 HMI_DEBUG("wm:pm", "Call");
79 int PolicyManager::initialize() {
80 HMI_DEBUG("wm:pm", "Call");
85 for (unsigned int i=0; i<STM_NUM_EVT; i++) {
86 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
87 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
90 for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
91 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
92 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
95 for (unsigned int i=0; i<STM_NUM_ARA; i++) {
96 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
97 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
101 ret = this->loadRoleDb();
103 HMI_ERROR("wm:pm", "Load role.db Error!!");
108 ret = this->loadLayoutDb();
110 HMI_ERROR("wm:pm", "Load layout.db Error!!");
114 // Initialize current/previous state of layers
115 pm::AreaState init_area;
116 pm::LayoutState init_layout;
117 init_area.name = "none";
118 init_area.category = "none";
119 init_area.role = "none";
120 init_layout.area_list.push_back(init_area);
122 for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
123 const char* layer_name = stm::gStmLayerName[i];
124 pm::g_crr_layers[layer_name].name = layer_name;
125 pm::g_crr_layers[layer_name].layout_state = init_layout;
128 pm::g_prv_layers = pm::g_crr_layers;
130 // Initialize StateTransitioner
131 stm::stmInitialize();
133 // Initialize sd_event loop
134 ret = this->initializeSdEventLoop();
136 HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
143 int PolicyManager::initializeSdEventLoop() {
144 // Get default event loop object
145 int ret = sd_event_new(&(pm::event_loop));
147 HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
151 // Create thread for sd_event and detach
152 std::thread sd_event_loop([this]() {
154 sd_event_run(pm::event_loop, 1000);
157 sd_event_loop.detach();
162 static void addStateToJson(
163 const char* key, int is_changed, const char* state, json_object** json_out) {
164 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
165 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
169 json_object* json_obj = json_object_new_object();
170 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
172 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
173 json_object_object_add(json_obj, "state", json_object_new_string(state));
175 json_object_object_add(*json_out, key, json_obj);
178 static void addStateToJson(const char* layer_name, unsigned int changed,
179 pm::AreaList area_list, json_object** json_out) {
180 if ((nullptr == layer_name) || (1 < changed) || (nullptr == json_out)) {
181 HMI_ERROR("wm:pm", "Invalid argument!!!");
185 json_object* json_areas = json_object_new_array();
186 json_object* json_tmp;
187 for (pm::AreaState as : area_list) {
188 json_tmp = json_object_new_object();
189 json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
190 json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
191 json_object_array_add(json_areas, json_tmp);
194 json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
195 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
196 json_object_object_add(*json_out, "areas", json_areas);
200 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role);
201 static int checkPolicy(sd_event_source *source, void *data) {
202 HMI_DEBUG("wm:pm", "Call");
203 HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
205 int event_data = *((int*)data);
207 int event_no, category_no, area_no;
208 event_no = (event_data & STM_MSK_EVT_NO) - 1;
209 category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
210 area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
211 HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
212 stm::gStmEventName[event_no],
213 stm::gStmCategoryName[category_no],
214 stm::gStmAreaName[area_no]);
217 stm::stm_state_t crr_state;
218 int ret = stm::stmTransitionState(event_data, &crr_state);
220 HMI_ERROR("wm:pm", "Error!!");
224 HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
225 crr_state.parking_brake.is_changed,
226 crr_state.parking_brake.state,
227 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
228 HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
229 crr_state.accel_pedal.is_changed,
230 crr_state.accel_pedal.state,
231 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
232 HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
233 crr_state.lightstatus_brake.is_changed,
234 crr_state.lightstatus_brake.state,
235 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
236 HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
237 crr_state.car.is_changed,
239 stm::gStmCarStateNo2Name[crr_state.car.state]);
240 HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
241 crr_state.lamp.is_changed,
242 crr_state.lamp.state,
243 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
244 HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
245 crr_state.restriction_mode.is_changed,
246 crr_state.restriction_mode.state,
247 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
248 HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
249 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
250 crr_state.layer[stm::gStmLayerNoHomescreen].state,
251 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
252 HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
253 crr_state.layer[stm::gStmLayerNoApps].is_changed,
254 crr_state.layer[stm::gStmLayerNoApps].state,
255 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
256 HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
257 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
258 crr_state.layer[stm::gStmLayerNoRestriction].state,
259 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
260 HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
261 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
262 crr_state.layer[stm::gStmLayerNoOnScreen].state,
263 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
265 // Store previous layers
266 pm::g_prv_layers = pm::g_crr_layers;
268 std::string req_role = pm::g_event_info_list[event_data];
269 std::string req_evt = std::string(stm::gStmEventName[event_no]);
270 std::string req_ctg = std::string(stm::gStmCategoryName[category_no]);
271 std::string req_area = std::string(stm::gStmAreaName[area_no]);
272 HMI_DEBUG("wm:pm", "REQ: event:%s role%s category:%s area:%s",
273 req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
276 for (int layer_no = stm::gStmLayerNoMin;
277 layer_no <= stm::gStmLayerNoMax; layer_no++) {
278 const char* layer_name = stm::gStmLayerName[layer_no];
279 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
282 // If restriction mode is changed off -> on,
283 // store current state for state of restriction mode off
284 if ((crr_state.restriction_mode.is_changed)
285 && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
286 HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
287 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
290 // If car state is changed car_stop -> car_run,
291 // store current state for state of car stop
292 if ((crr_state.car.is_changed)
293 && (stm::gStmCarStateNoRun == crr_state.car.state)) {
294 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
295 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
300 // This layer is changed?
301 if (crr_state.layer[layer_no].is_changed) {
302 // Get previous layout name of this layer
303 pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
304 std::string prv_layout_name = prv_layout_state.name;
306 // Get current layout name of this layer
307 int crr_layout_state_no = crr_state.layer[layer_no].state;
308 std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
310 pm::LayoutState crr_layout_state;
312 if ((crr_state.restriction_mode.is_changed)
313 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
314 // If restriction mode is changed on -> off,
315 // restore state of restriction mode off
316 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
317 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
319 if ((crr_state.car.is_changed)
320 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
321 // If car state is changed car_run -> car_stop,
322 // restore state of car stop
323 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
324 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
328 // Copy previous layout state for current
329 crr_layout_state = prv_layout_state;
331 if (prv_layout_name == crr_layout_name) {
332 HMI_DEBUG("wm:lm", "Previous layout is same with current");
335 // If previous layout is NOT same with current,
336 // current areas is set with default value
337 HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
338 crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
339 crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
340 crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
343 // Create candidate list
344 std::map<std::string, pm::AreaList> cand_list;
345 for (int ctg_no=stm::gStmCategoryNoMin;
346 ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
347 const char* ctg = stm::gStmCategoryName[ctg_no];
348 HMI_DEBUG("wm:pm", "ctg:%s", ctg);
350 // Create candidate list for category from the previous displayed categories
351 pm::AreaList tmp_cand_list;
352 for (pm::AreaState area_state : prv_layout_state.area_list) {
353 if (std::string(ctg) == area_state.category) {
354 // If there is the category which is same with new category in previous layout,
356 HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
357 area_state.category.c_str(), area_state.role.c_str());
358 tmp_cand_list.push_back(area_state);
362 int candidate_num = prv_layout_state.category_num[ctg];
363 int blank_num = crr_layout_state.category_num[ctg];
364 HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
366 // If requested event is "activate"
367 // and there are requested category and area,
368 // update area with requested role in current layout.
369 bool request_for_this_layer = false;
370 bool updated = false;
371 if ((ctg == req_ctg) && ("activate" == req_evt) ) {
372 HMI_DEBUG("wm:pm", "requested event is activate");
373 for (pm::AreaState &as : crr_layout_state.area_list) {
374 if (as.category == req_ctg) {
375 request_for_this_layer = true;
377 if (as.name == req_area) {
378 HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
379 as.name.c_str(), as.category.c_str(), as.role.c_str());
388 // If NOT updated: there is not requested area in new layout,
389 // so push requested role to candidate list
390 if (request_for_this_layer && (!updated)) {
391 HMI_DEBUG("wm:pm", "Push request to candidate list");
392 pm::AreaState area_state;
393 area_state.name = req_area;
394 area_state.category = req_ctg;
395 area_state.role = req_role;
396 tmp_cand_list.push_back(area_state);
400 // Compare number of candidate/blank,
401 // And remove role in order of the oldest as necessary
402 if (candidate_num < blank_num) {
403 // Refer history stack
404 // and add to the top of tmp_cand_list in order to the newest
405 while (candidate_num != blank_num) {
406 pm::AreaState area_state;
407 area_state.name = "";
408 area_state.category = ctg;
409 if (0 != crr_layout_state.role_history[ctg].size()) {
410 HMI_ERROR("wm:pm", "Use role in history stack:%s",
411 crr_layout_state.role_history[ctg].back().c_str());
412 area_state.role = crr_layout_state.role_history[ctg].back();
413 crr_layout_state.role_history[ctg].pop_back();
416 HMI_ERROR("wm:pm", "There is no role in history stack!!");
417 area_state.role = "";
419 tmp_cand_list.push_back(area_state);
423 else if (candidate_num > blank_num) {
424 HMI_DEBUG("wm:pm", "candidate_num > blank_num");
426 // Remove the oldest role from candidate list
427 while (candidate_num != blank_num) {
428 std::string removed_role = tmp_cand_list.begin()->role;
429 HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
430 removed_role.c_str());
431 tmp_cand_list.erase(tmp_cand_list.begin());
434 // Push removed data to history stack
435 crr_layout_state.role_history[ctg].push_back(removed_role);
438 else { // (candidate_num == blank_num)
442 cand_list[ctg] = tmp_cand_list;
446 for (pm::AreaState &as : crr_layout_state.area_list) {
447 HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
448 as.name.c_str(), as.category.c_str());
450 HMI_DEBUG("wm:pm", "Update this area with role:%s",
451 cand_list[as.category].begin()->role.c_str());
452 as.role = cand_list[as.category].begin()->role;
453 cand_list[as.category].erase(cand_list[as.category].begin());
457 // Update current layout of this layer
458 pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
463 for (auto itr : pm::g_crr_layers) {
464 pm::LayerState ls = itr.second;
465 HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
466 HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
468 for (pm::AreaState as : ls.layout_state.area_list) {
469 HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
470 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
471 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
475 json_object* json_out = json_object_new_object();
479 // "parking_brake": {
480 // "is_changed": <bool>,
481 // "state": <const char*>
483 addStateToJson("parking_brake",
484 crr_state.parking_brake.is_changed,
485 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
489 // "is_changed": <bool>,
490 // "state": <const char*>
492 addStateToJson("accel_pedal",
493 crr_state.accel_pedal.is_changed,
494 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
497 // "lightstatus_brake": {
498 // "is_changed": <bool>,
499 // "state": <const char*>
501 addStateToJson("lightstatus_brake",
502 crr_state.lightstatus_brake.is_changed,
503 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
507 // "is_changed": <bool>,
508 // "state": <const char*>
510 addStateToJson("car",
511 crr_state.car.is_changed,
512 stm::gStmCarStateNo2Name[crr_state.car.state],
516 // "is_changed": <bool>,
517 // "state": <const char*>
519 addStateToJson("lamp",
520 crr_state.lamp.is_changed,
521 stm::gStmLampStateNo2Name[crr_state.lamp.state],
524 // "restriction_mode": {
525 // "is_changed": <bool>,
526 // "state": <const char*>
528 addStateToJson("restriction_mode",
529 crr_state.restriction_mode.is_changed,
530 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
533 // Create layout information
538 // "changed": <bool>,
541 // "name":<const char*>,
542 // "role":<const char*>
549 json_object* json_layer = json_object_new_array();
550 json_object* json_tmp;
551 for (int layer_no = stm::gStmLayerNoMin;
552 layer_no <= stm::gStmLayerNoMax; layer_no++) {
553 const char* layer_name = stm::gStmLayerName[layer_no];
554 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
556 json_tmp = json_object_new_object();
557 addStateToJson(layer_name,
558 crr_state.layer[layer_no].is_changed,
559 pm::g_crr_layers[layer_name].layout_state.area_list,
561 json_object_array_add(json_layer, json_tmp);
564 // Add json array of layer
565 json_object_object_add(json_out, "layers", json_layer);
567 // Notify state is changed
568 if (nullptr != pm::callback.onStateTransitioned) {
569 pm::callback.onStateTransitioned(json_out);
572 if (crr_state.car.is_changed) {
573 if (stm::gStmCarStateNoRun == crr_state.car.state) {
574 // Set delay event(restriction mode on)
575 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, nullptr);
577 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
578 // Set event(restriction mode off)
579 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, nullptr);
581 // Stop timer for restriction on event
582 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
583 != pm::event_source_list.end()) {
584 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
585 sd_event_source *event_source
586 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
587 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
589 HMI_ERROR("wm:pm", "Failed to stop timer");
595 // Release json_object
596 json_object_put(json_out);
601 // Destroy sd_event_source object
602 sd_event_source_unref(source);
604 // Remove event source from list
605 if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
606 pm::event_source_list.erase(event_data);
609 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
613 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
614 int ret = checkPolicy(source, data);
618 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role)
620 HMI_DEBUG("wm:pm", "Call");
621 HMI_DEBUG("wm:pm", "event:0x%x", event);
624 if (nullptr == role) {
625 pm::g_event_info_list[event] = std::string("");
628 pm::g_event_info_list[event] = std::string(role);
632 int ret = sd_event_add_defer(pm::event_loop, NULL,
633 &checkPolicy, new int(event));
635 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
636 pm::g_event_info_list.erase(event);
642 struct timespec time_spec;
643 clock_gettime(CLOCK_MONOTONIC, &time_spec);
645 // Calculate timer fired time
646 uint64_t usec = (time_spec.tv_sec * 1000000)
647 + (time_spec.tv_nsec / 1000)
651 struct sd_event_source* event_source;
652 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
653 &timerEvent, new int(event));
655 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
656 pm::g_event_info_list.erase(event);
660 // Store event source
661 pm::event_source_list[event] = event_source;
667 void PolicyManager::registerCallback(CallbackTable callback) {
668 pm::callback.onStateTransitioned = callback.onStateTransitioned;
669 pm::callback.onError = callback.onError;
672 int PolicyManager::inputEvent(json_object* json_in) {
673 HMI_DEBUG("wm:pm", "Call");
676 if (nullptr == json_in) {
677 HMI_ERROR("wm:pm", "Argument is NULL!!");
681 // Get event from json_object
682 const char* event = this->getStringFromJson(json_in, "event");
684 if (nullptr != event) {
685 // Convert name to number
686 event_no = this->eventname2no_[event];
687 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
690 // Get role from json_object
691 const char* role = this->getStringFromJson(json_in, "role");
693 if (nullptr != role) {
694 HMI_DEBUG("wm:pm", "role(%s)", role);
696 // Convert role to category
697 const char* category = this->role2category_[role].c_str();
698 if (0 == strcmp("", category)) {
699 HMI_ERROR("wm:pm", "Error!!");
702 HMI_DEBUG("wm:pm", "category(%s)", category);
704 // Convert name to number
705 category_no = categoryname2no_[category];
706 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
709 // Get areat from json_object
710 const char* area = this->getStringFromJson(json_in, "area");
712 if (nullptr != area) {
713 // Convert name to number
714 area_no = areaname2no_[area];
715 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
719 checkPolicyEntry((event_no | category_no | area_no), 0, role);
724 extern const char* kDefaultRoleDb;
725 int PolicyManager::loadRoleDb() {
726 HMI_DEBUG("wm:pm", "Call");
728 std::string file_name;
730 // Get afm application installed dir
731 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
732 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
734 if (!afm_app_install_dir) {
735 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
738 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
742 json_object* json_obj;
743 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
745 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
746 json_obj = json_tokener_parse(kDefaultRoleDb);
748 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
750 json_object* json_roles;
751 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
752 HMI_ERROR("wm:pm", "Parse Error!!");
756 int len = json_object_array_length(json_roles);
757 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
758 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
760 json_object* json_tmp;
761 const char* category;
764 for (int i=0; i<len; i++) {
765 json_tmp = json_object_array_get_idx(json_roles, i);
767 category = this->getStringFromJson(json_tmp, "category");
768 roles = this->getStringFromJson(json_tmp, "role");
769 areas = this->getStringFromJson(json_tmp, "area");
771 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
772 HMI_ERROR("wm:pm", "Parse Error!!");
776 // Parse roles by '|'
777 std::vector<std::string> vct_roles;
778 vct_roles = this->parseString(std::string(roles), '|');
780 // Parse areas by '|'
781 std::vector<std::string> vct_areas;
782 vct_areas = this->parseString(std::string(areas), '|');
784 // Set role, category, default area
785 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
786 // Delete space from role and area name
787 std::string role = this->deleteSpace(*itr);
788 std::string area = this->deleteSpace(vct_areas[0]);
790 this->role2category_[role] = std::string(category);
791 this->role2defaultarea_[role] = area;
794 this->category2role_[std::string(category)] = std::string(roles);
798 HMI_DEBUG("wm:pm", "Check role2category_");
799 for (auto& x:this->role2category_){
800 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
803 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
804 for (auto& x:this->role2defaultarea_){
805 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
808 HMI_DEBUG("wm:pm", "Check category2role_");
809 for (auto& x:this->category2role_){
810 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
816 extern const char* kDefaultLayoutDb;
817 int PolicyManager::loadLayoutDb() {
818 HMI_DEBUG("wm:lm", "Call");
820 // Get afm application installed dir
821 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
822 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
824 std::string file_name;
825 if (!afm_app_install_dir) {
826 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
829 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
833 json_object* json_obj;
834 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
836 HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
837 json_obj = json_tokener_parse(kDefaultLayoutDb);
839 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
842 HMI_DEBUG("wm:pm", "Perse layouts");
843 json_object* json_cfg;
844 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
845 HMI_ERROR("wm:pm", "Parse Error!!");
849 int len = json_object_array_length(json_cfg);
850 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
851 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
855 const char* category;
856 for (int i=0; i<len; i++) {
857 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
859 layout = this->getStringFromJson(json_tmp, "name");
860 if (nullptr == layout) {
861 HMI_ERROR("wm:pm", "Parse Error!!");
864 HMI_DEBUG("wm:pm", "> layout:%s", layout);
866 json_object* json_area_array;
867 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
868 HMI_ERROR("wm:pm", "Parse Error!!");
872 int len_area = json_object_array_length(json_area_array);
873 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
874 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
876 pm::LayoutState layout_state;
877 pm::AreaState area_state;
878 std::map<std::string, int> category_num;
879 for (int ctg_no = stm::gStmCategoryNoMin;
880 ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
881 const char* ctg_name = stm::gStmCategoryName[ctg_no];
882 category_num[ctg_name] = 0;
885 for (int j=0; j<len_area; j++) {
886 json_object* json_area = json_object_array_get_idx(json_area_array, j);
889 const char* area = this->getStringFromJson(json_area, "name");
890 if (nullptr == area) {
891 HMI_ERROR("wm:pm", "Parse Error!!");
894 area_state.name = std::string(area);
895 HMI_DEBUG("wm:pm", ">> area:%s", area);
897 // Get app attribute of the area
898 category = this->getStringFromJson(json_area, "category");
899 if (nullptr == category) {
900 HMI_ERROR("wm:pm", "Parse Error!!");
903 area_state.category = std::string(category);
904 category_num[category]++;
905 HMI_DEBUG("wm:pm", ">>> category:%s", category);
907 role = this->getStringFromJson(json_area, "role");
908 if (nullptr != role) {
909 // Role is NOT essential here
910 area_state.role = std::string(role);
913 area_state.role = std::string("");
916 HMI_DEBUG("wm:pm", ">>> role:%s", role);
918 layout_state.area_list.push_back(area_state);
922 layout_state.name = layout;
923 layout_state.category_num = category_num;
924 pm::g_default_layouts[layout] = layout_state;
927 // initialize for none layout
928 pm::LayoutState none_layout_state;
929 memset(&none_layout_state, 0, sizeof(none_layout_state));
930 none_layout_state.name = "none";
931 pm::g_default_layouts["none"] = none_layout_state;
934 for(auto itr_layout = pm::g_default_layouts.begin();
935 itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
936 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
938 for (auto itr_area = itr_layout->second.area_list.begin();
939 itr_area != itr_layout->second.area_list.end(); ++itr_area) {
940 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
941 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
942 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
944 for (auto itr_role = itr_area->second.begin();
945 itr_role != itr_area->second.end(); ++itr_role) {
946 HMI_DEBUG("wm:pm", ">>> >>> >>> attribute:%s, name:%s",
947 itr_role->first.c_str(), itr_role->second.c_str());
953 // Release json_object
954 json_object_put(json_obj);
960 // This function will be removed because json_helper has same function.
961 // json_helper should be library.
962 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
963 if ((nullptr == obj) || (nullptr == key)) {
964 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
969 if (!json_object_object_get_ex(obj, key, &tmp)) {
970 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
974 return json_object_get_string(tmp);
978 // This function will be removed because json_helper has same function.
979 // json_helper should be library.
980 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
981 const int input_size = 128;
984 if ((nullptr == file) || (nullptr == obj)) {
985 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
989 HMI_DEBUG("wm:jh", "Input file: %s", file);
992 FILE *fp = fopen(file, "rb");
994 HMI_ERROR("wm:jh", "Could not open file");
999 struct json_tokener *tokener = json_tokener_new();
1000 enum json_tokener_error json_error;
1001 char buffer[input_size];
1004 size_t len = fread(buffer, sizeof(char), input_size, fp);
1005 *obj = json_tokener_parse_ex(tokener, buffer, len);
1006 if (nullptr != *obj) {
1007 HMI_DEBUG("wm:jh", "File input is success");
1012 json_error = json_tokener_get_error(tokener);
1013 if ((json_tokener_continue != json_error)
1014 || (input_size > len)) {
1015 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1016 (input_size * block_cnt), json_tokener_error_desc(json_error));
1017 HMI_ERROR("wm:jh", "\n%s", buffer);
1027 // Free json_tokener
1028 json_tokener_free(tokener);
1033 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1034 // Parse string by delimiter
1035 std::vector<std::string> vct;
1036 std::stringstream ss{str};
1038 while (std::getline(ss, buf, delimiter)) {
1046 std::string PolicyManager::deleteSpace(std::string str) {
1047 std::string ret = str;
1049 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1055 const char* kDefaultRoleDb = "{ \
1058 \"category\": \"homescreen\", \
1059 \"role\": \"homescreen\", \
1060 \"area\": \"full\", \
1063 \"category\": \"map\", \
1064 \"role\": \"map\", \
1065 \"area\": \"full | normal | split.main\", \
1068 \"category\": \"general\", \
1069 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1070 \"area\": \"normal\", \
1073 \"category\": \"phone\", \
1074 \"role\": \"phone\", \
1075 \"area\": \"normal\", \
1078 \"category\": \"splitable\", \
1079 \"role\": \"splitable1 | splitable2\", \
1080 \"area\": \"normal | split.main | split.sub\", \
1083 \"category\": \"popup\", \
1084 \"role\": \"popup\", \
1085 \"area\": \"on_screen\", \
1088 \"category\": \"system_alert\", \
1089 \"role\": \"system_alert\", \
1090 \"area\": \"on_screen\", \
1093 \"category\": \"tbt\", \
1094 \"role\": \"tbt\", \
1095 \"area\": \"hud\", \
1101 const char* kDefaultLayoutDb = "{ \
1105 \"layer\": \"on_screen\", \
1108 \"name\": \"pop_up\", \
1109 \"role\": \"incomming_call\" \
1115 \"layer\": \"on_screen\", \
1118 \"name\": \"system_alert\", \
1119 \"role\": \"system_alert\" \
1125 \"layer\": \"apps\", \
1128 \"name\": \"normal\", \
1135 \"layer\": \"apps\", \
1138 \"name\": \"split.main\", \
1142 \"name\": \"split.sub\", \
1143 \"category\": \"hvac\" \
1149 \"layer\": \"apps\", \
1152 \"name\": \"full\", \
1159 \"layer\": \"apps\", \
1162 \"name\": \"normal\", \
1163 \"category\": \"splitable\" \
1169 \"layer\": \"apps\", \
1172 \"name\": \"split.main\", \
1173 \"category\": \"splitable\" \
1176 \"name\": \"split.sub\", \
1177 \"category\": \"splitable\" \
1183 \"layer\": \"apps\", \
1186 \"name\": \"normal\", \
1187 \"category\": \"general\" \
1193 \"layer\": \"homescreen\", \
1196 \"name\": \"full\", \
1197 \"role\": \"homescreen\" \
1204 \"name\": \"normal\", \
1213 \"name\": \"split.main\", \
1222 \"name\": \"split.sub\", \
1231 \"name\": \"full\", \
1240 \"name\": \"pop_up\", \
1249 \"name\": \"system_alert\", \