6ae80c6c9d157dc13f16a1473e8cc8ce5a58930f
[apps/agl-service-windowmanager.git] / src / policy_manager / policy_manager.cpp
1 /*
2  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <fstream>
19 #include <sstream>
20 #include <istream>
21 #include <thread>
22 #include <map>
23 #include <queue>
24 #include <systemd/sd-event.h>
25 #include <json-c/json.h>
26 #include "policy_manager.hpp"
27 #include "hmi-debug.h"
28
29 namespace stm {
30 extern "C" {
31 #include "dummy_stm.h"
32 }
33 } // namespace stm
34
35
36 namespace pm {
37 typedef struct AreaState {
38     std::string name;
39     std::string category;
40     std::string role;
41 } AreaState;
42
43 typedef std::vector<AreaState> AreaList;
44 typedef struct LayoutState {
45     std::string name;
46     std::map<std::string, int> category_num;
47     AreaList area_list;
48     std::map<std::string, std::vector<std::string>> role_history;
49 } LayoutState;
50
51 typedef struct LayerState {
52     std::string name;
53     LayoutState layout_state;
54 } LayerState;
55
56 typedef struct EventInfo {
57     int event;
58     std::string role;
59     uint64_t delay;
60 } EventInfo;
61
62 struct sd_event* event_loop;
63 std::map<int, struct sd_event_source*> event_source_list;
64 std::map<int, std::string> g_req_role_list;
65 PolicyManager::CallbackTable callback;
66 std::queue<EventInfo> g_event_info_queue;
67
68 std::unordered_map<std::string, LayerState> g_prv_layers;
69 std::unordered_map<std::string, LayerState> g_crr_layers;
70 std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
71 std::unordered_map<std::string, LayoutState> g_default_layouts;
72 }  // namespace pm
73
74
75 PolicyManager::PolicyManager() :
76   eventname2no_(),
77   categoryname2no_(),
78   areaname2no_(),
79   role2category_(),
80   category2role_(),
81   role2defaultarea_()
82 {
83     HMI_DEBUG("wm:pm", "Call");
84 }
85
86 int PolicyManager::initialize() {
87     HMI_DEBUG("wm:pm", "Call");
88
89     int ret = 0;
90
91     // Create convert map
92     for (int i = stm::gStmEventNoMin; i <= stm::gStmEventNoMax; i++) {
93         HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
94         this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
95     }
96
97     for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
98         HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
99         this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
100     }
101
102     for (int i = stm::gStmAreaNoMin; i <= stm::gStmAreaNoMax; i++) {
103         HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
104         this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
105     }
106
107     // Load role.db
108     ret = this->loadRoleDb();
109     if (0 > ret) {
110         HMI_ERROR("wm:pm", "Load role.db Error!!");
111         return ret;
112     }
113
114     // Load layout.db
115     ret = this->loadLayoutDb();
116     if (0 > ret) {
117         HMI_ERROR("wm:pm", "Load layout.db Error!!");
118         return ret;
119     }
120
121     // Initialize current/previous state of layers
122     pm::AreaState init_area;
123     pm::LayoutState init_layout;
124     init_area.name     = "none";
125     init_area.category = "none";
126     init_area.role     = "none";
127     init_layout.area_list.push_back(init_area);
128
129     for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
130         const char* layer_name = stm::gStmLayerName[i];
131         pm::g_crr_layers[layer_name].name          = layer_name;
132         pm::g_crr_layers[layer_name].layout_state  = init_layout;
133     }
134
135     pm::g_prv_layers = pm::g_crr_layers;
136
137     // Initialize StateTransitioner
138     stm::stmInitialize();
139
140     // Initialize sd_event loop
141     ret = this->initializeSdEventLoop();
142     if (0 > ret) {
143         HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
144         return ret;
145     }
146
147     return ret;
148 }
149
150 int PolicyManager::initializeSdEventLoop() {
151     // Get default event loop object
152     int ret = sd_event_new(&(pm::event_loop));
153     if (0 > ret) {
154         HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
155         return -1;
156     }
157
158     // Create thread for sd_event and detach
159     std::thread sd_event_loop([this]() {
160         while (1) {
161             sd_event_run(pm::event_loop, 1000);
162         }
163     });
164     sd_event_loop.detach();
165
166     return 0;
167 }
168
169 static void addStateToJson(
170   const char* key, int is_changed, const char* state, json_object** json_out) {
171     if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
172         HMI_ERROR("wm:pm", "Argument is nullptr!!!");
173         return;
174     }
175
176     json_object* json_obj = json_object_new_object();
177     json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
178     if (is_changed) {
179         HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
180         json_object_object_add(json_obj, "state", json_object_new_string(state));
181     }
182     json_object_object_add(*json_out, key, json_obj);
183 }
184
185 static void addStateToJson(const char* layer_name, unsigned int changed,
186                            pm::AreaList area_list, json_object** json_out) {
187     if ((nullptr == layer_name) || (1 < changed) || (nullptr == json_out)) {
188         HMI_ERROR("wm:pm", "Invalid argument!!!");
189         return;
190     }
191
192     json_object* json_areas = json_object_new_array();
193     json_object* json_tmp;
194     for (pm::AreaState as : area_list) {
195         json_tmp = json_object_new_object();
196         json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
197         json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
198         json_object_array_add(json_areas, json_tmp);
199     }
200
201     json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
202     json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
203     json_object_object_add(*json_out, "areas", json_areas);
204 }
205
206 static void updateLocalState(int event_data, stm::stm_state_t crr_state) {
207     int event_no, category_no, area_no;
208
209     event_no    = (event_data & STM_MSK_EVT_NO) - 1;
210     category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
211     area_no     = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
212
213     std::string req_evt = std::string(stm::gStmEventName[event_no]);
214     std::string req_ctg = std::string(stm::gStmCategoryName[category_no]);
215     std::string req_area = std::string(stm::gStmAreaName[area_no]);
216     std::string req_role = pm::g_req_role_list[event_data];
217     HMI_DEBUG("wm:pm", "REQ: event:%s role:%s category:%s area:%s",
218         req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
219
220     // Store previous layers
221     pm::g_prv_layers = pm::g_crr_layers;
222
223     // Update layers
224     for (int layer_no = stm::gStmLayerNoMin;
225          layer_no <= stm::gStmLayerNoMax; layer_no++) {
226         const char* layer_name = stm::gStmLayerName[layer_no];
227         HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
228
229 #if 1
230         // If restriction mode is changed off -> on,
231         // store current state for state of restriction mode off
232         if ((crr_state.restriction_mode.is_changed)
233             && (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
234             HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
235             pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
236         }
237 #else
238         // If car state is changed car_stop -> car_run,
239         // store current state for state of car stop
240         if ((crr_state.car.is_changed)
241             && (stm::gStmCarStateNoRun == crr_state.car.state)) {
242             HMI_DEBUG("wm:lm", "Store current state for state of car stop");
243             pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
244         }
245 #endif
246
247
248         // This layer is changed?
249         if (crr_state.layer[layer_no].is_changed) {
250             // Get previous layout name of this layer
251             pm::LayoutState prv_layout_state =  pm::g_prv_layers[layer_name].layout_state;
252             std::string prv_layout_name = prv_layout_state.name;
253
254             // Get current layout name of this layer
255             int crr_layout_state_no =  crr_state.layer[layer_no].state;
256             std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
257
258             pm::LayoutState crr_layout_state;
259 #if 1
260             if ((crr_state.restriction_mode.is_changed)
261                 && (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
262                 // If restriction mode is changed on -> off,
263                 // restore state of restriction mode off
264                 HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
265                 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
266 #else
267             if ((crr_state.car.is_changed)
268                 && (stm::gStmCarStateNoStop == crr_state.car.state)) {
269                 // If car state is changed car_run -> car_stop,
270                 // restore state of car stop
271                 HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
272                 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
273 #endif
274             }
275             else {
276                 // Copy previous layout state for current
277                 crr_layout_state = prv_layout_state;
278
279                 if (prv_layout_name == crr_layout_name) {
280                     HMI_DEBUG("wm:lm", "Previous layout is same with current");
281                 }
282                 else {
283                     // If previous layout is NOT same with current,
284                     // current areas is set with default value
285                     HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
286                     crr_layout_state.name         = pm::g_default_layouts[crr_layout_name].name;
287                     crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
288                     crr_layout_state.area_list    = pm::g_default_layouts[crr_layout_name].area_list;
289                 }
290
291                 // Create candidate list
292                 std::map<std::string, pm::AreaList> cand_list;
293                 for (int ctg_no=stm::gStmCategoryNoMin;
294                      ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
295                     const char* ctg = stm::gStmCategoryName[ctg_no];
296                     HMI_DEBUG("wm:pm", "ctg:%s", ctg);
297
298                     // Create candidate list for category from the previous displayed categories
299                     pm::AreaList tmp_cand_list;
300                     for (pm::AreaState area_state : prv_layout_state.area_list) {
301                         if (std::string(ctg) == area_state.category) {
302                             // If there is the category which is same with new category in previous layout,
303                             // push it to list
304                             HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
305                                       area_state.category.c_str(), area_state.role.c_str());
306                             tmp_cand_list.push_back(area_state);
307                         }
308                     }
309
310                     int candidate_num = prv_layout_state.category_num[ctg];
311                     int blank_num = crr_layout_state.category_num[ctg];
312                     HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
313
314                     // If requested event is "activate"
315                     // and there are requested category and area,
316                     // update area with requested role in current layout.
317                     bool request_for_this_layer = false;
318                     bool updated = false;
319                     if ((ctg == req_ctg) && ("activate" == req_evt) ) {
320                         HMI_DEBUG("wm:pm", "requested event is activate");
321                         for (pm::AreaState &as : crr_layout_state.area_list) {
322                             if (as.category == req_ctg) {
323                                 request_for_this_layer = true;
324
325                                 if (as.name == req_area) {
326                                     HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
327                                               as.name.c_str(), as.category.c_str(), as.role.c_str());
328                                     as.role = req_role;
329                                     blank_num--;
330                                     updated = true;
331                                     break;
332                                 }
333                             }
334                         }
335
336                         // If NOT updated: there is not requested area in new layout, 
337                         // so push requested role to candidate list
338                         if (request_for_this_layer && (!updated)) {
339                             HMI_DEBUG("wm:pm", "Push request to candidate list");
340                             pm::AreaState area_state;
341                             area_state.name = req_area;
342                             area_state.category = req_ctg;
343                             area_state.role = req_role;
344                             tmp_cand_list.push_back(area_state);
345                         }
346                     }
347
348                     // Compare number of candidate/blank,
349                     // And remove role in order of the oldest as necessary
350                     if (candidate_num < blank_num) {
351                         // Refer history stack
352                         // and add to the top of tmp_cand_list in order to the newest
353                         while (candidate_num != blank_num) {
354                             pm::AreaState area_state;
355                             area_state.name = "";
356                             area_state.category = ctg;
357                             if (0 != crr_layout_state.role_history[ctg].size()) {
358                                 HMI_ERROR("wm:pm", "Use role in history stack:%s",
359                                           crr_layout_state.role_history[ctg].back().c_str());
360                                 area_state.role = crr_layout_state.role_history[ctg].back();
361                                 crr_layout_state.role_history[ctg].pop_back();
362                             }
363                             else {
364                                 HMI_ERROR("wm:pm", "There is no role in history stack!!");
365                                 area_state.role = "";
366                             }
367                             tmp_cand_list.push_back(area_state);
368                             candidate_num++;
369                         }
370                     }
371                     else if (candidate_num > blank_num) {
372                         HMI_DEBUG("wm:pm", "candidate_num > blank_num");
373
374                         // Remove the oldest role from candidate list
375                         while (candidate_num != blank_num) {
376                             std::string removed_role = tmp_cand_list.begin()->role;
377                             HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
378                                       removed_role.c_str());
379                             tmp_cand_list.erase(tmp_cand_list.begin());
380                             candidate_num--;
381
382                             // Push removed data to history stack
383                             crr_layout_state.role_history[ctg].push_back(removed_role);
384                         }
385                     }
386                     else {  // (candidate_num == blank_num)
387                         // nop
388                     }
389
390                     cand_list[ctg] = tmp_cand_list;
391                 }
392
393                 // Update areas
394                 for (pm::AreaState &as : crr_layout_state.area_list) {
395                     HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
396                               as.name.c_str(), as.category.c_str());
397                     if ("" == as.role) {
398                         HMI_DEBUG("wm:pm", "Update this area with role:%s",
399                                   cand_list[as.category].begin()->role.c_str());
400                         as.role = cand_list[as.category].begin()->role;
401                         cand_list[as.category].erase(cand_list[as.category].begin());
402                     }
403                 }
404             }
405             // Update current layout of this layer
406             pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
407         }
408     }
409
410     // Erase role for the event_data from list
411     pm::g_req_role_list.erase(event_data);
412
413     // Check
414     for (auto itr : pm::g_crr_layers) {
415         pm::LayerState ls = itr.second;
416         HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
417         HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
418
419         for (pm::AreaState as : ls.layout_state.area_list) {
420             HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
421             HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
422             HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
423         }
424     }
425 }
426
427 static void createOutputInformation(stm::stm_state_t crr_state, json_object **json_out) {
428     // Create result
429     // {
430     //     "parking_brake": {
431     //         "is_changed": <bool>,
432     //         "state": <const char*>
433     //     },
434     addStateToJson("parking_brake",
435                    crr_state.parking_brake.is_changed,
436                    stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
437                    json_out);
438
439     //     "accel_pedal": {
440     //         "is_changed": <bool>,
441     //         "state": <const char*>
442     //     },
443     addStateToJson("accel_pedal",
444                    crr_state.accel_pedal.is_changed,
445                    stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
446                    json_out);
447
448     //     "lightstatus_brake": {
449     //         "is_changed": <bool>,
450     //         "state": <const char*>
451     //     },
452     addStateToJson("lightstatus_brake",
453                    crr_state.lightstatus_brake.is_changed,
454                    stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
455                    json_out);
456
457     //     "car": {
458     //         "is_changed": <bool>,
459     //         "state": <const char*>
460     //     },
461     addStateToJson("car",
462                    crr_state.car.is_changed,
463                    stm::gStmCarStateNo2Name[crr_state.car.state],
464                    json_out);
465
466     //     "lamp": {
467     //         "is_changed": <bool>,
468     //         "state": <const char*>
469     //     },
470     addStateToJson("lamp",
471                    crr_state.lamp.is_changed,
472                    stm::gStmLampStateNo2Name[crr_state.lamp.state],
473                    json_out);
474
475     //     "restriction_mode": {
476     //         "is_changed": <bool>,
477     //         "state": <const char*>
478     //     },
479     addStateToJson("restriction_mode",
480                    crr_state.restriction_mode.is_changed,
481                    stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
482                    json_out);
483
484     // Create layout information
485     //
486     //     "layers": [
487     //     {
488     //         "homescreen": {
489     //             "changed": <bool>,
490     //             "areas": [
491     //             {
492     //                 "name":<const char*>,
493     //                 "role":<const char*>
494     //             }.
495     //             ...
496     //             ]
497     //         }
498     //     },
499     //     ...
500     json_object* json_layer = json_object_new_array();
501     json_object* json_tmp;
502     for (int layer_no = stm::gStmLayerNoMin;
503          layer_no <= stm::gStmLayerNoMax; layer_no++) {
504         const char* layer_name = stm::gStmLayerName[layer_no];
505         HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
506
507         json_tmp = json_object_new_object();
508         addStateToJson(layer_name,
509                        crr_state.layer[layer_no].is_changed,
510                        pm::g_crr_layers[layer_name].layout_state.area_list,
511                        &json_tmp);
512         json_object_array_add(json_layer, json_tmp);
513     }
514
515     // Add json array of layer
516     json_object_object_add(*json_out, "layers", json_layer);
517 }
518
519 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role);
520 static void controlTimerEvent(stm::stm_state_t crr_state) {
521     if (crr_state.car.is_changed) {
522         if (stm::gStmCarStateNoRun == crr_state.car.state) {
523             // Set delay event(restriction mode on)
524             checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, "");
525         }
526         else if (stm::gStmCarStateNoStop == crr_state.car.state) {
527             // Stop timer for restriction on event
528             if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
529               != pm::event_source_list.end()) {
530                 HMI_DEBUG("wm:pm", "Stop timer for restriction on");
531                 sd_event_source *event_source
532                     = pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
533                 int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
534                 if (0 > ret) {
535                     HMI_ERROR("wm:pm", "Failed to stop timer");
536                 }
537             }
538
539             // Set event(restriction mode off)
540             checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, "");
541         }
542     }
543 }
544
545 static int checkPolicy(sd_event_source *source, void *data) {
546     HMI_DEBUG("wm:pm", "Call");
547     HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
548
549     int event_data = *((int*)data);
550
551     int event_no, category_no, area_no;
552     event_no    = (event_data & STM_MSK_EVT_NO) - 1;
553     category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
554     area_no     = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
555     HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
556               stm::gStmEventName[event_no],
557               stm::gStmCategoryName[category_no],
558               stm::gStmAreaName[area_no]);
559
560     // Transition state
561     stm::stm_state_t crr_state;
562     int ret = stm::stmTransitionState(event_data, &crr_state);
563     if (0 > ret) {
564         HMI_ERROR("wm:pm", "Failed transition state");
565         if (nullptr != pm::callback.onError) {
566             json_object* json_out = json_object_new_object();
567             json_object_object_add(json_out, "message",
568                                    json_object_new_string("Failed to transition state"));
569             json_object_object_add(json_out, "event",
570                                    json_object_new_string(stm::gStmEventName[event_no]));
571             json_object_object_add(json_out, "role",
572                                    json_object_new_string(pm::g_req_role_list[event_data].c_str()));
573             json_object_object_add(json_out, "area",
574                                    json_object_new_string(stm::gStmAreaName[area_no]));
575             pm::callback.onError(json_out);
576             json_object_put(json_out);
577         }
578         return -1;
579     }
580
581     HMI_DEBUG("wm:pm", "parking brake state     (is_changed:%d state:%d:%s)",
582               crr_state.parking_brake.is_changed,
583               crr_state.parking_brake.state,
584               stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
585     HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
586               crr_state.accel_pedal.is_changed,
587               crr_state.accel_pedal.state,
588               stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
589     HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
590               crr_state.lightstatus_brake.is_changed,
591               crr_state.lightstatus_brake.state,
592               stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
593     HMI_DEBUG("wm:pm", "car state               (is_changed:%d state:%d:%s)",
594               crr_state.car.is_changed,
595               crr_state.car.state,
596               stm::gStmCarStateNo2Name[crr_state.car.state]);
597     HMI_DEBUG("wm:pm", "lamp state              (is_changed:%d state:%d:%s)",
598               crr_state.lamp.is_changed,
599               crr_state.lamp.state,
600               stm::gStmLampStateNo2Name[crr_state.lamp.state]);
601     HMI_DEBUG("wm:pm", "restriction mode state  (is_changed:%d state:%d:%s)",
602               crr_state.restriction_mode.is_changed,
603               crr_state.restriction_mode.state,
604               stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
605     HMI_DEBUG("wm:pm", "homescreen state        (is_changed:%d state:%d:%s)",
606               crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
607               crr_state.layer[stm::gStmLayerNoHomescreen].state,
608               stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
609     HMI_DEBUG("wm:pm", "apps state              (is_changed:%d state:%d:%s)",
610               crr_state.layer[stm::gStmLayerNoApps].is_changed,
611               crr_state.layer[stm::gStmLayerNoApps].state,
612               stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
613     HMI_DEBUG("wm:pm", "restriction state       (is_changed:%d state:%d:%s)",
614               crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
615               crr_state.layer[stm::gStmLayerNoRestriction].state,
616               stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
617     HMI_DEBUG("wm:pm", "on_screen state         (is_changed:%d state:%d:%s)",
618               crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
619               crr_state.layer[stm::gStmLayerNoOnScreen].state,
620               stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
621
622     // Update state which is managed by PolicyManager
623     updateLocalState(event_data, crr_state);
624
625     // Create output information for ResourceManager
626     json_object* json_out = json_object_new_object();
627     createOutputInformation(crr_state, &json_out);
628
629     // Notify changed state
630     if (nullptr != pm::callback.onStateTransitioned) {
631         pm::callback.onStateTransitioned(json_out);
632     }
633
634     // Start/Stop timer events
635     controlTimerEvent(crr_state);
636
637     // Release json_object
638     json_object_put(json_out);
639
640     // Release data
641     delete (int*)data;
642
643     // Destroy sd_event_source object
644     sd_event_source_unref(source);
645
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);
649     }
650
651     HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
652     return 0;
653 }
654
655 static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
656     HMI_DEBUG("wm:pm", "Call");
657
658     int ret = checkPolicy(source, data);
659     return ret;
660 }
661
662 static int checkPolicyEntry(int event, uint64_t delay_ms, std::string role)
663 {
664     HMI_DEBUG("wm:pm", "Call");
665     HMI_DEBUG("wm:pm", "event:0x%x delay:%d role:%s", event, delay_ms, role.c_str());
666
667     // Store requested role
668     pm::g_req_role_list[event] = role;
669
670     if (0 == delay_ms) {
671         int ret = sd_event_add_defer(pm::event_loop, NULL,
672                                      &checkPolicy, new int(event));
673         if (0 > ret) {
674             HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
675             pm::g_req_role_list.erase(event);
676             return -1;
677         }
678     }
679     else {
680         // Get current time
681         struct timespec time_spec;
682         clock_gettime(CLOCK_MONOTONIC, &time_spec);
683
684         // Calculate timer fired time
685         uint64_t usec = (time_spec.tv_sec * 1000000)
686             + (time_spec.tv_nsec / 1000)
687             + (delay_ms * 1000);
688
689         // Set timer
690         struct sd_event_source* event_source;
691         int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
692                                     &timerEvent, new int(event));
693         if (0 > ret) {
694             HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
695             pm::g_req_role_list.erase(event);
696             return -1;
697         }
698
699         // Store event source
700         pm::event_source_list[event] = event_source;
701     }
702
703     return 0;
704 }
705
706 void PolicyManager::registerCallback(CallbackTable callback) {
707     HMI_DEBUG("wm:pm", "Call");
708
709     pm::callback.onStateTransitioned = callback.onStateTransitioned;
710     pm::callback.onError             = callback.onError;
711 }
712
713 int PolicyManager::setInputEventData(json_object* json_in) {
714     HMI_DEBUG("wm:pm", "Call");
715
716     // Check arguments
717     if (nullptr == json_in) {
718         HMI_ERROR("wm:pm", "Argument is NULL!!");
719         return -1;
720     }
721
722     // Get event from json_object
723     const char* event = this->getStringFromJson(json_in, "event");
724     int event_no = 0;
725     if (nullptr != event) {
726         // Convert name to number
727         event_no = this->eventname2no_[event];
728         HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
729     }
730
731     // Get role from json_object
732     const char* role = this->getStringFromJson(json_in, "role");
733     int category_no = 0;
734     if (nullptr != role) {
735         HMI_DEBUG("wm:pm", "role(%s)", role);
736
737         // Convert role to category
738         const char* category = this->role2category_[role].c_str();
739         if (0 == strcmp("", category)) {
740             HMI_ERROR("wm:pm", "Error!!");
741             return -1;
742         }
743         HMI_DEBUG("wm:pm", "category(%s)", category);
744
745         // Convert name to number
746         category_no = categoryname2no_[category];
747         HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
748     }
749
750     // Get areat from json_object
751     const char* area = this->getStringFromJson(json_in, "area");
752     int area_no = 0;
753     if (nullptr != area) {
754         // Convert name to number
755         area_no = areaname2no_[area];
756         HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
757     }
758
759     // Set event info to the queue
760     pm::EventInfo event_info;
761     event_info.event = (event_no | category_no | area_no);
762     if (nullptr == role) {
763         event_info.role = std::string("");
764     }
765     else {
766         event_info.role = std::string(role);
767     }
768     event_info.delay = 0;
769     pm::g_event_info_queue.push(event_info);
770
771     return 0;
772 }
773
774 int PolicyManager::executeStateTransition() {
775     HMI_DEBUG("wm:pm", "Call");
776
777     int ret;
778     pm::EventInfo event_info;
779
780     while (!pm::g_event_info_queue.empty()) {
781         // Get event info from queue and delete
782         event_info = pm::g_event_info_queue.front();
783         pm::g_event_info_queue.pop();
784
785         // Set event info for checking policy
786         ret = checkPolicyEntry(event_info.event, event_info.delay, event_info.role);
787     }
788     return ret;
789 }
790
791 void PolicyManager::undoState() {
792     HMI_DEBUG("wm:pm", "Call");
793
794     // Undo state of STM
795     stm::stmUndoState();
796
797     pm::g_crr_layers = pm::g_prv_layers;
798 }
799
800 extern const char* kDefaultRoleDb;
801 int PolicyManager::loadRoleDb() {
802     HMI_DEBUG("wm:pm", "Call");
803
804     std::string file_name;
805
806     // Get afm application installed dir
807     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
808     HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
809
810     if (!afm_app_install_dir) {
811         HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
812     }
813     else {
814         file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
815     }
816
817     // Load role.db
818     json_object* json_obj;
819     int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
820     if (0 > ret) {
821         HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
822         json_obj = json_tokener_parse(kDefaultRoleDb);
823     }
824     HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
825
826     json_object* json_roles;
827     if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
828         HMI_ERROR("wm:pm", "Parse Error!!");
829         return -1;
830     }
831
832     int len = json_object_array_length(json_roles);
833     HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
834     HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
835
836     json_object* json_tmp;
837     const char* category;
838     const char* roles;
839     const char* areas;
840     for (int i=0; i<len; i++) {
841         json_tmp = json_object_array_get_idx(json_roles, i);
842
843         category = this->getStringFromJson(json_tmp, "category");
844         roles =  this->getStringFromJson(json_tmp, "role");
845         areas =  this->getStringFromJson(json_tmp, "area");
846
847         if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
848             HMI_ERROR("wm:pm", "Parse Error!!");
849             return -1;
850         }
851
852         // Parse roles by '|'
853         std::vector<std::string> vct_roles;
854         vct_roles = this->parseString(std::string(roles), '|');
855
856         // Parse areas by '|'
857         std::vector<std::string> vct_areas;
858         vct_areas = this->parseString(std::string(areas), '|');
859
860         // Set role, category, default area
861         for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
862             // Delete space from role and area name
863             std::string role = this->deleteSpace(*itr);
864             std::string area = this->deleteSpace(vct_areas[0]);
865
866             this->role2category_[role] = std::string(category);
867             this->role2defaultarea_[role] = area;
868         }
869
870         this->category2role_[std::string(category)] = std::string(roles);
871     }
872
873     // Check
874     HMI_DEBUG("wm:pm", "Check role2category_");
875     for (auto& x:this->role2category_){
876         HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
877     }
878
879     HMI_DEBUG("wm:pm", "Check role2defaultarea_");
880     for (auto& x:this->role2defaultarea_){
881         HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
882     }
883
884     HMI_DEBUG("wm:pm", "Check category2role_");
885     for (auto& x:this->category2role_){
886         HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
887     }
888
889     return 0;
890 }
891
892 extern const char* kDefaultLayoutDb;
893 int PolicyManager::loadLayoutDb() {
894     HMI_DEBUG("wm:lm", "Call");
895
896     // Get afm application installed dir
897     char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
898     HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
899
900     std::string file_name;
901     if (!afm_app_install_dir) {
902         HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
903     }
904     else {
905         file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
906     }
907
908     // Load layout.db
909     json_object* json_obj;
910     int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
911     if (0 > ret) {
912         HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
913         json_obj = json_tokener_parse(kDefaultLayoutDb);
914     }
915     HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
916
917     // Perse layouts
918     HMI_DEBUG("wm:pm", "Perse layouts");
919     json_object* json_cfg;
920     if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
921         HMI_ERROR("wm:pm", "Parse Error!!");
922         return -1;
923     }
924
925     int len = json_object_array_length(json_cfg);
926     HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
927     HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
928
929     const char* layout;
930     const char* role;
931     const char* category;
932     for (int i=0; i<len; i++) {
933         json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
934
935         layout = this->getStringFromJson(json_tmp, "name");
936         if (nullptr == layout) {
937             HMI_ERROR("wm:pm", "Parse Error!!");
938             return -1;
939         }
940         HMI_DEBUG("wm:pm", "> layout:%s", layout);
941
942         json_object* json_area_array;
943         if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
944           HMI_ERROR("wm:pm", "Parse Error!!");
945           return -1;
946         }
947
948         int len_area = json_object_array_length(json_area_array);
949         HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
950         HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
951
952         pm::LayoutState layout_state;
953         pm::AreaState area_state;
954         std::map<std::string, int> category_num;
955         for (int ctg_no = stm::gStmCategoryNoMin;
956              ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
957             const char* ctg_name = stm::gStmCategoryName[ctg_no];
958             category_num[ctg_name] = 0;
959         }
960
961         for (int j=0; j<len_area; j++) {
962             json_object* json_area = json_object_array_get_idx(json_area_array, j);
963
964             // Get area name
965             const char* area = this->getStringFromJson(json_area, "name");
966             if (nullptr == area) {
967               HMI_ERROR("wm:pm", "Parse Error!!");
968               return -1;
969             }
970             area_state.name = std::string(area);
971             HMI_DEBUG("wm:pm", ">> area:%s", area);
972
973             // Get app attribute of the area
974             category = this->getStringFromJson(json_area, "category");
975             if (nullptr == category) {
976                 HMI_ERROR("wm:pm", "Parse Error!!");
977                 return -1;
978             }
979             area_state.category = std::string(category);
980             category_num[category]++;
981             HMI_DEBUG("wm:pm", ">>> category:%s", category);
982
983             role = this->getStringFromJson(json_area, "role");
984             if (nullptr != role) {
985                 // Role is NOT essential here
986                 area_state.role = std::string(role);
987             }
988             else {
989                 area_state.role = std::string("");
990
991             }
992             HMI_DEBUG("wm:pm", ">>> role:%s", role);
993
994             layout_state.area_list.push_back(area_state);
995
996         }
997
998         layout_state.name = layout;
999         layout_state.category_num = category_num;
1000         pm::g_default_layouts[layout] = layout_state;
1001     }
1002
1003     // initialize for none layout
1004     pm::LayoutState none_layout_state;
1005     memset(&none_layout_state, 0, sizeof(none_layout_state));
1006     none_layout_state.name                 = "none";
1007     pm::g_default_layouts["none"] = none_layout_state;
1008
1009     // Check
1010     for(auto itr_layout = pm::g_default_layouts.begin();
1011       itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
1012         HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
1013
1014         for (auto itr_area = itr_layout->second.area_list.begin();
1015           itr_area != itr_layout->second.area_list.end(); ++itr_area) {
1016             HMI_DEBUG("wm:pm", ">>> >>> area    :%s", itr_area->name.c_str());
1017             HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
1018             HMI_DEBUG("wm:pm", ">>> >>> role    :%s", itr_area->role.c_str());
1019         }
1020     }
1021
1022     // Release json_object
1023     json_object_put(json_obj);
1024
1025     return 0;
1026 }
1027
1028 // TODO:
1029 // This function will be removed because json_helper has same function.
1030 // json_helper should be library.
1031 const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
1032     if ((nullptr == obj) || (nullptr == key)) {
1033         HMI_ERROR("wm:pm", "Argument is nullptr!!!");
1034         return nullptr;
1035     }
1036
1037     json_object* tmp;
1038     if (!json_object_object_get_ex(obj, key, &tmp)) {
1039         HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
1040         return nullptr;
1041     }
1042
1043     return json_object_get_string(tmp);
1044 }
1045
1046 // TODO:
1047 // This function will be removed because json_helper has same function.
1048 // json_helper should be library.
1049 int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
1050     const int input_size = 128;
1051     int ret = -1;
1052
1053     if ((nullptr == file) || (nullptr == obj)) {
1054         HMI_ERROR("wm:jh", "Argument is nullptr!!!");
1055         return ret;
1056     }
1057
1058     HMI_DEBUG("wm:jh", "Input file: %s", file);
1059
1060     // Open json file
1061     FILE *fp = fopen(file, "rb");
1062     if (nullptr == fp) {
1063         HMI_ERROR("wm:jh", "Could not open file");
1064         return ret;
1065     }
1066
1067     // Parse file data
1068     struct json_tokener *tokener = json_tokener_new();
1069     enum json_tokener_error json_error;
1070     char buffer[input_size];
1071     int block_cnt = 1;
1072     while (1) {
1073         size_t len = fread(buffer, sizeof(char), input_size, fp);
1074         *obj = json_tokener_parse_ex(tokener, buffer, len);
1075         if (nullptr != *obj) {
1076             HMI_DEBUG("wm:jh", "File input is success");
1077             ret = 0;
1078             break;
1079         }
1080
1081         json_error = json_tokener_get_error(tokener);
1082         if ((json_tokener_continue != json_error)
1083             || (input_size > len)) {
1084             HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
1085                       (input_size * block_cnt), json_tokener_error_desc(json_error));
1086             HMI_ERROR("wm:jh", "\n%s", buffer);
1087             *obj = nullptr;
1088             break;
1089         }
1090         block_cnt++;
1091     }
1092
1093     // Close json file
1094     fclose(fp);
1095
1096     // Free json_tokener
1097     json_tokener_free(tokener);
1098
1099     return ret;
1100 }
1101
1102 std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
1103     // Parse string by delimiter
1104     std::vector<std::string> vct;
1105     std::stringstream ss{str};
1106     std::string buf;
1107     while (std::getline(ss, buf, delimiter)) {
1108       if (!buf.empty()) {
1109         vct.push_back(buf);
1110       }
1111     }
1112     return vct;
1113 }
1114
1115 std::string PolicyManager::deleteSpace(std::string str) {
1116     std::string ret = str;
1117     size_t pos;
1118     while ((pos = ret.find_first_of(" ")) != std::string::npos) {
1119       ret.erase(pos, 1);
1120     }
1121     return ret;
1122 }
1123
1124 const char* kDefaultRoleDb = "{ \
1125     \"roles\":[ \
1126     { \
1127         \"category\": \"homescreen\", \
1128         \"role\": \"homescreen\", \
1129         \"area\": \"full\", \
1130     }, \
1131     { \
1132         \"category\": \"map\", \
1133         \"role\": \"map\", \
1134         \"area\": \"full | normal | split.main\", \
1135     }, \
1136     { \
1137         \"category\": \"general\", \
1138         \"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
1139         \"area\": \"normal\", \
1140     }, \
1141     { \
1142         \"category\": \"phone\", \
1143         \"role\": \"phone\", \
1144         \"area\": \"normal\", \
1145     }, \
1146     { \
1147         \"category\": \"splitable\", \
1148         \"role\": \"splitable1 | splitable2\", \
1149         \"area\": \"normal | split.main | split.sub\", \
1150     }, \
1151     { \
1152         \"category\": \"popup\", \
1153         \"role\": \"popup\", \
1154         \"area\": \"on_screen\", \
1155     }, \
1156     { \
1157         \"category\": \"system_alert\", \
1158         \"role\": \"system_alert\", \
1159         \"area\": \"on_screen\", \
1160     }, \
1161     { \
1162         \"category\": \"tbt\", \
1163         \"role\": \"tbt\", \
1164         \"area\": \"hud\", \
1165     } \
1166     ] \
1167 }";
1168
1169
1170 const char* kDefaultLayoutDb = "{ \
1171     \"layouts\": [ \
1172         { \
1173             \"name\": \"pu\", \
1174             \"layer\": \"on_screen\", \
1175             \"areas\": [ \
1176                 { \
1177                     \"name\": \"pop_up\", \
1178                     \"role\": \"incomming_call\" \
1179                 } \
1180             ] \
1181         }, \
1182         { \
1183             \"name\": \"sa\", \
1184             \"layer\": \"on_screen\", \
1185             \"areas\": [ \
1186                 { \
1187                     \"name\": \"system_alert\", \
1188                     \"role\": \"system_alert\" \
1189                 } \
1190             ] \
1191         }, \
1192         { \
1193             \"name\": \"m1\", \
1194             \"layer\": \"apps\", \
1195             \"areas\": [ \
1196                 { \
1197                     \"name\": \"normal\", \
1198                     \"role\": \"map\" \
1199                 } \
1200             ] \
1201         }, \
1202         { \
1203             \"name\": \"m2\", \
1204             \"layer\": \"apps\", \
1205             \"areas\": [ \
1206                 { \
1207                     \"name\": \"split.main\", \
1208                     \"role\": \"map\" \
1209                 }, \
1210                 { \
1211                     \"name\": \"split.sub\", \
1212                     \"category\": \"hvac\" \
1213                 } \
1214             ] \
1215         }, \
1216         { \
1217             \"name\": \"mf\", \
1218             \"layer\": \"apps\", \
1219             \"areas\": [ \
1220                 { \
1221                     \"name\": \"full\", \
1222                     \"role\": \"map\" \
1223                 } \
1224             ] \
1225         }, \
1226         { \
1227             \"name\": \"s1\", \
1228             \"layer\": \"apps\", \
1229             \"areas\": [ \
1230                 { \
1231                     \"name\": \"normal\", \
1232                     \"category\": \"splitable\" \
1233                 } \
1234             ] \
1235         }, \
1236         { \
1237             \"name\": \"s2\", \
1238             \"layer\": \"apps\", \
1239             \"areas\": [ \
1240                 { \
1241                     \"name\": \"split.main\", \
1242                     \"category\": \"splitable\" \
1243                 }, \
1244                 { \
1245                     \"name\": \"split.sub\", \
1246                     \"category\": \"splitable\" \
1247                 } \
1248             ] \
1249         }, \
1250         { \
1251             \"name\": \"g\", \
1252             \"layer\": \"apps\", \
1253             \"areas\": [ \
1254                 { \
1255                     \"name\": \"normal\", \
1256                     \"category\": \"general\" \
1257                 } \
1258             ] \
1259         }, \
1260         { \
1261             \"name\": \"hs\", \
1262             \"layer\": \"homescreen\", \
1263             \"areas\": [ \
1264                 { \
1265                     \"name\": \"full\", \
1266                     \"role\": \"homescreen\" \
1267                 } \
1268             ] \
1269         } \
1270     ], \
1271     \"areas\": [ \
1272         { \
1273             \"name\": \"normal\", \
1274             \"rect\": { \
1275                 \"x\": 0, \
1276                 \"y\": 218, \
1277                 \"w\": 1080, \
1278                 \"h\": 1488 \
1279             } \
1280         }, \
1281         { \
1282             \"name\": \"split.main\", \
1283             \"rect\": { \
1284                 \"x\": 0, \
1285                 \"y\": 218, \
1286                 \"w\": 1080, \
1287                 \"h\": 744 \
1288             } \
1289         }, \
1290         { \
1291             \"name\": \"split.sub\", \
1292             \"rect\": { \
1293                 \"x\": 0, \
1294                 \"y\": 962, \
1295                 \"w\": 1080, \
1296                 \"h\": 744 \
1297             } \
1298         }, \
1299         { \
1300             \"name\": \"full\", \
1301             \"rect\": { \
1302                 \"x\": 0, \
1303                 \"y\": 0, \
1304                 \"w\": 1080, \
1305                 \"h\": 1920 \
1306             } \
1307         }, \
1308         { \
1309             \"name\": \"pop_up\", \
1310             \"rect\": { \
1311                 \"x\": 0, \
1312                 \"y\": 640, \
1313                 \"w\": 1080, \
1314                 \"h\": 640 \
1315             } \
1316         }, \
1317         { \
1318             \"name\": \"system_alert\", \
1319             \"rect\": { \
1320                 \"x\": 0, \
1321                 \"y\": 640, \
1322                 \"w\": 1080, \
1323                 \"h\": 640 \
1324             } \
1325         } \
1326     ] \
1327 }";