2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <systemd/sd-event.h>
24 #include <json-c/json.h>
25 #include "policy_manager.hpp"
26 #include "hmi-debug.h"
30 #include "dummy_stm.h"
36 typedef struct AreaState {
42 typedef struct LayoutState {
44 std::map<std::string, int> category_num;
45 // int category_num[stm::gStmCategoryNoNum];
46 std::vector<AreaState> area_list;
47 std::map<std::string, std::string> last_invisibled_role;
50 typedef struct LayerState {
52 LayoutState layout_state;
55 struct sd_event* event_loop;
56 std::map<int, struct sd_event_source*> event_source_list;
57 std::map<int, std::string> g_event_info_list;
58 PolicyManager::CallbackTable callback;
60 std::unordered_map<std::string, LayerState> g_prv_layers;
61 std::unordered_map<std::string, LayerState> g_crr_layers;
62 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
63 std::unordered_map<std::string, LayoutState> g_default_layouts;
67 PolicyManager::PolicyManager() :
75 HMI_DEBUG("wm:pm", "Call");
78 int PolicyManager::initialize() {
79 HMI_DEBUG("wm:pm", "Call");
84 for (unsigned int i=0; i<STM_NUM_EVT; i++) {
85 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
86 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
89 for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
90 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
91 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
94 for (unsigned int i=0; i<STM_NUM_ARA; i++) {
95 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
96 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
100 ret = this->loadRoleDb();
102 HMI_ERROR("wm:pm", "Load role.db Error!!");
107 ret = this->loadLayoutDb();
109 HMI_ERROR("wm:pm", "Load layout.db Error!!");
113 // Initialize current/previous state of layers
114 pm::AreaState init_area;
115 pm::LayoutState init_layout;
116 init_area.name = "none";
117 init_area.category = "none";
118 init_area.role = "none";
119 init_layout.area_list.push_back(init_area);
121 for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
122 const char* layer_name = stm::gStmLayerName[i];
123 pm::g_crr_layers[layer_name].name = layer_name;
124 pm::g_crr_layers[layer_name].layout_state = init_layout;
127 pm::g_prv_layers = pm::g_crr_layers;
129 // Initialize StateTransitioner
130 stm::stmInitialize();
132 // Initialize sd_event loop
133 ret = this->initializeSdEventLoop();
135 HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
142 int PolicyManager::initializeSdEventLoop() {
143 // Get default event loop object
144 int ret = sd_event_new(&(pm::event_loop));
146 HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
150 // Create thread for sd_event and detach
151 std::thread sd_event_loop([this]() {
153 sd_event_run(pm::event_loop, 1000);
156 sd_event_loop.detach();
161 static void addStateToJson(
162 const char* key, int is_changed, const char* state, json_object** json_out) {
163 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
164 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
168 json_object* json_obj = json_object_new_object();
169 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
171 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
172 json_object_object_add(json_obj, "state", json_object_new_string(state));
174 json_object_object_add(*json_out, key, json_obj);
177 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role);
178 static int checkPolicy(sd_event_source *source, void *data) {
179 HMI_DEBUG("wm:pm", "Call");
180 HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
182 int event_data = *((int*)data);
184 int event_no, category_no, area_no;
185 event_no = (event_data & STM_MSK_EVT_NO) - 1;
186 category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
187 area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
188 HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
189 stm::gStmEventName[event_no],
190 stm::gStmCategoryName[category_no],
191 stm::gStmAreaName[area_no]);
194 stm::stm_state_t crr_state;
195 int ret = stm::stmTransitionState(event_data, &crr_state);
197 HMI_ERROR("wm:pm", "Error!!");
201 HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
202 crr_state.parking_brake.is_changed,
203 crr_state.parking_brake.state,
204 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
205 HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
206 crr_state.accel_pedal.is_changed,
207 crr_state.accel_pedal.state,
208 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
209 HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
210 crr_state.lightstatus_brake.is_changed,
211 crr_state.lightstatus_brake.state,
212 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
213 HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
214 crr_state.car.is_changed,
216 stm::gStmCarStateNo2Name[crr_state.car.state]);
217 HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
218 crr_state.lamp.is_changed,
219 crr_state.lamp.state,
220 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
221 HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
222 crr_state.restriction_mode.is_changed,
223 crr_state.restriction_mode.state,
224 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
225 HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
226 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
227 crr_state.layer[stm::gStmLayerNoHomescreen].state,
228 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
229 HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
230 crr_state.layer[stm::gStmLayerNoApps].is_changed,
231 crr_state.layer[stm::gStmLayerNoApps].state,
232 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
233 HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
234 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
235 crr_state.layer[stm::gStmLayerNoRestriction].state,
236 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
237 HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
238 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
239 crr_state.layer[stm::gStmLayerNoOnScreen].state,
240 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
242 #if 1 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
243 // Store previous layers
244 pm::g_prv_layers = pm::g_crr_layers;
246 std::string role = pm::g_event_info_list[event_data];
247 std::string event = std::string(stm::gStmEventName[event_no]);
248 std::string category = std::string(stm::gStmCategoryName[category_no]);
249 std::string area = std::string(stm::gStmAreaName[area_no]);
252 for (int layer_no = stm::gStmLayerNoMin;
253 layer_no <= stm::gStmLayerNoMax; layer_no++) {
254 const char* layer_name = stm::gStmLayerName[layer_no];
255 HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
258 // If restriction mode is changed off -> on,
259 // store current state for state of restriction mode off
260 if ((crr_state.restriction_mode.is_changed)
261 && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
262 HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
263 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
266 // If car state is changed car_stop -> car_run,
267 // store current state for state of car stop
268 if ((crr_state.car.is_changed)
269 && (stm::gStmCarStateNoRun == crr_state.car.state)) {
270 HMI_DEBUG("wm:lm", "Store current state for state of car stop");
271 pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
276 // This layer is changed?
277 if (crr_state.layer[layer_no].is_changed) {
278 // Get previous layout name of this layer
279 pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
280 std::string prv_layout_name = prv_layout_state.name;
282 // Get current layout name of this layer
283 int crr_layout_state_no = crr_state.layer[layer_no].state;
284 std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
286 pm::LayoutState crr_layout_state;
288 if ((crr_state.restriction_mode.is_changed)
289 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
290 // If restriction mode is changed on -> off,
291 // restore state of restriction mode off
292 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
293 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
295 if ((crr_state.car.is_changed)
296 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
297 // If car state is changed car_run -> car_stop,
298 // restore state of car stop
299 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
300 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
304 else if ("none" == crr_layout_name) {
305 // If current layout is "none",
306 // current areas is set with "none"
307 HMI_DEBUG("wm:pm", "Current layout is \"none\"");
308 pm::AreaState crr_area_state;
309 crr_area_state.name = "none";
310 crr_area_state.category = "none";
311 crr_area_state.role = "none";
312 crr_layout_state.name = "none";
313 crr_layout_state.area_list.push_back(crr_area_state);
317 // Copy previous layout state for current
318 crr_layout_state = prv_layout_state;
320 if (prv_layout_name == crr_layout_name) {
321 HMI_DEBUG("wm:lm", "Previous layout is same with current");
324 // If previous layout is NOT same with current,
325 // current areas is set with default value
326 HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
327 crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
328 crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
329 crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
332 // Update role in new area
334 if (crr_state.restriction_mode.is_changed) {
335 // Updating role is not necessary
336 // because new_role is not specified
337 // when restriction mode is changed
338 HMI_DEBUG("wm:lm", "Updating role is not necessary because new_role is not specified when restriction mode is changed");
340 if (crr_state.car.is_changed) {
341 // Updating role is not necessary
342 // because new_role is not specified
343 // when car state is changed
344 HMI_DEBUG("wm:lm", "Updating role is not necessary because new_role is not specified when car state is changed");
349 std::map<std::string, std::vector<pm::AreaState>> ctg_list;
350 for (int ctg_no=stm::gStmCategoryNoMin;
351 ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
352 const char* ctg = stm::gStmCategoryName[ctg_no];
353 HMI_DEBUG("wm:pm", "ctg:%s", ctg);
355 // Create area list of previous app
356 std::vector<pm::AreaState> area_list;
357 for (pm::AreaState area_state : prv_layout_state.area_list) {
358 if (std::string(ctg) == area_state.category) {
359 // If there is the category which is same with new category in previous layout,
360 // push it to area list
361 HMI_DEBUG("wm:pm", "push area_state to prv list");
362 area_list.push_back(area_state);
366 int prv_ctg_num = prv_layout_state.category_num[ctg];
367 int crr_ctg_num = crr_layout_state.category_num[ctg];
368 HMI_DEBUG("wm:pm", "crr_ctg_num:%d prv_ctg_num:%d", crr_ctg_num, prv_ctg_num);
370 // Remove state of area which is used no longer from area list
371 if (crr_ctg_num < prv_ctg_num) {
372 HMI_DEBUG("wm:pm", "crr_ctg_num < prv_ctg_num");
373 if (0 == strcmp(ctg, category.c_str())) {
374 for (auto i = area_list.begin(); i != area_list.end(); ++i) {
375 if ((role == i->role) && (area == i->name)) {
376 HMI_DEBUG("wm:pm", "store removed role:%s", i->role.c_str());
377 crr_layout_state.last_invisibled_role[category] = i->role;
379 HMI_DEBUG("wm:pm", "remove area state which have same role with requested role");
384 if (crr_ctg_num == prv_ctg_num) {
391 HMI_DEBUG("wm:pm", "store removed role:%s", area_list.begin()->role.c_str());
392 crr_layout_state.last_invisibled_role[ctg] = area_list.begin()->role;
394 HMI_DEBUG("wm:pm", "remove the oldest area data");
395 area_list.erase(area_list.begin());
398 else if (crr_ctg_num >= prv_ctg_num) {
399 HMI_DEBUG("wm:pm", "crr_ctg_num >= prv_ctg_num");
400 if (0 == strcmp(ctg, category.c_str())) {
401 HMI_DEBUG("wm:pm", "category of requested role is same");
402 if ((crr_ctg_num == prv_ctg_num) && (0 != prv_ctg_num)) {
403 HMI_DEBUG("wm:pm", "store removed role:%s", area_list.begin()->role.c_str());
404 crr_layout_state.last_invisibled_role[ctg] = area_list.begin()->role;
406 HMI_DEBUG("wm:pm", "remove the oldest area data for adding requested role");
407 area_list.erase(area_list.begin());
413 for (pm::AreaState area_state : area_list) {
414 HMI_DEBUG("wm:pm", "area_list name:%s category:%s role:%s",
415 area_state.name.c_str(),
416 area_state.category.c_str(),
417 area_state.role.c_str());
420 ctg_list[ctg] = area_list;
423 if (event == "activate") {
424 HMI_DEBUG("wm:pm", "event is activate");
425 // First, update area for requested role
426 bool request_for_this_layer = false;
427 bool updated = false;
428 for (pm::AreaState &as : crr_layout_state.area_list) {
429 if (as.category == category) {
430 request_for_this_layer = true;
431 if (as.name == area) {
432 HMI_DEBUG("wm:pm", "update area for requested category:%s role:%s area:%s", category.c_str(), role.c_str(), area.c_str());
440 // If NOT updated: there is not requested area in new layout,
441 // requested role is used later.
442 if (request_for_this_layer && (!updated)) {
443 HMI_DEBUG("wm:pm", "requested role is used later category:%s role:%s area:%s", category.c_str(), role.c_str(), area.c_str());
444 pm::AreaState area_state;
445 area_state.name = area;
446 area_state.category = category;
447 area_state.role = role;
448 ctg_list[category].push_back(area_state);
453 for (pm::AreaState &as : crr_layout_state.area_list) {
454 if (as.role != role) { // This conditional expression is useful in only when requested activate event
455 HMI_DEBUG("wm:pm", "update areas for category:%s", as.category.c_str());
456 if (0 != ctg_list[as.category].size()) {
457 HMI_DEBUG("wm:pm", "update role to %s by using area list this category",
458 ctg_list[as.category].begin()->role);
459 as.role = ctg_list[as.category].begin()->role;
460 ctg_list[as.category].erase(ctg_list[as.category].begin());
463 HMI_DEBUG("wm:pm", "there is no area list for this category, so update area by last invisibled role:", crr_layout_state.last_invisibled_role[as.category]);
464 as.role = crr_layout_state.last_invisibled_role[as.category];
465 crr_layout_state.last_invisibled_role.erase(as.category);
471 // Update current layout of this layer
472 pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
477 for (auto itr : pm::g_crr_layers) {
478 pm::LayerState ls = itr.second;
479 HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
480 HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
482 for (pm::AreaState as : ls.layout_state.area_list) {
483 HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
484 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
485 HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
488 #endif // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
490 json_object* json_out = json_object_new_object();
494 // "parking_brake": {
495 // "is_changed": <bool>,
496 // "state": <const char*>
498 addStateToJson("parking_brake",
499 crr_state.parking_brake.is_changed,
500 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
504 // "is_changed": <bool>,
505 // "state": <const char*>
507 addStateToJson("accel_pedal",
508 crr_state.accel_pedal.is_changed,
509 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
512 // "lightstatus_brake": {
513 // "is_changed": <bool>,
514 // "state": <const char*>
516 addStateToJson("lightstatus_brake",
517 crr_state.lightstatus_brake.is_changed,
518 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
522 // "is_changed": <bool>,
523 // "state": <const char*>
525 addStateToJson("car",
526 crr_state.car.is_changed,
527 stm::gStmCarStateNo2Name[crr_state.car.state],
531 // "is_changed": <bool>,
532 // "state": <const char*>
534 addStateToJson("lamp",
535 crr_state.lamp.is_changed,
536 stm::gStmLampStateNo2Name[crr_state.lamp.state],
539 // "restriction_mode": {
540 // "is_changed": <bool>,
541 // "state": <const char*>
543 addStateToJson("restriction_mode",
544 crr_state.restriction_mode.is_changed,
545 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
549 json_object* json_layer = json_object_new_array();
550 json_object* json_tmp;
554 // "is_changed": <bool>,
555 // "state": <const char*>
560 json_tmp = json_object_new_object();
561 addStateToJson("homescreen",
562 crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
563 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state],
565 json_object_array_add(json_layer, json_tmp);
569 // "is_changed": <bool>,
570 // "state": <const char*>
573 json_tmp = json_object_new_object();
574 addStateToJson("apps",
575 crr_state.layer[stm::gStmLayerNoApps].is_changed,
576 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state],
578 json_object_array_add(json_layer, json_tmp);
582 // "is_changed": <bool>,
583 // "state": <const char*>
586 json_tmp = json_object_new_object();
587 addStateToJson("restriction",
588 crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
589 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state],
591 json_object_array_add(json_layer, json_tmp);
595 // "is_changed": <bool>,
596 // "state": <const char*>
599 json_tmp = json_object_new_object();
600 addStateToJson("on_screen",
601 crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
602 stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state],
604 json_object_array_add(json_layer, json_tmp);
606 // Add json array of layer
607 json_object_object_add(json_out, "layers", json_layer);
609 // Notify state is changed
610 if (nullptr != pm::callback.onStateTransitioned) {
611 pm::callback.onStateTransitioned(json_out);
614 if (crr_state.car.is_changed) {
615 if (stm::gStmCarStateNoRun == crr_state.car.state) {
616 // Set delay event(restriction mode on)
617 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, nullptr);
619 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
620 // Set event(restriction mode off)
621 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, nullptr);
623 // Stop timer for restriction on event
624 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
625 != pm::event_source_list.end()) {
626 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
627 sd_event_source *event_source
628 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
629 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
631 HMI_ERROR("wm:pm", "Failed to stop timer");
637 // Release json_object
638 json_object_put(json_out);
643 // Destroy sd_event_source object
644 sd_event_source_unref(source);
646 // Remove event source from list
647 if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
648 pm::event_source_list.erase(event_data);
651 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
655 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
656 int ret = checkPolicy(source, data);
660 static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role)
662 HMI_DEBUG("wm:pm", "Call");
663 HMI_DEBUG("wm:pm", "event:0x%x", event);
666 if (nullptr == role) {
667 pm::g_event_info_list[event] = std::string("");
670 pm::g_event_info_list[event] = std::string(role);
674 int ret = sd_event_add_defer(pm::event_loop, NULL,
675 &checkPolicy, new int(event));
677 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
678 pm::g_event_info_list.erase(event);
684 struct timespec time_spec;
685 clock_gettime(CLOCK_MONOTONIC, &time_spec);
687 // Calculate timer fired time
688 uint64_t usec = (time_spec.tv_sec * 1000000)
689 + (time_spec.tv_nsec / 1000)
693 struct sd_event_source* event_source;
694 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
695 &timerEvent, new int(event));
697 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
698 pm::g_event_info_list.erase(event);
702 // Store event source
703 pm::event_source_list[event] = event_source;
709 void PolicyManager::registerCallback(CallbackTable callback) {
710 pm::callback.onStateTransitioned = callback.onStateTransitioned;
711 pm::callback.onError = callback.onError;
714 int PolicyManager::inputEvent(json_object* json_in) {
715 HMI_DEBUG("wm:pm", "Call");
718 if (nullptr == json_in) {
719 HMI_ERROR("wm:pm", "Argument is NULL!!");
723 // Get event from json_object
724 const char* event = this->getStringFromJson(json_in, "event");
726 if (nullptr != event) {
727 // Convert name to number
728 event_no = this->eventname2no_[event];
729 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
732 // Get role from json_object
733 const char* role = this->getStringFromJson(json_in, "role");
735 if (nullptr != role) {
736 HMI_DEBUG("wm:pm", "role(%s)", role);
738 // Convert role to category
739 const char* category = this->role2category_[role].c_str();
740 if (0 == strcmp("", category)) {
741 HMI_ERROR("wm:pm", "Error!!");
744 HMI_DEBUG("wm:pm", "category(%s)", category);
746 // Convert name to number
747 category_no = categoryname2no_[category];
748 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
751 // Get areat from json_object
752 const char* area = this->getStringFromJson(json_in, "area");
754 if (nullptr != area) {
755 // Convert name to number
756 area_no = areaname2no_[area];
757 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
761 checkPolicyEntry((event_no | category_no | area_no), 0, role);
766 std::string PolicyManager::roleToCategory(const char* role) {
767 return this->role2category_[role];
770 extern const char* kDefaultRoleDb;
771 int PolicyManager::loadRoleDb() {
772 HMI_DEBUG("wm:pm", "Call");
774 std::string file_name;
776 // Get afm application installed dir
777 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
778 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
780 if (!afm_app_install_dir) {
781 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
784 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
788 json_object* json_obj;
789 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
791 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
792 json_obj = json_tokener_parse(kDefaultRoleDb);
794 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
796 json_object* json_roles;
797 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
798 HMI_ERROR("wm:pm", "Parse Error!!");
802 int len = json_object_array_length(json_roles);
803 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
804 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
806 json_object* json_tmp;
807 const char* category;
810 for (int i=0; i<len; i++) {
811 json_tmp = json_object_array_get_idx(json_roles, i);
813 category = this->getStringFromJson(json_tmp, "category");
814 roles = this->getStringFromJson(json_tmp, "role");
815 areas = this->getStringFromJson(json_tmp, "area");
817 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
818 HMI_ERROR("wm:pm", "Parse Error!!");
822 // Parse roles by '|'
823 std::vector<std::string> vct_roles;
824 vct_roles = this->parseString(std::string(roles), '|');
826 // Parse areas by '|'
827 std::vector<std::string> vct_areas;
828 vct_areas = this->parseString(std::string(areas), '|');
830 // Set role, category, default area
831 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
832 // Delete space from role and area name
833 std::string role = this->deleteSpace(*itr);
834 std::string area = this->deleteSpace(vct_areas[0]);
836 this->role2category_[role] = std::string(category);
837 this->role2defaultarea_[role] = area;
840 this->category2role_[std::string(category)] = std::string(roles);
844 HMI_DEBUG("wm:pm", "Check role2category_");
845 for (auto& x:this->role2category_){
846 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
849 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
850 for (auto& x:this->role2defaultarea_){
851 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
854 HMI_DEBUG("wm:pm", "Check category2role_");
855 for (auto& x:this->category2role_){
856 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
862 extern const char* kDefaultLayoutDb;
863 int PolicyManager::loadLayoutDb() {
864 HMI_DEBUG("wm:lm", "Call");
866 // Get afm application installed dir
867 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
868 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
870 std::string file_name;
871 if (!afm_app_install_dir) {
872 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
875 file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
879 json_object* json_obj;
880 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
882 HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
883 json_obj = json_tokener_parse(kDefaultLayoutDb);
885 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
888 HMI_DEBUG("wm:pm", "Perse layouts");
889 json_object* json_cfg;
890 if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
891 HMI_ERROR("wm:pm", "Parse Error!!");
895 int len = json_object_array_length(json_cfg);
896 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
897 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
901 const char* category;
902 for (int i=0; i<len; i++) {
903 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
905 layout = this->getStringFromJson(json_tmp, "name");
906 if (nullptr == layout) {
907 HMI_ERROR("wm:pm", "Parse Error!!");
910 HMI_DEBUG("wm:pm", "> layout:%s", layout);
912 json_object* json_area_array;
913 if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
914 HMI_ERROR("wm:pm", "Parse Error!!");
918 int len_area = json_object_array_length(json_area_array);
919 HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
920 HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
922 pm::LayoutState layout_state;
923 pm::AreaState area_state;
924 std::map<std::string, int> category_num;
925 for (int ctg_no = stm::gStmCategoryNoMin;
926 ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
927 const char* ctg_name = stm::gStmCategoryName[ctg_no];
928 category_num[ctg_name] = 0;
931 for (int j=0; j<len_area; j++) {
932 json_object* json_area = json_object_array_get_idx(json_area_array, j);
935 const char* area = this->getStringFromJson(json_area, "name");
936 if (nullptr == area) {
937 HMI_ERROR("wm:pm", "Parse Error!!");
940 area_state.name = std::string(area);
941 HMI_DEBUG("wm:pm", ">> area:%s", area);
943 // Get app attribute of the area
944 category = this->getStringFromJson(json_area, "category");
945 if (nullptr == category) {
946 HMI_ERROR("wm:pm", "Parse Error!!");
949 area_state.category = std::string(category);
950 category_num[category]++;
951 HMI_DEBUG("wm:pm", ">>> category:%s", category);
953 role = this->getStringFromJson(json_area, "role");
954 if (nullptr != role) {
955 // Role is NOT essential here
956 area_state.role = std::string(role);
957 HMI_DEBUG("wm:pm", ">>> role:%s", role);
960 layout_state.area_list.push_back(area_state);
964 layout_state.name = layout;
965 layout_state.category_num = category_num;
966 pm::g_default_layouts[layout] = layout_state;
969 // initialize for none layout
970 pm::LayoutState none_layout_state;
971 memset(&none_layout_state, 0, sizeof(none_layout_state));
972 none_layout_state.name = "none";
973 pm::g_default_layouts["none"] = none_layout_state;
976 for(auto itr_layout = pm::g_default_layouts.begin();
977 itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
978 HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
980 for (auto itr_area = itr_layout->second.area_list.begin();
981 itr_area != itr_layout->second.area_list.end(); ++itr_area) {
982 HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
983 HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
984 HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
986 for (auto itr_role = itr_area->second.begin();
987 itr_role != itr_area->second.end(); ++itr_role) {
988 HMI_DEBUG("wm:pm", ">>> >>> >>> attribute:%s, name:%s",
989 itr_role->first.c_str(), itr_role->second.c_str());
995 // Release json_object
996 json_object_put(json_obj);
1002 // This function will be removed because json_helper has same function.
1003 // json_helper should be library.
1004 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
1005 if ((nullptr == obj) || (nullptr == key)) {
1006 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
1011 if (!json_object_object_get_ex(obj, key, &tmp)) {
1012 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1016 return json_object_get_string(tmp);
1020 // This function will be removed because json_helper has same function.
1021 // json_helper should be library.
1022 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
1023 const int input_size = 128;
1026 if ((nullptr == file) || (nullptr == obj)) {
1027 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
1031 HMI_DEBUG("wm:jh", "Input file: %s", file);
1034 FILE *fp = fopen(file, "rb");
1035 if (nullptr == fp) {
1036 HMI_ERROR("wm:jh", "Could not open file");
1041 struct json_tokener *tokener = json_tokener_new();
1042 enum json_tokener_error json_error;
1043 char buffer[input_size];
1046 size_t len = fread(buffer, sizeof(char), input_size, fp);
1047 *obj = json_tokener_parse_ex(tokener, buffer, len);
1048 if (nullptr != *obj) {
1049 HMI_DEBUG("wm:jh", "File input is success");
1054 json_error = json_tokener_get_error(tokener);
1055 if ((json_tokener_continue != json_error)
1056 || (input_size > len)) {
1057 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1058 (input_size * block_cnt), json_tokener_error_desc(json_error));
1059 HMI_ERROR("wm:jh", "\n%s", buffer);
1069 // Free json_tokener
1070 json_tokener_free(tokener);
1075 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1076 // Parse string by delimiter
1077 std::vector<std::string> vct;
1078 std::stringstream ss{str};
1080 while (std::getline(ss, buf, delimiter)) {
1088 std::string PolicyManager::deleteSpace(std::string str) {
1089 std::string ret = str;
1091 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1097 const char* kDefaultRoleDb = "{ \
1100 \"category\": \"homescreen\", \
1101 \"role\": \"homescreen\", \
1102 \"area\": \"full\", \
1105 \"category\": \"map\", \
1106 \"role\": \"map\", \
1107 \"area\": \"full | normal | split.main\", \
1110 \"category\": \"general\", \
1111 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1112 \"area\": \"normal\", \
1115 \"category\": \"phone\", \
1116 \"role\": \"phone\", \
1117 \"area\": \"normal\", \
1120 \"category\": \"splitable\", \
1121 \"role\": \"splitable1 | splitable2\", \
1122 \"area\": \"normal | split.main | split.sub\", \
1125 \"category\": \"popup\", \
1126 \"role\": \"popup\", \
1127 \"area\": \"on_screen\", \
1130 \"category\": \"system_alert\", \
1131 \"role\": \"system_alert\", \
1132 \"area\": \"on_screen\", \
1135 \"category\": \"tbt\", \
1136 \"role\": \"tbt\", \
1137 \"area\": \"hud\", \
1143 const char* kDefaultLayoutDb = "{ \
1147 \"layer\": \"on_screen\", \
1150 \"name\": \"pop_up\", \
1151 \"role\": \"incomming_call\" \
1157 \"layer\": \"on_screen\", \
1160 \"name\": \"system_alert\", \
1161 \"role\": \"system_alert\" \
1167 \"layer\": \"apps\", \
1170 \"name\": \"normal\", \
1177 \"layer\": \"apps\", \
1180 \"name\": \"split.main\", \
1184 \"name\": \"split.sub\", \
1185 \"category\": \"hvac\" \
1191 \"layer\": \"apps\", \
1194 \"name\": \"full\", \
1201 \"layer\": \"apps\", \
1204 \"name\": \"normal\", \
1205 \"category\": \"splitable\" \
1211 \"layer\": \"apps\", \
1214 \"name\": \"split.main\", \
1215 \"category\": \"splitable\" \
1218 \"name\": \"split.sub\", \
1219 \"category\": \"splitable\" \
1225 \"layer\": \"apps\", \
1228 \"name\": \"normal\", \
1229 \"category\": \"general\" \
1235 \"layer\": \"homescreen\", \
1238 \"name\": \"full\", \
1239 \"role\": \"homescreen\" \
1246 \"name\": \"normal\", \
1255 \"name\": \"split.main\", \
1264 \"name\": \"split.sub\", \
1273 \"name\": \"full\", \
1282 \"name\": \"pop_up\", \
1291 \"name\": \"system_alert\", \