add source for ces2019
[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();
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(std::string ecu_name);
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 struct Mode
91     {
92         std::string state;
93         bool changed;
94     } Mode;
95
96     typedef std::vector<std::string> Areas;
97     typedef std::vector<std::string> Categories;
98     typedef std::vector<std::string> Roles;
99
100     StmState *p_crr_state;
101     StmState *p_prv_state;
102
103     // Convert map
104     std::unordered_map<std::string, int> eventname2no;
105     std::unordered_map<std::string, int> categoryname2no;
106     std::unordered_map<std::string, int> areaname2no;
107
108     std::unordered_map<std::string, std::string> role2category;
109     std::unordered_map<std::string, std::string> category2role;
110     std::unordered_map<std::string, Areas>       category2areas;
111     std::unordered_map<std::string, Categories>  layer2categories;
112
113     std::queue<EventInfo> event_info_queue;
114     std::map<int, struct sd_event_source *> event_source_list;
115     std::map<int, std::string> req_role_list;
116
117     CallbackTable callback;
118
119     std::unordered_map<std::string, Mode> prv_car_elements;
120     std::unordered_map<std::string, Mode> crr_car_elements;
121
122     std::unordered_map<std::string, LayerState> prv_layers;
123     std::unordered_map<std::string, LayerState> crr_layers;
124
125     std::unordered_map<std::string, LayerState> prv_layers_car_stop;
126
127     std::unordered_map<std::string, LayoutState> default_layouts;
128
129     std::map<std::string, Roles> crr_invisible_role_history;
130     std::map<std::string, Roles> prv_invisible_role_history;
131
132     std::string ecu_name;
133
134     void initializeState();
135     void initializeModeState();
136     void initializeLayerState();
137     void updateState(int event_id);
138     void updateModeState();
139     void updateLayer(int event_id);
140     int updateLayout(int event_id, int layer_no,
141                      std::string crr_layout_name, LayoutState &crr_layout_state);
142     void createOutputInformation(json_object **json_out);
143     void controlTimerEvent();
144     int setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role);
145
146     void pushInvisibleRoleHistory(std::string category, std::string role);
147     std::string popInvisibleRoleHistory(std::string category);
148
149     bool changedRestrictionModeTo2On();
150     bool changedRestrictionMode2OnToOther();
151     bool changedLightstatusBrakeOffToOn();
152     bool changedLightstatusBrakeOnToOff();
153
154     int loadRolesConfigFile();
155     int loadLayoutsConfigFile();
156
157     void dumpLayerState(std::unordered_map<std::string, LayerState> &layers);
158     void dumpInvisibleRoleHistory();
159
160     void addStateToJson(const char *name, bool changed,
161                         std::string state, json_object **json_out);
162     void addStateToJson(const char *layer_name, bool changed,
163                         AreaList area_list, json_object **json_out);
164     const char *getStringFromJson(json_object *obj, const char *key);
165     int inputJsonFilie(const char *file, json_object **obj);
166
167     std::vector<std::string> parseString(std::string str, char delimiter);
168     std::string deleteSpace(std::string str);
169
170     static const char *kDefaultRolesConfig;
171     static const char *kDefaultLayoutsConfig;
172 };
173
174 #endif // TMCAGLWM_POLICY_MANAGER_HPP