Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boardd: wait for safety_setter_thread to finish while quitting panda_state_thread #21961

Merged
merged 4 commits into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions selfdrive/boardd/boardd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <bitset>
#include <cassert>
#include <cerrno>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <future>
#include <thread>
#include <unordered_map>

Expand All @@ -34,20 +36,19 @@
#define CUTOFF_IL 200
#define SATURATE_IL 1600
#define NIBBLE_TO_HEX(n) ((n) < 10 ? (n) + '0' : ((n) - 10) + 'a')
using namespace std::chrono_literals;

std::atomic<bool> safety_setter_thread_running(false);
std::atomic<bool> ignition(false);

ExitHandler do_exit;


std::string get_time_str(const struct tm &time) {
char s[30] = {'\0'};
std::strftime(s, std::size(s), "%Y-%m-%d %H:%M:%S", &time);
return s;
}

void safety_setter_thread(Panda *panda) {
bool safety_setter_thread(Panda *panda) {
LOGD("Starting safety setter thread");
// diagnostic only is the default, needed for VIN query
panda->set_safety_model(cereal::CarParams::SafetyModel::ELM327);
Expand All @@ -57,8 +58,7 @@ void safety_setter_thread(Panda *panda) {
// switch to SILENT when CarVin param is read
while (true) {
if (do_exit || !panda->connected) {
safety_setter_thread_running = false;
return;
return false;
};

std::string value_vin = p.get("CarVin");
Expand All @@ -78,8 +78,7 @@ void safety_setter_thread(Panda *panda) {
LOGW("waiting for params to set safety model");
while (true) {
if (do_exit || !panda->connected) {
safety_setter_thread_running = false;
return;
return false;
};

if (p.getBool("ControlsReady")) {
Expand All @@ -101,8 +100,7 @@ void safety_setter_thread(Panda *panda) {
LOGW("setting safety model: %d with param %d", (int)safety_model, safety_param);

panda->set_safety_model(safety_model, safety_param);

safety_setter_thread_running = false;
return true;
}


Expand Down Expand Up @@ -349,6 +347,7 @@ void send_peripheral_state(PubMaster *pm, Panda *panda) {
void panda_state_thread(PubMaster *pm, Panda * peripheral_panda, Panda *panda, bool spoofing_started) {
Params params;
bool ignition_last = false;
std::future<bool> safety_future;

LOGD("start panda state thread");

Expand All @@ -360,10 +359,8 @@ void panda_state_thread(PubMaster *pm, Panda * peripheral_panda, Panda *panda, b
// clear VIN, CarParams, and set new safety on car start
if (ignition && !ignition_last) {
params.clearAll(CLEAR_ON_IGNITION_ON);

if (!safety_setter_thread_running) {
safety_setter_thread_running = true;
std::thread(safety_setter_thread, panda).detach();
if (!safety_future.valid() || safety_future.wait_for(0ms) == std::future_status::ready) {
safety_future = std::async(std::launch::async, safety_setter_thread, panda);
} else {
LOGW("Safety setter thread already running");
}
Expand Down