Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/minidexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,9 @@ void CMiniDexed::UpdateNetwork()
{
LOGNOTE ("Syslog server is not enabled in configuration");
}

m_UI.InitUDP ();

m_bNetworkReady = true;
}

Expand Down
54 changes: 53 additions & 1 deletion src/userinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <circle/startup.h>
#include <string.h>
#include <assert.h>
#include <circle/net/netsubsystem.h>
#include <circle/net/in.h>

LOGMODULE ("ui");

Expand All @@ -50,6 +52,37 @@ CUserInterface::~CUserInterface (void)
delete m_pLCD;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): The UDP socket allocated in InitUDP is not released in the destructor, leading to a leak.

Please also delete m_pUDPSendSocket in the destructor (with a null check if needed) so the UDP socket is properly released.

}

bool CUserInterface::InitUDP (void)
{
// UDP MIDI send socket setup (default: broadcast 255.255.255.255:1999)
//m_UDPDestAddress.Set(0xFFFFFFFF); // Broadcast by default
m_UDPDestAddress.Set(0x2c00000a); //
m_UDPDestPort = 1306;
#if 0
if (m_pConfig->GetUdpMidiIPAddress().IsSet())
{
m_UDPDestAddress.Set( m_pConfig->GetUdpMidiIPAddress() );
}
#endif
CString IPAddressString;
m_UDPDestAddress.Format(&IPAddressString);
// address 0.0.0.0 disables transmit
if (!m_UDPDestAddress.IsNull())
{
CNetSubSystem* pNet = CNetSubSystem::Get();
m_pUDPSendSocket = new CSocket(pNet, IPPROTO_UDP);
m_pUDPSendSocket->Connect(m_UDPDestAddress, m_UDPDestPort);
m_pUDPSendSocket->SetOptionBroadcast(TRUE);
LOGNOTE("UDP display initialized. target is %s",
(const char*)IPAddressString);
}
else
LOGNOTE("UDP display disabled. target was %s",
(const char*)IPAddressString);

return TRUE;
}

bool CUserInterface::Initialize (void)
{
assert (m_pConfig);
Expand Down Expand Up @@ -152,6 +185,7 @@ bool CUserInterface::Initialize (void)
}
assert (m_pLCD);


m_pLCDBuffered = new CWriteBufferDevice (m_pLCD);
assert (m_pLCDBuffered);
// clear sceen and go to top left corner
Expand Down Expand Up @@ -228,7 +262,8 @@ void CUserInterface::DisplayWrite (const char *pMenu, const char *pParam, const
assert (pParam);
assert (pValue);

CString Msg ("\x1B[H\E[?25l"); // cursor home and off
CString Msg ("\x1B[H\E[?25l"); // cursor home and off
CString Msg2;

// first line
Msg.Append (pParam);
Expand All @@ -243,6 +278,8 @@ void CUserInterface::DisplayWrite (const char *pMenu, const char *pParam, const
}

Msg.Append (pMenu);
Msg2.Append (Msg);
Msg2.Append ("\n");

// second line
CString Value (" ");
Expand All @@ -267,13 +304,16 @@ void CUserInterface::DisplayWrite (const char *pMenu, const char *pParam, const
}

Msg.Append (Value);
Msg2.Append (Value);

if (Value.GetLength () < m_pConfig->GetLCDColumns ())
{
Msg.Append ("\x1B[K"); // clear end of line
Msg2.Append ("\x1B[K"); // clear end of line
}

LCDWrite (Msg);
UDPWrite (Msg2);
}

void CUserInterface::LCDWrite (const char *pString)
Expand All @@ -284,6 +324,18 @@ void CUserInterface::LCDWrite (const char *pString)
}
}

void CUserInterface::UDPWrite (const char *pString)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nitpick (bug_risk): UDPWrite re-computes strlen and uses %u with a size_t value, which can be tightened up.

Two small tweaks:

  • Cache strlen(pString) in a local size_t len and reuse it for both SendTo and logging.
  • Since strlen returns size_t, use %zu in the log (or cast to unsigned) to avoid a type/format mismatch on some platforms.

{
if (m_pUDPSendSocket) {
int res = m_pUDPSendSocket->SendTo(pString, strlen(pString), 0, m_UDPDestAddress, m_UDPDestPort);
if (res < 0) {
LOGERR("Failed to send %u bytes to UDP display", strlen(pString));
} else {
// LOGDBG("Sent %u bytes to UDP display", strlen(pString));
}
}
}

void CUserInterface::EncoderEventHandler (CKY040::TEvent Event)
{
switch (Event)
Expand Down
9 changes: 9 additions & 0 deletions src/userinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <circle/writebuffer.h>
#include <circle/i2cmaster.h>
#include <circle/spimaster.h>
#include <circle/net/ipaddress.h>
#include <circle/net/socket.h>

class CMiniDexed;

Expand All @@ -42,6 +44,8 @@ class CUserInterface

bool Initialize (void);

bool InitUDP (void);

void Process (void);

void ParameterChanged (void);
Expand All @@ -60,6 +64,7 @@ class CUserInterface

private:
void LCDWrite (const char *pString); // Print to optional HD44780 display
void UDPWrite (const char *pString); // Print to optional HD44780 display

void EncoderEventHandler (CKY040::TEvent Event);
static void EncoderEventStub (CKY040::TEvent Event, void *pParam);
Expand Down Expand Up @@ -89,6 +94,10 @@ class CUserInterface
bool m_bSwitchPressed;

CUIMenu m_Menu;

CSocket* m_pUDPSendSocket = nullptr;
CIPAddress m_UDPDestAddress;
unsigned m_UDPDestPort = 1306;
};

#endif
Loading