forked from f1xpl/openauto
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
339 lines (289 loc) · 10.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <string>
#include <filesystem>
#include <utility>
#include "installer/main.hpp"
#include "easylogging++.h"
#include "sys/mount.h"
#include <getopt.h>
#define MINI_CASE_SENSITIVE
#include <ini.h>
INITIALIZE_EASYLOGGINGPP
int installer::mkdir(const fs::path &path) {
fs::directory_entry dir_path{path};
if (!dir_path.exists()) {
if (!fs::create_directories(dir_path)) {
LOG(ERROR) << "Couldn't create " << dir_path;
return -1;
}
installed_files.emplace_back(path);
}
LOG(INFO) << path << " Created";
return 0;
}
void installer::backup(const fs::path &path) {
fs::path backup_destination = backup_path;
backup_destination += path;
if (!std::filesystem::exists(backup_destination)) {
if (mkdir(backup_destination.parent_path()) == 0) {
LOG(INFO) << "Backing up " << path << " to " << backup_destination << std::endl;
try {
fs::copy(path, backup_destination);
}
catch (fs::filesystem_error &error) {
LOG(INFO) << backup_destination << " " << error.what() << std::endl;
}
}
}
backup_files.emplace_back(backup_destination);
}
void installer::install_bds() {
/// Configure BDS so that Android Auto can connect to our Bluetooth service to get the WiFi settings
backup("/jci/bds/BdsConfiguration.xml");
tinyxml2::XMLDocument doc;
doc.LoadFile("/jci/bds/BdsConfiguration.xml");
tinyxml2::XMLNode *docRoot = doc.FirstChild();
tinyxml2::XMLElement *serviceconfig = docRoot->FirstChildElement("serviceConfiguration");
if (serviceconfig == nullptr) {
LOG(DEBUG) << "Couldn't find serviceConfiguration in /jci/bds/BdsConfiguration.xml";
} else {
int lastId = 0;
bool bdsconfigured = false;
for (tinyxml2::XMLElement *element = serviceconfig->FirstChildElement(); element != nullptr;
element = element->NextSiblingElement()) {
lastId = element->IntAttribute("id", lastId);
if (std::string(element->Attribute("name")) == "AndroidAuto") {
bdsconfigured = true;
LOG(INFO) << "/jci/bds/BdsConfiguration.xml already configured";
}
}
if (!bdsconfigured) {
tinyxml2::XMLElement *element = serviceconfig->InsertNewChildElement("serialPort");
element->SetAttribute("id", lastId + 1);
element->SetAttribute("name", "AndroidAuto");
element->SetAttribute("noOfInstances", 1);
element->SetAttribute("critical", false);
element->SetAttribute("enabled", true);
element->SetAttribute("uuidServer", "4DE17A0052CB11E6BDF40800200C9A66");
element->SetAttribute("uuidClient", "4DE17A0052CB11E6BDF40800200C9A66");
element->SetAttribute("writeDelay", 3);
doc.SaveFile("/jci/bds/BdsConfiguration.xml");
LOG(INFO) << "/jci/bds/BdsConfiguration.xml configured";
}
}
}
bool installer::checkAapaVersion() {
/// Check if CMU Firmware already has Android Auto built in.
mINI::INIFile file("/jci/version.ini");
mINI::INIStructure ini;
file.read(ini);
return ini["VersionInfo"].has("JCI_BLM_AAPA-IHU");
}
void installer::setup_sm() {
/// Configure SM to start autoapp on boot.
backup("/jci/sm/sm.conf");
tinyxml2::XMLDocument doc;
doc.LoadFile("/jci/sm/sm.conf");
tinyxml2::XMLNode *docRoot = doc.FirstChild()->NextSibling();
LOG(DEBUG) << docRoot->GetLineNum();
tinyxml2::XMLElement *serviceconfig = docRoot->FirstChildElement("services");
if (serviceconfig == nullptr) {
LOG(DEBUG) << "Couldn't find services in /jci/sm/sm.conf";
} else {
bool serviceconfigured = false;
for (tinyxml2::XMLElement *element = serviceconfig->FirstChildElement(); element != nullptr;
element = element->NextSiblingElement()) {
if (std::string(element->Attribute("name")) == "autoapp") {
serviceconfigured = true;
LOG(INFO) << "/jci/sm/sm.conf already configured";
}
}
if (!serviceconfigured) {
tinyxml2::XMLElement *element = serviceconfig->InsertNewChildElement("service");
element->SetAttribute("type", "process");
element->SetAttribute("name", "autoapp");
element->SetAttribute("path", "/mnt/data_persist/dev/bin/autoapp");
element->SetAttribute("autorun", "yes");
element->SetAttribute("reset_board", "no");
element->SetAttribute("retry_count", 6);
element->SetAttribute("args", "");
tinyxml2::XMLElement *dependancy = element->InsertNewChildElement("dependency");
dependancy->SetAttribute("type", "service");
dependancy->SetAttribute("value", "bds");
dependancy = element->InsertNewChildElement("dependency");
dependancy->SetAttribute("type", "service");
dependancy->SetAttribute("value", "audio_manager");
dependancy = element->InsertNewChildElement("dependency");
dependancy->SetAttribute("type", "service");
dependancy->SetAttribute("value", "start_wifi");
}
if (checkAapaVersion()) {
for (tinyxml2::XMLElement *element = serviceconfig->FirstChildElement(); element != nullptr;
element = element->NextSiblingElement()) {
if (std::string(element->Attribute("name")) == "jciAAPA") {
LOG(INFO) << "Disabling jciAAPA";
element->SetAttribute("autorun", false);
}
if (std::string(element->Attribute("name")) == "aap_service") {
LOG(INFO) << "Disabling aap_service";
element->SetAttribute("autorun", false);
}
}
}
doc.SaveFile("/jci/sm/sm.conf");
LOG(INFO) << "/jci/sm/sm.conf configured";
}
}
void installer::setup_mmui() {
/// Setup the MMUI configuration.
/// We need to increase the priority of androidauto in MMUI, so that it doesn't loose video focus to phone calls
tinyxml2::XMLDocument doc;
doc.LoadFile("/jci/mmui/mmui_config.xml");
tinyxml2::XMLNode *docRoot = doc.FirstChild()->NextSibling();
LOG(DEBUG) << docRoot->GetLineNum();
for (tinyxml2::XMLElement *element = docRoot->FirstChildElement(); element != nullptr;
element = element->NextSiblingElement()) {
if (std::string(element->Attribute("name")) == "androidauto" && int(element->Attribute("priority")) != 20) {
backup("/jci/mmui/mmui_config.xml");
element->SetAttribute("priority", 20);
LOG(DEBUG) << "Reset androidauto priority to 20";
doc.SaveFile("/jci/mmui/mmui_config.xml");
break;
}
}
}
void installer::configure_opera() {
/// Configure Opera settings for Android Auto if not using a version of CMU Firmware that has it built in.
backup("/jci/opera/opera_home/opera.ini");
mINI::INIFile file("/jci/opera/opera_home/opera.ini");
mINI::INIStructure ini;
file.read(ini);
std::string &user_javascript = ini["User Prefs"]["User JavaScript"];
std::string &allow_xmlhttprequests = ini["User Prefs"]["Allow File XMLHttpRequest"];
user_javascript.assign("1");
allow_xmlhttprequests.assign("1");
file.write(ini, false);
if (fs::exists("/jci/opera/opera_dir/userjs/fps.js")) {
backup("/jci/opera/opera_dir/userjs/fps.js");
fs::remove("/jci/opera/opera_dir/userjs/fps.js");
}
}
void installer::copy_file(const fs::path &from, const fs::path &to) {
if (std::filesystem::exists(to.parent_path())) {
mkdir(to.parent_path());
installed_files.emplace_back(to.parent_path());
}
try {
std::filesystem::copy(from, to);
}
catch (fs::filesystem_error &error) {
LOG(INFO) << to << " " << error.what() << std::endl;
}
installed_files.emplace_back(to);
}
void installer::install_files() {
if (!checkAapaVersion()) {
for (std::filesystem::recursive_directory_iterator i("jci"), end; i != end; ++i) {
if (!is_directory(i->path())) {
std::filesystem::path path = "/";
path += std::filesystem::relative(i->path());
copy_file(std::filesystem::absolute(i->path()), path);
}
}
}
mkdir("/mnt/data_persist/dev/bin");
copy_file(fs::path("autoapp"), "/mnt/data_persist/dev/bin/autoapp");
if (std::filesystem::exists("autoapp_configuration.toml")) {
copy_file("autoapp_configuration.toml", "/mnt/data_persist/dev/bin/autoapp_configuration.toml");
}
}
void installer::generate_uninstaller() {
fs::path uninstall = "/mnt/data_persist/dev/bin/autoapp.uninstall";
installed_files.emplace_back(uninstall);
std::ofstream uninstallScript;
uninstallScript.open("/mnt/data_persist/dev/bin/autoapp.uninstall");
uninstallScript << "#!/bin/ash\n";
uninstallScript << "mount -o remount,rw / \n";
for (const auto &file : backup_files) {
std::filesystem::path destination = "/";
destination += std::filesystem::relative(std::filesystem::absolute(file), backup_path);
uninstallScript << "mv " << file << " " << destination << " \n";
}
uninstallScript << "rm -rf";
for (const auto &file : installed_files) {
uninstallScript << " \\\n" << file;
}
uninstallScript << "printf \"Uninstalled\\n\"\n";
uninstallScript.close();
fs::permissions(uninstall, fs::perms::owner_exec | fs::perms::group_exec, fs::perm_options::add);
}
installer::installer(fs::path backup_dir) : backup_path(std::move(backup_dir)) {
}
void usage(const char *path) {
const char *basename = strrchr(path, '/');
basename = basename != nullptr ? basename + 1 : path;
std::cout << "usage: " << basename << " [OPTION]\n";
std::cout << " -h, --help\t\t" <<"Print this help and exit.\n";
std::cout << " -w, --no-wifi\t\t" << "disable WiFi\n";
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) {
int opt;
int help_flag = 0;
bool wifi = true;
struct option longopts[] = {
{"help", no_argument, &help_flag, 1},
{"no-wifi", optional_argument, nullptr, 'w'},
{nullptr}
};
if (argc > 1) {
while (true) {
opt = getopt_long(argc, argv, "hw", longopts, nullptr);
if (opt == -1) {
/* a return value of -1 indicates that there are no more options */
break;
}
switch (opt) {
case 'h':
help_flag = 1;
break;
case 'w':
wifi = false;
break;
case '?':
return 1;
default:
break;
}
}
}
if (help_flag == 1) {
usage(argv[0]);
return 0;
}
if (mount("", "/", "", MS_REMOUNT | MS_NOATIME, "") != 0) {
LOG(ERROR) << "Couldn't remount / rw";
exit(1);
}
fs::path uninstaller = "/mnt/data_persist/dev/bin/autoapp.uninstall";
if (fs::exists(uninstaller)) {
LOG(INFO) << "Running uninstaller to cleanup old install";
system(uninstaller.c_str());
}
const auto path_backup = fs::absolute("/mnt/data_persist/dev/backup/");
installer install(path_backup);
install.mkdir(path_backup);
fs::directory_entry dir_backup{path_backup};
if (wifi) {
install.install_bds();
}
install.setup_sm();
// Set up the right configuration depending on CMU version
if (!install.checkAapaVersion()) {
install.configure_opera();
}
install.setup_mmui();
install.install_files();
install.generate_uninstaller();
if (mount("", "/", "", MS_REMOUNT | MS_NOATIME | MS_RDONLY, "") != 0) {
LOG(ERROR) << "Couldn't remount / ro";
}
}