Apollo 10.0
自动驾驶开放平台
hmi_util.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
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
18
19#include <vector>
20
21#include "absl/strings/str_cat.h"
22#include "absl/strings/str_join.h"
23#include "absl/strings/str_split.h"
24
25#include "cyber/common/file.h"
28
29namespace apollo {
30namespace dreamview {
31namespace util {
34using google::protobuf::Map;
35
36std::string HMIUtil::TitleCase(std::string_view origin) {
37 std::vector<std::string> parts = absl::StrSplit(origin, '_');
38 for (auto &part : parts) {
39 if (!part.empty()) {
40 // Upper case the first char.
41 part[0] = static_cast<char>(toupper(part[0]));
42 }
43 }
44
45 return absl::StrJoin(parts, " ");
46}
47
48// List files by pattern and return a dict of {file_title: file_path}.
49Map<std::string, std::string> ListFilesAsDict(std::string_view dir,
50 std::string_view extension) {
51 Map<std::string, std::string> result;
52 const std::string pattern = absl::StrCat(dir, "/*", extension);
53 for (const std::string &file_path : cyber::common::Glob(pattern)) {
54 // Remove the extension and convert to title case as the file title.
55 const std::string filename = cyber::common::GetFileName(file_path);
56 const std::string file_title = HMIUtil::TitleCase(
57 filename.substr(0, filename.length() - extension.length()));
58 result.insert({file_title, file_path});
59 }
60 return result;
61}
62
63// List subdirs and return a dict of {subdir_title: subdir_path}.
64Map<std::string, std::string> HMIUtil::ListDirAsDict(const std::string &dir) {
65 Map<std::string, std::string> result;
66 const auto subdirs = cyber::common::ListSubPaths(dir);
67 for (const auto &subdir : subdirs) {
68 const auto subdir_title = HMIUtil::TitleCase(subdir);
69 const auto subdir_path = absl::StrCat(dir, "/", subdir);
70 result.insert({subdir_title, subdir_path});
71 }
72 return result;
73}
74
75HMIConfig HMIUtil::LoadConfig(std::string config_path) {
76 HMIConfig config;
77 if (config_path.empty()) {
78 *config.mutable_modes() =
79 ListFilesAsDict(FLAGS_dv_hmi_modes_config_path, ".pb.txt");
80 Map<std::string, std::string> dv_plus_modes =
81 ListFilesAsDict(FLAGS_dv_plus_hmi_modes_config_path, ".pb.txt");
82 for (auto iter = dv_plus_modes.begin(); iter != dv_plus_modes.end();
83 iter++) {
84 config.mutable_modes()->insert({iter->first, iter->second});
85 }
86 } else {
87 // Get available modes, maps and vehicles by listing data directory.
88 *config.mutable_modes() =
89 ListFilesAsDict(config_path, ".pb.txt");
90 }
91 ACHECK(!config.modes().empty())
92 << "No modes config loaded";
93
94 *config.mutable_maps() = ListDirAsDict(FLAGS_maps_data_path);
95 *config.mutable_vehicles() = ListDirAsDict(FLAGS_vehicles_config_path);
96 AINFO << "Loaded HMI config: " << config.DebugString();
97 return config;
98}
99
100HMIMode HMIUtil::LoadMode(const std::string &mode_config_path) {
101 HMIMode mode;
102 ACHECK(cyber::common::GetProtoFromFile(mode_config_path, &mode))
103 << "Unable to parse HMIMode from file " << mode_config_path;
105 // For global components.
106 HMIMode mode_temp;
107 ACHECK(cyber::common::GetProtoFromFile(FLAGS_global_components_config_path,
108 &mode_temp))
109 << "Unable to parse HMIMode from file "
110 << FLAGS_global_components_config_path;
111 for (const auto &iter : mode_temp.global_components()) {
112 (*mode.mutable_global_components())[iter.first] = iter.second;
113 }
114 AINFO << "Loaded HMI mode: " << mode.DebugString();
115 return mode;
116}
117
119 // Translate cyber_modules to regular modules.
120 for (const auto &iter : mode->cyber_modules()) {
121 const std::string &module_name = iter.first;
122 const CyberModule &cyber_module = iter.second;
123 // Each cyber module should have at least one dag file.
124 ACHECK(!cyber_module.dag_files().empty())
125 << "None dag file is provided for " << module_name;
126
127 Module &module = LookupOrInsert(mode->mutable_modules(), module_name, {});
128 module.set_required_for_safety(cyber_module.required_for_safety());
129
130 // Construct start_command:
131 // nohup mainboard -p <process_group> -d <dag> ... &
132 module.set_start_command("nohup mainboard");
133 const auto &process_group = cyber_module.process_group();
134 if (!process_group.empty()) {
135 absl::StrAppend(module.mutable_start_command(), " -p ", process_group);
136 }
137 for (const std::string &dag : cyber_module.dag_files()) {
138 absl::StrAppend(module.mutable_start_command(), " -d ", dag);
139 }
140 absl::StrAppend(module.mutable_start_command(), " &");
141
142 // Construct stop_command: pkill -f '<dag[0]>'
143 const std::string &first_dag = cyber_module.dag_files(0);
144 module.set_stop_command(absl::StrCat("pkill -f \"", first_dag, "\""));
145 // Construct process_monitor_config.
146 module.mutable_process_monitor_config()->add_command_keywords("mainboard");
147 module.mutable_process_monitor_config()->add_command_keywords(first_dag);
148 }
149 mode->clear_cyber_modules();
150}
151
152} // namespace util
153} // namespace dreamview
154} // namespace apollo
static std::string TitleCase(std::string_view origin)
Convert a string to be title-like.
Definition hmi_util.cc:36
static google::protobuf::Map< std::string, std::string > ListDirAsDict(const std::string &dir)
List all directory as a dict in a directory.
Definition hmi_util.cc:64
static void TranslateCyberModules(HMIMode *mode)
transfer the mode's cyber modules to modules.
Definition hmi_util.cc:118
static apollo::dreamview::HMIMode LoadMode(const std::string &mode_config_path)
Load HMIMode.
Definition hmi_util.cc:100
static apollo::dreamview::HMIConfig LoadConfig(const std::string config_path="")
Load HMIConfig.
Definition hmi_util.cc:75
#define ACHECK(cond)
Definition log.h:80
#define AINFO
Definition log.h:42
Some map util functions.
std::vector< std::string > Glob(const std::string &pattern)
Expand path pattern to matched paths.
Definition file.cc:212
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::string GetFileName(const std::string &path, const bool remove_extension)
Definition file.cc:415
std::vector< std::string > ListSubPaths(const std::string &directory_path, const unsigned char d_type)
List sub-paths.
Definition file.cc:353
Map< std::string, std::string > ListFilesAsDict(std::string_view dir, std::string_view extension)
Definition hmi_util.cc:49
class register implement
Definition arena_queue.h:37
map< string, string > modes
map< string, CyberModule > cyber_modules
map< string, MonitoredComponent > global_components