Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
mm::core Namespace Reference

Classes

class  CyclicTimer
 Precision cyclic timer for fixed-period loops. More...
 
class  SingleInstanceLock
 RAII holder of the process-wide single-instance lock. More...
 
struct  SystemInfo
 

Functions

std::filesystem::path exeDir ()
 Return the directory that contains the running executable.
 
void openInBrowser (const std::string &url)
 Open the given URL in the system default browser.
 
std::expected< SingleInstanceLock, std::string > acquireSingleInstanceLock ()
 Acquire the process-wide lock that permits only one running Motion Master.
 
SystemInfo collectSystemInfo ()
 
void to_json (nlohmann::json &j, const SystemInfo &info)
 
bool isUrlSafeId (std::string_view s)
 Whether s is a valid URL-safe identifier usable as a path segment and pub/sub topic.
 
template<typename T >
std::optional< T > parseHexOrDec (std::string_view s)
 Parses an unsigned integer from a string in decimal or hexadecimal notation.
 
template<typename T >
std::array< uint8_t, sizeof(T)> toBytes (T value, std::endian order=std::endian::little)
 Encodes an integer into a fixed-width byte array in the given byte order.
 
template<typename T >
fromBytes (std::span< const uint8_t > bytes, std::endian order=std::endian::little)
 Decodes an integer from a raw byte buffer interpreted in the given byte order.
 

Function Documentation

◆ acquireSingleInstanceLock()

std::expected< SingleInstanceLock, std::string > mm::core::acquireSingleInstanceLock ( )

Acquire the process-wide lock that permits only one running Motion Master.

Non-blocking. Returns the held lock when no other instance holds it; fails when one already does. The lock guards the whole process — acquired before the EtherCAT NIC is claimed and before the servers bind — so a second master can never drive the same bus alongside the first. The exclusive port bind is only a backstop: it fires too late (the NIC is already claimed by then) and misses instances configured on different ports, so the lock, not the port, is the real guard.

  • Linux/macOS: flock(LOCK_EX|LOCK_NB) on <temp_dir>/motion-master.lock (machine-wide).
  • Windows: a named mutex in the session-local namespace (per interactive session — a standard user cannot create objects in the machine-global namespace).
    Returns
    The held lock on success; an error string describing the conflict on failure.

◆ collectSystemInfo()

SystemInfo mm::core::collectSystemInfo ( )

◆ exeDir()

std::filesystem::path mm::core::exeDir ( )

Return the directory that contains the running executable.

argv[0] is not used — it can be a relative path, a bare command name resolved via PATH, or a symlink.

Platform behaviour

  • Linux: resolves /proc/self/exe via std::filesystem::canonical.
  • macOS: queries the path via _NSGetExecutablePath (no /proc).
  • Windows: queries the path via GetModuleFileNameW.
    Returns
    Absolute path to the executable's parent directory.

◆ fromBytes()

template<typename T >
T mm::core::fromBytes ( std::span< const uint8_t >  bytes,
std::endian  order = std::endian::little 
)

Decodes an integer from a raw byte buffer interpreted in the given byte order.

Reads up to sizeof(T) bytes. A short buffer is treated as if zero-padded (the missing most-significant bytes read as zero) — defensive against a slave returning fewer bytes than the type implies; excess bytes are ignored. Accumulation is done in the unsigned representation so the shift is well-defined for signed T. Defaults to little-endian (EtherCAT/CoE wire order).

Template Parameters
TIntegral result type whose size fixes how many bytes are consumed.
Parameters
bytesSource buffer (a std::vector<uint8_t> binds implicitly).
orderByte order to interpret bytes in (default little-endian).
Returns
The decoded value.

◆ isUrlSafeId()

bool mm::core::isUrlSafeId ( std::string_view  s)
inline

Whether s is a valid URL-safe identifier usable as a path segment and pub/sub topic.

Accepts a non-empty string of at most 64 characters drawn from [A-Za-z0-9._-] — the unreserved subset that needs no percent-encoding in a URL path segment and carries no special meaning to the uWebSockets topic parser. Rejecting '/' is doubly load-bearing: the id is a single REST path segment (e.g. GET /api/monitorings/{topic}), so a '/' would spill into extra segments and miss the route; and uWebSockets treats it as a topic hierarchy delimiter. Comparison is case-sensitive, so "Motor" and "motor" are distinct identifiers.

Parameters
sCandidate identifier.
Returns
true if every character is allowed and the length is in [1, 64].

◆ openInBrowser()

void mm::core::openInBrowser ( const std::string &  url)

Open the given URL in the system default browser.

Non-blocking — returns immediately after spawning the browser process. Uses xdg-open on Linux, open on macOS, and ShellExecute on Windows.

Parameters
urlURL to open.

◆ parseHexOrDec()

template<typename T >
std::optional< T > mm::core::parseHexOrDec ( std::string_view  s)

Parses an unsigned integer from a string in decimal or hexadecimal notation.

Accepts a 0x / 0X prefix (C-style) or a #x / #X prefix (as EtherCAT ESI/XML files write hex constants, e.g. "#x00000201") to select hexadecimal; any other input is parsed as decimal. Trailing characters and out-of-range values are rejected.

Template Parameters
TUnsigned integer type to parse into (e.g. uint16_t, uint8_t).
Parameters
sInput string view.
Returns
The parsed value on success, or std::nullopt if s is not a valid decimal or hexadecimal representation of a T value.

◆ to_json()

void mm::core::to_json ( nlohmann::json &  j,
const SystemInfo info 
)

◆ toBytes()

template<typename T >
std::array< uint8_t, sizeof(T)> mm::core::toBytes ( value,
std::endian  order = std::endian::little 
)

Encodes an integer into a fixed-width byte array in the given byte order.

The width is sizeof(T), so toBytes<uint8_t> yields one byte, uint16_t two, and so on. Signed values are encoded through their unsigned representation (two's complement), so the shift is always well-defined. Defaults to little-endian (EtherCAT/CoE wire order); pass std::endian::big for the reverse. The returned array is the natural argument for a byte-span sink such as FieldbusDriver::writeSdo — a temporary result lives to the end of the enclosing call.

Template Parameters
TIntegral type whose size fixes the byte count.
Parameters
valueValue to encode.
orderByte order of the output (default little-endian).
Returns
A std::array of sizeof(T) bytes in order.