Commit 51ef1a6f8dbd8df8e0b1826e1040ecccd6aec7c6

Authored by Peter M. Groen
2 parents 641cdb64 bf715122

Merge branch 'feat/pgroen/cli_tools' into 'master'

Added CLI Tools

See merge request !25
CMakeLists.txt
@@ -9,16 +9,20 @@ project_header(osdev_mqtt) @@ -9,16 +9,20 @@ project_header(osdev_mqtt)
9 9
10 add_subdirectory(src) 10 add_subdirectory(src)
11 11
12 -if(ENABLE_EXAMPLES) 12 +if(ENABLE_EXAMPLES STREQUAL "ON")
13 add_subdirectory(examples/connect) 13 add_subdirectory(examples/connect)
14 add_subdirectory(examples/pub) 14 add_subdirectory(examples/pub)
15 add_subdirectory(examples/sub) 15 add_subdirectory(examples/sub)
16 add_subdirectory(examples/subunsub) 16 add_subdirectory(examples/subunsub)
17 endif() 17 endif()
18 18
19 -if(ENABLE_TESTS) 19 +if(ENABLE_TESTS STREQUAL "ON")
20 add_subdirectory(test) 20 add_subdirectory(test)
21 endif() 21 endif()
22 22
  23 +if(ENABLE_TOOLS STREQUAL "ON")
  24 + add_subdirectory(tools/publish_cli)
  25 +endif()
  26 +
23 include(packaging) 27 include(packaging)
24 package_component() 28 package_component()
README.md
@@ -33,10 +33,13 @@ Just check your package manager for the correct package-name. @@ -33,10 +33,13 @@ Just check your package manager for the correct package-name.
33 33
34 ## Building the examples and tests 34 ## Building the examples and tests
35 ### Tests 35 ### Tests
36 -Normally only the library is being build. By adding `-DENABLE_TESTING` to the cmake step, the unittests are being build also. Make sure a broker is running on localhost and type `test/mqtt_test` to run the tests. 36 +Normally only the library is being build. By adding `-DENABLE_TESTING=ON` to the cmake step, the unittests are being build also. Make sure a broker is running on localhost and type `test/mqtt_test` to run the tests.
37 37
38 ### Examples 38 ### Examples
39 -By adding the flag `-DENABLE_EXAMPLES` to the cmake step, the examples are being build together with the library. 39 +By adding the flag `-DENABLE_EXAMPLES=ON` to the cmake step, the examples are being build together with the library.
  40 +
  41 +### Tools
  42 +By adding the flag `-DENABLE_TOOLS=ON` to the cmake step, the tools are being build together with the library.
