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.
17 #include "pm_wrapper.hpp"
18 #include "json_helper.hpp"
24 static PMWrapper *g_context;
29 static void onStateTransitioned(json_object *json_out)
31 g_context->updateStates(json_out);
34 static void onError(json_object *json_out)
36 HMI_DEBUG("error message from PolicyManager:%s",
37 json_object_get_string(json_out));
39 g_context->processError();
44 PMWrapper::PMWrapper() {}
46 int PMWrapper::initialize()
50 ret = this->pm.initialize();
53 HMI_ERROR("wm:pmw", "Faild to initialize PolicyManager");
61 void PMWrapper::registerCallback(StateTransitionHandler on_state_transitioned,
62 ErrorHandler on_error)
64 this->on_state_transitioned = on_state_transitioned;
65 this->on_error = on_error;
67 PolicyManager::CallbackTable my_callback;
68 my_callback.onStateTransitioned = onStateTransitioned;
69 my_callback.onError = onError;
70 this->pm.registerCallback(my_callback);
73 int PMWrapper::setInputEventData(Task task, std::string role, std::string area)
76 if (Task::TASK_ALLOCATE == task)
80 else if (Task::TASK_RELEASE == task)
89 json_object *json_in = json_object_new_object();
90 json_object_object_add(json_in, "event", json_object_new_string(event));
91 json_object_object_add(json_in, "role", json_object_new_string(role.c_str()));
92 json_object_object_add(json_in, "area", json_object_new_string(area.c_str()));
95 ret = this->pm.setInputEventData(json_in);
98 HMI_ERROR("wm:pmw", "Faild to set input event data to PolicyManager");
100 json_object_put(json_in);
105 int PMWrapper::executeStateTransition()
108 ret = this->pm.executeStateTransition();
111 HMI_ERROR("wm:pmw", "Failed to execute state transition for PolicyManager");
117 void PMWrapper::undoState()
119 this->pm.undoState();
121 this->crrlayer2rolestate = this->prvlayer2rolestate;
124 void PMWrapper::updateStates(json_object *json_out)
126 std::vector<WMAction> actions;
128 HMI_DEBUG("json_out dump:%s", json_object_get_string(json_out));
130 this->createLayoutChangeAction(json_out, actions);
132 this->on_state_transitioned(actions);
135 void PMWrapper::createLayoutChangeAction(json_object *json_out, std::vector<WMAction> &actions)
137 // Get displayed roles from previous layout
138 json_object *json_layers;
139 if (!json_object_object_get_ex(json_out, "layers", &json_layers))
141 HMI_DEBUG("Not found key \"layers\"");
145 int len = json_object_array_length(json_layers);
146 HMI_DEBUG("json_layers len:%d", len);
148 for (int i = 0; i < len; i++)
150 json_object *json_tmp = json_object_array_get_idx(json_layers, i);
152 std::string layer_name = jh::getStringFromJson(json_tmp, "name");
153 json_bool changed = jh::getBoolFromJson(json_tmp, "changed");
154 HMI_DEBUG("layer:%s changed:%d", layer_name.c_str(), changed);
158 json_object *json_areas;
159 if (!json_object_object_get_ex(json_tmp, "areas", &json_areas))
161 HMI_DEBUG("Not found key \"areas\"");
165 int len = json_object_array_length(json_areas);
166 HMI_DEBUG("json_layers len:%d", len);
168 // Store previous role state in this layer
169 this->prvlayer2rolestate[layer_name] = this->crrlayer2rolestate[layer_name];
172 RoleState prv_roles = this->prvlayer2rolestate[layer_name];
173 for (int j = 0; j < len; j++)
175 json_object *json_tmp2 = json_object_array_get_idx(json_areas, j);
177 std::string area_name = jh::getStringFromJson(json_tmp2, "name");
178 std::string role_name = jh::getStringFromJson(json_tmp2, "role");
180 crr_roles[role_name] = area_name;
182 auto i_prv = prv_roles.find(role_name);
183 HMI_DEBUG("current role:%s area:%s",
184 role_name.c_str(), area_name.c_str());
186 // If current role does not exist in previous
187 if (prv_roles.end() == i_prv)
189 HMI_DEBUG("current role does not exist in previous");
191 // Set activate action
192 bool end_draw_finished = false;
199 TaskVisible::VISIBLE,
202 actions.push_back(act);
206 HMI_DEBUG("previous role:%s area:%s",
207 i_prv->first.c_str(), i_prv->second.c_str());
209 // If current role exists in previous and area is different with previous
210 if (area_name != i_prv->second)
212 HMI_DEBUG("current role exists in previous and area is different with previous");
214 // Set activate action
215 bool end_draw_finished = false;
222 TaskVisible::VISIBLE,
225 actions.push_back(act);
228 // Remove role which exist in current list from previous list
229 prv_roles.erase(i_prv);
233 // Deactivate roles which remains in previous list
234 // because these are not displayed in current layout
235 for (auto i_prv : prv_roles)
237 HMI_DEBUG("Deactivate role:%s", i_prv.first.c_str());
239 // Set deactivate action
240 bool end_draw_finished = true;
247 TaskVisible::INVISIBLE,
250 actions.push_back(act);
253 // Update previous role list
254 this->crrlayer2rolestate[layer_name] = crr_roles;
259 void PMWrapper::processError()