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 struct sd_event* event_loop;
37 std::map<int, struct sd_event_source*> event_source_list;
38 PolicyManager::CallbackTable callback;
42 PolicyManager::PolicyManager() :
50 HMI_DEBUG("wm:pm", "Call");
53 int PolicyManager::initialize() {
54 HMI_DEBUG("wm:pm", "Call");
59 for (unsigned int i=0; i<STM_NUM_EVT; i++) {
60 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
61 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
64 for (unsigned int i=0; i<STM_NUM_CTG; i++) {
65 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
66 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
69 for (unsigned int i=0; i<STM_NUM_ARA; i++) {
70 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
71 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
75 ret = this->loadRoleDb();
77 HMI_ERROR("wm:pm", "Load role.db Error!!");
81 // Initialize StateTransitioner
84 // Initialize sd_event loop
85 ret = this->initializeSdEventLoop();
87 HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
94 int PolicyManager::initializeSdEventLoop() {
95 // Get default event loop object
96 int ret = sd_event_new(&(pm::event_loop));
98 HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
102 // Create thread for sd_event and detach
103 std::thread sd_event_loop([this]() {
105 sd_event_run(pm::event_loop, 1000);
108 sd_event_loop.detach();
113 static void addStateToJson(
114 const char* key, int is_changed, const char* state, json_object** json_out) {
115 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
116 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
120 json_object* json_obj = json_object_new_object();
121 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
123 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
124 json_object_object_add(json_obj, "state", json_object_new_string(state));
126 json_object_object_add(*json_out, key, json_obj);
129 static int checkPolicyEntry(int event, uint64_t delay_ms);
130 static int checkPolicy(sd_event_source *source, void *data) {
131 HMI_DEBUG("wm:pm", "Call");
132 HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
134 int event = *((int*)data);
136 int event_no, category_no, area_no;
137 event_no = event & STM_MSK_EVT_NO;
138 category_no = event & STM_MSK_CTG_NO;
139 area_no = event & STM_MSK_ARA_NO;
140 HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
141 stm::gStmEventName[event_no - 1],
142 stm::gStmCategoryName[(category_no >> 8) - 1],
143 stm::gStmAreaName[(area_no >> 16) - 1]);
146 stm::stm_state_t crr_state;
147 int ret = stm::stmTransitionState(event, &crr_state);
149 HMI_ERROR("wm:pm", "Error!!");
153 HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
154 crr_state.parking_brake.is_changed,
155 crr_state.parking_brake.state,
156 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
157 HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
158 crr_state.accel_pedal.is_changed,
159 crr_state.accel_pedal.state,
160 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
161 HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
162 crr_state.lightstatus_brake.is_changed,
163 crr_state.lightstatus_brake.state,
164 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
165 HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
166 crr_state.car.is_changed,
168 stm::gStmCarStateNo2Name[crr_state.car.state]);
169 HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
170 crr_state.lamp.is_changed,
171 crr_state.lamp.state,
172 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
173 HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
174 crr_state.restriction_mode.is_changed,
175 crr_state.restriction_mode.state,
176 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
177 HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
178 crr_state.layer.homescreen.is_changed,
179 crr_state.layer.homescreen.state,
180 stm::gStmLayoutNo2Name[crr_state.layer.homescreen.state]);
181 HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
182 crr_state.layer.apps.is_changed,
183 crr_state.layer.apps.state,
184 stm::gStmLayoutNo2Name[crr_state.layer.apps.state]);
185 HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
186 crr_state.layer.restriction.is_changed,
187 crr_state.layer.restriction.state,
188 stm::gStmLayoutNo2Name[crr_state.layer.restriction.state]);
189 HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
190 crr_state.layer.on_screen.is_changed,
191 crr_state.layer.on_screen.state,
192 stm::gStmLayoutNo2Name[crr_state.layer.on_screen.state]);
194 json_object* json_out = json_object_new_object();
198 // "parking_brake": {
199 // "is_changed": <bool>,
200 // "state": <const char*>
202 addStateToJson("parking_brake",
203 crr_state.parking_brake.is_changed,
204 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
208 // "is_changed": <bool>,
209 // "state": <const char*>
211 addStateToJson("accel_pedal",
212 crr_state.accel_pedal.is_changed,
213 stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
216 // "lightstatus_brake": {
217 // "is_changed": <bool>,
218 // "state": <const char*>
220 addStateToJson("lightstatus_brake",
221 crr_state.lightstatus_brake.is_changed,
222 stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
226 // "is_changed": <bool>,
227 // "state": <const char*>
229 addStateToJson("car",
230 crr_state.car.is_changed,
231 stm::gStmCarStateNo2Name[crr_state.car.state],
235 // "is_changed": <bool>,
236 // "state": <const char*>
238 addStateToJson("lamp",
239 crr_state.lamp.is_changed,
240 stm::gStmLampStateNo2Name[crr_state.lamp.state],
243 // "restriction_mode": {
244 // "is_changed": <bool>,
245 // "state": <const char*>
247 addStateToJson("restriction_mode",
248 crr_state.restriction_mode.is_changed,
249 stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
253 json_object* json_layer = json_object_new_array();
254 json_object* json_tmp;
258 // "is_changed": <bool>,
259 // "state": <const char*>
264 json_tmp = json_object_new_object();
265 addStateToJson("homescreen",
266 crr_state.layer.homescreen.is_changed,
267 stm::gStmLayoutNo2Name[crr_state.layer.homescreen.state],
269 json_object_array_add(json_layer, json_tmp);
273 // "is_changed": <bool>,
274 // "state": <const char*>
277 json_tmp = json_object_new_object();
278 addStateToJson("apps",
279 crr_state.layer.apps.is_changed,
280 stm::gStmLayoutNo2Name[crr_state.layer.apps.state],
282 json_object_array_add(json_layer, json_tmp);
286 // "is_changed": <bool>,
287 // "state": <const char*>
290 json_tmp = json_object_new_object();
291 addStateToJson("restriction",
292 crr_state.layer.restriction.is_changed,
293 stm::gStmLayoutNo2Name[crr_state.layer.restriction.state],
295 json_object_array_add(json_layer, json_tmp);
299 // "is_changed": <bool>,
300 // "state": <const char*>
303 json_tmp = json_object_new_object();
304 addStateToJson("on_screen",
305 crr_state.layer.on_screen.is_changed,
306 stm::gStmLayoutNo2Name[crr_state.layer.on_screen.state],
308 json_object_array_add(json_layer, json_tmp);
310 // Add json array of layer
311 json_object_object_add(json_out, "layers", json_layer);
313 // Notify state is changed
314 if (nullptr != pm::callback.onStateTransitioned) {
315 pm::callback.onStateTransitioned(json_out);
318 if (crr_state.car.is_changed) {
319 if (stm::gStmCarStateNoRun == crr_state.car.state) {
320 // Set delay event(restriction mode on)
321 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000);
323 else if (stm::gStmCarStateNoStop == crr_state.car.state) {
324 // Set event(restriction mode off)
325 checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0);
327 // Stop timer for restriction on event
328 if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
329 != pm::event_source_list.end()) {
330 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
331 sd_event_source *event_source
332 = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
333 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
335 HMI_ERROR("wm:pm", "Failed to stop timer");
341 // Release json_object
342 json_object_put(json_out);
347 // Destroy sd_event_source object
348 sd_event_source_unref(source);
350 // Remove event source from list
351 if (pm::event_source_list.find(event) != pm::event_source_list.end()) {
352 pm::event_source_list.erase(event);
355 HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
359 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
360 checkPolicy(source, data);
363 static int checkPolicyEntry(int event, uint64_t delay_ms)
365 HMI_DEBUG("wm:pm", "Call");
366 HMI_DEBUG("wm:pm", "event:0x%x", event);
369 int ret = sd_event_add_defer(pm::event_loop, NULL,
370 &checkPolicy, new int(event));
372 HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
378 struct timespec time_spec;
379 clock_gettime(CLOCK_MONOTONIC, &time_spec);
381 // Calculate timer fired time
382 uint64_t usec = (time_spec.tv_sec * 1000000)
383 + (time_spec.tv_nsec / 1000)
387 struct sd_event_source* event_source;
388 int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
389 &timerEvent, new int(event));
391 HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
395 // Store event source
396 pm::event_source_list[event] = event_source;
402 void PolicyManager::registerCallback(CallbackTable callback) {
403 pm::callback.onStateTransitioned = callback.onStateTransitioned;
404 pm::callback.onError = callback.onError;
407 int PolicyManager::inputEvent(json_object* json_in) {
408 HMI_DEBUG("wm:pm", "Call");
411 if (nullptr == json_in) {
412 HMI_ERROR("wm:pm", "Argument is NULL!!");
416 // Get event from json_object
417 const char* event = this->getStringFromJson(json_in, "event");
419 if (nullptr != event) {
420 // Convert name to number
421 event_no = this->eventname2no_[event];
422 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
425 // Get role from json_object
426 const char* role = this->getStringFromJson(json_in, "role");
428 if (nullptr != role) {
429 HMI_DEBUG("wm:pm", "role(%s)", role);
431 // Convert role to category
432 const char* category = this->role2category_[role].c_str();
433 if (0 == strcmp("", category)) {
434 HMI_ERROR("wm:pm", "Error!!");
437 HMI_DEBUG("wm:pm", "category(%s)", category);
439 // Convert name to number
440 category_no = categoryname2no_[category];
441 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
444 // Get areat from json_object
445 const char* area = this->getStringFromJson(json_in, "area");
447 if (nullptr != area) {
448 // Convert name to number
449 area_no = areaname2no_[area];
450 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
454 checkPolicyEntry((event_no | category_no | area_no), 0);
459 std::string PolicyManager::roleToCategory(const char* role) {
460 return this->role2category_[role];
463 extern const char* kDefaultRoleDb;
464 int PolicyManager::loadRoleDb() {
465 HMI_DEBUG("wm:pm", "Call");
467 std::string file_name;
469 // Get afm application installed dir
470 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
471 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
473 if (!afm_app_install_dir) {
474 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
477 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
481 json_object* json_obj;
482 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
484 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
485 json_obj = json_tokener_parse(kDefaultRoleDb);
487 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
489 json_object* json_roles;
490 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
491 HMI_ERROR("wm:pm", "Parse Error!!");
495 int len = json_object_array_length(json_roles);
496 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
497 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
499 json_object* json_tmp;
500 const char* category;
503 for (int i=0; i<len; i++) {
504 json_tmp = json_object_array_get_idx(json_roles, i);
506 category = this->getStringFromJson(json_tmp, "category");
507 roles = this->getStringFromJson(json_tmp, "role");
508 areas = this->getStringFromJson(json_tmp, "area");
510 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
511 HMI_ERROR("wm:pm", "Parse Error!!");
515 // Parse roles by '|'
516 std::vector<std::string> vct_roles;
517 vct_roles = this->parseString(std::string(roles), '|');
519 // Parse areas by '|'
520 std::vector<std::string> vct_areas;
521 vct_areas = this->parseString(std::string(areas), '|');
523 // Set role, category, default area
524 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
525 // Delete space from role and area name
526 std::string role = this->deleteSpace(*itr);
527 std::string area = this->deleteSpace(vct_areas[0]);
529 this->role2category_[role] = std::string(category);
530 this->role2defaultarea_[role] = area;
533 this->category2role_[std::string(category)] = std::string(roles);
537 HMI_DEBUG("wm:pm", "Check role2category_");
538 for (auto& x:this->role2category_){
539 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
542 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
543 for (auto& x:this->role2defaultarea_){
544 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
547 HMI_DEBUG("wm:pm", "Check category2role_");
548 for (auto& x:this->category2role_){
549 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
556 // This function will be removed because json_helper has same function.
557 // json_helper should be library.
558 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
559 if ((nullptr == obj) || (nullptr == key)) {
560 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
565 if (!json_object_object_get_ex(obj, key, &tmp)) {
566 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
570 return json_object_get_string(tmp);
574 // This function will be removed because json_helper has same function.
575 // json_helper should be library.
576 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
577 const int input_size = 128;
580 if ((nullptr == file) || (nullptr == obj)) {
581 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
585 HMI_DEBUG("wm:jh", "Input file: %s", file);
588 FILE *fp = fopen(file, "rb");
590 HMI_ERROR("wm:jh", "Could not open file");
595 struct json_tokener *tokener = json_tokener_new();
596 enum json_tokener_error json_error;
597 char buffer[input_size];
600 size_t len = fread(buffer, sizeof(char), input_size, fp);
601 *obj = json_tokener_parse_ex(tokener, buffer, len);
602 if (nullptr != *obj) {
603 HMI_DEBUG("wm:jh", "File input is success");
608 json_error = json_tokener_get_error(tokener);
609 if ((json_tokener_continue != json_error)
610 || (input_size > len)) {
611 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
612 (input_size * block_cnt), json_tokener_error_desc(json_error));
613 HMI_ERROR("wm:jh", "\n%s", buffer);
624 json_tokener_free(tokener);
629 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
630 // Parse string by delimiter
631 std::vector<std::string> vct;
632 std::stringstream ss{str};
634 while (std::getline(ss, buf, delimiter)) {
642 std::string PolicyManager::deleteSpace(std::string str) {
643 std::string ret = str;
645 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
651 const char* kDefaultRoleDb = "{ \
654 \"category\": \"homescreen\", \
655 \"role\": \"homescreen\", \
656 \"area\": \"full\", \
659 \"category\": \"map\", \
661 \"area\": \"full | normal | split.main\", \
664 \"category\": \"general\", \
665 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
666 \"area\": \"normal\", \
669 \"category\": \"phone\", \
670 \"role\": \"phone\", \
671 \"area\": \"normal\", \
674 \"category\": \"splitable\", \
675 \"role\": \"splitable1 | splitable2\", \
676 \"area\": \"normal | split.main | split.sub\", \
679 \"category\": \"popup\", \
680 \"role\": \"popup\", \
681 \"area\": \"on_screen\", \
684 \"category\": \"system_alert\", \
685 \"role\": \"system_alert\", \
686 \"area\": \"on_screen\", \
689 \"category\": \"tbt\", \