Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 17 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,100 +115,108 @@
token: ${{ secrets.RELEASE_GITHUB_TOKEN }}

build_linux:
runs-on: ubuntu-latest
name: "Ubuntu Latest, Shared: ${{ matrix.shared }}"
runs-on: ${{ matrix.runner }}
name: "Ubuntu ${{ matrix.arch }}, Shared: ${{ matrix.shared }}"
strategy:
matrix:
shared: ["True", "False"]

arch: ["amd64", "arm64"]
include:
- arch: amd64
runner: ubuntu-26.04
# Native arm64 runner for Raspberry Pi (64-bit OS). 22.04 keeps glibc at 2.35
# so the package runs on Raspberry Pi OS Bookworm (2.36) and newer.
- arch: arm64
runner: ubuntu-26.04-arm

steps:
- name: Install dependencies
shell: bash
run: |
sudo apt update
sudo apt install -y build-essential cmake gcc-11 g++-11 python3 autoconf automake

- name: Checkout Agent
uses: actions/checkout@v4

- name: Restore cached conan packages
id: cache
uses: actions/cache@v4
with:
path: ~/.conan2
key: ${{ runner.os }}-build-${{ matrix.shared }}-${{ hashFiles('**/conanfile.py') }}
key: ${{ runner.os }}-${{ runner.arch }}-build-${{ matrix.shared }}-${{ hashFiles('**/conanfile.py') }}

- name: Install Conan
uses: turtlebrowser/[email protected]

- name: Setup Conan
if: steps.cache.outputs.cache-hit != 'true'
run: |
conan profile detect -f

- name: Cleanup Prior Build
if: steps.cache.outputs.cache-hit == 'true'
continue-on-error: true
run: |
conan remove mtconnect_agent -c

- name: Build and Test C++ Agent
shell: bash
run: |
export CTEST_OUTPUT_ON_FAILURE=TRUE
conan create . --build=missing -pr conan/profiles/gcc -o '&:shared=${{ matrix.shared }}' -o '&:with_docs=False' -o '&:cpack=True' -o '&:cpack_name=dist' -o '&:cpack_destination=${{ github.workspace }}'
conan create . --build=missing -pr conan/profiles/gcc -o '&:shared=${{ matrix.shared }}' -o '&:with_docs=False' -o '&:cpack=True' -o '&:cpack_name=dist' -o '&:cpack_destination=${{ github.workspace }}'

- name: Clean conan cache
continue-on-error: true
run: |
conan remove mtconnect_agent -c
conan cache clean "*" --source --build --download

- name: Derive Package Version
if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.shared == 'False' }}
run: |
PACKAGE_VERSION=${{ github.ref_name }}
if [[ $PACKAGE_VERSION == v*.*.*.* ]]; then
echo "PACKAGE_VERSION=${PACKAGE_VERSION:1}" >> $GITHUB_ENV
elif [[ $PACKAGE_VERSION == [0-9].*.*.* ]]; then
echo "PACKAGE_VERSION=${PACKAGE_VERSION:0}" >> $GITHUB_ENV
else
echo "PACKAGE_VERSION=0.0.0.0" >> $GITHUB_ENV
fi

- name: Prepare Debian Package
if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.shared == 'False' }}
shell: bash
working-directory: ${{ github.workspace }}
run: |
mkdir -p pkgroot/home/edge/
ls -lah pkgroot
tar -xzf dist.tar.gz -C pkgroot/
mv pkgroot/dist pkgroot/usr

- name: Create Debian Package
if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.shared == 'False' }}
id: create_debian_package
uses: jiro4989/build-deb-action@v3
with:
package: mtconnect-agent
package_root: ${{ github.workspace}}/pkgroot
version: ${{ env.PACKAGE_VERSION }}
arch: amd64
desc: "MTConnect Agent for Ununtu"
arch: ${{ matrix.arch }}
desc: "MTConnect Agent for Ubuntu (${{ matrix.arch }})"
maintainer: Datanomix <[email protected]>

