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"
25 #include "dummy_stm.h"
28 #include "hmi-debug.h"
31 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 = 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 = 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 = 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);
124 HMI_DEBUG("wm:pm", "set event:0x%x", (event_no | category_no | area_no));
125 int ret = stm::stmTransitionState((event_no | category_no | area_no),
126 &(this->current_state_));
128 HMI_ERROR("wm:pm", "Error!!");
134 // "parking_brake": {
135 // "is_changed": <bool>,
136 // "state": <const char*>
138 HMI_DEBUG("wm", "parking brake state (is_changed:%d state:%d:%s)",
139 this->current_state_.parking_brake.is_changed,
140 this->current_state_.parking_brake.state,
141 stm::gStmParkingBrakeStateNo2Name[this->current_state_.parking_brake.state]);
142 this->addStateToJson("parking_brake",
143 this->current_state_.parking_brake.is_changed,
144 stm::gStmParkingBrakeStateNo2Name[this->current_state_.parking_brake.state],
148 // "is_changed": <bool>,
149 // "state": <const char*>
151 HMI_DEBUG("wm", "car state (is_changed:%d state:%d:%s)",
152 this->current_state_.car.is_changed,
153 this->current_state_.car.state,
154 stm::gStmCarStateNo2Name[this->current_state_.car.state]);
155 this->addStateToJson("car",
156 this->current_state_.car.is_changed,
157 stm::gStmCarStateNo2Name[this->current_state_.car.state],
161 // "is_changed": <bool>,
162 // "state": <const char*>
164 HMI_DEBUG("wm", "lamp state (is_changed:%d state:%d:%s)",
165 this->current_state_.lamp.is_changed,
166 this->current_state_.lamp.state,
167 stm::gStmLampStateNo2Name[this->current_state_.lamp.state]);
168 this->addStateToJson("lamp",
169 this->current_state_.lamp.is_changed,
170 stm::gStmLampStateNo2Name[this->current_state_.lamp.state],
176 // "is_changed": <bool>,
177 // "state": <const char*>
180 json_object* json_layer = json_object_new_array();
181 json_object* json_tmp = json_object_new_object();
182 this->addStateToJson("on_screen",
183 this->current_state_.layer.on_screen.is_changed,
184 stm::gStmLayoutNo2Name[this->current_state_.layer.on_screen.state],
186 json_object_array_add(json_layer, json_tmp);
190 // "is_changed": <bool>,
191 // "state": <const char*>
194 json_tmp = json_object_new_object();
195 this->addStateToJson("restriction",
196 this->current_state_.layer.restriction.is_changed,
197 stm::gStmLayoutNo2Name[this->current_state_.layer.restriction.state],
199 json_object_array_add(json_layer, json_tmp);
203 // "is_changed": <bool>,
204 // "state": <const char*>
207 json_tmp = json_object_new_object();
208 this->addStateToJson("apps",
209 this->current_state_.layer.apps.is_changed,
210 stm::gStmLayoutNo2Name[this->current_state_.layer.apps.state],
212 json_object_array_add(json_layer, json_tmp);
216 // "is_changed": <bool>,
217 // "state": <const char*>
222 json_tmp = json_object_new_object();
223 this->addStateToJson("homescreen",
224 this->current_state_.layer.homescreen.is_changed,
225 stm::gStmLayoutNo2Name[this->current_state_.layer.homescreen.state],
227 json_object_array_add(json_layer, json_tmp);
229 // Add json array of layer
230 json_object_object_add(*json_out, "layers", json_layer);
235 std::string PolicyManager::roleToCategory(const char* role) {
236 return this->role2category_[role];
239 extern const char* kDefaultRoleDb;
240 int PolicyManager::loadRoleDb() {
241 HMI_DEBUG("wm:pm", "Call");
243 std::string file_name;
245 // Get afm application installed dir
246 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
247 HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
249 if (!afm_app_install_dir) {
250 HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
253 file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
257 HMI_DEBUG("wm:pm", "file_name:%s", file_name.c_str());
258 json_object* json_obj = json_object_from_file(file_name.c_str());
259 if (nullptr == json_obj) {
260 HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
261 json_obj = json_tokener_parse(kDefaultRoleDb);
263 HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
265 json_object* json_roles;
266 if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
267 HMI_ERROR("wm:pm", "Parse Error!!");
271 int len = json_object_array_length(json_roles);
272 HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
273 HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
275 json_object* json_tmp;
276 const char* category;
279 for (int i=0; i<len; i++) {
280 json_tmp = json_object_array_get_idx(json_roles, i);
282 category = this->getStringFromJson(json_tmp, "category");
283 roles = this->getStringFromJson(json_tmp, "role");
284 areas = this->getStringFromJson(json_tmp, "area");
286 if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
287 HMI_ERROR("wm:pm", "Parse Error!!");
291 // Parse roles by '|'
292 std::vector<std::string> vct_roles;
293 vct_roles = this->parseString(std::string(roles), '|');
295 // Parse areas by '|'
296 std::vector<std::string> vct_areas;
297 vct_areas = this->parseString(std::string(areas), '|');
299 // Set role, category, default area
300 for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
301 // Delete space from role and area name
302 std::string role = this->deleteSpace(*itr);
303 std::string area = this->deleteSpace(vct_areas[0]);
305 this->role2category_[role] = std::string(category);
306 this->role2defaultarea_[role] = area;
309 this->category2role_[std::string(category)] = std::string(roles);
313 HMI_DEBUG("wm:pm", "Check role2category_");
314 for (auto& x:this->role2category_){
315 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
318 HMI_DEBUG("wm:pm", "Check role2defaultarea_");
319 for (auto& x:this->role2defaultarea_){
320 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
323 HMI_DEBUG("wm:pm", "Check category2role_");
324 for (auto& x:this->category2role_){
325 HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
331 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
332 if ((nullptr == obj) || (nullptr == key)) {
333 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
338 if (!json_object_object_get_ex(obj, key, &tmp)) {
339 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
343 return json_object_get_string(tmp);
346 int PolicyManager::getIntFromJson(json_object* obj, const char* key) {
347 if ((nullptr == obj) || (nullptr == key)) {
348 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
353 if (!json_object_object_get_ex(obj, key, &tmp)) {
354 HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
358 return json_object_get_int(tmp);
361 void PolicyManager::addStateToJson(
362 const char* key, int is_changed, const char* state, json_object** json_out) {
363 if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
364 HMI_ERROR("wm:pm", "Argument is nullptr!!!");
368 json_object* json_obj = json_object_new_object();
369 json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
371 HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
372 json_object_object_add(json_obj, "state", json_object_new_string(state));
374 json_object_object_add(*json_out, key, json_obj);
377 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
378 // Parse string by delimiter
379 std::vector<std::string> vct;
380 std::stringstream ss{str};
382 while (std::getline(ss, buf, delimiter)) {
390 std::string PolicyManager::deleteSpace(std::string str) {
391 std::string ret = str;
393 while ((pos = ret.find_first_of(" ")) != std::string::npos) {
399 const char* kDefaultRoleDb = "{ \
402 \"category\": \"homescreen\", \
403 \"role\": \"homescreen\", \
404 \"area\": \"full\", \
407 \"category\": \"map\", \
409 \"area\": \"full | normal | split.main\", \
412 \"category\": \"general\", \
413 \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
414 \"area\": \"normal\", \
417 \"category\": \"phone\", \
418 \"role\": \"phone\", \
419 \"area\": \"normal\", \
422 \"category\": \"splitable\", \
423 \"role\": \"splitable1 | splitable2\", \
424 \"area\": \"normal | split.main | split.sub\", \
427 \"category\": \"popup\", \
428 \"role\": \"popup\", \
429 \"area\": \"on_screen\", \
432 \"category\": \"system_alert\", \
433 \"role\": \"system_alert\", \
434 \"area\": \"on_screen\", \
437 \"category\": \"tbt\", \