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;
33 explicit PolicyManager();
36 using Handler = std::function<void(json_object *)>;
40 Handler onStateTransitioned;
44 int initialize(std::string ecu_name);
45 void registerCallback(CallbackTable callback_table);
46 int setInputEventData(json_object *json_in);
47 int executeStateTransition();
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);
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;
61 typedef struct EventInfo
68 typedef struct AreaState
75 typedef std::vector<AreaState> AreaList;
76 typedef struct LayoutState
79 std::map<std::string, int> category_num;
83 typedef struct LayerState
86 LayoutState layout_state;
96 typedef std::vector<std::string> Areas;
97 typedef std::vector<std::string> Categories;
98 typedef std::vector<std::string> Roles;
100 StmState *p_crr_state;
101 StmState *p_prv_state;
104 std::unordered_map<std::string, int> eventname2no;
105 std::unordered_map<std::string, int> categoryname2no;
106 std::unordered_map<std::string, int> areaname2no;
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;
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;
117 CallbackTable callback;
119 std::unordered_map<std::string, Mode> prv_car_elements;
120 std::unordered_map<std::string, Mode> crr_car_elements;
122 std::unordered_map<std::string, LayerState> prv_layers;
123 std::unordered_map<std::string, LayerState> crr_layers;
125 std::unordered_map<std::string, LayerState> prv_layers_car_stop;
127 std::unordered_map<std::string, LayoutState> default_layouts;
129 std::map<std::string, Roles> crr_invisible_role_history;
130 std::map<std::string, Roles> prv_invisible_role_history;
132 std::string ecu_name;
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);
146 void pushInvisibleRoleHistory(std::string category, std::string role);
147 std::string popInvisibleRoleHistory(std::string category);
149 bool changedRestrictionModeTo2On();
150 bool changedRestrictionMode2OnToOther();
151 // bool changedLightstatusBrakeOffToOn();
152 // bool changedLightstatusBrakeOnToOff();
153 bool changedAccelPedalOffToOn();
154 bool changedAccelPedalOnToOff();
156 int loadRolesConfigFile();
157 int loadLayoutsConfigFile();
159 void dumpLayerState(std::unordered_map<std::string, LayerState> &layers);
160 void dumpInvisibleRoleHistory();
162 void addStateToJson(const char *name, bool changed,
163 std::string state, json_object **json_out);
164 void addStateToJson(const char *layer_name, bool changed,
165 AreaList area_list, json_object **json_out);
166 const char *getStringFromJson(json_object *obj, const char *key);
167 int inputJsonFilie(const char *file, json_object **obj);
169 std::vector<std::string> parseString(std::string str, char delimiter);
170 std::string deleteSpace(std::string str);
172 static const char *kDefaultRolesConfig;
173 static const char *kDefaultLayoutsConfig;
176 #endif // TMCAGLWM_POLICY_MANAGER_HPP