Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create cmake-multi-platform.yml #13

Merged
merged 4 commits into from
Oct 21, 2024
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
77 changes: 77 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: CMake on multiple platforms

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations.
fail-fast: false

# Currently supported:
# - Linux + GCC
# - Linux + CLANG
# Planned:
# - Windows + CL
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang, cl]
include:
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: cl
- os: windows-latest
c_compiler: gcc
- os: windows-latest
c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"
fetch-depth: 1

# Dependecies needed only for Linux - Modbus Communication
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libnet1-dev
if: matrix.os == 'ubuntu-latest'

- name: Set reusable strings
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

- name: Configure CMake
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DMODBUS_EXAMPLE=ON
-DMODBUS_TESTS=ON
-DMODBUS_COMMUNICATION=${{ matrix.os == 'windows-latest' && 'OFF' || matrix.os == 'ubuntu-latest' && 'ON' }}
-S ${{ github.workspace }}

- name: Build
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
run: ${{ steps.strings.outputs.build-output-dir }}/tests/Google_Tests_run
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ project(protocolConverter)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -pedantic")
if (MSVC)
add_compile_options(/W3)
else()
add_compile_options(-Wall -Wextra -Werror -pedantic)
endif()

option(MODBUS_EXAMPLE "Build example program" OFF)
option(MODBUS_TESTS "Build tests" OFF)
Expand Down
12 changes: 10 additions & 2 deletions include/MB/modbusRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ModbusRequest {
}

/**
* Simple constructor, that allows to create "dummy" ModbusResponse
* Simple constructor, that allows to create "dummy" ModbusRequest
* object. May be useful in some cases.
*/
explicit ModbusRequest(
Expand All @@ -80,7 +80,15 @@ class ModbusRequest {
uint16_t address = 0, uint16_t registersNumber = 0,
std::vector<ModbusCell> values = {}) noexcept;

ModbusRequest(const ModbusRequest &) = default;
/**
* Copy constructor for the response.
*/
ModbusRequest(const ModbusRequest&);

/**
* Equal operator for the response.
*/
ModbusRequest& operator=(const ModbusRequest &);

//! Returns string representation of object
[[nodiscard]] std::string toString() const noexcept;
Expand Down
10 changes: 9 additions & 1 deletion include/MB/modbusResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ class ModbusResponse {
uint16_t address = 0, uint16_t registersNumber = 0,
std::vector<ModbusCell> values = {});

ModbusResponse(const ModbusResponse &) = default;
/**
* Copy constructor for the response.
*/
ModbusResponse(const ModbusResponse&);

/**
* Equal operator for the response.
*/
ModbusResponse& operator=(const ModbusResponse &);

//! Converts object to it's string representation
[[nodiscard]] std::string toString() const;
Expand Down
4 changes: 4 additions & 0 deletions include/MB/modbusUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,8 @@ inline void pushUint16(std::vector<uint8_t> &buffer, const uint16_t val) {
buffer.push_back(low);
}

//! Ignore some value explicitly
template<typename T>
inline void ignore_result(T&& v) { (void)v; }

} // namespace MB::utils
3 changes: 2 additions & 1 deletion src/Serial/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under: MIT License <http://opensource.org/licenses/MIT>

#include "Serial/connection.hpp"
#include "modbusUtils.hpp"
#include <sys/poll.h>

