Skip to content

Commit

Permalink
- embedded: auto generate mac address, cfg cmd now requires only name…
Browse files Browse the repository at this point in the history
… and optional server port

- broker handler unnamed devices
  • Loading branch information
bcsanches committed Jul 15, 2024
1 parent 732a031 commit 1372e93
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 129 deletions.
17 changes: 17 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@
- Added support to config flip interval on quad inverter
- Lua sections now publish state to JMRI thought VirtualSensors

## Broker

- Support to unnamed devices (auto generate a name with IP)

## SharpTerminal

- Server selection screen accepts ENTER as a shortcut
- Now uses NetCore
- Added csproj file (removed from CMAKE)

## SharpEEPromViewer

- Support to legacy (deprecated) lumps
- Support to new lumps
- Now uses NetCore
- Added csproj file (removed from CMAKE)

## Scripts

- section scripts seems to be working

## Embedded

- Auto generate mac address
- Updated Session and NetUdp lumps
- cfg command now only requires name, server port is optional (use default port if not provided)
- added clr command to erase EEPROM
- arduino will blink according to state:
- slow flash: network init
- fast flag: trying to connect
Expand Down
33 changes: 17 additions & 16 deletions src/ArduinoLib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@

set(ArduinoLib_SRCS
Arduino.cpp
DynamicLibrary.cpp
EEPROM.cpp
Ethercard.cpp
Serial.cpp
Servo.cpp
DynamicLibrary.cpp
EEPROM.cpp
Ethercard.cpp
Serial.cpp
Servo.cpp
)

set(ArduinoLib_HDRS
Arduino.h
ArduinoDefs.h
ArduinoLib.h
ArduinoLibDefs.h
DynamicLibrary.h
EEPROM.h
EEPROMLib.h
Ethercard.h
Serial.h
Servo.h
avr/pgmspace.h
WString.h
ArduinoDefs.h
ArduinoLib.h
ArduinoLibDefs.h
DynamicLibrary.h
EEPROM.h
EEPROMLib.h
Ethercard.h
Serial.h
Servo.h
avr/boot.h
avr/pgmspace.h
WString.h
)

add_compile_definitions(ARDUINOLIB_EXPORTS)
Expand Down
18 changes: 18 additions & 0 deletions src/ArduinoLib/avr/boot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2019 - Bruno Sanches. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0.

#pragma once

#include <stdint.h>

inline uint8_t boot_signature_byte_get(uint16_t address)
{
return 0;
}
11 changes: 11 additions & 0 deletions src/BrokerLib/dcc/DccLiteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ namespace dcclite::broker
return;
}

if (!name[0])
{
dcclite::Log::Warn("[DccLiteService::{}] [OnNet_Hello] Hello from {} with blank name.",
this->GetName(),
senderAddress
);

auto newName = fmt::format("IP_{}_{}", senderAddress, senderAddress.GetPort());
strcpy(name, newName.c_str());
}

dcclite::Log::Info("[DccLiteService::{}] [OnNet_Hello] received hello from {}, starting handshake", this->GetName(), name);

EventHub::PostEvent<NetworkHelloEvent>(std::ref(*this), senderAddress, RName{ name }, remoteSessionToken, remoteConfigToken);
Expand Down
78 changes: 37 additions & 41 deletions src/Embedded/LiteDecoder/src/NetUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
#include "Storage.h"
#include "Strings.h"

//#include <avr/pgmspace.h>
#include <avr/boot.h>

#define ARP_PATCH 1

#define MODULE_NAME F("NetUdp")

static uint8_t g_u8Mac[] = { 0x00,0x00,0x00,0x00,0x00,0x00 };

#define BUFFER_SIZE (256+96)

#ifndef BCS_ARDUINO_EMULATOR
Expand All @@ -32,8 +30,7 @@ uint8_t Ethernet::buffer[BUFFER_SIZE];

constexpr uint16_t SRC_PORT = 7203;

#define MAX_NODE_NAME 16
static char g_szNodeName[MAX_NODE_NAME + 1] = { 0 };
static char g_szNodeName[NetUdp::MAX_NODE_NAME + 1] = { 0 };

