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 #if 1 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
266 // Store previous layers
267 pm::g_prv_layers = pm::g_crr_layers;
269 std::string req_role = pm::g_event_info_list[event_data];
270 std::string req_evt = std::string(stm::gStmEventName[event_no]);
271 std::string req_ctg = std::string(stm::gStmCategoryName[category_no]);
272 std::string req_area = std::string(stm::gStmAreaName[area_no]);
273 HMI_DEBUG("wm:pm", "REQ: event:%s role%s category:%s area:%s",
274 req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
277 for (int layer_no = stm::gStmLayerNoMin;
278 layer_no <= stm::gStmLayerNoMax; layer_no++) {
279 const char* layer_name = stm::gStmLayerName[layer_no];
280 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
283 // If restriction mode is changed off -> on,
284 // store current state for state of restriction mode off
285 if ((crr_state.restriction_mode.is_changed)
286 && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
287 HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
288 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
291 // If car state is changed car_stop -> car_run,
292 // store current state for state of car stop
293 if ((crr_state.car.is_changed)
294 && (stm::gStmCarStateNoRun == crr_state.car.state)) {
295 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
296 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
301 // This layer is changed?
302 if (crr_state.layer[layer_no].is_changed) {
303 // Get previous layout name of this layer
304 pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
305 std::string prv_layout_name = prv_layout_state.name;
307 // Get current layout name of this layer
308 int crr_layout_state_no = crr_state.layer[layer_no].state;
309 std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
311 pm::LayoutState crr_layout_state;
313 if ((crr_state.restriction_mode.is_changed)
314 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
315 // If restriction mode is changed on -> off,
316 // restore state of restriction mode off
317 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
318 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
320 if ((crr_state.car.is_changed)
321 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
322 // If car state is changed car_run -> car_stop,
323 // restore state of car stop
324 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
325 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
329 // Copy previous layout state for current
330 crr_layout_state = prv_layout_state;
332 if (prv_layout_name == crr_layout_name) {
333 HMI_DEBUG("wm:lm", "Previous layout is same with current");
336 // If previous layout is NOT same with current,
337 // current areas is set with default value
338 HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
339 crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
340 crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
341 crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
344 // Create candidate list
345 std::map<std::string, pm::AreaList> cand_list;
346 for (int ctg_no=stm::gStmCategoryNoMin;
347 ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
348 const char* ctg = stm::gStmCategoryName[ctg_no];
349 HMI_DEBUG("wm:pm", "ctg:%s", ctg);
351 // Create candidate list for category from the previous displayed categories
352 pm::AreaList tmp_cand_list;
353 for (pm::AreaState area_state : prv_layout_state.area_list) {
354 if (std::string(ctg) == area_state.category) {
355 // If there is the category which is same with new category in previous layout,
357 HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
358 area_state.category.c_str(), area_state.role.c_str());
359 tmp_cand_list.push_back(area_state);
363 int candidate_num = prv_layout_state.category_num[ctg];
364 int blank_num = crr_layout_state.category_num[ctg];
365 HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
367 // If requested event is "activate"
368 // and there are requested category and area,
369 // update area with requested role in current layout.
370 bool request_for_this_layer = false;
371 bool updated = false;
372 if ((ctg == req_ctg) && ("activate" == req_evt) ) {
373 HMI_DEBUG("wm:pm", "requested event is activate");
374 for (pm::AreaState &as : crr_layout_state.area_list) {
375 if (as.category == req_ctg) {
376 request_for_this_layer = true;
378 if (as.name == req_area) {
379 HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
380 as.name.c_str(), as.category.c_str(), as.role.c_str());
389 // If NOT updated: there is not requested area in new layout,
390 // so push requested role to candidate list
391 if (request_for_this_layer && (!updated)) {
392 HMI_DEBUG("wm:pm", "Push request to candidate list");
393 pm::AreaState area_state;
394 area_state.name = req_area;
395 area_state.category = req_ctg;
396 area_state.role = req_role;
397 tmp_cand_list.push_back(area_state);
401 // Compare number of candidate/blank,
402 // And remove role in order of the oldest as necessary
403 if (candidate_num < blank_num) {
404 // Refer history stack
405 // and add to the top of tmp_cand_list in order to the newest
406 while (candidate_num != blank_num) {
407 pm::AreaState area_state;
408 area_state.name = "";
409 area_state.category = ctg;
410 if (0 != crr_layout_state.role_history[ctg].size()) {
411 HMI_ERROR("wm:pm", "Use role in history stack:%s",
412 crr_layout_state.role_history[ctg].back().c_str());
413 area_state.role = crr_layout_state.role_history[ctg].back();
414 crr_layout_state.role_history[ctg].pop_back();
417 HMI_ERROR("wm:pm", "There is no role in history stack!!");
418 area_state.role = "";
420 tmp_cand_list.push_back(area_state);
424 else if (candidate_num > blank_num) {
425 HMI_DEBUG("wm:pm", "candidate_num > blank_num");
427 // Remove the oldest role from candidate list
428 while (candidate_num != blank_num) {
429 std::string removed_role = tmp_cand_list.begin()->role;
430 HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
431 removed_role.c_str());
432 tmp_cand_list.erase(tmp_cand_list.begin());
435 // Push removed data to history stack
436 crr_layout_state.role_history[ctg].push_back(removed_role);
439 else { // (candidate_num == blank_num)
443 cand_list[ctg] = tmp_cand_list;
447 for (pm::AreaState &as : crr_layout_state.area_list) {
448 HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
449 as.name.c_str(), as.category.c_str());
451 HMI_DEBUG("wm:pm", "Update this area with role:%s",
452 cand_list[as.category].begin()->role.c_str());
453 as.role = cand_list[as.category].begin()->role;
454 cand_list[as.category].erase(cand_list[as.category].begin());
458 // Update current layout of this layer
459 pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
464 for (auto itr : pm::g_crr_layers) {
465 pm::LayerState ls = itr.second;
466 HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
467 HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
469 for (pm::AreaState as : ls.layout_state.area_list) {
470 HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
471 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
472 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
475 #endif // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
477 json_object* json_out = json_object_new_object();
481 // "parking_brake": {
482 // "is_changed": <bool>,
483 // "state": <const char*>
485 addStateToJson("parking_brake",
486 crr_state.parking_brake.is_changed,
487 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
491 // "is_changed": <bool>,
492 // "state": <const char*>
494 addStateToJson("accel_pedal",
495 crr_state.accel_pedal.is_changed,
496 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
499 // "lightstatus_brake": {
500 // "is_changed": <bool>,
501 // "state": <const char*>
503 addStateToJson("lightstatus_brake",
504 crr_state.lightstatus_brake.is_changed,
505 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
509 // "is_changed": <bool>,
510 // "state": <const char*>
512 addStateToJson("car",
513 crr_state.car.is_changed,
514 stm::gStmCarStateNo2Name[crr_state.car.state],
518 // "is_changed": <bool>,
519 // "state": <const char*>
521 addStateToJson("lamp",
522 crr_state.lamp.is_changed,
523 stm::gStmLampStateNo2Name[crr_state.lamp.state],
526 // "restriction_mode": {
527 // "is_changed": <bool>,
528 // "state": <const char*>
530 addStateToJson("restriction_mode",
531 crr_state.restriction_mode.is_changed,
532 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
536 json_object* json_layer = json_object_new_array();
537 json_object* json_tmp;
541 // "changed": <bool>,
544 // "name":<const char*>,
545 // "role":<const char*>
551 json_tmp = json_object_new_object();
552 addStateToJson("homescreen",
553 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
555 pm::g_crr_layers["homescreen"].layout_state.name.c_str(),
557 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state],
560 json_object_array_add(json_layer, json_tmp);
564 // "is_changed": <bool>,
565 // "state": <const char*>
568 json_tmp = json_object_new_object();
569 addStateToJson("apps",
570 crr_state.layer[stm::gStmLayerNoApps].is_changed,
572 pm::g_crr_layers["apps"].layout_state.name.c_str(),
574 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state],
577 json_object_array_add(json_layer, json_tmp);
581 // "is_changed": <bool>,
582 // "state": <const char*>
585 json_tmp = json_object_new_object();
586 addStateToJson("restriction",
587 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
589 pm::g_crr_layers["restriction"].layout_state.name.c_str(),
591 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state],
594 json_object_array_add(json_layer, json_tmp);
598 // "is_changed": <bool>,
599 // "state": <const char*>
602 json_tmp = json_object_new_object();
603 addStateToJson("on_screen",
604 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
606 pm::g_crr_layers["on_screen"].layout_state.name.c_str(),
608 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state],
611 json_object_array_add(json_layer, json_tmp);
613 // Add json array of layer
614 json_object_object_add(json_out, "layers", json_layer);
616 // Notify state is changed
617 if (nullptr != pm::callback.onStateTransitioned) {
618 pm::callback.onStateTransitioned(json_out);
621 if (crr_state.car.is_changed) {
622 if (stm::gStmCarStateNoRun == crr_state.car.state) {
623 // Set delay event(restriction mode on)
624 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, nullptr);
626 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
627 // Set event(restriction mode off)
628 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, nullptr);
630 // Stop timer for restriction on event
631 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
632 != pm::event_source_list.end()) {
633 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
634 sd_event_source *event_source
635 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
636 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
638 HMI_ERROR("wm:pm", "Failed to stop timer");
644 // Release json_object
645 json_object_put(json_out);
650 // Destroy sd_event_source object
651 sd_event_source_unref(source);
653 // Remove event source from list
654 if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
655 pm::event_source_list.erase(event_data);
658 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
662 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
663 int ret = checkPolicy(source, data);
667 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role)
669 HMI_DEBUG("wm:pm", "Call");
670 HMI_DEBUG("wm:pm", "event:0x%x", event);
673 if (nullptr == role) {
674 pm::g_event_info_list[event] = std::string("");
677 pm::g_event_info_list[event] = std::string(role);
681 int ret = sd_event_add_defer(pm::event_loop, NULL,
682 &checkPolicy, new int(event));
684 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
685 pm::g_event_info_list.erase(event);
691 struct timespec time_spec;
692 clock_gettime(CLOCK_MONOTONIC, &time_spec);
694 // Calculate timer fired time
695 uint64_t usec = (time_spec.tv_sec * 1000000)
696 + (time_spec.tv_nsec / 1000)
700 struct sd_event_source* event_source;
701 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
702 &timerEvent, new int(event));
704 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
705 pm::g_event_info_list.erase(event);
709 // Store event source
710 pm::event_source_list[event] = event_source;
716 void PolicyManager::registerCallback(CallbackTable callback) {
717 pm::callback.onStateTransitioned = callback.onStateTransitioned;
718 pm::callback.onError = callback.onError;
721 int PolicyManager::inputEvent(json_object* json_in) {
722 HMI_DEBUG("wm:pm", "Call");
725 if (nullptr == json_in) {
726 HMI_ERROR("wm:pm", "Argument is NULL!!");
730 // Get event from json_object
731 const char* event = this->getStringFromJson(json_in, "event");
733 if (nullptr != event) {
734 // Convert name to number
735 event_no = this->eventname2no_[event];
736 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
739 // Get role from json_object
740 const char* role = this->getStringFromJson(json_in, "role");
742 if (nullptr != role) {
743 HMI_DEBUG("wm:pm", "role(%s)", role);
745 // Convert role to category
746 const char* category = this->role2category_[role].c_str();
747 if (0 == strcmp("", category)) {
748 HMI_ERROR("wm:pm", "Error!!");
751 HMI_DEBUG("wm:pm", "category(%s)", category);
753 // Convert name to number
754 category_no = categoryname2no_[category];
755 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
758 // Get areat from json_object
759 const char* area = this->getStringFromJson(json_in, "area");
761 if (nullptr != area) {
762 // Convert name to number
763 area_no = areaname2no_[area];
764 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
768 checkPolicyEntry((event_no | category_no | area_no), 0, role);
773 std::string PolicyManager::roleToCategory(const char* role) {
774 return this->role2category_[role];
777 extern const char* kDefaultRoleDb;
778 int PolicyManager::loadRoleDb() {
779 HMI_DEBUG("wm:pm", "Call");
781 std::string file_name;
783 // Get afm application installed dir
784 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
785 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
787 if (!afm_app_install_dir) {
788 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
791 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
795 json_object* json_obj;
796 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
798 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
799 json_obj = json_tokener_parse(kDefaultRoleDb);
801 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
803 json_object* json_roles;
804 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
805 HMI_ERROR("wm:pm", "Parse Error!!");
809 int len = json_object_array_length(json_roles);
810 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
811 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
813 json_object* json_tmp;
814 const char* category;
817 for (int i=0; i<len; i++) {
818 json_tmp = json_object_array_get_idx(json_roles, i);
820 category = this->getStringFromJson(json_tmp, "category");
821 roles = this->getStringFromJson(json_tmp, "role");
822 areas = this->getStringFromJson(json_tmp, "area");
824 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
825 HMI_ERROR("wm:pm", "Parse Error!!");
829 // Parse roles by '|'
830 std::vector<std::string> vct_roles;
831 vct_roles = this->parseString(std::string(roles), '|');
833 // Parse areas by '|'
834 std::vector<std::string> vct_areas;
835 vct_areas = this->parseString(std::string(areas), '|');
837 // Set role, category, default area
838 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
839 // Delete space from role and area name
840 std::string role = this->deleteSpace(*itr);
841 std::string area = this->deleteSpace(vct_areas[0]);
843 this->role2category_[role] = std::string(category);
844 this->role2defaultarea_[role] = area;
847 this->category2role_[std::string(category)] = std::string(roles);
851 HMI_DEBUG("wm:pm", "Check role2category_");
852 for (auto& x:this->role2category_){
853 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
856 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
857 for (auto& x:this->role2defaultarea_){
858 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
861 HMI_DEBUG("wm:pm", "Check category2role_");
862 for (auto& x:this->category2role_){
863 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
869 extern const char* kDefaultLayoutDb;
870 int PolicyManager::loadLayoutDb() {
871 HMI_DEBUG("wm:lm", "Call");
873 // Get afm application installed dir
874 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
875 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
877 std::string file_name;
878 if (!afm_app_install_dir) {
879 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
882 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
886 json_object* json_obj;
887 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
889 HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
890 json_obj = json_tokener_parse(kDefaultLayoutDb);
892 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
895 HMI_DEBUG("wm:pm", "Perse layouts");
896 json_object* json_cfg;
897 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
898 HMI_ERROR("wm:pm", "Parse Error!!");
902 int len = json_object_array_length(json_cfg);
903 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
904 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
908 const char* category;
909 for (int i=0; i<len; i++) {
910 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
912 layout = this->getStringFromJson(json_tmp, "name");
913 if (nullptr == layout) {
914 HMI_ERROR("wm:pm", "Parse Error!!");
917 HMI_DEBUG("wm:pm", "> layout:%s", layout);
919 json_object* json_area_array;
920 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
921 HMI_ERROR("wm:pm", "Parse Error!!");
925 int len_area = json_object_array_length(json_area_array);
926 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
927 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
929 pm::LayoutState layout_state;
930 pm::AreaState area_state;
931 std::map<std::string, int> category_num;
932 for (int ctg_no = stm::gStmCategoryNoMin;
933 ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
934 const char* ctg_name = stm::gStmCategoryName[ctg_no];
935 category_num[ctg_name] = 0;
938 for (int j=0; j<len_area; j++) {
939 json_object* json_area = json_object_array_get_idx(json_area_array, j);
942 const char* area = this->getStringFromJson(json_area, "name");
943 if (nullptr == area) {
944 HMI_ERROR("wm:pm", "Parse Error!!");
947 area_state.name = std::string(area);
948 HMI_DEBUG("wm:pm", ">> area:%s", area);
950 // Get app attribute of the area
951 category = this->getStringFromJson(json_area, "category");
952 if (nullptr == category) {
953 HMI_ERROR("wm:pm", "Parse Error!!");
956 area_state.category = std::string(category);
957 category_num[category]++;
958 HMI_DEBUG("wm:pm", ">>> category:%s", category);
960 role = this->getStringFromJson(json_area, "role");
961 if (nullptr != role) {
962 // Role is NOT essential here
963 area_state.role = std::string(role);
966 area_state.role = std::string("");
969 HMI_DEBUG("wm:pm", ">>> role:%s", role);
971 layout_state.area_list.push_back(area_state);
975 layout_state.name = layout;
976 layout_state.category_num = category_num;
977 pm::g_default_layouts[layout] = layout_state;
980 // initialize for none layout
981 pm::LayoutState none_layout_state;
982 memset(&none_layout_state, 0, sizeof(none_layout_state));
983 none_layout_state.name = "none";
984 pm::g_default_layouts["none"] = none_layout_state;
987 for(auto itr_layout = pm::g_default_layouts.begin();
988 itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
989 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
991 for (auto itr_area = itr_layout->second.area_list.begin();
992 itr_area != itr_layout->second.area_list.end(); ++itr_area) {
993 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
994 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
995 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
997 for (auto itr_role = itr_area->second.begin();
998 itr_role != itr_area->second.end(); ++itr_role) {
999 HMI_DEBUG("wm:pm", ">>> >>> >>> attribute:%s, name:%s",
1000 itr_role->first.c_str(), itr_role->second.c_str());
1006 // Release json_object
1007 json_object_put(json_obj);
1013 // This function will be removed because json_helper has same function.
1014 // json_helper should be library.
1015 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
1016 if ((nullptr == obj) || (nullptr == key)) {
1017 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
1022 if (!json_object_object_get_ex(obj, key, &tmp)) {
1023 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1027 return json_object_get_string(tmp);
1031 // This function will be removed because json_helper has same function.
1032 // json_helper should be library.
1033 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
1034 const int input_size = 128;
1037 if ((nullptr == file) || (nullptr == obj)) {
1038 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
1042 HMI_DEBUG("wm:jh", "Input file: %s", file);
1045 FILE *fp = fopen(file, "rb");
1046 if (nullptr == fp) {
1047 HMI_ERROR("wm:jh", "Could not open file");
1052 struct json_tokener *tokener = json_tokener_new();
1053 enum json_tokener_error json_error;
1054 char buffer[input_size];
1057 size_t len = fread(buffer, sizeof(char), input_size, fp);
1058 *obj = json_tokener_parse_ex(tokener, buffer, len);
1059 if (nullptr != *obj) {
1060 HMI_DEBUG("wm:jh", "File input is success");
1065 json_error = json_tokener_get_error(tokener);
1066 if ((json_tokener_continue != json_error)
1067 || (input_size > len)) {
1068 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1069 (input_size * block_cnt), json_tokener_error_desc(json_error));
1070 HMI_ERROR("wm:jh", "\n%s", buffer);
1080 // Free json_tokener
1081 json_tokener_free(tokener);
1086 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1087 // Parse string by delimiter
1088 std::vector<std::string> vct;
1089 std::stringstream ss{str};
1091 while (std::getline(ss, buf, delimiter)) {
1099 std::string PolicyManager::deleteSpace(std::string str) {
1100 std::string ret = str;
1102 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1108 const char* kDefaultRoleDb = "{ \
1111 \"category\": \"homescreen\", \
1112 \"role\": \"homescreen\", \
1113 \"area\": \"full\", \
1116 \"category\": \"map\", \
1117 \"role\": \"map\", \
1118 \"area\": \"full | normal | split.main\", \
1121 \"category\": \"general\", \
1122 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1123 \"area\": \"normal\", \
1126 \"category\": \"phone\", \
1127 \"role\": \"phone\", \
1128 \"area\": \"normal\", \
1131 \"category\": \"splitable\", \
1132 \"role\": \"splitable1 | splitable2\", \
1133 \"area\": \"normal | split.main | split.sub\", \
1136 \"category\": \"popup\", \
1137 \"role\": \"popup\", \
1138 \"area\": \"on_screen\", \
1141 \"category\": \"system_alert\", \
1142 \"role\": \"system_alert\", \
1143 \"area\": \"on_screen\", \
1146 \"category\": \"tbt\", \
1147 \"role\": \"tbt\", \
1148 \"area\": \"hud\", \
1154 const char* kDefaultLayoutDb = "{ \
1158 \"layer\": \"on_screen\", \
1161 \"name\": \"pop_up\", \
1162 \"role\": \"incomming_call\" \
1168 \"layer\": \"on_screen\", \
1171 \"name\": \"system_alert\", \
1172 \"role\": \"system_alert\" \
1178 \"layer\": \"apps\", \
1181 \"name\": \"normal\", \
1188 \"layer\": \"apps\", \
1191 \"name\": \"split.main\", \
1195 \"name\": \"split.sub\", \
1196 \"category\": \"hvac\" \
1202 \"layer\": \"apps\", \
1205 \"name\": \"full\", \
1212 \"layer\": \"apps\", \
1215 \"name\": \"normal\", \
1216 \"category\": \"splitable\" \
1222 \"layer\": \"apps\", \
1225 \"name\": \"split.main\", \
1226 \"category\": \"splitable\" \
1229 \"name\": \"split.sub\", \
1230 \"category\": \"splitable\" \
1236 \"layer\": \"apps\", \
1239 \"name\": \"normal\", \
1240 \"category\": \"general\" \
1246 \"layer\": \"homescreen\", \
1249 \"name\": \"full\", \
1250 \"role\": \"homescreen\" \
1257 \"name\": \"normal\", \
1266 \"name\": \"split.main\", \
1275 \"name\": \"split.sub\", \
1284 \"name\": \"full\", \
1293 \"name\": \"pop_up\", \
1302 \"name\": \"system_alert\", \