47f7d265b8e67d604c945fe5aeef1b2651850281
[apps/agl-service-windowmanager.git] / policy_manager.hpp
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 #ifndef TMCAGLWM_POLICY_MANAGER_HPP
18 #define TMCAGLWM_POLICY_MANAGER_HPP
19
20
21 #include <unordered_map>
22 #include <vector>
23 #include <queue>
24
25 struct json_object;
26 struct sd_event;
27 struct sd_event_source;
28
29 namespace stm {
30 struct StmState;
31 }
32
33 class PolicyManager {
34
35 public:
36     explicit PolicyManager();
37     ~PolicyManager() = default;
38
39     using Handler = std::function<void(json_object *)>;
40
41     typedef struct {
42         Handler  onStateTransitioned;
43         Handler  onError;
44     } CallbackTable;
45
46     int initialize();
47     void registerCallback(CallbackTable callback_table);
48     int setInputEventData(json_object* json_in);
49     int executeStateTransition();
50     void undoState();
51
52     int transitionState(sd_event_source *source, void *data);
53     int timerEvent(sd_event_source *source, uint64_t usec, void *data);
54
55 private:
56     // Disable copy and move
57     PolicyManager(PolicyManager const &) = delete;
58     PolicyManager &operator=(PolicyManager const &) = delete;
59     PolicyManager(PolicyManager &&) = delete;
60     PolicyManager &operator=(PolicyManager &&) = delete;
61
62     typedef struct EventInfo {
63         int event;
64         std::string role;
65         uint64_t delay;
66     } EventInfo;
67
68     typedef struct AreaState {
69         std::string name;
70         std::string category;
71         std::string role;
72     } AreaState;
73
74     typedef std::vector<AreaState> AreaList;
75     typedef struct LayoutState {
76         std::string name;
77         std::map<std::string, int> category_num;
78         AreaList area_list;
79         std::map<std::string, std::vector<std::string>> role_history;
80     } LayoutState;
81
82     typedef struct LayerState {
83         std::string name;
84         LayoutState layout_state;
85         bool changed;
86     } LayerState;
87
88     typedef struct CarElement {
89         std::string state;
90         bool changed;
91     } CarElement;
92
93     // Convert map
94     std::unordered_map<std::string, int> eventname2no;
95     std::unordered_map<std::string, int> categoryname2no;
96     std::unordered_map<std::string, int> areaname2no;
97
98     std::unordered_map<std::string, std::string> role2category;
99     std::unordered_map<std::string, std::string> category2role;
100     std::unordered_map<std::string, std::string> role2defaultarea;
101
102     std::queue<EventInfo> event_info_queue;
103
104     struct sd_event* event_loop;
105     CallbackTable callback;
106
107     std::map<int, struct sd_event_source*> event_source_list;
108     std::map<int, std::string> req_role_list;
109
110     std::unordered_map<std::string, CarElement> prv_car_elements;
111     std::unordered_map<std::string, CarElement> crr_car_elements;
112
113     std::unordered_map<std::string, LayerState> prv_layers;
114     std::unordered_map<std::string, LayerState> crr_layers;
115
116     std::unordered_map<std::string, LayerState> prv_layers_car_stop;
117
118     std::unordered_map<std::string, LayoutState> default_layouts;
119
120     void initializeState();
121     void initializeCarElementState();
122     void initializeLayerState();
123     int initializeSdEventLoop();
124     void updateState(int event_data, stm::StmState crr_state);
125     void updateCarElementState(stm::StmState crr_state);
126     void updateLayerState(int event_data, stm::StmState crr_state);
127     void createOutputInformation(stm::StmState crr_state, json_object **json_out);
128     void controlTimerEvent(stm::StmState crr_state);
129     int setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role);
130
131     // Load role.db
132     int loadRoleDb();
133
134     // Load layout.db
135     int loadLayoutDb();
136
137     void addStateToJson(const char* name, bool changed,
138                                                 std::string state, json_object** json_out);
139     void addStateToJson(const char* layer_name, bool changed,
140                                                 AreaList area_list, json_object** json_out);
141     const char* getStringFromJson(json_object* obj, const char* key);
142     int inputJsonFilie(const char* file, json_object** obj);
143
144     std::vector<std::string> parseString(std::string str, char delimiter);
145     std::string deleteSpace(std::string str);
146 };
147
148
149 #endif  // TMCAGLWM_POLICY_MANAGER_HPP