enum States
{
Expand All @@ -55,14 +52,15 @@ void NetUdp::LoadConfig(Storage::EpromStream &stream)

g_szNodeName[MAX_NODE_NAME] = 0;

for(int i = 0;i < 6; ++i)
{
stream.Get(g_u8Mac[i]);
}

#ifdef ARDUINO_AVR_MEGA2560
if(oldConfig)
{
uint8_t mac;
for(int i = 0;i < 6; ++i)
{
stream.Get(mac);
}

uint16_t unusedSrcPort;
stream.Get(unusedSrcPort);
}
Expand All @@ -75,48 +73,38 @@ void NetUdp::SaveConfig(Storage::EpromStream &stream)
{
for(int i = 0;i < MAX_NODE_NAME; ++i)
stream.Put(g_szNodeName[i]);

for(int i = 0;i < 6; ++i)
stream.Put(g_u8Mac[i]);
}

bool NetUdp::Configure(const char *nodeName, const uint8_t *mac)
void NetUdp::Configure(const char *nodeName)
{
strncpy(g_szNodeName, nodeName, sizeof(g_szNodeName));
g_szNodeName[MAX_NODE_NAME] = 0;

memcpy(g_u8Mac, mac, sizeof(g_u8Mac));

return true;
}

bool NetUdp::Init(ReceiveCallback_t callback)
static void GenerateMac(uint8_t mac[6])
{
{
bool validMac = false;
for(int i = 0;i < 6; ++i)
{
if(g_u8Mac[i])
{
validMac = true;
break;
}
}
for(int i = 0;i < 6; ++i)
{
// take the fist 3 and last 3 of the serial.
// the first 5 of 8 are at 0x0E to 0x013
// the last 3 of 8 are at 0x15 to 0x017
mac[i] = boot_signature_byte_get(0x0E + i + (i>2? 4 : 0));
}

if(!validMac)
{
//Console::SendLogEx(MODULE_NAME, F("no"), ' ', F("mac"));
DCCLITE_LOG_MODULE_LN(F("no ") << F("mac ") << FSTR_OK);
mac[0] &= 0xFE;
mac[0] |= 0x02;
}

return false;
}
}
bool NetUdp::Init(ReceiveCallback_t callback)
{
uint8_t mac[6];

GenerateMac(mac);
{
auto stream = DCCLITE_LOG_MODULE_EX(Console::OutputStream{}) << F("mac ");

for(int i = 0;i < 6; ++i)
stream.HexNumber(g_u8Mac[i]) << ':';
stream.HexNumber(mac[i]) << ':';

stream << DCCLITE_ENDL;
}
Expand All @@ -126,9 +114,9 @@ bool NetUdp::Init(ReceiveCallback_t callback)
DCCLITE_LOG_MODULE_LN(F("ether begin try ") << i);

#ifdef ARDUINO_AVR_MEGA2560
if (ether.begin(BUFFER_SIZE, g_u8Mac, 53) == 0)
if (ether.begin(BUFFER_SIZE, mac, 53) == 0)
#else
if (ether.begin(BUFFER_SIZE, g_u8Mac, 10) == 0)
if (ether.begin(BUFFER_SIZE, mac, 10) == 0)
#endif
{
//Console::SendLogEx(MODULE_NAME, F("ether"), '.', F("begin"), ' ', FSTR_NOK);
Expand Down Expand Up @@ -242,9 +230,17 @@ void NetUdp::LogStatus()
#else
Console::OutputStream output;

output << MODULE_NAME << FSTR_NAME << ": " << g_szNodeName << ' ';
DCCLITE_LOG_MODULE_EX(output) << FSTR_NAME << ": " << g_szNodeName << ' ';

uint8_t mac[6];

GenerateMac(mac);

for(int i = 0;i < 6; ++i)
output.HexNumber(g_u8Mac[i]);
{
output.HexNumber(mac[i]);
output << ' ';
}

output << ' ' << FSTR_PORT << F(": ") << SRC_PORT << DCCLITE_ENDL;
#endif
Expand Down
5 changes: 3 additions & 2 deletions src/Embedded/LiteDecoder/src/NetUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ namespace Storage
class EpromStream;
}


namespace NetUdp
{
constexpr uint8_t MAX_NODE_NAME = 16;

typedef void(*ReceiveCallback_t)(
uint16_t dest_port, ///< Port the packet was sent to
uint8_t src_ip[4], ///< IP address of the sender
Expand All @@ -36,7 +37,7 @@ namespace NetUdp

extern void SaveConfig(Storage::EpromStream &stream);

extern bool Configure(const char *nodeName, const uint8_t *mac);
extern void Configure(const char *nodeName);

extern bool Init(ReceiveCallback_t callback);

Expand Down
8 changes: 2 additions & 6 deletions src/Embedded/LiteDecoder/src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,8 @@ void Session::SaveConfig(Storage::EpromStream &stream)
}

bool Session::Configure(uint16_t srvport)
{
DecoderManager::DestroyAll();

g_uSrvPort = srvport;

return true;
{
g_uSrvPort = srvport == 0 ? dcclite::DEFAULT_DCCLITE_SERVER_PORT : srvport;
}

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

extern void SaveConfig(Storage::EpromStream &stream);

extern bool Configure(uint16_t srvport);
//Set port to use to connect to server, pass 0 to use default port
extern bool Configure(uint16_t srvport = 0);

extern void Update(const unsigned long ticks, const bool stateChangeDetectedHint);

Expand Down
Loading

0 comments on commit 1372e93

Please sign in to comment.