Skip to content

Commit

Permalink
Added code for emulator management on SharpConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
bcsanches committed Dec 22, 2024
1 parent 24e40a5 commit cbfb51a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 105 deletions.
14 changes: 7 additions & 7 deletions src/ArduinoLib/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ namespace ArduinoLib
DynamicLibrary g_ModuleLib;
std::string g_strModuleName;

void Setup(std::string moduleName, dcclite::Logger_t log, const char *projectPath)
void Setup(std::string moduleName, dcclite::Logger_t log, const char *deviceName)
{
dcclite::LogReplace(log);

#error todo

//
//Check if device name rom exists, if not, load the module, init session, save it... unload module and continue

g_ModuleLib.Load(moduleName);

g_strModuleName = std::move(moduleName);

g_pfnSetup = reinterpret_cast<ArduinoProc_t>(g_ModuleLib.GetSymbol("setup"));
g_pfnLoop = reinterpret_cast<ArduinoProc_t>(g_ModuleLib.GetSymbol("loop"));

if (projectPath)
{

}

g_Clock = dcclite::Clock();

detail::RomSetupModule(g_strModuleName);
detail::RomSetupModule(deviceName ? deviceName : g_strModuleName);

//initialize client
g_pfnSetup();
Expand Down
4 changes: 2 additions & 2 deletions src/ArduinoLib/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ namespace ArduinoLib::detail
1 - First, check if the rom state file exists, if yes load it
If no rom state, check for a backup file if yes, check for rom temporary, if exists, rename it and goevilto 1.
- that should not happen, but if a backup and no temporary rom, rename backup and goevilto 1
If no rom state, check for a backup file if yes, check for rom temporary, if exists, rename it and goto 1.
- that should not happen, but if a backup and no temporary rom, rename backup and goto 1
if no backup file, sorry, no rom state... clear it
Expand Down
22 changes: 4 additions & 18 deletions src/Broker/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
#include <LogUtils.h>
#include <PathUtils.h>


#include "sys/Broker.h"
#include "sys/EventHub.h"
#include "sys/Thinker.h"


#include <spdlog/logger.h>

constexpr auto BUILD_NUM = DCCLITE_VERSION;
Expand Down Expand Up @@ -89,28 +87,16 @@ int main(int argc, char **argv)

dcclite::broker::Broker broker{ (argc == 1) ? "MyRailroad" : argv[1] };

dcclite::Log::Info("Ready, main loop...");

unsigned frameCount = 0;
auto startTime = dcclite::Clock::DefaultClock_t::now();
dcclite::Log::Info("Ready, main loop...");

while (!g_fExitRequested.test(std::memory_order_relaxed))
{
auto now = dcclite::Clock::DefaultClock_t::now();

++frameCount;
if ((now - startTime) >= std::chrono::seconds{ 1 })
{
startTime += std::chrono::seconds{ 1 };

//dcclite::Log::Debug("[{}]", frameCount);
frameCount = 0;
}


auto timeout = dcclite::broker::Thinker::UpdateThinkers(now);

dcclite::broker::EventHub::PumpEvents(timeout);
}
dcclite::broker::EventHub::PumpEvents(timeout);
}
}
catch (std::exception &ex)
{
Expand Down
22 changes: 8 additions & 14 deletions src/BrokerLib/sys/Broker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,19 @@ namespace dcclite::broker
static RName GetServiceClassName(const rapidjson::Value &data)
{
const char *className = dcclite::json::GetString(data, "class", "service block");
auto rclassName = RName::TryGetName(className);
if (!rclassName)
{
throw std::runtime_error(fmt::format("[Broker] [CreateBrokerService] error: service type {} not registered", className));
}

return rclassName;
if (auto rclassName = RName::TryGetName(className))
return rclassName;

throw std::runtime_error(fmt::format("[Broker] [CreateBrokerService] error: service type {} not registered", className));
}

static ServiceFactory &FindServiceFactory(const rapidjson::Value &data)
{
auto className = GetServiceClassName(data);
auto factory = ServiceFactory::TryFindFactory(className);
if (!factory)
{
throw std::runtime_error(fmt::format("[Broker] [CreateBrokerService] error: unknown service type {}", className));
}
auto className = GetServiceClassName(data);
if (auto factory = ServiceFactory::TryFindFactory(className))
return *factory;

return *factory;
throw std::runtime_error(fmt::format("[Broker] [CreateBrokerService] error: unknown service type {}", className));
}

static std::unique_ptr<Service> CreateBrokerService(const ServiceFactory &factory, Broker &broker, const rapidjson::Value &data, const Project &project)
Expand Down
14 changes: 1 addition & 13 deletions src/Embedded/LiteDecoder/src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,8 @@ static uint16_t g_uFreeRam = UINT16_MAX;
//
//

void Session::LoadConfig(Storage::EpromStream &stream, bool oldConfig)
void Session::LoadConfig(Storage::EpromStream &stream)
{
if(oldConfig)
{
uint8_t oldSrvIp;
for (int i = 0; i < 4; ++i)
stream.Get(oldSrvIp);
}

stream.Get(g_uSrvPort);

Session::LogStatus();
Expand Down Expand Up @@ -181,15 +174,13 @@ namespace ConnectionStateManager

static void LogInvalidPacket(const FlashStringHelper_t *fstr, dcclite::MsgTypes type)
{
//Console::SendLogEx(MODULE_NAME, FSTR_INVALID, ' ', F("pkt"), ' ', fstr, ' ', static_cast<int>(type));
DCCLITE_LOG_MODULE_LN(FSTR_INVALID << F("pkt ") << fstr << ' ' << static_cast<int>(type));
}

static bool IsValidServer(uint8_t src_ip[4], uint16_t src_port)
{
if (memcmp(src_ip, g_u8ServerIp, sizeof(g_u8ServerIp)) || (g_uSrvPort != src_port))
{
//Console::SendLogEx(MODULE_NAME, FSTR_UNKNOWN, ' ', F("ip"));
DCCLITE_LOG_MODULE_LN(FSTR_UNKNOWN << F("ip "));
return false;
}
Expand All @@ -199,8 +190,6 @@ static bool IsValidServer(uint8_t src_ip[4], uint16_t src_port)

void Session::LogStatus()
{
//Console::SendLogEx(MODULE_NAME, "srv", ':', ' ', Console::IpPrinter(g_u8ServerIp), ':', g_uSrvPort);

Console::OutputStream stream;

stream << '[' << MODULE_NAME << ']' << F(" srv: ");
Expand Down Expand Up @@ -234,7 +223,6 @@ static void GotoOfflineState()

static void OfflineTick(const unsigned long ticks)
{
//Console::SendLogEx(MODULE_NAME, FSTR_BROADCAST, ':', g_uSrvPort);
DCCLITE_LOG_MODULE_LN(FSTR_BROADCAST << ':' << g_uSrvPort);

//
Expand Down
2 changes: 1 addition & 1 deletion src/Embedded/LiteDecoder/src/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace dcclite

namespace Session
{
extern void LoadConfig(Storage::EpromStream &stream, bool oldConfig = false);
extern void LoadConfig(Storage::EpromStream &stream);
#
extern void SaveConfig(Storage::EpromStream &stream);

Expand Down
41 changes: 13 additions & 28 deletions src/Embedded/LiteDecoder/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,22 @@ bool Console::Custom_ParseCommand(const char *command)

return true;
}

NetUdp::Configure(nodeName);

int srvport = 0;
auto token = parser.GetNumber(srvport);
if((token != dcclite::Tokens::END_OF_BUFFER) && (token != dcclite::Tokens::NUMBER))
if (token == dcclite::Tokens::NUMBER)
{
Session::Configure(srvport);
}
else if(token != dcclite::Tokens::END_OF_BUFFER)
{
//Console::SendLogEx(MODULE_NAME, FSTR_NOK, " ", FSTR_SRVPORT);
DCCLITE_LOG << MODULE_NAME << FSTR_NOK << ' ' << FSTR_SRVPORT << DCCLITE_ENDL;

return true;
}

NetUdp::Configure(nodeName);
Session::Configure(srvport);
}

DCCLITE_LOG_MODULE_LN(FSTR_OK);

Expand Down Expand Up @@ -114,7 +117,6 @@ bool Console::Custom_ParseCommand(const char *command)
#define SESSION_STORAGE_ID F("Sson002")

#define NET_UDP_OLD_STORAGE_ID F("NetU002")
#define SESSION_OLD_STORAGE_ID F("Sson001")

bool Storage::Custom_LoadModules(const Storage::Lump &lump, Storage::EpromStream &stream)
{
Expand Down Expand Up @@ -142,28 +144,16 @@ bool Storage::Custom_LoadModules(const Storage::Lump &lump, Storage::EpromStream
}

if (FStrNCmp(lump.m_archName, NET_UDP_STORAGE_ID, FStrLen(NET_UDP_STORAGE_ID)) == 0)
{
//Console::SendLogEx(MODULE_NAME, "net", "udp", ' ', "cfg");
{
DCCLITE_LOG_MODULE_LN(MODULE_NAME << F(" net udp cfg"));

NetUdp::LoadConfig(stream);

return true;
}

if (FStrNCmp(lump.m_archName, SESSION_OLD_STORAGE_ID, FStrLen(SESSION_STORAGE_ID)) == 0)
{
//Console::SendLogEx(MODULE_NAME, FSTR_SESSION, ' ', "cfg");
DCCLITE_LOG_MODULE_LN(FSTR_SESSION << F(" OLD OLD OLD cfg"));

Session::LoadConfig(stream, true);

return true;
}

if (FStrNCmp(lump.m_archName, SESSION_STORAGE_ID, FStrLen(SESSION_STORAGE_ID)) == 0)
{
//Console::SendLogEx(MODULE_NAME, FSTR_SESSION, ' ', "cfg");
{
DCCLITE_LOG_MODULE_LN(FSTR_SESSION << F(" cfg"));

Session::LoadConfig(stream);
Expand Down Expand Up @@ -205,14 +195,12 @@ void Storage_LoadDecoders(uint32_t position)
stream.Get(lump.m_uLength);

if (FStrNCmp(lump.m_archName, DECODERS_STORAGE_ID, FStrLen(DECODERS_STORAGE_ID)) != 0)
{
//Console::SendLogEx(MODULE_NAME, FSTR_UNKNOWN, ' ', FSTR_LUMP, ' ', lump.m_archName);
{
DCCLITE_LOG << MODULE_NAME << FSTR_UNKNOWN << ' ' << FSTR_LUMP << ' ' << lump.m_archName << DCCLITE_ENDL;

return;
}

//Console::SendLogEx(MODULE_NAME, F("Loading config"));

DCCLITE_LOG_MODULE_LN(F("Loading config"));

DecoderManager::LoadConfig(stream);
Expand All @@ -237,10 +225,7 @@ void setup()
{
Storage_LoadDecoders(g_uDecodersPosition);
}



//Console::SendLogEx(FSTR_SETUP, " ", FSTR_OK);

DCCLITE_LOG_MODULE_LN(FSTR_SETUP << ' ' << FSTR_OK);
}

Expand Down
10 changes: 3 additions & 7 deletions src/Embedded/lib/DecodersCommon/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ extern size_t Storage::Length() noexcept
}

bool Storage::LoadConfig()
{
//Console::SendLog(MODULE_NAME, "init %d", sizeof(STORAGE_MAGIC));
//Console::SendLogEx(MODULE_NAME, FSTR_INIT, ' ', static_cast<unsigned>(FStrLen(STORAGE_MAGIC)));
{
DCCLITE_LOG_MODULE_LN(FSTR_INIT << ' ' << static_cast<unsigned>(FStrLen(STORAGE_MAGIC)));

Lump header;
Expand Down Expand Up @@ -112,8 +110,7 @@ bool Storage::LoadConfig()

break;
}

//Console::SendLogEx(MODULE_NAME, FSTR_UNKNOWN, ' ', FSTR_LUMP, ' ', lump.m_archName);

DCCLITE_LOG << MODULE_NAME << ' ' << FSTR_UNKNOWN << ' ' << FSTR_LUMP << ' ' << lump.m_archName << DCCLITE_ENDL;

stream.Skip(lump.m_uLength);
Expand All @@ -122,8 +119,7 @@ bool Storage::LoadConfig()
break;
}
}

//Console::SendLogEx(MODULE_NAME, FSTR_OK);

DCCLITE_LOG_MODULE_LN(FSTR_OK);

return true;
Expand Down
13 changes: 11 additions & 2 deletions src/SharpTools/SharpTerminal/EmulatorManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace SharpTerminal
{
class Emulator
{
string m_strDeviceName;

public Emulator(string deviceName)
{
m_strDeviceName = deviceName;

if (string.IsNullOrWhiteSpace(m_strDeviceName))
{
throw new ArgumentNullException(nameof(deviceName));
}
}
}

internal static class EmulatorManager
Expand All @@ -27,7 +36,7 @@ public static void StartEmulator(string deviceName)
return;
}

var emulator = new Emulator();
var emulator = new Emulator(deviceName);
g_mapEmulators[deviceName] = emulator;
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cbfb51a

Please sign in to comment.