Skip to content

Commit 2986396

Browse files
disable openhd ?
1 parent 6b518d7 commit 2986396

File tree

7 files changed

+144
-3
lines changed

7 files changed

+144
-3
lines changed

inc/sysutil_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct SysutilConfig {
8181
// Generic configuration.
8282
std::optional<bool> gen_enable_last_known_position;
8383
std::optional<int> gen_rf_metrics_level;
84+
// Service control.
85+
std::optional<bool> disable_openhd_service;
8486
};
8587

8688
// Result of attempting to load the config file.

inc/sysutil_debug.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#ifndef SYSUTIL_DEBUG_H
2525
#define SYSUTIL_DEBUG_H
2626

27+
#include <optional>
2728
#include <string>
2829

2930
namespace sysutil {
@@ -40,6 +41,9 @@ std::string build_debug_response();
4041
bool is_debug_update(const std::string& line);
4142
// Applies a debug update request and returns a response payload.
4243
std::string handle_debug_update(const std::string& line);
44+
// Syncs the OpenHD debug marker and optionally restarts OpenHD services.
45+
bool apply_openhd_debug_marker(const std::optional<bool>& enabled,
46+
bool restart_services);
4347

4448
} // namespace sysutil
4549

src/openhd_sys_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ int main(int argc, char* argv[]) {
392392

393393
sysutil::init_platform_info();
394394
sysutil::init_debug_info();
395+
gDebug = gDebug || sysutil::debug_enabled();
395396
sysutil::apply_hostname_if_enabled();
396397
sysutil::init_wifi_info();
397398
bool wifi_retry_active = !sysutil::has_openhd_wifibroadcast_cards();

src/sysutil_config.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ ConfigLoadResult load_sysutil_config(SysutilConfig& config) {
138138
extract_bool_field(content, "gen_enable_last_known_position");
139139
config.gen_rf_metrics_level =
140140
extract_int_field(content, "gen_rf_metrics_level");
141+
config.disable_openhd_service =
142+
extract_bool_field(content, "disable_openhd_service");
141143
return ConfigLoadResult::Loaded;
142144
}
143145

@@ -237,6 +239,7 @@ bool write_sysutil_config(const SysutilConfig& config) {
237239
write_bool("gen_enable_last_known_position",
238240
config.gen_enable_last_known_position);
239241
write_int("gen_rf_metrics_level", config.gen_rf_metrics_level);
242+
write_bool("disable_openhd_service", config.disable_openhd_service);
240243

241244
file << "\n}\n";
242245
return static_cast<bool>(file);

src/sysutil_debug.cpp

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
#include "sysutil_debug.h"
2525

2626
#include <filesystem>
27+
#include <fstream>
2728
#include <optional>
2829
#include <sstream>
30+
#include <cstdlib>
2931

3032
#include "sysutil_config.h"
3133
#include "sysutil_protocol.h"
@@ -36,8 +38,9 @@ namespace {
3638
// Legacy debug.txt locations that enable debug mode once.
3739
constexpr const char* kDebugFilePaths[] = {
3840
"/boot/openhd/debug.txt",
39-
"/usr/local/share/openhd/debug.txt",
4041
};
42+
// Persistent OpenHD debug marker for log verbosity.
43+
constexpr const char* kOpenhdDebugMarker = "/usr/local/share/openhd/debug.txt";
4144

4245
// Cached debug state for this process.
4346
std::optional<bool> g_debug_enabled;
@@ -57,6 +60,39 @@ bool remove_file(const std::string& path) {
5760
return std::filesystem::remove(path, ec);
5861
}
5962

63+
bool has_systemctl() {
64+
return std::filesystem::exists("/bin/systemctl") ||
65+
std::filesystem::exists("/usr/bin/systemctl");
66+
}
67+
68+
bool run_cmd(const std::string& cmd) {
69+
return std::system(cmd.c_str()) == 0;
70+
}
71+
72+
bool touch_file(const std::string& path) {
73+
std::error_code ec;
74+
auto parent = std::filesystem::path(path).parent_path();
75+
if (!parent.empty()) {
76+
std::filesystem::create_directories(parent, ec);
77+
}
78+
if (ec) {
79+
return false;
80+
}
81+
if (std::filesystem::exists(path, ec)) {
82+
return true;
83+
}
84+
std::ofstream file(path);
85+
return static_cast<bool>(file);
86+
}
87+
88+
void restart_openhd_services_if_needed() {
89+
if (!has_systemctl()) {
90+
return;
91+
}
92+
(void)run_cmd("systemctl try-restart openhd.service openhd_rpi.service "
93+
"openhd_mod.service openhd-x20.service");
94+
}
95+
6096
} // namespace
6197

6298
// Initializes debug state from config and debug.txt triggers.
@@ -73,12 +109,19 @@ void init_debug_info() {
73109
g_debug_enabled = false;
74110
}
75111

112+
bool debug_marker_seen = false;
76113
for (const auto* path : kDebugFilePaths) {
77114
if (!file_exists(path)) {
78115
continue;
79116
}
80-
g_debug_enabled = true;
117+
debug_marker_seen = true;
81118
(void)remove_file(path);
119+
}
120+
if (file_exists(kOpenhdDebugMarker)) {
121+
debug_marker_seen = true;
122+
}
123+
if (debug_marker_seen) {
124+
g_debug_enabled = true;
82125
SysutilConfig updated_config;
83126
const auto load_result = load_sysutil_config(updated_config);
84127
if (load_result != ConfigLoadResult::Error) {
@@ -140,6 +183,8 @@ std::string handle_debug_update(const std::string& line) {
140183
const bool ok = write_sysutil_config(config);
141184
if (ok) {
142185
g_debug_enabled = *requested;
186+
(void)apply_openhd_debug_marker(requested,
187+
!config.disable_openhd_service.value_or(false));
143188
}
144189

145190
std::ostringstream out;
@@ -149,4 +194,23 @@ std::string handle_debug_update(const std::string& line) {
149194
return out.str();
150195
}
151196

197+
bool apply_openhd_debug_marker(const std::optional<bool>& enabled,
198+
bool restart_services) {
199+
if (!enabled.has_value()) {
200+
return true;
201+
}
202+
const bool want_debug = *enabled;
203+
bool ok = true;
204+
if (want_debug) {
205+
ok = touch_file(kOpenhdDebugMarker);
206+
} else {
207+
ok = remove_file(kOpenhdDebugMarker);
208+
}
209+
g_debug_enabled = want_debug;
210+
if (restart_services) {
211+
restart_openhd_services_if_needed();
212+
}
213+
return ok;
214+
}
215+
152216
} // namespace sysutil

src/sysutil_settings.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "sysutil_camera.h"
3636
#include "sysutil_config.h"
37+
#include "sysutil_debug.h"
3738
#include "sysutil_hostname.h"
3839
#include "sysutil_protocol.h"
3940
#include "sysutil_status.h"
@@ -175,6 +176,23 @@ void sync_settings_from_files() {
175176
}
176177
}
177178

179+
if (auto disable_openhd =
180+
extract_bool_field(content, "disable_openhd_service");
181+
disable_openhd.has_value()) {
182+
config.disable_openhd_service = *disable_openhd;
183+
changed = true;
184+
}
185+
if (auto debug = extract_bool_field(content, "debug");
186+
debug.has_value()) {
187+
config.debug_enabled = *debug;
188+
changed = true;
189+
} else if (auto debug_enabled =
190+
extract_bool_field(content, "debug_enabled");
191+
debug_enabled.has_value()) {
192+
config.debug_enabled = *debug_enabled;
193+
changed = true;
194+
}
195+
178196
// sbc field is currently ignored as platform detection handles it.
179197
}
180198
remove_file_if_exists(json_path.c_str());
@@ -283,6 +301,8 @@ std::string build_settings_response() {
283301
const bool gen_enable_last_known_position =
284302
config.gen_enable_last_known_position.value_or(false);
285303
const int gen_rf_metrics_level = config.gen_rf_metrics_level.value_or(0);
304+
const bool disable_openhd_service =
305+
config.disable_openhd_service.value_or(false);
286306

287307
std::ostringstream out;
288308
out << "{\"type\":\"sysutil.settings.response\",\"ok\":true"
@@ -328,7 +348,9 @@ std::string build_settings_response() {
328348
<< ",\"microhard_telemetry_port\":" << microhard_telemetry_port
329349
<< ",\"gen_enable_last_known_position\":"
330350
<< (gen_enable_last_known_position ? "true" : "false")
331-
<< ",\"gen_rf_metrics_level\":" << gen_rf_metrics_level << "}\n";
351+
<< ",\"gen_rf_metrics_level\":" << gen_rf_metrics_level
352+
<< ",\"disable_openhd_service\":"
353+
<< (disable_openhd_service ? "true" : "false") << "}\n";
332354
return out.str();
333355
}
334356

@@ -341,6 +363,7 @@ std::string handle_settings_update(const std::string& line) {
341363

342364
bool changed = false;
343365
bool hostname_related_change = false;
366+
bool debug_changed = false;
344367
if (auto reset_requested = extract_bool_field(line, "reset_requested");
345368
reset_requested.has_value()) {
346369
config.reset_requested = *reset_requested;
@@ -513,11 +536,33 @@ std::string handle_settings_update(const std::string& line) {
513536
config.gen_rf_metrics_level = *gen_rf_metrics_level;
514537
changed = true;
515538
}
539+
if (auto disable_openhd_service =
540+
extract_bool_field(line, "disable_openhd_service");
541+
disable_openhd_service.has_value()) {
542+
config.disable_openhd_service = *disable_openhd_service;
543+
changed = true;
544+
}
545+
if (auto debug = extract_bool_field(line, "debug"); debug.has_value()) {
546+
config.debug_enabled = *debug;
547+
changed = true;
548+
debug_changed = true;
549+
} else if (auto debug_enabled =
550+
extract_bool_field(line, "debug_enabled");
551+
debug_enabled.has_value()) {
552+
config.debug_enabled = *debug_enabled;
553+
changed = true;
554+
debug_changed = true;
555+
}
516556

517557
bool ok = true;
518558
if (changed) {
519559
ok = write_sysutil_config(config);
520560
}
561+
if (ok && debug_changed) {
562+
const bool restart_openhd =
563+
!config.disable_openhd_service.value_or(false);
564+
(void)apply_openhd_debug_marker(config.debug_enabled, restart_openhd);
565+
}
521566
if (ok && hostname_related_change) {
522567
apply_hostname_if_enabled();
523568
}

src/sysutil_video.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "sysutil_video.h"
2525
#include "sysutil_config.h"
26+
#include "sysutil_debug.h"
2627
#include "sysutil_platform.h"
2728
#include "sysutil_protocol.h"
2829
#include "sysutil_status.h"
@@ -137,6 +138,18 @@ bool has_systemctl() {
137138
std::filesystem::exists("/usr/bin/systemctl");
138139
}
139140

141+
void apply_openhd_service_disable() {
142+
if (!has_systemctl()) {
143+
set_status("sysutils.services", "Service status",
144+
"systemctl missing; cannot disable OpenHD service", 2);
145+
return;
146+
}
147+
run_cmd("systemctl stop openhd.service openhd_rpi.service openhd_mod.service");
148+
run_cmd("systemctl disable openhd.service openhd_rpi.service openhd_mod.service");
149+
set_status("sysutils.services", "Service status",
150+
"OpenHD service disabled via sysutils config", 1);
151+
}
152+
140153
bool ensure_qopenhd_getty_dropin() {
141154
std::error_code ec;
142155
const std::string dropin_dir = "/etc/systemd/system/qopenhd.service.d";
@@ -391,6 +404,15 @@ void start_openhd_services_if_needed() {
391404
const bool ground = is_ground_mode();
392405
const bool rockchip = is_rockchip_platform();
393406

407+
SysutilConfig config;
408+
if (load_sysutil_config(config) == ConfigLoadResult::Loaded) {
409+
(void)apply_openhd_debug_marker(config.debug_enabled, false);
410+
if (config.disable_openhd_service.value_or(false)) {
411+
apply_openhd_service_disable();
412+
return;
413+
}
414+
}
415+
394416
if (!systemd_ok) {
395417
set_status("sysutils.services", "Service status",
396418
"systemctl missing; cannot manage services", 2);

0 commit comments

Comments
 (0)