using namespace MB::Serial;
Expand Down Expand Up @@ -139,7 +140,7 @@ std::vector<uint8_t> Connection::send(std::vector<uint8_t> data) {
// most cases)
tcflush(_fd, TCOFLUSH);
// Write
write(_fd, data.begin().base(), data.size());
utils::ignore_result(write(_fd, data.begin().base(), data.size()));
// It may be a good idea to use tcdrain, although it has tendency to not
// work as expected tcdrain(_fd);

Expand Down
25 changes: 13 additions & 12 deletions src/TCP/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under: MIT License <http://opensource.org/licenses/MIT>

#include "TCP/connection.hpp"
#include <cstdint>
#include <sys/poll.h>
#include <sys/socket.h>

Expand All @@ -25,16 +26,16 @@ std::vector<uint8_t> Connection::sendRequest(const MB::ModbusRequest &req) {
std::vector<uint8_t> rawReq;
rawReq.reserve(6);

rawReq.push_back(reinterpret_cast<const uint8_t *>(&_messageID)[1]);
rawReq.push_back(reinterpret_cast<const uint8_t *>(&_messageID)[0]);
rawReq.push_back(static_cast<const uint8_t&&>(reinterpret_cast<const uint8_t *>(&_messageID)[1]));
rawReq.push_back(static_cast<uint8_t>(_messageID));
rawReq.push_back(0x00);
rawReq.push_back(0x00);

std::vector<uint8_t> dat = req.toRaw();

uint32_t size = dat.size();
rawReq.push_back(reinterpret_cast<uint16_t *>(&size)[1]);
rawReq.push_back(reinterpret_cast<uint16_t *>(&size)[0]);
rawReq.push_back(static_cast<const uint16_t&&>(reinterpret_cast<const uint16_t *>(&size)[1]));
rawReq.push_back(static_cast<uint16_t>(size));

rawReq.insert(rawReq.end(), dat.begin(), dat.end());

Expand All @@ -47,16 +48,16 @@ std::vector<uint8_t> Connection::sendResponse(const MB::ModbusResponse &res) {
std::vector<uint8_t> rawReq;
rawReq.reserve(6);

rawReq.push_back(reinterpret_cast<const uint8_t *>(&_messageID)[1]);
rawReq.push_back(reinterpret_cast<const uint8_t *>(&_messageID)[0]);
rawReq.push_back(static_cast<const uint8_t&&>(reinterpret_cast<const uint8_t *>(&_messageID)[1]));
rawReq.push_back(static_cast<uint8_t>(_messageID));
rawReq.push_back(0x00);
rawReq.push_back(0x00);

std::vector<uint8_t> dat = res.toRaw();

uint32_t size = dat.size();
rawReq.push_back(reinterpret_cast<uint16_t *>(&size)[1]);
rawReq.push_back(reinterpret_cast<uint16_t *>(&size)[0]);
rawReq.push_back(static_cast<const uint16_t&&>(reinterpret_cast<const uint16_t *>(&size)[1]));
rawReq.push_back(static_cast<uint16_t>(size));

rawReq.insert(rawReq.end(), dat.begin(), dat.end());

Expand All @@ -69,16 +70,16 @@ std::vector<uint8_t> Connection::sendException(const MB::ModbusException &ex) {
std::vector<uint8_t> rawReq;
rawReq.reserve(6);

rawReq.push_back(reinterpret_cast<const uint8_t *>(&_messageID)[1]);
rawReq.push_back(reinterpret_cast<const uint8_t *>(&_messageID)[0]);
rawReq.push_back(static_cast<const uint8_t&&>(reinterpret_cast<const uint8_t *>(&_messageID)[1]));
rawReq.push_back(static_cast<uint8_t>(_messageID));
rawReq.push_back(0x00);
rawReq.push_back(0x00);

std::vector<uint8_t> dat = ex.toRaw();

uint32_t size = dat.size();
rawReq.push_back(reinterpret_cast<uint16_t *>(&size)[1]);
rawReq.push_back(reinterpret_cast<uint16_t *>(&size)[0]);
rawReq.push_back(static_cast<const uint16_t&&>(reinterpret_cast<const uint16_t *>(&size)[1]));
rawReq.push_back(static_cast<uint16_t>(size));

rawReq.insert(rawReq.end(), dat.begin(), dat.end());

Expand Down
17 changes: 17 additions & 0 deletions src/modbusRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ ModbusRequest::ModbusRequest(uint8_t slaveId, utils::MBFunctionCode functionCode
}
}

ModbusRequest::ModbusRequest(const ModbusRequest& reference) :
_slaveID(reference.slaveID()),
_functionCode(reference.functionCode()),
_address(reference.registerAddress()),
_registersNumber(reference.numberOfRegisters()),
_values(reference.registerValues()){ }


ModbusRequest& ModbusRequest::operator=(const ModbusRequest &reference) {
this->_slaveID = reference.slaveID();
this->_functionCode = reference.functionCode();
this->_address = reference.registerAddress();
this->_registersNumber = reference.numberOfRegisters();
this->_values = reference.registerValues();
return *this;
}

ModbusRequest::ModbusRequest(const std::vector<uint8_t> &inputData, bool CRC) {
try {
if (inputData.size() < 3)
Expand Down
17 changes: 17 additions & 0 deletions src/modbusResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ ModbusResponse::ModbusResponse(uint8_t slaveId, utils::MBFunctionCode functionCo
}
}

ModbusResponse::ModbusResponse(const ModbusResponse& reference) :
_slaveID(reference.slaveID()),
_functionCode(reference.functionCode()),
_address(reference.registerAddress()),
_registersNumber(reference.numberOfRegisters()),
_values(reference.registerValues()){ }


ModbusResponse& ModbusResponse::operator=(const ModbusResponse &reference) {
this->_slaveID = reference.slaveID();
this->_functionCode = reference.functionCode();
this->_address = reference.registerAddress();
this->_registersNumber = reference.numberOfRegisters();
this->_values = reference.registerValues();
return *this;
}

ModbusResponse::ModbusResponse(std::vector<uint8_t> inputData, bool CRC) {
try {
if (inputData.size() < 3)
Expand Down