Skip to content

Commit

Permalink
Clang format in the CI
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Mazur <mateusz.mazur@e.email>
  • Loading branch information
Mazurel committed Nov 11, 2024
1 parent 96661a0 commit 96f9d71
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 125 deletions.
2 changes: 0 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
Expand All @@ -26,7 +25,6 @@ AlignConsecutiveDeclarations:
AlignFunctionPointers: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Clang formatting checker

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

jobs:
build:
runs-on: ubuntu-latest
container:
image: silkeh/clang # https://hub.docker.com/r/silkeh/clang
env:
DRY: "TRUE"

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

- name: Build
run: DRY=TRUE bash reformat-all.sh
2 changes: 1 addition & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void createRequest() {

// Create CRC and pointer to its bytes
uint16_t CRC = MB::utils::calculateCRC(rawed);
auto CRCptr = reinterpret_cast<uint8_t *>(&CRC);
auto CRCptr = reinterpret_cast<uint8_t *>(&CRC);

// Show byted CRC for request
std::cout << "CRC for the above code: ";
Expand Down
8 changes: 4 additions & 4 deletions include/MB/TCP/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Connection {
static const unsigned int DefaultTCPTimeout = 500;

private:
int _sockfd = -1;
int _sockfd = -1;
uint16_t _messageID = 0;
int _timeout = Connection::DefaultTCPTimeout;
int _timeout = Connection::DefaultTCPTimeout;

public:
explicit Connection() noexcept : _sockfd(-1), _messageID(0) {};
Expand All @@ -39,8 +39,8 @@ class Connection {
if (_sockfd != -1 && _sockfd != other._sockfd)
::close(_sockfd);

_sockfd = other._sockfd;
_messageID = other._messageID;
_sockfd = other._sockfd;
_messageID = other._messageID;
other._sockfd = -1;

return *this;
Expand Down
8 changes: 4 additions & 4 deletions include/MB/TCP/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class Server {

Server(const Server &) = delete;
Server(Server &&moved) {
_serverfd = moved._serverfd;
_port = moved._port;
_serverfd = moved._serverfd;
_port = moved._port;
moved._serverfd = -1;
}
Server &operator=(Server &&moved) {
if (this == &moved)
return *this;

_serverfd = moved._serverfd;
_port = moved._port;
_serverfd = moved._serverfd;
_port = moved._port;
moved._serverfd = -1;
return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions include/MB/modbusCell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class ModbusCell {
*/
constexpr ModbusCell(bool coil) : _value(coil) {};

ModbusCell(const ModbusCell &other) noexcept = default;
ModbusCell(const ModbusCell &other) noexcept = default;
ModbusCell &operator=(const ModbusCell &other) noexcept = default;
ModbusCell &operator=(ModbusCell &&other) noexcept = default;
ModbusCell &operator=(ModbusCell &&other) noexcept = default;

/**
* @brief Static wrapper for cell construction.
Expand Down
2 changes: 1 addition & 1 deletion include/MB/modbusException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ModbusException : public std::exception {
//! Sets SlaveID
void setSlaveID(uint8_t slaveId) noexcept {
_validSlave = true;
_slaveId = slaveId;
_slaveId = slaveId;
}

//! Returns detected error code
Expand Down
2 changes: 1 addition & 1 deletion include/MB/modbusRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ModbusRequest {
* object. May be useful in some cases.
*/
explicit ModbusRequest(
uint8_t slaveId = 0,
uint8_t slaveId = 0,
utils::MBFunctionCode functionCode = static_cast<utils::MBFunctionCode>(0),
uint16_t address = 0, uint16_t registersNumber = 0,
std::vector<ModbusCell> values = {}) noexcept;
Expand Down
2 changes: 1 addition & 1 deletion include/MB/modbusResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ModbusResponse {
* object. May be useful in some cases.
*/
ModbusResponse(
uint8_t slaveId = 0,
uint8_t slaveId = 0,
utils::MBFunctionCode functionCode = static_cast<utils::MBFunctionCode>(0),
uint16_t address = 0, uint16_t registersNumber = 0,
std::vector<ModbusCell> values = {});
Expand Down
42 changes: 21 additions & 21 deletions include/MB/modbusUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ namespace MB::utils {
*/
enum MBErrorCode : uint8_t {
// Documentation modbus errors
IllegalFunction = 0x01,
IllegalDataAddress = 0x02,
IllegalDataValue = 0x03,
SlaveDeviceFailure = 0x04,
Acknowledge = 0x05,
SlaveDeviceBusy = 0x06,
NegativeAcknowledge = 0x07,
MemoryParityError = 0x08,
GatewayPathUnavailable = 0x10,
IllegalFunction = 0x01,
IllegalDataAddress = 0x02,
IllegalDataValue = 0x03,
SlaveDeviceFailure = 0x04,
Acknowledge = 0x05,
SlaveDeviceBusy = 0x06,
NegativeAcknowledge = 0x07,
MemoryParityError = 0x08,
GatewayPathUnavailable = 0x10,
GatewayTargetDeviceFailedToRespond = 0x11,

// Custom modbus errors for Modbus for C++ library
ErrorCodeCRCError = 0b0111111,
InvalidCRC = 0b01111110,
InvalidByteOrder = 0b01111101,
InvalidMessageID = 0b01111100,
ProtocolError = 0b01111011,
ConnectionClosed = 0b01111010,
Timeout = 0b01111001,
InvalidCRC = 0b01111110,
InvalidByteOrder = 0b01111101,
InvalidMessageID = 0b01111100,
ProtocolError = 0b01111011,
ConnectionClosed = 0b01111010,
Timeout = 0b01111001,
// Specific for modbus response, when number of registers is too big
// See issue: https://github.com/Mazurel/Modbus/issues/3
NumberOfRegistersInvalid = 0b01111000,
NumberOfValuesInvalid = 0b01110111,
NumberOfValuesInvalid = 0b01110111,
};

//! Checks if error code is modbus standard error code
Expand Down Expand Up @@ -125,17 +125,17 @@ inline std::string mbErrorCodeToStr(MBErrorCode code) noexcept {
//! All modbus standard function codes + Undefined one
enum MBFunctionCode : uint8_t {
// Reading functions
ReadDiscreteOutputCoils = 0x01,
ReadDiscreteInputContacts = 0x02,
ReadDiscreteOutputCoils = 0x01,
ReadDiscreteInputContacts = 0x02,
ReadAnalogOutputHoldingRegisters = 0x03,
ReadAnalogInputRegisters = 0x04,
ReadAnalogInputRegisters = 0x04,

// Single write functions
WriteSingleDiscreteOutputCoil = 0x05,
WriteSingleDiscreteOutputCoil = 0x05,
WriteSingleAnalogOutputRegister = 0x06,

// Multiple write functions
WriteMultipleDiscreteOutputCoils = 0x0F,
WriteMultipleDiscreteOutputCoils = 0x0F,
WriteMultipleAnalogOutputHoldingRegisters = 0x10,

// User defined
Expand Down
12 changes: 11 additions & 1 deletion reformat-all.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/bin/sh

find . -name '*.cpp' -o -name '*.hpp' | xargs clang-format -i -style=file
ALL_FORMATABLE_FILES=$(find src include tests example -name '*.cpp' -o -name '*.hpp')
CLANG_FORMAT_ARGS="-i -style=file --verbose"

echo "*** Modbus Clang Format Wrapper **"
clang-format --version

if [[ "$DRY" = "TRUE" ]]; then
exec clang-format --dry-run $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES
else
exec clang-format $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES
fi
8 changes: 4 additions & 4 deletions src/Serial/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ std::vector<uint8_t> Connection::awaitRawMessage() {
std::vector<uint8_t> data(1024);

pollfd waitingFD;
waitingFD.fd = this->_fd;
waitingFD.events = POLLIN;
waitingFD.fd = this->_fd;
waitingFD.events = POLLIN;
waitingFD.revents = POLLIN;

if (::poll(&waitingFD, 1, _timeout) <= 0) {
Expand Down Expand Up @@ -148,8 +148,8 @@ std::vector<uint8_t> Connection::send(std::vector<uint8_t> data) {
}

Connection::Connection(Connection &&moved) noexcept {
_fd = moved._fd;
_termios = moved._termios;
_fd = moved._fd;
_termios = moved._termios;
moved._fd = -1;
}

Expand Down
22 changes: 11 additions & 11 deletions src/TCP/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using namespace MB::TCP;

Connection::Connection(const int sockfd) noexcept {
_sockfd = sockfd;
_sockfd = sockfd;
_messageID = 0;
}

Expand Down Expand Up @@ -96,8 +96,8 @@ std::vector<uint8_t> Connection::sendException(const MB::ModbusException &ex) {

std::vector<uint8_t> Connection::awaitRawMessage() {
pollfd pfd;
pfd.fd = this->_sockfd;
pfd.events = POLLIN;
pfd.fd = this->_sockfd;
pfd.events = POLLIN;
pfd.revents = POLLIN;
if (::poll(&pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) {
throw MB::ModbusException(MB::utils::ConnectionClosed);
Expand All @@ -121,8 +121,8 @@ std::vector<uint8_t> Connection::awaitRawMessage() {

MB::ModbusRequest Connection::awaitRequest() {
pollfd pfd;
pfd.fd = this->_sockfd;
pfd.events = POLLIN;
pfd.fd = this->_sockfd;
pfd.events = POLLIN;
pfd.revents = POLLIN;
if (::poll(&pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) {
throw MB::ModbusException(MB::utils::Timeout);
Expand Down Expand Up @@ -152,8 +152,8 @@ MB::ModbusRequest Connection::awaitRequest() {

MB::ModbusResponse Connection::awaitResponse() {
pollfd pfd;
pfd.fd = this->_sockfd;
pfd.events = POLLIN;
pfd.fd = this->_sockfd;
pfd.events = POLLIN;
pfd.revents = POLLIN;

if (::poll(&pfd, 1, this->_timeout) <= 0) {
Expand Down Expand Up @@ -189,8 +189,8 @@ Connection::Connection(Connection &&moved) noexcept {
if (_sockfd != -1 && moved._sockfd != _sockfd)
::close(_sockfd);

_sockfd = moved._sockfd;
_messageID = moved._messageID;
_sockfd = moved._sockfd;
_messageID = moved._messageID;
moved._sockfd = -1;
}

Expand All @@ -201,8 +201,8 @@ Connection Connection::with(std::string addr, int port) {

sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = ::htons(port);
server.sin_addr = {inet_addr(addr.c_str())};
server.sin_port = ::htons(port);
server.sin_addr = {inet_addr(addr.c_str())};

if (::connect(sock, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)) < 0)
throw std::runtime_error("Cannot connect, errno = " + std::to_string(errno));
Expand Down
6 changes: 3 additions & 3 deletions src/TCP/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using namespace MB::TCP;

Server::Server(int port) {
_port = port;
_port = port;
_serverfd = socket(AF_INET, SOCK_STREAM, 0);

if (_serverfd == -1)
Expand All @@ -18,9 +18,9 @@ Server::Server(int port) {

_server = {};

_server.sin_family = AF_INET;
_server.sin_family = AF_INET;
_server.sin_addr.s_addr = INADDR_ANY;
_server.sin_port = ::htons(_port);
_server.sin_port = ::htons(_port);

if (::bind(_serverfd, reinterpret_cast<struct sockaddr *>(&_server),
sizeof(_server)) < 0)
Expand Down
14 changes: 7 additions & 7 deletions src/modbusException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ using namespace MB;
ModbusException::ModbusException(const std::vector<uint8_t> &inputData,
bool checkCRC) noexcept {
const std::size_t PACKET_SIZE_WITHOUT_CRC = 3;
const std::size_t PACKET_SIZE_WITH_CRC = 5;
const std::size_t PACKET_SIZE_WITH_CRC = 5;

if (inputData.size() !=
((checkCRC) ? PACKET_SIZE_WITH_CRC : PACKET_SIZE_WITHOUT_CRC)) {
_slaveId = 0xFF;
_slaveId = 0xFF;
_functionCode = utils::Undefined;
_validSlave = false;
_errorCode = utils::InvalidByteOrder;
_validSlave = false;
_errorCode = utils::InvalidByteOrder;
return;
}

_slaveId = inputData[0];
_slaveId = inputData[0];
_functionCode = static_cast<utils::MBFunctionCode>(0b01111111 & inputData[1]);
_validSlave = true;
_errorCode = static_cast<utils::MBErrorCode>(inputData[2]);
_validSlave = true;
_errorCode = static_cast<utils::MBErrorCode>(inputData[2]);

if (checkCRC) {
const auto actualCrc = *reinterpret_cast<const uint16_t *>(&inputData[3]);
Expand Down
Loading

0 comments on commit 96f9d71

Please sign in to comment.