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

CVS-154698, Implement windows specific status codes. #2846

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
68 changes: 42 additions & 26 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
#include <stdlib.h>

// TODO: Write windows/linux specific status codes.
#include "ovms_exit_codes.hpp"
#ifdef __linux__
#include <netinet/in.h>
#include <sys/socket.h>
#include <sysexits.h>
#elif _WIN32
#include <ntstatus.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#endif
#include <unistd.h>

Expand Down Expand Up @@ -341,19 +344,11 @@ void Server::shutdownModules() {

static int statusToExitCode(const Status& status) {
if (status.ok()) {
#ifdef __linux__
return EX_OK;
#elif _WIN32
return 0;
#endif
return OVMS_EX_OK;
} else if (status == StatusCode::OPTIONS_USAGE_ERROR) {
#ifdef __linux__
return EX_USAGE;
#elif _WIN32
return 3;
#endif
return OVMS_EX_USAGE;
}
return EXIT_FAILURE;
return OVMS_EX_FAILURE;
}

// OVMS Start
Expand All @@ -362,23 +357,44 @@ int Server::start(int argc, char** argv) {
#ifdef __linux__
installSignalHandlers();
#endif
CLIParser parser;
ServerSettingsImpl serverSettings;
ModelsSettingsImpl modelsSettings;
parser.parse(argc, argv);
parser.prepare(&serverSettings, &modelsSettings);
Status ret = start(&serverSettings, &modelsSettings);
ModulesShutdownGuard shutdownGuard(*this);
if (!ret.ok()) {
return statusToExitCode(ret);
}
while (!shutdown_request) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));

#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
SPDLOG_ERROR("Failed to initialize Winsock");
return OVMS_EX_FAILURE;
}
if (shutdown_request == 2) {
SPDLOG_ERROR("Illegal operation. OVMS started on unsupported device");
#endif
int result = OVMS_EX_OK;

try {
CLIParser parser;
ServerSettingsImpl serverSettings;
ModelsSettingsImpl modelsSettings;
parser.parse(argc, argv);
parser.prepare(&serverSettings, &modelsSettings);
Status ret = start(&serverSettings, &modelsSettings);
ModulesShutdownGuard shutdownGuard(*this);
if (!ret.ok()) {
return statusToExitCode(ret);
}
while (!shutdown_request) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
if (shutdown_request == 2) {
SPDLOG_ERROR("Illegal operation. OVMS started on unsupported device");
}
SPDLOG_INFO("Shutting down");
} catch (const std::exception& e) {
SPDLOG_ERROR("Exception; {}", e.what());
result = OVMS_EX_FAILURE;
return result;
}
SPDLOG_INFO("Shutting down");

#ifdef _WIN32
WSACleanup();
#endif

return EXIT_SUCCESS;
}

Expand Down