40 43
41 In build/bin there are two examples, test_mqtt_pu and test_mqtt_sub. Have a broker running, like mosquitto or flashmq capable of accepting anonymous connections. Start the "sub" part and couple of moments later the "pub" part. If all went well, you should see two screens in sync running. 44 In build/bin there are two examples, test_mqtt_pu and test_mqtt_sub. Have a broker running, like mosquitto or flashmq capable of accepting anonymous connections. Start the "sub" part and couple of moments later the "pub" part. If all went well, you should see two screens in sync running.
42 45
debug_log.txt deleted
1 -With broker present  
2 -  
3 -Jul 02 00:50:19 intelnuc64.osdev.nl test_connection[30797]: MQTT Client started[30797]: [MqttClient::MqttClient]  
4 -Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::eventHandler][30797]: ConnectionTest - starting event handler.  
5 -Jul 02 00:50:19 intelnuc64.osdev.nl MqttClient[30797]: ConnectionTest - Request connect  
6 -Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho][30797]: ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - Setting the extra onConnected callback.  
7 -Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][30797]: ConnectionTest - connection status of wrapped client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a changed to 2  
8 -Jul 02 00:50:19 intelnuc64.osdev.nl ClientPaho[30797]: ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - starting callback event handler  
9 -Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho::onConnectSuccess][30797]: onConnectSucces triggered..  
10 -Jul 02 00:50:19 intelnuc64.osdev.nl [ClientPaho][30797]: onConnectSuccessOnInstance ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a - connected to endpoint localhost:1883 (mqtt version 4, session present FALSE )  
11 -Jul 02 00:50:19 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][30797]: ConnectionTest - connection status of wrapped client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a changed to 4  
12 -Jul 02 00:50:30 intelnuc64.osdev.nl mosquitto[29463]: 1656715830: Client ConnectionTest_1_83746b87-431f-4ce1-82e3-2543e9b0a37a closed its connection.  
13 -  
14 -  
15 -Without a broker present  
16 -  
17 -Jul 02 00:55:33 intelnuc64.osdev.nl test_connection[31574]: MQTT Client started[31574]: [MqttClient::MqttClient]  
18 -Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::eventHandler][31574]: ConnectionTest - starting event handler.  
19 -Jul 02 00:55:33 intelnuc64.osdev.nl MqttClient[31574]: ConnectionTest - Request connect  
20 -Jul 02 00:55:33 intelnuc64.osdev.nl [ClientPaho][31574]: ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - Setting the extra onConnected callback.  
21 -Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][31574]: ConnectionTest - connection status of wrapped client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 changed to 2  
22 -Jul 02 00:55:33 intelnuc64.osdev.nl ClientPaho[31574]: ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - starting callback event handler  
23 -Jul 02 00:55:33 intelnuc64.osdev.nl ClientPaho[31574]: onConnectFailureOnInstanceConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 - connection failed with code MQTTASYNC_FAILURE (TCP/TLS connect failure)  
24 -Jul 02 00:55:33 intelnuc64.osdev.nl [MqttClient::connectionStatusChanged][31574]: ConnectionTest - connection status of wrapped client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 changed to 0  
25 -  
26 -Jul 02 00:55:42 intelnuc64.osdev.nl systemd[1]: Starting Mosquitto MQTT Broker...  
27 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: mosquitto version 2.0.14 starting  
28 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Config loaded from /etc/mosquitto/mosquitto.conf.  
29 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Starting in local only mode. Connections will only be possible from clients running on this machine.  
30 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Create a configuration file which defines a listener to allow remote access.  
31 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: For more details see https://mosquitto.org/documentation/authentication-methods/  
32 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Opening ipv4 listen socket on port 1883.  
33 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: Opening ipv6 listen socket on port 1883.  
34 -Jul 02 00:55:42 intelnuc64.osdev.nl mosquitto[31610]: 1656716142: mosquitto version 2.0.14 running  
35 -Jul 02 00:55:45 intelnuc64.osdev.nl mosquitto[31610]: 1656716145: New connection from 127.0.0.1:42196 on port 1883.  
36 -Jul 02 00:55:45 intelnuc64.osdev.nl mosquitto[31610]: 1656716145: New client connected from 127.0.0.1:42196 as ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 (p2, c1, k5).  
37 -Jul 02 00:55:53 intelnuc64.osdev.nl mosquitto[31610]: 1656716153: Client ConnectionTest_1_9eaee6bb-325b-406a-ae70-b4f3e7a41268 closed its connection.  
38 -  
39 -  
tools/publish_cli/CMakeLists.txt 0 → 100644
  1 +cmake_minimum_required(VERSION 3.12)
  2 +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/submodules/cmake)
  3 +
  4 +include(projectheader)
  5 +project_header(publish_cli)
  6 +
  7 +find_package( Boost REQUIRED COMPONENTS regex )
  8 +
  9 +include(compiler)
  10 +
  11 +include_directories(
  12 + ${CMAKE_CURRENT_SOURCE_DIR}/../../include
  13 +)
  14 +
  15 +set(SRC_LIST
  16 + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.h
  17 + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparserbase.cpp
  18 + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.h
  19 + ${CMAKE_CURRENT_SOURCE_DIR}/argumentparser.cpp
  20 + ${CMAKE_CURRENT_SOURCE_DIR}/filereader.h
  21 + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.h
  22 + ${CMAKE_CURRENT_SOURCE_DIR}/publisher.cpp
  23 + ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
  24 +)
  25 +
  26 +add_executable( ${PROJECT_NAME}
  27 + ${SRC_LIST}
  28 +)
  29 +
  30 +target_link_libraries(
  31 + ${PROJECT_NAME}
  32 + mqtt-cpp
  33 +)
  34 +
  35 +set_target_properties( ${PROJECT_NAME} PROPERTIES
  36 + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
  37 + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
  38 + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
  39 +)
  40 +
  41 +include(installation)
  42 +install_application()
  43 +
