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.
21 #include <json-c/json.h>
22 #include "policy_manager.hpp"
23 #include "hmi-debug.h"
27 #include "dummy_stm.h"
32 PolicyManager::PolicyManager() :
40 HMI_DEBUG("wm:pm", "Call");
43 int PolicyManager::initialize() {
44 HMI_DEBUG("wm:pm", "Call");
49 for (unsigned int i=0; i<STM_NUM_EVT; i++) {
50 HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
51 this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
54 for (unsigned int i=0; i<STM_NUM_CTG; i++) {
55 HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
56 this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
59 for (unsigned int i=0; i<STM_NUM_ARA; i++) {
60 HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
61 this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
67 HMI_ERROR("wm:pm", "Load role.db Error!!");
71 // Initialize StateTransitioner
77 int PolicyManager::checkPolicy(json_object* json_in, json_object** json_out) {
78 HMI_DEBUG("wm:pm", "Call");
81 if ((nullptr == json_in) || (nullptr == json_out)) {
82 HMI_ERROR("wm:pm", "Argument is NULL!!");
86 // Get event from json_object
87 const char* event = this->getStringFromJson(json_in, "event");
89 if (nullptr != event) {
90 // Convert name to number
91 event_no = this->eventname2no_[event];
92 HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
95 // Get role from json_object
96 const char* role = this->getStringFromJson(json_in, "role");
98 if (nullptr != role) {
99 HMI_DEBUG("wm:pm", "role(%s)", role);
101 // Convert role to category
102 const char* category = this->role2category_[role].c_str();
103 if (0 == strcmp("", category)) {
104 HMI_ERROR("wm:pm", "Error!!");
107 HMI_DEBUG("wm:pm", "category(%s)", category);
109 // Convert name to number
110 category_no = categoryname2no_[category];
111 HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
114 // Get areat from json_object
115 const char* area = this->getStringFromJson(json_in, "area");
117 if (nullptr != area) {
118 // Convert name to number
119 area_no = areaname2no_[area];
120 HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
123 HMI_DEBUG("wm:pm", "set event:0x%x", (event_no | category_no | area_no));
126 stm::stm_state_t crr_state;
127 int ret = stm::stmTransitionState((event_no | category_no | area_no), &crr_state);
129 HMI_ERROR("wm:pm", "Error!!");
135 // "parking_brake": {
136 // "is_changed": <bool>,
137 // "state": <const char*>
139 HMI_DEBUG("wm", "parking brake state (is_changed:%d state:%d:%s)",
140 crr_state.parking_brake.is_changed,
141 crr_state.parking_brake.state,
142 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
143 this->addStateToJson("parking_brake",
144 crr_state.parking_brake.is_changed,
145 stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
149 // "is_changed": <bool>,
150 // "state": <const char*>
152 HMI_DEBUG("wm", "car state (is_changed:%d state:%d:%s)",
153 crr_state.car.is_changed,
155 stm::gStmCarStateNo2Name[crr_state.car.state]);
156 this->addStateToJson("car",
157 crr_state.car.is_changed,
158 stm::gStmCarStateNo2Name[crr_state.car.state],
162 // "is_changed": <bool>,
163 // "state": <const char*>
165 HMI_DEBUG("wm", "lamp state (is_changed:%d state:%d:%s)",
166 crr_state.lamp.is_changed,
167 crr_state.lamp.state,
168 stm::gStmLampStateNo2Name[crr_state.lamp.state]);
169 this->addStateToJson("lamp",
170 crr_state.lamp.is_changed,
171 stm::gStmLampStateNo2Name[crr_state.lamp.state],
177 // "is_changed": <bool>,
178 // "state": <const char*>
181 json_object* json_layer = json_object_new_array();
182 json_object* json_tmp = json_object_new_object();
183 HMI_DEBUG("wm", "on_screen state (is_changed:%d state:%d:%s)",
184 crr_state.layer.on_screen.is_changed,
185 crr_state.layer.on_screen.state,
186 stm::gStmLayoutNo2Name[crr_state.layer.on_screen.state]);
187 this->addStateToJson("on_screen",
188 crr_state.layer.on_screen.is_changed,
189 stm::gStmLayoutNo2Name[crr_state.layer.on_screen.state],
191 json_object_array_add(json_layer, json_tmp);
195 // "is_changed": <bool>,
196 // "state": <const char*>
199 HMI_DEBUG("wm", "restriction state (is_changed:%d state:%d:%s)",
200 crr_state.layer.restriction.is_changed,
201 crr_state.layer.restriction.state,
202 stm::gStmLayoutNo2Name[crr_state.layer.restriction.state]);
203 json_tmp = json_object_new_object();
204 this->addStateToJson("restriction",
205 crr_state.layer.restriction.is_changed,
206 stm::gStmLayoutNo2Name[crr_state.layer.restriction.state],
208 json_object_array_add(json_layer, json_tmp);
212 // "is_changed": <bool>,
213 // "state": <const char*>
216 HMI_DEBUG("wm", "apps state (is_changed:%d state:%d:%s)",
217 crr_state.layer.apps.is_changed,
218 crr_state.layer.apps.state,
219 stm::gStmLayoutNo2Name[crr_state.layer.apps.state]);
220 json_tmp = json_object_new_object();
221 this->addStateToJson("apps",
222 crr_state.layer.apps.is_changed,
223 stm::gStmLayoutNo2Name[crr_state.layer.apps.state],
225 json_object_array_add(json_layer, json_tmp);
229 // "is_changed": <bool>,
230 // "state": <const char*>
235 HMI_DEBUG("wm", "homescreen state (is_changed:%d state:%d:%s)",
236 crr_state.layer.homescreen.is_changed,
237 crr_state.layer.homescreen.state,
238 stm::gStmLayoutNo2Name[crr_state.layer.homescreen.state]);
239 json_tmp = json_object_new_object();
240 this->addStateToJson("homescreen",
241 crr_state.layer.homescreen.is_changed,
242 stm::gStmLayoutNo2Name[crr_state.layer.homescreen.state],
244 json_object_array_add(json_layer, json_tmp);
246 // Add json array of layer
247 json_object_object_add(*json_out, "layers", json_layer);
252 std::string PolicyManager::roleToCategory(const char* role) {
253 return this->role2category_[role];
256 extern const char* kDefaultRoleDb;
257 int PolicyManager::loadRoleDb() {
258 HMI_DEBUG("wm:pm", "Call");
260 std::string file_name;
262 // Get afm application installed dir
263 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
264 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
266 if (!afm_app_install_dir) {
267 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
270 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
274 json_object* json_obj;
275 int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
277 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
278 json_obj = json_tokener_parse(kDefaultRoleDb);
280 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
282 json_object* json_roles;
283 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
284 HMI_ERROR("wm:pm", "Parse Error!!");
288 int len = json_object_array_length(json_roles);
289 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
290 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
292 json_object* json_tmp;
293 const char* category;
296 for (int i=0; i<len; i++) {
297 json_tmp = json_object_array_get_idx(json_roles, i);
299 category = this->getStringFromJson(json_tmp, "category");
300 roles = this->getStringFromJson(json_tmp, "role");
301 areas = this->getStringFromJson(json_tmp, "area");
303 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
304 HMI_ERROR("wm:pm", "Parse Error!!");
308 // Parse roles by '|'
309 std::vector<std::string> vct_roles;
310 vct_roles = this->parseString(std::string(roles), '|');
312 // Parse areas by '|'
313 std::vector<std::string> vct_areas;
314 vct_areas = this->parseString(std::string(areas), '|');
316 // Set role, category, default area
317 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
318 // Delete space from role and area name
319 std::string role = this->deleteSpace(*itr);
320 std::string area = this->deleteSpace(vct_areas[0]);
322 this->role2category_[role] = std::string(category);
323 this->role2defaultarea_[role] = area;
326 this->category2role_[std::string(category)] = std::string(roles);
330 HMI_DEBUG("wm:pm", "Check role2category_");
331 for (auto& x:this->role2category_){
332 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
335 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
336 for (auto& x:this->role2defaultarea_){
337 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
340 HMI_DEBUG("wm:pm", "Check category2role_");
341 for (auto& x:this->category2role_){
342 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
349 // This function will be removed because json_helper has same function.
350 // json_helper should be library.
351 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
352 if ((nullptr == obj) || (nullptr == key)) {
353 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
358 if (!json_object_object_get_ex(obj, key, &tmp)) {
359 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
363 return json_object_get_string(tmp);
367 // This function will be removed because json_helper has same function.
368 // json_helper should be library.
369 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
370 const int input_size = 128;
373 if ((nullptr == file) || (nullptr == obj)) {
374 HMI_ERROR("wm:jh", "Argument is nullptr!!!");
378 HMI_DEBUG("wm:jh", "Input file: %s", file);
381 FILE *fp = fopen(file, "rb");
383 HMI_ERROR("wm:jh", "Could not open file");
388 struct json_tokener *tokener = json_tokener_new();
389 enum json_tokener_error json_error;
390 char buffer[input_size];
393 size_t len = fread(buffer, sizeof(char), input_size, fp);
394 *obj = json_tokener_parse_ex(tokener, buffer, len);
395 if (nullptr != *obj) {
396 HMI_DEBUG("wm:jh", "File input is success");
401 json_error = json_tokener_get_error(tokener);
402 if ((json_tokener_continue != json_error)
403 || (input_size > len)) {
404 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
405 (input_size * block_cnt), json_tokener_error_desc(json_error));
406 HMI_ERROR("wm:jh", "\n%s", buffer);
417 json_tokener_free(tokener);
422 void PolicyManager::addStateToJson(
423 const char* key, int is_changed, const char* state, json_object** json_out) {
424 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
425 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
429 json_object* json_obj = json_object_new_object();
430 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
432 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
433 json_object_object_add(json_obj, "state", json_object_new_string(state));
435 json_object_object_add(*json_out, key, json_obj);
438 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
439 // Parse string by delimiter
440 std::vector<std::string> vct;
441 std::stringstream ss{str};
443 while (std::getline(ss, buf, delimiter)) {
451 std::string PolicyManager::deleteSpace(std::string str) {
452 std::string ret = str;
454 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
460 const char* kDefaultRoleDb = "{ \
463 \"category\": \"homescreen\", \
464 \"role\": \"homescreen\", \
465 \"area\": \"full\", \
468 \"category\": \"map\", \
470 \"area\": \"full | normal | split.main\", \
473 \"category\": \"general\", \
474 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
475 \"area\": \"normal\", \
478 \"category\": \"phone\", \
479 \"role\": \"phone\", \
480 \"area\": \"normal\", \
483 \"category\": \"splitable\", \
484 \"role\": \"splitable1 | splitable2\", \
485 \"area\": \"normal | split.main | split.sub\", \
488 \"category\": \"popup\", \
489 \"role\": \"popup\", \
490 \"area\": \"on_screen\", \
493 \"category\": \"system_alert\", \
494 \"role\": \"system_alert\", \
495 \"area\": \"on_screen\", \
498 \"category\": \"tbt\", \