Skip to content

Commit

Permalink
Communication session fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarBLG committed Aug 29, 2023
1 parent 0978327 commit 4f9a51f
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 50 deletions.
6 changes: 2 additions & 4 deletions DMI/graphics/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ using namespace std;
unordered_set<window*> active_windows;
unordered_set<window*> old_windows;
extern Mode mode;
bool dmi_active;
void displayETCS()
{
if (mode == Mode::SL || mode == Mode::NP || mode == Mode::PS)
{
return;
}
updateAcks();
if (!dmi_active) return;
for(auto it=active_windows.begin(); it!=active_windows.end(); ++it)
{
window *w = *it;
Expand Down
5 changes: 5 additions & 0 deletions DMI/tcp/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,13 @@ std::unique_ptr<BasePlatform::BusSocket> evc_socket;
void data_received(BasePlatform::BusSocket::ReceiveResult &&result)
{
evc_socket->receive().then(data_received).detach();
extern bool dmi_active;
if (std::holds_alternative<BasePlatform::BusSocket::Message>(result))
parseData(std::move(std::get<BasePlatform::BusSocket::Message>(result).data));
else if (std::holds_alternative<BasePlatform::BusSocket::JoinNotification>(result))
dmi_active = true;
else if (std::holds_alternative<BasePlatform::BusSocket::LeaveNotification>(result))
dmi_active = false;
}
void write_command(std::string command, std::string value)
{
Expand Down
21 changes: 16 additions & 5 deletions EVC/DMI/dmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "../Procedures/mode_transition.h"
#include "../Procedures/level_transition.h"
#include "../TrackConditions/track_condition.h"
#include "../TrainSubsystems/train_interface.h"
#include "track_ahead_free.h"
#include "text_message.h"
#include "windows.h"
Expand All @@ -46,11 +47,7 @@ void dmi_receive_handler(BasePlatform::BusSocket::ReceiveResult &&result)
}
void start_dmi()
{
dmi_socket = platform->open_socket("evc_dmi", BasePlatform::BusSocket::PeerId::fourcc("EVC"));
if (dmi_socket) {
dmi_socket->receive().then(dmi_receive_handler).detach();
platform->delay(100).then(dmi_update_func).detach();
}
platform->delay(100).then(dmi_update_func).detach();
}
extern double V_target;
extern double V_perm;
Expand Down Expand Up @@ -174,6 +171,20 @@ void dmi_update_func()
sendtoor = get_milliseconds() - lastor > 250;
if (sendtoor) lastor = get_milliseconds();
json j;
if ((!cab_active[0] && !cab_active[1]) || mode == Mode::NP || mode == Mode::PS || mode == Mode::SL) {
dmi_socket = nullptr;
platform->delay(1000).then(dmi_update_func).detach();
return;
}
if (!dmi_socket) {
dmi_socket = platform->open_socket("evc_dmi", BasePlatform::BusSocket::PeerId::fourcc("EVC"));
if (dmi_socket) {
dmi_socket->receive().then(dmi_receive_handler).detach();
} else {
platform->delay(5000).then(dmi_update_func).detach();
return;
}
}
j["AllowedSpeedMpS"] = V_perm;
j["InterventionSpeedMpS"] = V_sbi;
j["TargetSpeedMpS"] = V_target;
Expand Down
47 changes: 37 additions & 10 deletions EVC/Euroradio/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bool handover_report_max = false;
distance rbc_transition_position;
safe_radio_status radio_status_driver;
std::map<contact_info, communication_session*> active_sessions;
bool radio_hole = false;
void communication_session::open(int ntries)
{
pending_ack.remove_if([](const msg_expecting_ack &mack){return mack.nid_ack.find(-1) != mack.nid_ack.end();});
Expand All @@ -42,6 +43,7 @@ void communication_session::open(int ntries)
initsent = false;
status = session_status::Establishing;
radio_status = safe_radio_status::Disconnected;
connection = nullptr;
for (auto it : active_sessions) {
if (it.second != this && it.second != accepting_rbc && it.second != handing_over_rbc && it.second != supervising_rbc)
it.second->close();
Expand Down Expand Up @@ -194,8 +196,20 @@ void communication_session::update()
if (!isRBC && level != Level::N1 && !closing) {
close();
}

bool radio_powered = false;
for (auto *t : mobile_terminals) {
if (t->powered) {
radio_powered = true;
break;
}
}
bool radio = radio_powered && !radio_hole;

if (status == session_status::Establishing) {
if (tried == 0 || ((tried<ntries || ntries <= 0) && radio_status == safe_radio_status::Failed)) {
if (!radio) {
finalize();
} else if (tried == 0 || ((tried<ntries || ntries <= 0) && radio_status == safe_radio_status::Failed)) {
setup_connection();
} else if (tried >= ntries && ntries > 0 && radio_status == safe_radio_status::Failed) {
status = session_status::Inactive;
Expand All @@ -212,13 +226,15 @@ void communication_session::update()
connection_timer = true;
if (train_data_ack_pending && train_data_ack_sent)
train_data_ack_sent = false;
if (get_milliseconds()-last_active > T_keep_session * 1000) {
auto ack = pending_ack;
finalize();
open(0);
pending_ack = ack;
} else {
setup_connection();
if (radio) {
if (get_milliseconds()-last_active > T_keep_session * 1000) {
auto ack = pending_ack;
finalize();
open(0);
pending_ack = ack;
} else {
setup_connection();
}
}
} else if (!closing) {
if ((train_data_valid && train_data_ack_pending && !train_data_ack_sent) || (VERSION_X(version) == 1 && train_running_number_valid && !train_running_number_sent)) {
Expand Down Expand Up @@ -293,6 +309,8 @@ void communication_session::send(std::shared_ptr<euroradio_message_traintotrack>
ack = {27, 28};
else if (msg->NID_MESSAGE == 132 && ((((MA_request*)msg.get())->Q_MARQSTREASON>>Q_MARQSTREASON_t::StartSelectedByDriverBit) & 1) == 1)
ack = {2, 3};
else if (msg->NID_MESSAGE == 150)
ack = {-1};
else if (msg->NID_MESSAGE == 155)
ack = {32};
else if (msg->NID_MESSAGE == 156)
Expand Down Expand Up @@ -347,6 +365,8 @@ void update_euroradio()
handover_report_max = true;
position_report_reasons[10] = true;
}
if (handing_over_rbc && handing_over_rbc->status == session_status::Establishing)
handing_over_rbc->finalize();
}
if (handing_over_rbc && d_minsafefront(rbc_transition_position) - L_TRAIN < rbc_transition_position && !handover_report_min) {
position_report_reasons[4] = true;
Expand All @@ -373,7 +393,8 @@ void update_euroradio()
} else {
radio_status_driver = safe_radio_status::Disconnected;
}
bool radio_hole = false;
bool prev_radio_hole = radio_hole;
radio_hole = false;
for (auto it = track_conditions.begin(); it != track_conditions.end(); ++it) {
auto *tc = it->get();
if (tc->condition == TrackConditions::RadioHole) {
Expand Down Expand Up @@ -413,6 +434,12 @@ void update_euroradio()
radio_reaction_applied = false;
radio_reaction_reconnected = false;
}
if (prev_radio_hole && !radio_hole) {
for (auto &kvp : active_sessions) {
if (kvp.second->status == session_status::Inactive)
kvp.second->open(0);
}
}
if (supervising_rbc && supervising_rbc->status == session_status::Established && (level == Level::N2 || level == Level::N3))
operate_version(supervising_rbc->version, true);
}
Expand Down Expand Up @@ -504,7 +531,7 @@ void terminate_session(contact_info info)
if (it.second == handing_over_rbc)
handing_over_rbc = nullptr;
if (it.second == accepting_rbc)
accepting_rbc = nullptr;
accepting_rbc = handing_over_rbc = nullptr;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion EVC/OR_interface/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ extern distance d_estfront;
extern bool EB_command;
extern bool SB_command;
extern bool desk_open;
extern bool sleep_signal;
double or_dist;
int TimeOffset::offset;
ORserver::ParameterManager manager;
Expand Down
13 changes: 2 additions & 11 deletions EVC/Packets/information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,8 @@ void session_management_information::handle()
contact_info info = {session.NID_C, session.NID_RBC, session.NID_RADIO};
if (session.Q_RBC == Q_RBC_t::EstablishSession) {
set_supervising_rbc(info);
if (supervising_rbc && mode != Mode::SH && mode != Mode::PS) {
bool radio = false;
for (auto *t : mobile_terminals) {
if (t->powered) {
radio = true;
break;
}
}
if (radio)
supervising_rbc->open(0);
}
if (supervising_rbc && mode != Mode::SH && mode != Mode::PS)
supervising_rbc->open(0);
} else {
terminate_session(info);
}
Expand Down
15 changes: 15 additions & 0 deletions EVC/Packets/radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ struct taf_granted : euroradio_message_traintotrack
copy_position_report(r);
}
};
struct end_mission_message : euroradio_message_traintotrack
{
end_mission_message()
{
NID_MESSAGE.rawdata = 150;
}
void copy(bit_manipulator &r) override
{
NID_MESSAGE.copy(r);
L_MESSAGE.copy(r);
T_TRAIN.copy(r);
NID_ENGINE.copy(r);
copy_position_report(r);
}
};
struct text_message_ack_message : euroradio_message_traintotrack
{
NID_TEXTMESSAGE_t NID_TEXTMESSAGE;
Expand Down
14 changes: 8 additions & 6 deletions EVC/Position/distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@ double distance::operator-(const distance d) const
}
distance d_maxsafefront(int orientation, double reference)
{
if (d_estfront.get()-reference < 0)
return distance((d_estfront.get()-reference)*0.99-odometer_orientation*(reference==0 && !lrbgs.empty() ? lrbgs.back().locacc : Q_NVLOCACC), orientation, reference);
return distance((d_estfront.get()-reference)*1.01+odometer_orientation*(reference==0 && !lrbgs.empty() ? lrbgs.back().locacc : Q_NVLOCACC), orientation, reference);
double marg = 1.01;
if ((d_estfront.get()-reference)*odometer_orientation < 0)
marg = 0.99;
return distance((d_estfront.get()-reference)*marg+odometer_orientation*(reference==0 && !lrbgs.empty() ? lrbgs.back().locacc : Q_NVLOCACC), orientation, reference);
}
distance d_minsafefront(int orientation, double reference)
{
if (d_estfront.get()-reference < 0)
return distance((d_estfront.get()-reference)*1.01+odometer_orientation*(reference==0 && !lrbgs.empty() ? lrbgs.back().locacc : Q_NVLOCACC), orientation, reference);
return distance((d_estfront.get()-reference)*0.99-odometer_orientation*(reference==0 && !lrbgs.empty() ? lrbgs.back().locacc : Q_NVLOCACC), orientation, reference);
double marg = 0.99;
if ((d_estfront.get()-reference)*odometer_orientation < 0)
marg = 1.01;
return distance((d_estfront.get()-reference)*marg-odometer_orientation*(reference==0 && !lrbgs.empty() ? lrbgs.back().locacc : Q_NVLOCACC), orientation, reference);
}
distance d_maxsafefront(const distance&ref)
{
Expand Down
23 changes: 17 additions & 6 deletions EVC/Procedures/level_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ void perform_transition()
transition_border = lti.start;
else
transition_border = {};
priority_levels = lti.priority_table;
priority_levels_valid = true;
ongoing_transition = {};
STM_max_speed = {};
STM_system_speed = {};
Expand Down Expand Up @@ -145,6 +143,21 @@ void perform_transition()
ma_params = {30000, (int64_t)(T_CYCRQSTD*1000), 30000};
taf_request = {};
}
if (prevlevel == Level::N2 || prevlevel == Level::N3) {
bool supported = false;
for (auto &lev : lti.priority_table) {
if (lev.level == Level::N2 || lev.level == Level::N3) {
supported = true;
break;
}
}
if (!supported) {
for (auto &kvp : active_sessions) {
if (kvp.second->status == session_status::Establishing)
kvp.second->finalize();
}
}
}
save_level();
}
void update_level_status()
Expand Down Expand Up @@ -191,22 +204,20 @@ void level_transition_received(level_transition_information info)
transition_buffer.clear();
transition_buffer.push_back({});
}
priority_levels = info.priority_table;
priority_levels_valid = true;
if (info.leveldata.level == level && (level != Level::NTC || info.leveldata.nid_ntc == nid_ntc)) {
ongoing_transition = {};
STM_max_speed = {};
STM_system_speed = {};
priority_levels = info.priority_table;
priority_levels_valid = true;
save_level();
return;
}
if (mode == Mode::SH || mode == Mode::PS) {
sh_transition = info;
priority_levels = info.priority_table;
ongoing_transition = {};
STM_max_speed = {};
STM_system_speed = {};
priority_levels_valid = true;
save_level();
return;
}
Expand Down
17 changes: 11 additions & 6 deletions EVC/Procedures/mode_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,17 @@ void update_mode_status()
end_mission = true;
if (end_mission) {
ongoing_mission = false;
if (handing_over_rbc)
handing_over_rbc->close();
if (accepting_rbc)
accepting_rbc->close();
if (supervising_rbc)
supervising_rbc->close();
for (auto &kvp : active_sessions) {
if (!kvp.second->isRBC)
kvp.second->close();
else if (kvp.second->status == session_status::Establishing)
kvp.second->finalize();
}
if (supervising_rbc) {
auto *msg = new end_mission_message();
fill_message(msg);
supervising_rbc->send(std::shared_ptr<euroradio_message_traintotrack>(msg));
}
}
std::vector<deleted_information> info = deleted_informations[mode];
for (int i=0; i<info.size(); i++) {
Expand Down
8 changes: 7 additions & 1 deletion EVC/Procedures/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ bool status_changed;
void update_SoM()
{
som_step save_status = som_status;
if (som_active && !cab_active[0] && !cab_active[1])
if (som_active && !cab_active[0] && !cab_active[1]) {
for (auto &kvp : active_sessions) {
// TODO: Check against SRS
// 3.5.3.8 only mentions stop trying to establish
kvp.second->close();
}
desk_closed_som();
}
if (mode != Mode::SB || (!cab_active[0] && !cab_active[1])) {
som_active = false;
som_status = S0;
Expand Down

0 comments on commit 4f9a51f

Please sign in to comment.