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 #ifndef TMCAGLWM_POLICY_MANAGER_HPP
18 #define TMCAGLWM_POLICY_MANAGER_HPP
21 #include <unordered_map>
27 struct sd_event_source;
34 explicit PolicyManager();
35 ~PolicyManager() = default;
37 using Handler = std::function<void(json_object *)>;
41 Handler onStateTransitioned;
46 void registerCallback(CallbackTable callback_table);
47 int setInputEventData(json_object *json_in);
48 int executeStateTransition();
51 // Do not use these functions
52 int transitionState(sd_event_source *source, void *data);
53 int timerEvent(sd_event_source *source, uint64_t usec, void *data);
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;
62 typedef struct EventInfo
69 typedef struct AreaState
76 typedef std::vector<AreaState> AreaList;
77 typedef struct LayoutState
80 std::map<std::string, int> category_num;
84 typedef struct LayerState
87 LayoutState layout_state;
92 std::unordered_map<std::string, int> eventname2no;
93 std::unordered_map<std::string, int> categoryname2no;
94 std::unordered_map<std::string, int> areaname2no;
96 std::unordered_map<std::string, std::string> role2category;
97 std::unordered_map<std::string, std::string> category2role;
98 std::unordered_map<std::string, std::string> role2defaultarea;
100 struct sd_event *event_loop;
101 std::queue<EventInfo> event_info_queue;
102 std::map<int, struct sd_event_source *> event_source_list;
103 std::map<int, std::string> req_role_list;
105 CallbackTable callback;
107 std::unordered_map<std::string, LayerState> prv_layers;
108 std::unordered_map<std::string, LayerState> crr_layers;
110 std::unordered_map<std::string, LayoutState> default_layouts;
112 std::map<std::string, std::vector<std::string>> role_history;
114 void initializeState();
115 void initializeLayerState();
116 void updateState(int event_data, StmState crr_state);
117 void updateLayerState(int event_data, StmState crr_state);
118 void createOutputInformation(StmState crr_state, json_object **json_out);
119 int setStateTransitionProcessToSystemd(int event, uint64_t delay_ms, std::string role);
121 void pushRoleHistory(std::string category, std::string role);
122 std::string popRoleHistory(std::string category);
127 void dumpLayerState(std::unordered_map<std::string, LayerState> &layers);
128 void dumpRoleHistory();
130 void addStateToJson(const char *name, bool changed,
131 std::string state, json_object **json_out);
132 void addStateToJson(const char *layer_name, bool changed,
133 AreaList area_list, json_object **json_out);
134 const char *getStringFromJson(json_object *obj, const char *key);
135 int inputJsonFilie(const char *file, json_object **obj);
137 std::vector<std::string> parseString(std::string str, char delimiter);
138 std::string deleteSpace(std::string str);
140 static const char *kDefaultRoleDb;
141 static const char *kDefaultLayoutDb;
144 #endif // TMCAGLWM_POLICY_MANAGER_HPP