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;
36 explicit PolicyManager();
37 ~PolicyManager() = default;
39 using Handler = std::function<void(json_object *)>;
42 Handler onStateTransitioned;
47 void registerCallback(CallbackTable callback_table);
48 int setInputEventData(json_object* json_in);
49 int executeStateTransition();
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 {
68 typedef struct AreaState {
74 typedef std::vector<AreaState> AreaList;
75 typedef struct LayoutState {
77 std::map<std::string, int> category_num;
79 std::map<std::string, std::vector<std::string>> role_history;
82 typedef struct LayerState {
84 LayoutState layout_state;
88 typedef struct CarElement {
94 std::unordered_map<std::string, int> eventname2no;
95 std::unordered_map<std::string, int> categoryname2no;
96 std::unordered_map<std::string, int> areaname2no;
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;
102 std::queue<EventInfo> event_info_queue;
104 struct sd_event* event_loop;
105 CallbackTable callback;
107 std::map<int, struct sd_event_source*> event_source_list;
108 std::map<int, std::string> req_role_list;
110 std::unordered_map<std::string, CarElement> prv_car_elements;
111 std::unordered_map<std::string, CarElement> crr_car_elements;
113 std::unordered_map<std::string, LayerState> prv_layers;
114 std::unordered_map<std::string, LayerState> crr_layers;
116 std::unordered_map<std::string, LayerState> prv_layers_car_stop;
118 std::unordered_map<std::string, LayoutState> default_layouts;
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);
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);
144 std::vector<std::string> parseString(std::string str, char delimiter);
145 std::string deleteSpace(std::string str);
149 #endif // TMCAGLWM_POLICY_MANAGER_HPP