tools/publish_cli/argumentparser.cpp 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2024 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#include "argumentparser.h"
  23 +
  24 +#include <iostream>
  25 +
  26 +ArgumentParser::ArgumentParser(int num_of_args, char* argv[])
  27 + : ArgumentParserBase(num_of_args, argv)
  28 +{
  29 + if (count() == 0)
  30 + {
  31 + printUsage();
  32 + }
  33 +}
  34 +
  35 +std::string ArgumentParser::getHost()
  36 +{
  37 + // -h | --host
  38 + std::string host = getArgument("-h");
  39 + if (host.empty())
  40 + {
  41 + host = getArgument("--host");
  42 + if(host.empty())
  43 + {
  44 + host = "127.0.0.1";
  45 + }
  46 + }
  47 +
  48 + return host;
  49 +}
  50 +
  51 +std::string ArgumentParser::getPort()
  52 +{
  53 + // -p | --port
  54 + std::string port = getArgument("-p");
  55 + if (port.empty())
  56 + {
  57 + port = getArgument("--port");
  58 + if(port.empty())
  59 + {
  60 + port = "1883";
  61 + }
  62 + }
  63 +
  64 + return port;
  65 +}
  66 +
  67 +std::string ArgumentParser::getUserName()
  68 +{
  69 + // -u | --username
  70 + std::string username = getArgument("-u");
  71 + if (username.empty())
  72 + {
  73 + username = getArgument("--username");
  74 + }
  75 +
  76 + return username;
  77 +}
  78 +
  79 +std::string ArgumentParser::getPassword()
  80 +{
  81 + // -pw | --password
  82 + std::string password = getArgument("-pw");
  83 + if (password.empty())
  84 + {
  85 + password = getArgument("--password");
  86 + }
  87 +
  88 + return password;
  89 +}
  90 +
  91 +std::string ArgumentParser::getTopic()
  92 +{
  93 + // -t | --topic
  94 + std::string topic = getArgument("-t");
  95 + if (topic.empty())
  96 + {
  97 + topic = getArgument("--topic");
  98 + }
  99 +
  100 + return topic;
  101 +}
  102 +
  103 +std::string ArgumentParser::getFile()
  104 +{
  105 + // -f | --file
  106 + std::string file = getArgument("-f");
  107 + if (file.empty())
  108 + {
  109 + file = getArgument("--file");
  110 + }
  111 +
  112 + return file;
  113 +}
  114 +
  115 +std::string ArgumentParser::getMessage()
  116 +{
  117 + // -m | --message
  118 + std::string message = getArgument("-m");
  119 + if (message.empty())
  120 + {
  121 + message = getArgument("--message");
  122 + }
  123 +
  124 + return message;
  125 +}
  126 +
  127 +void ArgumentParser::printUsage()
  128 +{
  129 + std::cout << "Usage: " << getProgramName() << " [options]" << std::endl;
  130 + std::cout << "Options:" << std::endl;
  131 + std::cout << " -h | --host <host> Hostname or IP address of the MQTT broker. (Default 127.0.0.1)" << std::endl;
  132 + std::cout << " -p | --port <port> Port number of the MQTT broker. (Default 1883)" << std::endl;
  133 + std::cout << " -u | --username <user> Username for authentication" << std::endl;
  134 + std::cout << " -pw | --password <pass> Password for authentication" << std::endl;
  135 + std::cout << " -t | --topic <topic> Topic to publish the message to" << std::endl;
  136 + std::cout << " -f | --file <file> File to publish" << std::endl;
  137 + std::cout << " -m | --message <message> Message to publish" << std::endl;
  138 + std::cout << std::endl;
  139 + std::cout << "Example(s): " << getProgramName() << " -h localhost -p 1883 -u user -pw password -t topic -f file.txt" << std::endl;
  140 + std::cout << " " << getProgramName() << " -h localhost -p 1883 -t topic -f file.txt" << std::endl;
  141 + std::cout << " " << getProgramName() << " -t topic -f file.txt" << std::endl;
  142 + std::cout << std::endl;
  143 +}
