Skip to content

Commit

Permalink
Support for Lumissil reset in EvseSlac
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Claussen <[email protected]>
  • Loading branch information
corneliusclaussen committed Oct 2, 2024
1 parent f19861c commit 4e61ff1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
3 changes: 2 additions & 1 deletion lib/staging/slac/fsm/evse/include/slac/fsm/evse/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ struct EvseSlacConfig {
struct chip_reset_struct {
bool enabled = false;
int timeout_ms = 500;
int delay_ms = 100;
int delay_before_reset_ms = 100;
int delay_after_reset_ms = 100;
} chip_reset;

// Settings for LINK_STATUS detection
Expand Down
16 changes: 13 additions & 3 deletions lib/staging/slac/fsm/evse/include/slac/fsm/evse/states/others.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ struct ResetState : public FSMSimpleState {

void enter() final;
CallbackReturnType callback() final;
};

struct UpdateNMKState : public FSMSimpleState {
using FSMSimpleState::FSMSimpleState;

HandleEventReturnType handle_event(AllocatorType&, Event) final;

void enter() final;
CallbackReturnType callback() final;

// for now returns true if CM_SET_KEY_CNF is received
bool handle_slac_message(slac::messages::HomeplugMessage&);

bool setup_has_been_send{false};
bool set_key_req_has_been_send{false};

Check notice on line 31 in lib/staging/slac/fsm/evse/include/slac/fsm/evse/states/others.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/staging/slac/fsm/evse/include/slac/fsm/evse/states/others.hpp#L31

struct member 'UpdateNMKState::set_key_req_has_been_send' is never used.
};

struct ResetChipState : public FSMSimpleState {
Expand All @@ -37,10 +46,11 @@ struct ResetChipState : public FSMSimpleState {
bool chip_reset_has_been_sent{false};

enum class SubState {
DELAY,
DELAY_BEFORE_RESET,
SEND_RESET,
DELAY_AFTER_RESET,
DONE,
} sub_state{SubState::DELAY};
} sub_state{SubState::DELAY_BEFORE_RESET};
};

struct IdleState : public FSMSimpleState {
Expand Down
71 changes: 50 additions & 21 deletions lib/staging/slac/fsm/evse/src/states/others.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <slac/fsm/evse/states/matching.hpp>

#include "../misc.hpp"

#include <unistd.h>
namespace slac::fsm::evse {

static auto create_cm_set_key_req(uint8_t const* session_nmk) {
Expand All @@ -34,45 +34,63 @@ void ResetState::enter() {
}

FSMSimpleState::HandleEventReturnType ResetState::handle_event(AllocatorType& sa, Event ev) {
const auto& cfg = ctx.slac_config;
if (ev == Event::RESET) {
// For Qualcomm, we need to do a config NMK and then a reset.
// For Lumissil, we do a reset first and then a config NMK
if (ctx.modem_vendor == ModemVendor::Lumissil and cfg.chip_reset.enabled) {
return sa.create_simple<ResetChipState>(ctx);
} else {
return sa.create_simple<UpdateNMKState>(ctx);
}
} else {
return sa.PASS_ON;
}
}

FSMSimpleState::CallbackReturnType ResetState::callback() {
return Event::RESET;
}

void UpdateNMKState::enter() {
ctx.log_info("Entered UpdateNMKState state");
sleep(10); // FIXME we need to find out when Lumissil is ready after reset
}

FSMSimpleState::HandleEventReturnType UpdateNMKState::handle_event(AllocatorType& sa, Event ev) {
const auto& cfg = ctx.slac_config;
if (ev == Event::SLAC_MESSAGE) {
if (handle_slac_message(ctx.slac_message_payload)) {
if (cfg.chip_reset.enabled) {
if (cfg.chip_reset.enabled and ctx.modem_vendor == ModemVendor::Qualcomm) {
// If chip reset is enabled in config, go to ResetChipState and from there to IdleState
return sa.create_simple<ResetChipState>(ctx);
} else {
// If chip reset is disabled, go to IdleState directly
// If chip reset is disabled or not Qualcomm, go to IdleState directly
return sa.create_simple<IdleState>(ctx);
}
} else {
return sa.PASS_ON;
}
} else if (ev == Event::RESET) {
return sa.create_simple<ResetState>(ctx);
} else {
return sa.PASS_ON;
}
}

FSMSimpleState::CallbackReturnType ResetState::callback() {
FSMSimpleState::CallbackReturnType UpdateNMKState::callback() {
const auto& cfg = ctx.slac_config;
if (setup_has_been_send == false) {
if (set_key_req_has_been_send == false) {
auto set_key_req = create_cm_set_key_req(cfg.session_nmk);

ctx.log_info("New NMK key: " + format_nmk(cfg.session_nmk));

ctx.send_slac_message(cfg.plc_peer_mac, set_key_req);

setup_has_been_send = true;

set_key_req_has_been_send = true;
return cfg.set_key_timeout_ms;
} else {
ctx.log_info("CM_SET_KEY_REQ timeout - failed to setup NMK key");
return {};
}
}

bool ResetState::handle_slac_message(slac::messages::HomeplugMessage& message) {
bool UpdateNMKState::handle_slac_message(slac::messages::HomeplugMessage& message) {
const auto mmtype = message.get_mmtype();
if (mmtype != (slac::defs::MMTYPE_CM_SET_KEY | slac::defs::MMTYPE_MODE_CNF)) {
// unexpected message
Expand All @@ -92,22 +110,34 @@ void ResetChipState::enter() {
FSMSimpleState::HandleEventReturnType ResetChipState::handle_event(AllocatorType& sa, Event ev) {
if (ev == Event::SLAC_MESSAGE) {
if (handle_slac_message(ctx.slac_message_payload)) {
return sa.create_simple<IdleState>(ctx);
if (ctx.modem_vendor == ModemVendor::Lumissil) {
return sa.create_simple<UpdateNMKState>(ctx);
} else {
return sa.create_simple<IdleState>(ctx);
}
} else {
return sa.PASS_ON;
}
} else if (ev == Event::SUCCESS) {
return sa.create_simple<IdleState>(ctx);
if (ctx.modem_vendor == ModemVendor::Lumissil) {
return sa.create_simple<UpdateNMKState>(ctx);
} else {
return sa.create_simple<IdleState>(ctx);
}
} else {
return sa.PASS_ON;
}
}

FSMSimpleState::CallbackReturnType ResetChipState::callback() {
const auto& cfg = ctx.slac_config;
if (sub_state == SubState::DELAY) {
if (sub_state == SubState::DELAY_BEFORE_RESET) {
sub_state = SubState::SEND_RESET;
return cfg.chip_reset.delay_ms;
return cfg.chip_reset.delay_before_reset_ms;

} else if (sub_state == SubState::DELAY_AFTER_RESET) {
sub_state = SubState::DONE;
return Event::SUCCESS;

} else if (sub_state == SubState::SEND_RESET) {

Expand All @@ -121,11 +151,10 @@ FSMSimpleState::CallbackReturnType ResetChipState::callback() {
} else if (ctx.modem_vendor == ModemVendor::Lumissil) {
slac::messages::lumissil::nscm_reset_device_req reset_req;
ctx.log_info("Resetting HW Chip using NSCM_RESET_DEVICE.REQ");
sub_state = SubState::DONE;
sub_state = SubState::DELAY_AFTER_RESET;
ctx.send_slac_message(cfg.plc_peer_mac, reset_req);
// CG5317 does not reply to the reset packet
return Event::SUCCESS;

// CG5317 does not reply to the reset packet, so just wait a configured delay
return cfg.chip_reset.delay_after_reset_ms;
} else {
ctx.log_info("Chip reset not supported on this chip");
}
Expand Down
2 changes: 1 addition & 1 deletion modules/EvseSlac/main/slacImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void slacImpl::run() {
fsm_ctx.slac_config.sounding_atten_adjustment = config.sounding_attenuation_adjustment;

fsm_ctx.slac_config.chip_reset.enabled = config.do_chip_reset;
fsm_ctx.slac_config.chip_reset.delay_ms = config.chip_reset_delay_ms;
fsm_ctx.slac_config.chip_reset.delay_before_reset_ms = config.chip_reset_delay_ms;
fsm_ctx.slac_config.chip_reset.timeout_ms = config.chip_reset_timeout_ms;

fsm_ctx.slac_config.link_status.do_detect = config.link_status_detection;
Expand Down

0 comments on commit 4e61ff1

Please sign in to comment.