Skip to content

Commit

Permalink
Start work on fixing serial subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Nov 14, 2024
1 parent 611343b commit c31212f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 96 deletions.
46 changes: 24 additions & 22 deletions src/serial/command_handlers/estop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
#include "Convert.h"
#include "EStopManager.h"

void _handleEStopEnabledCommand(std::string_view arg, bool isAutomated)
void _handleEStopEnabledGetCommand(std::string_view arg, bool isAutomated)
{
bool enabled;
if (arg.empty()) {
if (!OpenShock::Config::GetEStopEnabled(enabled)) {
SERPR_ERROR("Failed to get EStop enabled from config");
return;
}

// Get EStop enabled
SERPR_RESPONSE("EStopEnabled|%s", enabled ? "true" : "false");
if (!OpenShock::Config::GetEStopEnabled(enabled)) {
SERPR_ERROR("Failed to get EStop enabled from config");
return;
}

// Get EStop enabled
SERPR_RESPONSE("EStopEnabled|%s", enabled ? "true" : "false");
}

void _handleEStopEnabledSetCommand(std::string_view arg, bool isAutomated)
{
bool enabled;
if (!OpenShock::Convert::ToBool(arg, enabled)) {
SERPR_ERROR("Invalid argument (must be a boolean)");
return;
Expand All @@ -36,20 +37,21 @@ void _handleEStopEnabledCommand(std::string_view arg, bool isAutomated)
SERPR_SUCCESS("Saved config");
}

void _handleEStopPinCommand(std::string_view arg, bool isAutomated)
void _handleEStopPinGetCommand(std::string_view arg, bool isAutomated)
{
gpio_num_t estopPin;
if (arg.empty()) {
if (!OpenShock::Config::GetEStopGpioPin(estopPin)) {
SERPR_ERROR("Failed to get EStop pin from config");
return;
}

// Get EStop pin
SERPR_RESPONSE("EStopPin|%hhi", static_cast<int8_t>(estopPin));
if (!OpenShock::Config::GetEStopGpioPin(estopPin)) {
SERPR_ERROR("Failed to get EStop pin from config");
return;
}

// Get EStop pin
SERPR_RESPONSE("EStopPin|%hhi", static_cast<int8_t>(estopPin));
}

void _handleEStopPinSetCommand(std::string_view arg, bool isAutomated)
{
gpio_num_t estopPin;
if (!OpenShock::Convert::ToGpioNum(arg, estopPin)) {
SERPR_ERROR("Invalid argument (number invalid or out of range)");
return;
Expand All @@ -72,12 +74,12 @@ OpenShock::Serial::CommandGroup OpenShock::Serial::CommandHandlers::EStopHandler
{
auto group = OpenShock::Serial::CommandGroup("estop"sv);

auto& getEnabledCommand = group.addCommand("enabled"sv, "Get the E-Stop enabled state."sv, _handleEStopEnabledCommand);
auto& setEnabledCommand = group.addCommand("enabled"sv, "Set the E-Stop enabled state."sv, _handleEStopEnabledCommand);
auto& getEnabledCommand = group.addCommand("enabled"sv, "Get the E-Stop enabled state."sv, _handleEStopEnabledGetCommand);
auto& setEnabledCommand = group.addCommand("enabled"sv, "Set the E-Stop enabled state."sv, _handleEStopEnabledSetCommand);
setEnabledCommand.addArgument("enabled"sv, "must be a boolean"sv, "true"sv);

auto& getPinCommand = group.addCommand("pin"sv, "Get the GPIO pin used for the E-Stop."sv, _handleEStopPinCommand);
auto& setPinCommand = group.addCommand("pin"sv, "Set the GPIO pin used for the E-Stop."sv, _handleEStopPinCommand);
auto& getPinCommand = group.addCommand("pin"sv, "Get the GPIO pin used for the E-Stop."sv, _handleEStopPinGetCommand);
auto& setPinCommand = group.addCommand("pin"sv, "Set the GPIO pin used for the E-Stop."sv, _handleEStopPinSetCommand);
setPinCommand.addArgument("pin"sv, "must be a number"sv, "4"sv);

return group;
Expand Down
144 changes: 70 additions & 74 deletions src/serial/command_handlers/lcgoverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,94 @@

const char* TAG = "Serial::CommandHandlers::LcgOverride";

void _handleLcgOverrideCommand(std::string_view arg, bool isAutomated) {
if (arg.empty()) {
std::string lcgOverride;
if (!OpenShock::Config::GetBackendLCGOverride(lcgOverride)) {
SERPR_ERROR("Failed to get LCG override from config");
return;
}

// Get LCG override
SERPR_RESPONSE("LcgOverride|%s", lcgOverride.c_str());
void _handleLcgOverrideCommand(std::string_view arg, bool isAutomated)
{
std::string lcgOverride;
if (!OpenShock::Config::GetBackendLCGOverride(lcgOverride)) {
SERPR_ERROR("Failed to get LCG override from config");
return;
}

if (OpenShock::StringStartsWith(arg, "clear"sv)) {
if (arg.size() != 5) {
SERPR_ERROR("Invalid command (clear command should not have any arguments)");
return;
}

bool result = OpenShock::Config::SetBackendLCGOverride(std::string());
if (result) {
SERPR_SUCCESS("Cleared LCG override");
} else {
SERPR_ERROR("Failed to clear LCG override");
}
// Get LCG override
SERPR_RESPONSE("LcgOverride|%s", lcgOverride.c_str());
}

void _handleLcgOverrideClearCommand(std::string_view arg, bool isAutomated)
{
if (arg.size() != 5) {
SERPR_ERROR("Invalid command (clear command should not have any arguments)");
return;
}

if (OpenShock::StringStartsWith(arg, "set "sv)) {
if (arg.size() <= 4) {
SERPR_ERROR("Invalid command (set command should have an argument)");
return;
}

std::string_view domain = arg.substr(4);

if (domain.size() + 40 >= OPENSHOCK_URI_BUFFER_SIZE) {
SERPR_ERROR("Domain name too long, please try increasing the \"OPENSHOCK_URI_BUFFER_SIZE\" constant in source code");
return;
}

char uri[OPENSHOCK_URI_BUFFER_SIZE];
sprintf(uri, "https://%.*s/1", static_cast<int>(domain.size()), domain.data());

auto resp = OpenShock::HTTP::GetJSON<OpenShock::Serialization::JsonAPI::LcgInstanceDetailsResponse>(
uri,
{
{"Accept", "application/json"}
},
OpenShock::Serialization::JsonAPI::ParseLcgInstanceDetailsJsonResponse,
{200}
);

if (resp.result != OpenShock::HTTP::RequestResult::Success) {
SERPR_ERROR("Tried to connect to \"%.*s\", but failed with status [%d], refusing to save domain to config", domain.size(), domain.data(), resp.code);
return;
}

OS_LOGI(
TAG,
"Successfully connected to \"%.*s\", name: %s, version: %s, current time: %s, country code: %s, FQDN: %s",
domain.size(),
domain.data(),
resp.data.name.c_str(),
resp.data.version.c_str(),
resp.data.currentTime.c_str(),
resp.data.countryCode.c_str(),
resp.data.fqdn.c_str()
);

bool result = OpenShock::Config::SetBackendLCGOverride(domain);

if (result) {
SERPR_SUCCESS("Saved config");
} else {
SERPR_ERROR("Failed to save config");
}
bool result = OpenShock::Config::SetBackendLCGOverride(std::string());
if (result) {
SERPR_SUCCESS("Cleared LCG override");
} else {
SERPR_ERROR("Failed to clear LCG override");
}
}

void _handleLcgOverrideSetCommand(std::string_view arg, bool isAutomated)
{
if (arg.size() <= 4) {
SERPR_ERROR("Invalid command (set command should have an argument)");
return;
}

SERPR_ERROR("Invalid subcommand");
std::string_view domain = arg.substr(4);

if (domain.size() + 40 >= OPENSHOCK_URI_BUFFER_SIZE) {
SERPR_ERROR("Domain name too long, please try increasing the \"OPENSHOCK_URI_BUFFER_SIZE\" constant in source code");
return;
}

char uri[OPENSHOCK_URI_BUFFER_SIZE];
sprintf(uri, "https://%.*s/1", static_cast<int>(domain.size()), domain.data());

auto resp = OpenShock::HTTP::GetJSON<OpenShock::Serialization::JsonAPI::LcgInstanceDetailsResponse>(
uri,
{
{"Accept", "application/json"}
},
OpenShock::Serialization::JsonAPI::ParseLcgInstanceDetailsJsonResponse,
{200}
);

if (resp.result != OpenShock::HTTP::RequestResult::Success) {
SERPR_ERROR("Tried to connect to \"%.*s\", but failed with status [%d], refusing to save domain to config", domain.size(), domain.data(), resp.code);
return;
}

OS_LOGI(
TAG,
"Successfully connected to \"%.*s\", name: %s, version: %s, current time: %s, country code: %s, FQDN: %s",
domain.size(),
domain.data(),
resp.data.name.c_str(),
resp.data.version.c_str(),
resp.data.currentTime.c_str(),
resp.data.countryCode.c_str(),
resp.data.fqdn.c_str()
);

bool result = OpenShock::Config::SetBackendLCGOverride(domain);

if (result) {
SERPR_SUCCESS("Saved config");
} else {
SERPR_ERROR("Failed to save config");
}
}

OpenShock::Serial::CommandGroup OpenShock::Serial::CommandHandlers::LcgOverrideHandler() {
auto group = OpenShock::Serial::CommandGroup("lcgoverride"sv);

auto& getCommand = group.addCommand("Get the domain overridden for LCG endpoint (if any)."sv, _handleLcgOverrideCommand);

auto& setCommand = group.addCommand("set"sv, "Set a domain to override the LCG endpoint."sv, _handleLcgOverrideCommand);
auto& setCommand = group.addCommand("set"sv, "Set a domain to override the LCG endpoint."sv, _handleLcgOverrideSetCommand);
setCommand.addArgument("domain"sv, "must be a string"sv, "eu1-gateway.shocklink.net"sv);

auto& clearCommand = group.addCommand("clear"sv, "Clear the overridden LCG endpoint."sv, _handleLcgOverrideCommand);
auto& clearCommand = group.addCommand("clear"sv, "Clear the overridden LCG endpoint."sv, _handleLcgOverrideClearCommand);

return group;
}

1 comment on commit c31212f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.8) reports: 1 file(s) not formatted
  • src/serial/command_handlers/lcgoverride.cpp

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.