798b706ef22c48bba7aae7f6f88e2b6f8faf7cd5
[apps/agl-service-windowmanager-2017.git] / policy_manager / 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 #include <functional>
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 struct StmState;
29
30 class PolicyManager
31 {
32   public:
33     explicit PolicyManager();
34     ~PolicyManager() = default;
35
36     using Handler = std::function<void(json_object *)>;
37
38     typedef struct
39     {
40         Handler onStateTransitioned;
41         Handler onError;
42     } CallbackTable;
43
44     int initialize();
45     void registerCallback(CallbackTable callback_table);
46     int setInputEventData(json_object *json_in);
47     int executeStateTransition();
48     void undoState();
49
50     // Do not use these functions
51     int transitionState(sd_event_source *source, void *data);
52     int timerEvent(sd_event_source *source, uint64_t usec, void *data);
53
54   private:
55     // Disable copy and move
56     PolicyManager(PolicyManager const &) = delete;
57     PolicyManager &operator=(PolicyManager const &) = delete;
58     PolicyManager(PolicyManager &&) = delete;
59     PolicyManager &operator=(PolicyManager &&) = delete;
60
61     typedef struct EventInfo
62     {
63         int event;
64         std::string role;
65         uint64_t delay;
66     } EventInfo;
67
68     typedef struct AreaState
69     {
70         std::string name;
71         std::string category;
72         std::string role;
73     } AreaState;
74
75     typedef std::vector<AreaState> AreaList;
76     typedef struct LayoutState
77     {
78         std::string name;
79         std::map<std::string, int> category_num;
80         AreaList area_list;
81     } LayoutState;
82
83     typedef struct LayerState
84     {
85         std::string name;
86         LayoutState layout_state;
87         bool changed;
88     } LayerState;
89
90     typedef std::vector<std::string> Areas;
91     typedef std::vector<std::string> Categories;
92     typedef std::vector<std::string> Roles;
93
94     // Convert map
95     std::unordered_map<std::string, int> eventname2no;
96     std::unordered_map<std::string, int> categoryname2no;
97     std::unordered_map<std::string, int> areaname2no;
98
99     std::unordered_map<std::string, std::string> role2category;
100     std::unordered_map<std::string, std::string> category2role;
101     std::unordered_map<std::string, Areas>       category2areas;
102     std::unordered_map<std::string, Categories>  layer2categories;
103
104     std::queue<EventInfo> event_info_queue;
105     std::map<int, struct sd_event_source *> event_source_list;
106     std::map<int, std::string> req_role_list;
107
108     CallbackTable callback;
109
110     std::unordered_map<std::string, LayerState> prv_layers;
111     std::unordered_map<std::string, LayerState> crr_layers;
112
113     std::unordered_map<std::string, LayoutState> default_layouts;
114
115     std::map<std::string, Roles> invisible_role_history;
116
117     void initializeState();
118     void initializeLayerState();
119     void updateState(int event_id, StmState crr_state);
120     void updateLayer(int event_id, StmState crr_state);
121     int updateLayout(int event_id, int layer_no,
122                      std::string crr_layout_name, LayoutState &crr_layout_state);
123     void createOutputInformation(StmState crr_state, json_object **json_out);
124     int setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role);
125
126     void pushInvisibleRoleHistory(std::string category, std::string role);
127     std::string popInvisibleRoleHistory(std::string category);
128
129     int loadRoleDb();
130     int loadStateDb();
131
132     void dumpLayerState(std::unordered_map<std::string, LayerState> &layers);
133     void dumpInvisibleRoleHistory();
134
135     void addStateToJson(const char *name, bool changed,
136                         std::string state, json_object **json_out);
137     void addStateToJson(const char *layer_name, bool changed,
138                         AreaList area_list, json_object **json_out);
139     const char *getStringFromJson(json_object *obj, const char *key);
140     int inputJsonFilie(const char *file, json_object **obj);
141
142     std::vector<std::string> parseString(std::string str, char delimiter);
143     std::string deleteSpace(std::string str);
144
145     static const char *kDefaultRoleDb;
146     static const char *kDefaultStateDb;
147 };
148
149 #endif // TMCAGLWM_POLICY_MANAGER_HPP