0 \ No newline at end of file 144 \ No newline at end of file
tools/publish_cli/argumentparser.h 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2024 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#pragma once
  23 +
  24 +#include "argumentparserbase.h"
  25 +
  26 +class ArgumentParser : public ArgumentParserBase
  27 +{
  28 +public:
  29 + ArgumentParser(int num_of_args, char* argv[]);
  30 + virtual ~ArgumentParser() {}
  31 +
  32 + std::string getHost();
  33 + std::string getPort();
  34 + std::string getUserName();
  35 + std::string getPassword();
  36 + std::string getTopic();
  37 + std::string getFile();
  38 + std::string getMessage();
  39 +
  40 + void printUsage();
  41 +};
0 \ No newline at end of file 42 \ No newline at end of file
tools/publish_cli/argumentparserbase.cpp 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2024 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#include "argumentparserbase.h"
  24 +
  25 +ArgumentParserBase::ArgumentParserBase(int num_of_args, char* argv[])
  26 +{
  27 + // Store the program name
  28 + m_programName = argv[0];
  29 +
  30 + // Store the arguments
  31 + for (int i = 1; i < num_of_args; i++)
  32 + {
  33 + std::string arg = argv[i];
  34 + if (arg[0] == '-')
  35 + {
  36 + if (i + 1 < num_of_args)
  37 + {
  38 + m_arguments[arg] = argv[i + 1];
  39 + }
  40 + else
  41 + {
  42 + m_arguments[arg] = "";
  43 + }
  44 + }
  45 + }
  46 +}
  47 +
  48 +ArgumentParserBase::~ArgumentParserBase()
  49 +{
  50 + m_arguments.clear();
  51 + m_programName.clear();
  52 +}
  53 +
  54 +std::string ArgumentParserBase::getArgument(const std::string &option)
  55 +{
  56 + auto it = m_arguments.find(option);
  57 + if (it != m_arguments.end())
  58 + {
  59 + return it->second;
  60 + }
  61 + return "";
  62 +}
tools/publish_cli/argumentparserbase.h 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2024 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#include <iostream>
  23 +#include <string>
  24 +#include <unordered_map>
  25 +
  26 +/*!
  27 + * @brief ArgumentParserBase class
  28 + */
  29 +class ArgumentParserBase
  30 +{
  31 +public:
  32 + ArgumentParserBase(int num_of_args, char* argv[]);
  33 + virtual ~ArgumentParserBase();
  34 +
  35 + /// @brief Returns the argument for the given option
  36 + std::string getArgument(const std::string &option);
  37 +
  38 + /// @brief Returns the program name
  39 + std::string getProgramName() const { return m_programName; }
  40 +
  41 + /// @brief Returns the number of arguments
  42 + int count() const { return m_arguments.size(); }
  43 +
  44 +private: // members (Giggity)
  45 + std::string m_programName = std::string();
  46 + std::unordered_map<std::string, std::string> m_arguments = {};
  47 +};
0 \ No newline at end of file 48 \ No newline at end of file
tools/publish_cli/filereader.h 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2024 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#pragma once
  23 +
  24 +#include <fstream>
  25 +
  26 +class FileReader
  27 +{
  28 + public:
  29 + static std::string read_file(const std::string &file_name)
  30 + {
  31 + std::ifstream file(file_name);
  32 + if (!file.is_open())
  33 + {
  34 + throw std::runtime_error("Could not open file: " + file_name);
  35 + }
  36 +
  37 + std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  38 + return content;
  39 + }
  40 +};
