2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <systemd/sd-event.h>
25 #include <json-c/json.h>
26 #include "policy_manager.hpp"
27 #include "hmi-debug.h"
31 #include "dummy_stm.h"
37 typedef struct AreaState {
43 typedef std::vector<AreaState> AreaList;
44 typedef struct LayoutState {
46 std::map<std::string, int> category_num;
48 std::map<std::string, std::vector<std::string>> role_history;
51 typedef struct LayerState {
53 LayoutState layout_state;
56 typedef struct EventInfo {
62 struct sd_event* event_loop;
63 std::map<int, struct sd_event_source*> event_source_list;
64 std::map<int, std::string> g_req_role_list;
65 PolicyManager::CallbackTable callback;
66 std::queue<EventInfo> g_event_info_queue;
68 std::unordered_map<std::string, LayerState> g_prv_layers;
69 std::unordered_map<std::string, LayerState> g_crr_layers;
70 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
71 std::unordered_map<std::string, LayoutState> g_default_layouts;
75 PolicyManager::PolicyManager() :
83 HMI_DEBUG("wm:pm", "Call");
86 int PolicyManager::initialize() {
87 HMI_DEBUG("wm:pm", "Call");
92 for (int i = stm::gStmEventNoMin; i <= stm::gStmEventNoMax; i++) {
93 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
94 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
97 for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
98 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
99 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
102 for (int i = stm::gStmAreaNoMin; i <= stm::gStmAreaNoMax; i++) {
103 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
104 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
108 ret = this->loadRoleDb();
110 HMI_ERROR("wm:pm", "Load role.db Error!!");
115 ret = this->loadLayoutDb();
117 HMI_ERROR("wm:pm", "Load layout.db Error!!");
121 // Initialize current/previous state of layers
122 pm::AreaState init_area;
123 pm::LayoutState init_layout;
124 init_area.name = "none";
125 init_area.category = "none";
126 init_area.role = "none";
127 init_layout.area_list.push_back(init_area);
129 for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
130 const char* layer_name = stm::gStmLayerName[i];
131 pm::g_crr_layers[layer_name].name = layer_name;
132 pm::g_crr_layers[layer_name].layout_state = init_layout;
135 pm::g_prv_layers = pm::g_crr_layers;
137 // Initialize StateTransitioner
138 stm::stmInitialize();
140 // Initialize sd_event loop
141 ret = this->initializeSdEventLoop();
143 HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
150 int PolicyManager::initializeSdEventLoop() {
151 // Get default event loop object
152 int ret = sd_event_new(&(pm::event_loop));
154 HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
158 // Create thread for sd_event and detach
159 std::thread sd_event_loop([this]() {
161 sd_event_run(pm::event_loop, 1000);
164 sd_event_loop.detach();
169 static void addStateToJson(
170 const char* key, int is_changed, const char* state, json_object** json_out) {
171 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
172 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
176 json_object* json_obj = json_object_new_object();
177 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
179 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
180 json_object_object_add(json_obj, "state", json_object_new_string(state));
182 json_object_object_add(*json_out, key, json_obj);
185 static void addStateToJson(const char* layer_name, unsigned int changed,
186 pm::AreaList area_list, json_object** json_out) {
187 if ((nullptr == layer_name) || (1 < changed) || (nullptr == json_out)) {
188 HMI_ERROR("wm:pm", "Invalid argument!!!");
192 json_object* json_areas = json_object_new_array();
193 json_object* json_tmp;
194 for (pm::AreaState as : area_list) {
195 json_tmp = json_object_new_object();
196 json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
197 json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
198 json_object_array_add(json_areas, json_tmp);
201 json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
202 json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
203 json_object_object_add(*json_out, "areas", json_areas);
206 static void updateLocalState(int event_data, stm::stm_state_t crr_state) {
207 int event_no, category_no, area_no;
209 event_no = (event_data & STM_MSK_EVT_NO) - 1;
210 category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
211 area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
213 std::string req_evt = std::string(stm::gStmEventName[event_no]);
214 std::string req_ctg = std::string(stm::gStmCategoryName[category_no]);
215 std::string req_area = std::string(stm::gStmAreaName[area_no]);
216 std::string req_role = pm::g_req_role_list[event_data];
217 HMI_DEBUG("wm:pm", "REQ: event:%s role:%s category:%s area:%s",
218 req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
220 // Store previous layers
221 pm::g_prv_layers = pm::g_crr_layers;
224 for (int layer_no = stm::gStmLayerNoMin;
225 layer_no <= stm::gStmLayerNoMax; layer_no++) {
226 const char* layer_name = stm::gStmLayerName[layer_no];
227 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
230 // If restriction mode is changed off -> on,
231 // store current state for state of restriction mode off
232 if ((crr_state.restriction_mode.is_changed)
233 && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
234 HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
235 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
238 // If car state is changed car_stop -> car_run,
239 // store current state for state of car stop
240 if ((crr_state.car.is_changed)
241 && (stm::gStmCarStateNoRun == crr_state.car.state)) {
242 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
243 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
248 // This layer is changed?
249 if (crr_state.layer[layer_no].is_changed) {
250 // Get previous layout name of this layer
251 pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
252 std::string prv_layout_name = prv_layout_state.name;
254 // Get current layout name of this layer
255 int crr_layout_state_no = crr_state.layer[layer_no].state;
256 std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
258 pm::LayoutState crr_layout_state;
260 if ((crr_state.restriction_mode.is_changed)
261 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
262 // If restriction mode is changed on -> off,
263 // restore state of restriction mode off
264 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
265 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
267 if ((crr_state.car.is_changed)
268 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
269 // If car state is changed car_run -> car_stop,
270 // restore state of car stop
271 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
272 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
276 // Copy previous layout state for current
277 crr_layout_state = prv_layout_state;
279 if (prv_layout_name == crr_layout_name) {
280 HMI_DEBUG("wm:lm", "Previous layout is same with current");
283 // If previous layout is NOT same with current,
284 // current areas is set with default value
285 HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
286 crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
287 crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
288 crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
291 // Create candidate list
292 std::map<std::string, pm::AreaList> cand_list;
293 for (int ctg_no=stm::gStmCategoryNoMin;
294 ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
295 const char* ctg = stm::gStmCategoryName[ctg_no];
296 HMI_DEBUG("wm:pm", "ctg:%s", ctg);
298 // Create candidate list for category from the previous displayed categories
299 pm::AreaList tmp_cand_list;
300 for (pm::AreaState area_state : prv_layout_state.area_list) {
301 if (std::string(ctg) == area_state.category) {
302 // If there is the category which is same with new category in previous layout,
304 HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
305 area_state.category.c_str(), area_state.role.c_str());
306 tmp_cand_list.push_back(area_state);
310 int candidate_num = prv_layout_state.category_num[ctg];
311 int blank_num = crr_layout_state.category_num[ctg];
312 HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
314 // If requested event is "activate"
315 // and there are requested category and area,
316 // update area with requested role in current layout.
317 bool request_for_this_layer = false;
318 bool updated = false;
319 if ((ctg == req_ctg) && ("activate" == req_evt) ) {
320 HMI_DEBUG("wm:pm", "requested event is activate");
321 for (pm::AreaState &as : crr_layout_state.area_list) {
322 if (as.category == req_ctg) {
323 request_for_this_layer = true;
325 if (as.name == req_area) {
326 HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
327 as.name.c_str(), as.category.c_str(), as.role.c_str());
336 // If NOT updated: there is not requested area in new layout,
337 // so push requested role to candidate list
338 if (request_for_this_layer && (!updated)) {
339 HMI_DEBUG("wm:pm", "Push request to candidate list");
340 pm::AreaState area_state;
341 area_state.name = req_area;
342 area_state.category = req_ctg;
343 area_state.role = req_role;
344 tmp_cand_list.push_back(area_state);
348 // Compare number of candidate/blank,
349 // And remove role in order of the oldest as necessary
350 if (candidate_num < blank_num) {
351 // Refer history stack
352 // and add to the top of tmp_cand_list in order to the newest
353 while (candidate_num != blank_num) {
354 pm::AreaState area_state;
355 area_state.name = "";
356 area_state.category = ctg;
357 if (0 != crr_layout_state.role_history[ctg].size()) {
358 HMI_ERROR("wm:pm", "Use role in history stack:%s",
359 crr_layout_state.role_history[ctg].back().c_str());
360 area_state.role = crr_layout_state.role_history[ctg].back();
361 crr_layout_state.role_history[ctg].pop_back();
364 HMI_ERROR("wm:pm", "There is no role in history stack!!");
365 area_state.role = "";
367 tmp_cand_list.push_back(area_state);
371 else if (candidate_num > blank_num) {
372 HMI_DEBUG("wm:pm", "candidate_num > blank_num");
374 // Remove the oldest role from candidate list
375 while (candidate_num != blank_num) {
376 std::string removed_role = tmp_cand_list.begin()->role;
377 HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
378 removed_role.c_str());
379 tmp_cand_list.erase(tmp_cand_list.begin());
382 // Push removed data to history stack
383 crr_layout_state.role_history[ctg].push_back(removed_role);
386 else { // (candidate_num == blank_num)
390 cand_list[ctg] = tmp_cand_list;
394 for (pm::AreaState &as : crr_layout_state.area_list) {
395 HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
396 as.name.c_str(), as.category.c_str());
398 HMI_DEBUG("wm:pm", "Update this area with role:%s",
399 cand_list[as.category].begin()->role.c_str());
400 as.role = cand_list[as.category].begin()->role;
401 cand_list[as.category].erase(cand_list[as.category].begin());
405 // Update current layout of this layer
406 pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
410 // Erase role for the event_data from list
411 pm::g_req_role_list.erase(event_data);
414 for (auto itr : pm::g_crr_layers) {
415 pm::LayerState ls = itr.second;
416 HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
417 HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
419 for (pm::AreaState as : ls.layout_state.area_list) {
420 HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
421 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
422 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
427 void createOutputInformation(stm::stm_state_t crr_state, json_object **json_out) {
430 // "parking_brake": {
431 // "is_changed": <bool>,
432 // "state": <const char*>
434 addStateToJson("parking_brake",
435 crr_state.parking_brake.is_changed,
436 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
440 // "is_changed": <bool>,
441 // "state": <const char*>
443 addStateToJson("accel_pedal",
444 crr_state.accel_pedal.is_changed,
445 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
448 // "lightstatus_brake": {
449 // "is_changed": <bool>,
450 // "state": <const char*>
452 addStateToJson("lightstatus_brake",
453 crr_state.lightstatus_brake.is_changed,
454 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
458 // "is_changed": <bool>,
459 // "state": <const char*>
461 addStateToJson("car",
462 crr_state.car.is_changed,
463 stm::gStmCarStateNo2Name[crr_state.car.state],
467 // "is_changed": <bool>,
468 // "state": <const char*>
470 addStateToJson("lamp",
471 crr_state.lamp.is_changed,
472 stm::gStmLampStateNo2Name[crr_state.lamp.state],
475 // "restriction_mode": {
476 // "is_changed": <bool>,
477 // "state": <const char*>
479 addStateToJson("restriction_mode",
480 crr_state.restriction_mode.is_changed,
481 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
484 // Create layout information
489 // "changed": <bool>,
492 // "name":<const char*>,
493 // "role":<const char*>
500 json_object* json_layer = json_object_new_array();
501 json_object* json_tmp;
502 for (int layer_no = stm::gStmLayerNoMin;
503 layer_no <= stm::gStmLayerNoMax; layer_no++) {
504 const char* layer_name = stm::gStmLayerName[layer_no];
505 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
507 json_tmp = json_object_new_object();
508 addStateToJson(layer_name,
509 crr_state.layer[layer_no].is_changed,
510 pm::g_crr_layers[layer_name].layout_state.area_list,
512 json_object_array_add(json_layer, json_tmp);
515 // Add json array of layer
516 json_object_object_add(*json_out, "layers", json_layer);
519 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role);
520 static int checkPolicy(sd_event_source *source, void *data) {
521 HMI_DEBUG("wm:pm", "Call");
522 HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
524 int event_data = *((int*)data);
526 int event_no, category_no, area_no;
527 event_no = (event_data & STM_MSK_EVT_NO) - 1;
528 category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
529 area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
530 HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
531 stm::gStmEventName[event_no],
532 stm::gStmCategoryName[category_no],
533 stm::gStmAreaName[area_no]);
536 stm::stm_state_t crr_state;
537 int ret = stm::stmTransitionState(event_data, &crr_state);
539 HMI_ERROR("wm:pm", "Failed transition state");
540 if (nullptr != pm::callback.onError) {
541 json_object* json_out = json_object_new_object();
542 json_object_object_add(json_out, "message",
543 json_object_new_string("Failed to transition state"));
544 json_object_object_add(json_out, "event",
545 json_object_new_string(stm::gStmEventName[event_no]));
546 json_object_object_add(json_out, "role",
547 json_object_new_string(pm::g_req_role_list[event_data].c_str()));
548 json_object_object_add(json_out, "area",
549 json_object_new_string(stm::gStmAreaName[area_no]));
550 pm::callback.onError(json_out);
551 json_object_put(json_out);
556 HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
557 crr_state.parking_brake.is_changed,
558 crr_state.parking_brake.state,
559 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
560 HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
561 crr_state.accel_pedal.is_changed,
562 crr_state.accel_pedal.state,
563 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
564 HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
565 crr_state.lightstatus_brake.is_changed,
566 crr_state.lightstatus_brake.state,
567 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
568 HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
569 crr_state.car.is_changed,
571 stm::gStmCarStateNo2Name[crr_state.car.state]);
572 HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
573 crr_state.lamp.is_changed,
574 crr_state.lamp.state,
575 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
576 HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
577 crr_state.restriction_mode.is_changed,
578 crr_state.restriction_mode.state,
579 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
580 HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
581 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
582 crr_state.layer[stm::gStmLayerNoHomescreen].state,
583 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
584 HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
585 crr_state.layer[stm::gStmLayerNoApps].is_changed,
586 crr_state.layer[stm::gStmLayerNoApps].state,
587 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
588 HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
589 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
590 crr_state.layer[stm::gStmLayerNoRestriction].state,
591 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
592 HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
593 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
594 crr_state.layer[stm::gStmLayerNoOnScreen].state,
595 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
597 // Update state which is managed by PolicyManager
598 updateLocalState(event_data, crr_state);
600 // Create output information for ResourceManager
601 json_object* json_out = json_object_new_object();
602 createOutputInformation(crr_state, &json_out);
604 // Notify state is changed
605 if (nullptr != pm::callback.onStateTransitioned) {
606 pm::callback.onStateTransitioned(json_out);
609 // Start/Stop timer event
610 if (crr_state.car.is_changed) {
611 if (stm::gStmCarStateNoRun == crr_state.car.state) {
612 // Set delay event(restriction mode on)
613 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, "");
615 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
616 // Set event(restriction mode off)
617 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, "");
619 // Stop timer for restriction on event
620 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
621 != pm::event_source_list.end()) {
622 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
623 sd_event_source *event_source
624 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
625 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
627 HMI_ERROR("wm:pm", "Failed to stop timer");
633 // Release json_object
634 json_object_put(json_out);
639 // Destroy sd_event_source object
640 sd_event_source_unref(source);
642 // Remove event source from list
643 if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
644 pm::event_source_list.erase(event_data);
647 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
651 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
652 HMI_DEBUG("wm:pm", "Call");
654 int ret = checkPolicy(source, data);
658 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
660 HMI_DEBUG("wm:pm", "Call");
661 HMI_DEBUG("wm:pm", "event:0x%x delay:%d role:%s", event, delay_ms, role.c_str());
663 // Store requested role
664 pm::g_req_role_list[event] = role;
667 int ret = sd_event_add_defer(pm::event_loop, NULL,
668 &checkPolicy, new int(event));
670 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
671 pm::g_req_role_list.erase(event);
677 struct timespec time_spec;
678 clock_gettime(CLOCK_MONOTONIC, &time_spec);
680 // Calculate timer fired time
681 uint64_t usec = (time_spec.tv_sec * 1000000)
682 + (time_spec.tv_nsec / 1000)
686 struct sd_event_source* event_source;
687 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
688 &timerEvent, new int(event));
690 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
691 pm::g_req_role_list.erase(event);
695 // Store event source
696 pm::event_source_list[event] = event_source;
702 void PolicyManager::registerCallback(CallbackTable callback) {
703 HMI_DEBUG("wm:pm", "Call");
705 pm::callback.onStateTransitioned = callback.onStateTransitioned;
706 pm::callback.onError = callback.onError;
709 int PolicyManager::setInputEventData(json_object* json_in) {
710 HMI_DEBUG("wm:pm", "Call");
713 if (nullptr == json_in) {
714 HMI_ERROR("wm:pm", "Argument is NULL!!");
718 // Get event from json_object
719 const char* event = this->getStringFromJson(json_in, "event");
721 if (nullptr != event) {
722 // Convert name to number
723 event_no = this->eventname2no_[event];
724 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
727 // Get role from json_object
728 const char* role = this->getStringFromJson(json_in, "role");
730 if (nullptr != role) {
731 HMI_DEBUG("wm:pm", "role(%s)", role);
733 // Convert role to category
734 const char* category = this->role2category_[role].c_str();
735 if (0 == strcmp("", category)) {
736 HMI_ERROR("wm:pm", "Error!!");
739 HMI_DEBUG("wm:pm", "category(%s)", category);
741 // Convert name to number
742 category_no = categoryname2no_[category];
743 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
746 // Get areat from json_object
747 const char* area = this->getStringFromJson(json_in, "area");
749 if (nullptr != area) {
750 // Convert name to number
751 area_no = areaname2no_[area];
752 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
755 // Set event info to the queue
756 pm::EventInfo event_info;
757 event_info.event = (event_no | category_no | area_no);
758 if (nullptr == role) {
759 event_info.role = std::string("");
762 event_info.role = std::string(role);
764 event_info.delay = 0;
765 pm::g_event_info_queue.push(event_info);
770 int PolicyManager::executeStateTransition() {
771 HMI_DEBUG("wm:pm", "Call");
774 pm::EventInfo event_info;
776 while (!pm::g_event_info_queue.empty()) {
777 // Get event info from queue and delete
778 event_info = pm::g_event_info_queue.front();
779 pm::g_event_info_queue.pop();
781 // Set event info for checking policy
782 ret = checkPolicyEntry(event_info.event, event_info.delay, event_info.role);
787 void PolicyManager::undoState() {
788 HMI_DEBUG("wm:pm", "Call");
793 pm::g_crr_layers = pm::g_prv_layers;
796 extern const char* kDefaultRoleDb;
797 int PolicyManager::loadRoleDb() {
798 HMI_DEBUG("wm:pm", "Call");
800 std::string file_name;
802 // Get afm application installed dir
803 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
804 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
806 if (!afm_app_install_dir) {
807 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
810 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
814 json_object* json_obj;
815 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
817 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
818 json_obj = json_tokener_parse(kDefaultRoleDb);
820 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
822 json_object* json_roles;
823 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
824 HMI_ERROR("wm:pm", "Parse Error!!");
828 int len = json_object_array_length(json_roles);
829 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
830 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
832 json_object* json_tmp;
833 const char* category;
836 for (int i=0; i<len; i++) {
837 json_tmp = json_object_array_get_idx(json_roles, i);
839 category = this->getStringFromJson(json_tmp, "category");
840 roles = this->getStringFromJson(json_tmp, "role");
841 areas = this->getStringFromJson(json_tmp, "area");
843 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
844 HMI_ERROR("wm:pm", "Parse Error!!");
848 // Parse roles by '|'
849 std::vector<std::string> vct_roles;
850 vct_roles = this->parseString(std::string(roles), '|');
852 // Parse areas by '|'
853 std::vector<std::string> vct_areas;
854 vct_areas = this->parseString(std::string(areas), '|');
856 // Set role, category, default area
857 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
858 // Delete space from role and area name
859 std::string role = this->deleteSpace(*itr);
860 std::string area = this->deleteSpace(vct_areas[0]);
862 this->role2category_[role] = std::string(category);
863 this->role2defaultarea_[role] = area;
866 this->category2role_[std::string(category)] = std::string(roles);
870 HMI_DEBUG("wm:pm", "Check role2category_");
871 for (auto& x:this->role2category_){
872 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
875 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
876 for (auto& x:this->role2defaultarea_){
877 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
880 HMI_DEBUG("wm:pm", "Check category2role_");
881 for (auto& x:this->category2role_){
882 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
888 extern const char* kDefaultLayoutDb;
889 int PolicyManager::loadLayoutDb() {
890 HMI_DEBUG("wm:lm", "Call");
892 // Get afm application installed dir
893 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
894 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
896 std::string file_name;
897 if (!afm_app_install_dir) {
898 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
901 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
905 json_object* json_obj;
906 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
908 HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
909 json_obj = json_tokener_parse(kDefaultLayoutDb);
911 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
914 HMI_DEBUG("wm:pm", "Perse layouts");
915 json_object* json_cfg;
916 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
917 HMI_ERROR("wm:pm", "Parse Error!!");
921 int len = json_object_array_length(json_cfg);
922 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
923 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
927 const char* category;
928 for (int i=0; i<len; i++) {
929 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
931 layout = this->getStringFromJson(json_tmp, "name");
932 if (nullptr == layout) {
933 HMI_ERROR("wm:pm", "Parse Error!!");
936 HMI_DEBUG("wm:pm", "> layout:%s", layout);
938 json_object* json_area_array;
939 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
940 HMI_ERROR("wm:pm", "Parse Error!!");
944 int len_area = json_object_array_length(json_area_array);
945 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
946 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
948 pm::LayoutState layout_state;
949 pm::AreaState area_state;
950 std::map<std::string, int> category_num;
951 for (int ctg_no = stm::gStmCategoryNoMin;
952 ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
953 const char* ctg_name = stm::gStmCategoryName[ctg_no];
954 category_num[ctg_name] = 0;
957 for (int j=0; j<len_area; j++) {
958 json_object* json_area = json_object_array_get_idx(json_area_array, j);
961 const char* area = this->getStringFromJson(json_area, "name");
962 if (nullptr == area) {
963 HMI_ERROR("wm:pm", "Parse Error!!");
966 area_state.name = std::string(area);
967 HMI_DEBUG("wm:pm", ">> area:%s", area);
969 // Get app attribute of the area
970 category = this->getStringFromJson(json_area, "category");
971 if (nullptr == category) {
972 HMI_ERROR("wm:pm", "Parse Error!!");
975 area_state.category = std::string(category);
976 category_num[category]++;
977 HMI_DEBUG("wm:pm", ">>> category:%s", category);
979 role = this->getStringFromJson(json_area, "role");
980 if (nullptr != role) {
981 // Role is NOT essential here
982 area_state.role = std::string(role);
985 area_state.role = std::string("");
988 HMI_DEBUG("wm:pm", ">>> role:%s", role);
990 layout_state.area_list.push_back(area_state);
994 layout_state.name = layout;
995 layout_state.category_num = category_num;
996 pm::g_default_layouts[layout] = layout_state;
999 // initialize for none layout
1000 pm::LayoutState none_layout_state;
1001 memset(&none_layout_state, 0, sizeof(none_layout_state));
1002 none_layout_state.name = "none";
1003 pm::g_default_layouts["none"] = none_layout_state;
1006 for(auto itr_layout = pm::g_default_layouts.begin();
1007 itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
1008 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
1010 for (auto itr_area = itr_layout->second.area_list.begin();
1011 itr_area != itr_layout->second.area_list.end(); ++itr_area) {
1012 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
1013 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
1014 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
1018 // Release json_object
1019 json_object_put(json_obj);
1025 // This function will be removed because json_helper has same function.
1026 // json_helper should be library.
1027 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
1028 if ((nullptr == obj) || (nullptr == key)) {
1029 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
1034 if (!json_object_object_get_ex(obj, key, &tmp)) {
1035 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1039 return json_object_get_string(tmp);
1043 // This function will be removed because json_helper has same function.
1044 // json_helper should be library.
1045 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
1046 const int input_size = 128;
1049 if ((nullptr == file) || (nullptr == obj)) {
1050 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
1054 HMI_DEBUG("wm:jh", "Input file: %s", file);
1057 FILE *fp = fopen(file, "rb");
1058 if (nullptr == fp) {
1059 HMI_ERROR("wm:jh", "Could not open file");
1064 struct json_tokener *tokener = json_tokener_new();
1065 enum json_tokener_error json_error;
1066 char buffer[input_size];
1069 size_t len = fread(buffer, sizeof(char), input_size, fp);
1070 *obj = json_tokener_parse_ex(tokener, buffer, len);
1071 if (nullptr != *obj) {
1072 HMI_DEBUG("wm:jh", "File input is success");
1077 json_error = json_tokener_get_error(tokener);
1078 if ((json_tokener_continue != json_error)
1079 || (input_size > len)) {
1080 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1081 (input_size * block_cnt), json_tokener_error_desc(json_error));
1082 HMI_ERROR("wm:jh", "\n%s", buffer);
1092 // Free json_tokener
1093 json_tokener_free(tokener);
1098 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1099 // Parse string by delimiter
1100 std::vector<std::string> vct;
1101 std::stringstream ss{str};
1103 while (std::getline(ss, buf, delimiter)) {
1111 std::string PolicyManager::deleteSpace(std::string str) {
1112 std::string ret = str;
1114 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1120 const char* kDefaultRoleDb = "{ \
1123 \"category\": \"homescreen\", \
1124 \"role\": \"homescreen\", \
1125 \"area\": \"full\", \
1128 \"category\": \"map\", \
1129 \"role\": \"map\", \
1130 \"area\": \"full | normal | split.main\", \
1133 \"category\": \"general\", \
1134 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1135 \"area\": \"normal\", \
1138 \"category\": \"phone\", \
1139 \"role\": \"phone\", \
1140 \"area\": \"normal\", \
1143 \"category\": \"splitable\", \
1144 \"role\": \"splitable1 | splitable2\", \
1145 \"area\": \"normal | split.main | split.sub\", \
1148 \"category\": \"popup\", \
1149 \"role\": \"popup\", \
1150 \"area\": \"on_screen\", \
1153 \"category\": \"system_alert\", \
1154 \"role\": \"system_alert\", \
1155 \"area\": \"on_screen\", \
1158 \"category\": \"tbt\", \
1159 \"role\": \"tbt\", \
1160 \"area\": \"hud\", \
1166 const char* kDefaultLayoutDb = "{ \
1170 \"layer\": \"on_screen\", \
1173 \"name\": \"pop_up\", \
1174 \"role\": \"incomming_call\" \
1180 \"layer\": \"on_screen\", \
1183 \"name\": \"system_alert\", \
1184 \"role\": \"system_alert\" \
1190 \"layer\": \"apps\", \
1193 \"name\": \"normal\", \
1200 \"layer\": \"apps\", \
1203 \"name\": \"split.main\", \
1207 \"name\": \"split.sub\", \
1208 \"category\": \"hvac\" \
1214 \"layer\": \"apps\", \
1217 \"name\": \"full\", \
1224 \"layer\": \"apps\", \
1227 \"name\": \"normal\", \
1228 \"category\": \"splitable\" \
1234 \"layer\": \"apps\", \
1237 \"name\": \"split.main\", \
1238 \"category\": \"splitable\" \
1241 \"name\": \"split.sub\", \
1242 \"category\": \"splitable\" \
1248 \"layer\": \"apps\", \
1251 \"name\": \"normal\", \
1252 \"category\": \"general\" \
1258 \"layer\": \"homescreen\", \
1261 \"name\": \"full\", \
1262 \"role\": \"homescreen\" \
1269 \"name\": \"normal\", \
1278 \"name\": \"split.main\", \
1287 \"name\": \"split.sub\", \
1296 \"name\": \"full\", \
1305 \"name\": \"pop_up\", \
1314 \"name\": \"system_alert\", \