- name: Release
if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.shared == 'False' }}
uses: softprops/action-gh-release@v2
with:
name: Version ${{ github.ref_name }}
draft: true
files: |
${{ github.workspace }}/${{ steps.create_debian_package.outputs.file_name }}
token: ${{ secrets.RELEASE_GITHUB_TOKEN }}

build_macos:

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}
runs-on: macos-latest
name: "MacOS Latest, Shared: ${{ matrix.shared }}"
strategy:
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Adapters {
Protocol = mqtt_broker
Device = Mill
Topics = Mill/#
MqttHost = 0.0.0.0 # bind address, default 0.0.0.0
MqttHost = 0.0.0.0 # bind address or host name, default 0.0.0.0
MqttPort = 8883 # default 1883, or 8883 with TLS
MqttTls = true
TlsCertificateChain = server.crt
Expand All @@ -291,6 +291,9 @@ Adapters {
}
```

* `MqttHost` may be a host name such as `localhost`; it is resolved when the broker starts, preferring an IPv4 address.
* A `#` in a value, as in `Topics = Mill/#`, is part of the value; a comment starts with a `#` at the start of a line or after a space.
* A JSON payload is ingested for the device named by a segment of its topic (name or uuid), eg. `Lathe/data`, and otherwise for the adapter's `Device`. Device keys inside the JSON still take precedence.
* Listener, transport, and credential settings (`MqttHost`, `MqttPort`, `MqttTls`, `MqttWs`, `MqttUserName`, `MqttPassword`, `MqttBrokerSecurity`) are only read from the adapter block, so they do not conflict with the MQTT sink settings. TLS certificate settings fall back to the top level values.
* `TlsVerifyClientCertificate = true` with `TlsClientCAs` rejects TLS clients without a valid client certificate. `TlsCertificatePassword` and `TlsDHKey` are optional.
* `MqttBrokerSecurity` is the path to an mqtt_cpp broker security JSON file for multiple users, salted `sha256` passwords, groups, and per-topic publish/subscribe ACLs. It replaces `MqttUserName`/`MqttPassword`. For example:
Expand Down
2 changes: 1 addition & 1 deletion agent_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.0.5
2.8.0.6
7 changes: 6 additions & 1 deletion src/mtconnect/configuration/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ namespace mtconnect {
using spirit::ascii::char_;

m_name %= lexeme[+(char_ - (space | char_("=\\{}") | eol))];
m_line %= no_skip[+(char_ - (char_("}#") | eol))];
// A # only starts a comment at the start of a value or after a blank, so a value can
// contain one, eg. the MQTT topic filter Mill/#. Blanks are kept when more of the value
// follows them.
m_word = (char_ - (blank | char_("}#") | eol)) >> *(char_ - (blank | char_('}') | eol));
m_line %= no_skip[m_word >> *hold[+blank >> m_word]];
m_string %= omit[char_("\"'")[_a = _1]] >> no_skip[*(char_ - char_(_a))] >> lit(_a);
m_value %= *blank > (m_string | m_line);
m_property = (m_name >> "=" > m_value > (eol | &char_("}#")))[property(_val, _1, _2)];
Expand Down Expand Up @@ -163,6 +167,7 @@ namespace mtconnect {

protected:
qi::rule<It, string(), Skipper> m_name;
qi::rule<It, string()> m_word;
qi::rule<It, string(), Skipper> m_line;
qi::rule<It, string(), Skipper, qi::locals<char>> m_string;
qi::rule<It, string(), Skipper> m_value;
Expand Down
34 changes: 33 additions & 1 deletion src/mtconnect/pipeline/topic_mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,51 @@ namespace mtconnect::pipeline {
return std::make_tuple(device, dataItem);
}

/// @brief the first device named, by name or uuid, by a segment of the topic
/// @param topic the topic
/// @return the device or nullptr if no segment names one
DevicePtr findTopicDevice(const std::string& topic)
{
std::vector<std::string> path;
boost::split(path, topic, boost::algorithm::is_any_of("/"));
for (const auto& tok : path)
{
if (tok.empty())
continue;
if (auto device = m_context->m_contract->findDevice(tok))
return device;
}
return nullptr;
}

EntityPtr operator()(entity::EntityPtr&& entity) override
{
PipelineMessagePtr result;
auto& body = entity->getValue<std::string>();
auto c = body[0];

// JSON may be pretty-printed, starting with whitespace, or start with a UTF-8 byte
// order mark, which the JSON parser does not accept and is removed.
static const std::string bom("\xEF\xBB\xBF");
size_t start = body.compare(0, bom.size(), bom) == 0 ? bom.size() : 0;
auto first = body.find_first_not_of(" \t\r\n", start);
auto c = first == std::string::npos ? '\0' : body[first];

DataItemPtr dataItem;
DevicePtr device;

if (c == '{' || c == '[')
{
entity::Properties props {entity->getProperties()};
if (start > 0)
props["VALUE"] = body.substr(start);

// A device named in the topic takes precedence over the default device
device = m_defaultDevice;
if (auto topic = entity->maybeGet<std::string>("topic"))
{
if (auto topicDevice = findTopicDevice(*topic))
device = topicDevice;
}
result = std::make_shared<JsonMessage>("JsonMessage", props);
}
else
Expand Down
42 changes: 35 additions & 7 deletions src/mtconnect/source/adapter/mqtt/mqtt_broker_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,38 @@ namespace mtconnect {
std::list<std::weak_ptr<mb::endpoint_t>> m_connections;
};

/// @brief The address to listen on: MqttHost as an IP address, or else the address the host
/// name resolves to. IPv4 is preferred, on Windows localhost often resolves to ::1
/// first and clients connecting to 127.0.0.1 would then be refused.
static std::optional<asio::ip::address> resolveBindAddress(const string& host)
{
boost::system::error_code ec;
auto address = asio::ip::make_address(host, ec);
if (!ec)
return address;

asio::io_context io;
asio::ip::tcp::resolver resolver(io);
auto results = resolver.resolve(host, "", ec);
if (ec || results.empty())
{
LOG(error) << "MQTT Broker: Cannot resolve bind address '" << host
<< "': " << (ec ? ec.message() : "no addresses"s);
return std::nullopt;
}

std::optional<asio::ip::address> result;
for (const auto& entry : results)
{
auto a = entry.endpoint().address();
if (a.is_v4())
return a;
if (!result)
result = a;
}
return result;
}

MqttBrokerAdapter::MqttBrokerAdapter(boost::asio::io_context& io,
pipeline::PipelineContextPtr pipelineContext,
const ConfigOptions& options,
Expand Down Expand Up @@ -479,17 +511,13 @@ namespace mtconnect {

m_pipeline.start();

boost::system::error_code ec;
auto address = asio::ip::make_address(m_host, ec);
if (ec)
{
LOG(error) << "MQTT Broker: Invalid bind address '" << m_host << "': " << ec.message();
auto address = resolveBindAddress(m_host);
if (!address)
return false;
}

try
{
m_broker->start(address, static_cast<uint16_t>(m_port));
m_broker->start(*address, static_cast<uint16_t>(m_port));
}
catch (std::exception& e)
{
Expand Down
23 changes: 23 additions & 0 deletions test_package/config_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,26 @@ TEST(ConfigParserTest, single_line_block)
auto mother = tree.get<string>("parents.mother");
ASSERT_EQ("father", mother);
}

// A # only starts a comment at the start of a value or after a blank, so MQTT
// topic filters such as Mill/# can be written without quotes.
TEST(ConfigParserTest, should_keep_hash_inside_a_value)
{
string cfg = R"DOC(
Topics = Mill/#
Wild = Mill/+/# # the comment after it is still a comment
Hashed = a#b
block {
Topics = Lathe/#
}
inline { Topics = Robot/# }
)DOC";

auto tree = Parser::parse(cfg);

ASSERT_EQ("Mill/#", tree.get<string>("Topics"));
ASSERT_EQ("Mill/+/#", tree.get<string>("Wild"));
ASSERT_EQ("a#b", tree.get<string>("Hashed"));
ASSERT_EQ("Lathe/#", tree.get<string>("block.Topics"));
ASSERT_EQ("Robot/#", tree.get<string>("inline.Topics"));
}
12 changes: 12 additions & 0 deletions test_package/mqtt_broker_adapter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ TEST_F(MqttBrokerAdapterTest, should_ingest_published_observations)
ASSERT_TRUE(waitForValue("p3", "204"));
}

// MqttHost may be a host name; localhost must accept clients connecting to 127.0.0.1.
TEST_F(MqttBrokerAdapterTest, should_bind_to_a_host_name)
{
startBroker({{MqttHost, "localhost"}});

auto client = MQTT_NS::make_async_client(ioc(), "127.0.0.1", m_adapter->getPort());
ASSERT_EQ(MQTT_NS::connect_return_code::accepted, connect(client));

publish(client, "LinuxCNC/line", "205");
ASSERT_TRUE(waitForValue("p3", "205"));
}

TEST_F(MqttBrokerAdapterTest, should_only_ingest_matching_topics)
{
startBroker({{Topics, "LinuxCNC/program"}});
Expand Down
49 changes: 48 additions & 1 deletion test_package/topic_mapping_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ TEST_F(TopicMappingTest, should_map_json_object_to_json_message)
ASSERT_TRUE(res);
ASSERT_TRUE(dynamic_pointer_cast<JsonMessage>(res));
ASSERT_FALSE(dynamic_pointer_cast<DataMessage>(res));
// The default device is attached; topic is not resolved for JSON messages
// The device is the one named in the topic, the default here; no data item is resolved
ASSERT_EQ(dev, res->m_device.lock());
ASSERT_FALSE(res->m_dataItem);
}
Expand Down Expand Up @@ -281,3 +281,50 @@ TEST_F(TopicMappingTest, should_handle_message_without_topic)
ASSERT_TRUE(dynamic_pointer_cast<DataMessage>(res));
ASSERT_FALSE(res->m_dataItem);
}

/// @test pretty-printed JSON, starting with whitespace, is still a JsonMessage
TEST_F(TopicMappingTest, should_map_json_with_leading_whitespace_to_json_message)
{
auto mapper = makeMapper();
for (const auto& body : {"\n{\n \"a\": 1\n}\n"s, " [{\"a\":1}]"s, "\r\n\t{\"a\":1}"s})
{
auto res = map(mapper, body, "topic");
ASSERT_TRUE(res);
ASSERT_TRUE(dynamic_pointer_cast<JsonMessage>(res)) << body;
}
}

/// @test a UTF-8 byte order mark is removed and the body mapped as JSON
TEST_F(TopicMappingTest, should_strip_byte_order_mark_from_json)
{
auto mapper = makeMapper();
auto res = map(mapper, "\xEF\xBB\xBF{\"a\":1}", "topic");
ASSERT_TRUE(res);
ASSERT_TRUE(dynamic_pointer_cast<JsonMessage>(res));
ASSERT_EQ("{\"a\":1}", res->getValue<string>());
}

/// @test JSON goes to the device named in the topic, not the default device
TEST_F(TopicMappingTest, should_route_json_to_the_device_in_the_topic)
{
makeDevice("Device", {{"id", "mill"s}, {"name", "mill"s}, {"uuid", "mill"s}});
auto lathe = makeDevice("Device", {{"id", "lathe"s}, {"name", "lathe"s}, {"uuid", "lathe"s}});
auto mapper = makeMapper("mill");

auto res = map(mapper, R"({"a":"ACTIVE"})", "lathe/data");
ASSERT_TRUE(res);
ASSERT_TRUE(dynamic_pointer_cast<JsonMessage>(res));
ASSERT_EQ(lathe, res->m_device.lock());
}

/// @test JSON whose topic names no device goes to the default device
TEST_F(TopicMappingTest, should_route_json_to_the_default_device_without_one_in_the_topic)
{
auto mill = makeDevice("Device", {{"id", "mill"s}, {"name", "mill"s}, {"uuid", "mill"s}});
makeDevice("Device", {{"id", "lathe"s}, {"name", "lathe"s}, {"uuid", "lathe"s}});
auto mapper = makeMapper("mill");

auto res = map(mapper, R"({"a":"ACTIVE"})", "plant/data");
ASSERT_TRUE(res);
ASSERT_EQ(mill, res->m_device.lock());
}
Loading