0 \ No newline at end of file 41 \ No newline at end of file
tools/publish_cli/main.cpp 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2024 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#include <iostream>
  23 +
  24 +#include "argumentparser.h"
  25 +#include "filereader.h"
  26 +#include "publisher.h"
  27 +
  28 +enum TIME_RES
  29 +{
  30 + T_MICRO,
  31 + T_MILLI,
  32 + T_SECONDS
  33 +};
  34 +
  35 +std::uint64_t getEpochUSecs()
  36 +{
  37 + auto tsUSec =std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
  38 + return static_cast<std::uint64_t>(tsUSec.time_since_epoch().count());
  39 +}
  40 +
  41 +
  42 +void sleepcp( int number, TIME_RES resolution = T_MILLI ) // Cross-platform sleep function
  43 +{
  44 + int factor = 0; // Should not happen..
  45 +
  46 + switch( resolution )
  47 + {
  48 + case T_MICRO:
  49 + factor = 1;
  50 + break;
  51 +
  52 + case T_MILLI:
  53 + factor = 1000;
  54 + break;
  55 +
  56 + case T_SECONDS:
  57 + factor = 1000000;
  58 + break;
  59 + }
  60 +
  61 + usleep( number * factor );
  62 +}
  63 +
  64 +int main(int argc, char* argv[])
  65 +{
  66 + ArgumentParser parser(argc, argv);
  67 +
  68 + std::string host = parser.getHost();
  69 + std::string port = parser.getPort();
  70 + std::string user = parser.getUserName();
  71 + std::string pass = parser.getPassword();
  72 + std::string topic = parser.getTopic();
  73 + std::string file = parser.getFile();
  74 + std::string message = parser.getMessage();
  75 +
  76 + if(!file.empty() && !message.empty())
  77 + {
  78 + std::cerr << "Error: Cannot specify both file and message" << std::endl;
  79 + return -1;
  80 + }
  81 +
  82 + if(file.empty() && message.empty())
  83 + {
  84 + std::cerr << "Error: Must specify either file or message" << std::endl;
  85 + return -1;
  86 + }
  87 +
  88 + std::string content = std::string();
  89 + if(!file.empty())
  90 + {
  91 + content = FileReader::read_file(file);
  92 + if(content.empty())
  93 + {
  94 + std::cerr << "Error: File is empty" << std::endl;
  95 + return -1;
  96 + }
  97 + }
  98 +
  99 + if(!message.empty())
  100 + {
  101 + content = message;
  102 + }
  103 +
  104 + // Ok, we have all the options, lets check the mandatory ones
  105 + if(topic.empty())
  106 + {
  107 + std::cerr << "Error: Must specify a topic" << std::endl;
  108 + return -1;
  109 + }
  110 +
  111 + // Create the MQTT client
  112 + Publisher oPublisher;
  113 + oPublisher.connect(host, std::stoi(port), user, pass);
  114 +
  115 + // Wait a couple of seconds to make sure the connection is established
  116 + sleepcp(2, T_SECONDS);
  117 +
  118 + oPublisher.publish(topic, content);
  119 + sleepcp(2, T_SECONDS);
  120 +
  121 + return 0;
  122 +}
0 \ No newline at end of file 123 \ No newline at end of file
tools/publish_cli/publisher.cpp 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#include "publisher.h"
  24 +
  25 +Publisher::Publisher()
  26 + : m_mqtt_client( "publish_cli" )
  27 +{
  28 +
  29 +}
  30 +
  31 +void Publisher::connect(const std::string &hostname, int portnumber, const std::string &username, const std::string &password, const std::string &lwt_topic, const std::string &lwt_message)
  32 +{
  33 + m_mqtt_client.connect( hostname, portnumber,
  34 + osdev::components::mqtt::Credentials( username, password ),
  35 + osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ), true,
  36 + osdev::components::log::LogSettings{ osdev::components::log::LogLevel::Debug, osdev::components::log::LogMask::None } );
  37 +
  38 + std::cout << "Client state : " << m_mqtt_client.state() << std::endl;
  39 +}
  40 +
  41 +void Publisher::publish( const std::string &message_topic, const std::string &message_payload )
  42 +{
  43 + osdev::components::mqtt::MqttMessage message( message_topic, true, false, message_payload );
  44 + std::cout << "[Publisher::publish] - Publising message : " << message_payload << " to topic : " << message_topic << std::endl;
  45 + osdev::components::mqtt::Token t_result = m_mqtt_client.publish( message, 0 );
  46 +}
tools/publish_cli/publisher.h 0 → 100644
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +#pragma once
  23 +
  24 +// std
  25 +#include <memory>
  26 +#include <string>
  27 +
  28 +// osdev::components::mqtt
  29 +#include "mqttclient.h"
  30 +#include "compat-c++14.h"
  31 +
  32 +class Publisher
  33 +{
  34 +public:
  35 + Publisher();
  36 +
  37 + virtual ~Publisher() {}
  38 +
  39 + void connect( const std::string &hostname, int portnumber = 1883, const std::string &username = std::string(), const std::string &password = std::string()
  40 + , const std::string &lwt_topic = std::string(), const std::string &lwt_message = std::string() );
  41 +
  42 + void publish( const std::string &message_topic, const std::string &message_payload );
  43 +
  44 +private:
  45 + osdev::components::mqtt::MqttClient m_mqtt_client;
  46 +};
0 \ No newline at end of file 47 \ No newline at end of file