Skip to content

Commit

Permalink
Add .clang-format and format code (closes #394)
Browse files Browse the repository at this point in the history
Thank you @pboettch for initial proposal.
  • Loading branch information
stephane committed Oct 18, 2022
1 parent d5512d9 commit dd45f19
Show file tree
Hide file tree
Showing 22 changed files with 1,130 additions and 919 deletions.
56 changes: 56 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
BasedOnStyle: LLVM
AlignArrayOfStructures: Left
AlignOperands: true
AlignConsecutiveAssignments: false
AlignConsecutiveMacros: true
AlignEscapedNewlines: Left
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 90
ConstructorInitializerAllOnOneLineOrOnePerLine: true
IncludeBlocks: Preserve
IndentWidth: 4
ObjCBlockIndentWidth: 4
PointerAlignment: Right
ReferenceAlignment: Right
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SeparateDefinitionBlocks: Always
UseTab: Never
PPIndentWidth: 2
53 changes: 21 additions & 32 deletions src/modbus-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdlib.h>

// clang-format off
#ifndef _MSC_VER
# include <stdint.h>
#else
Expand Down Expand Up @@ -69,21 +70,24 @@ static inline uint32_t bswap_32(uint32_t x)
return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
}
#endif
// clang-format on

/* Sets many bits from a single byte value (all 8 bits of the byte value are
set) */
void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value)
{
int i;

for (i=0; i < 8; i++) {
dest[idx+i] = (value & (1 << i)) ? 1 : 0;
for (i = 0; i < 8; i++) {
dest[idx + i] = (value & (1 << i)) ? 1 : 0;
}
}

/* Sets many bits from a table of bytes (only the bits between idx and
idx + nb_bits are set) */
void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
void modbus_set_bits_from_bytes(uint8_t *dest,
int idx,
unsigned int nb_bits,
const uint8_t *tab_byte)
{
unsigned int i;
Expand All @@ -99,8 +103,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,

/* Gets the byte value from many bits.
To obtain a full byte, set nb_bits to 8. */
uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,
unsigned int nb_bits)
uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits)
{
unsigned int i;
uint8_t value = 0;
Expand All @@ -111,8 +114,8 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,
nb_bits = 8;
}

for (i=0; i < nb_bits; i++) {
value |= (src[idx+i] << i);
for (i = 0; i < nb_bits; i++) {
value |= (src[idx + i] << i);
}

return value;
Expand All @@ -130,10 +133,7 @@ float modbus_get_float_abcd(const uint16_t *src)
c = (src[1] >> 8) & 0xFF;
d = (src[1] >> 0) & 0xFF;

i = (a << 24) |
(b << 16) |
(c << 8) |
(d << 0);
i = (a << 24) | (b << 16) | (c << 8) | (d << 0);
memcpy(&f, &i, 4);

return f;
Expand All @@ -151,10 +151,7 @@ float modbus_get_float_dcba(const uint16_t *src)
c = (src[1] >> 8) & 0xFF;
d = (src[1] >> 0) & 0xFF;

i = (d << 24) |
(c << 16) |
(b << 8) |
(a << 0);
i = (d << 24) | (c << 16) | (b << 8) | (a << 0);
memcpy(&f, &i, 4);

return f;
Expand All @@ -172,10 +169,7 @@ float modbus_get_float_badc(const uint16_t *src)
c = (src[1] >> 8) & 0xFF;
d = (src[1] >> 0) & 0xFF;

i = (b << 24) |
(a << 16) |
(d << 8) |
(c << 0);
i = (b << 24) | (a << 16) | (d << 8) | (c << 0);
memcpy(&f, &i, 4);

return f;
Expand All @@ -193,10 +187,7 @@ float modbus_get_float_cdab(const uint16_t *src)
c = (src[1] >> 8) & 0xFF;
d = (src[1] >> 0) & 0xFF;

i = (c << 24) |
(d << 16) |
(a << 8) |
(b << 0);
i = (c << 24) | (d << 16) | (a << 8) | (b << 0);
memcpy(&f, &i, 4);

return f;
Expand All @@ -208,18 +199,17 @@ float modbus_get_float(const uint16_t *src)
float f;
uint32_t i;

i = (((uint32_t)src[1]) << 16) + src[0];
i = (((uint32_t) src[1]) << 16) + src[0];
memcpy(&f, &i, sizeof(float));

return f;

}

/* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
void modbus_set_float_abcd(float f, uint16_t *dest)
{
uint32_t i;
uint8_t *out = (uint8_t*) dest;
uint8_t *out = (uint8_t *) dest;
uint8_t a, b, c, d;

memcpy(&i, &f, sizeof(uint32_t));
Expand All @@ -238,7 +228,7 @@ void modbus_set_float_abcd(float f, uint16_t *dest)
void modbus_set_float_dcba(float f, uint16_t *dest)
{
uint32_t i;
uint8_t *out = (uint8_t*) dest;
uint8_t *out = (uint8_t *) dest;
uint8_t a, b, c, d;

memcpy(&i, &f, sizeof(uint32_t));
Expand All @@ -251,14 +241,13 @@ void modbus_set_float_dcba(float f, uint16_t *dest)
out[1] = c;
out[2] = b;
out[3] = a;

}

/* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
void modbus_set_float_badc(float f, uint16_t *dest)
{
uint32_t i;
uint8_t *out = (uint8_t*) dest;
uint8_t *out = (uint8_t *) dest;
uint8_t a, b, c, d;

memcpy(&i, &f, sizeof(uint32_t));
Expand All @@ -277,7 +266,7 @@ void modbus_set_float_badc(float f, uint16_t *dest)
void modbus_set_float_cdab(float f, uint16_t *dest)
{
uint32_t i;
uint8_t *out = (uint8_t*) dest;
uint8_t *out = (uint8_t *) dest;
uint8_t a, b, c, d;

memcpy(&i, &f, sizeof(uint32_t));
Expand All @@ -298,6 +287,6 @@ void modbus_set_float(float f, uint16_t *dest)
uint32_t i;

memcpy(&i, &f, sizeof(uint32_t));
dest[0] = (uint16_t)i;
dest[1] = (uint16_t)(i >> 16);
dest[0] = (uint16_t) i;
dest[1] = (uint16_t) (i >> 16);
}
49 changes: 26 additions & 23 deletions src/modbus-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef MODBUS_PRIVATE_H
#define MODBUS_PRIVATE_H

// clang-format off
#ifndef _MSC_VER
# include <stdint.h>
# include <sys/time.h>
Expand All @@ -15,8 +16,9 @@
# include <time.h>
typedef int ssize_t;
#endif
#include <sys/types.h>
// clang-format on
#include <config.h>
#include <sys/types.h>

#include "modbus.h"

Expand All @@ -36,11 +38,11 @@ MODBUS_BEGIN_DECLS
#define _MODBUS_EXCEPTION_RSP_LENGTH 5

/* Timeouts in microsecond (0.5 s) */
#define _RESPONSE_TIMEOUT 500000
#define _BYTE_TIMEOUT 500000
#define _RESPONSE_TIMEOUT 500000
#define _BYTE_TIMEOUT 500000

typedef enum {
_MODBUS_BACKEND_TYPE_RTU=0,
_MODBUS_BACKEND_TYPE_RTU = 0,
_MODBUS_BACKEND_TYPE_TCP
} modbus_backend_type_t;

Expand Down Expand Up @@ -69,24 +71,25 @@ typedef struct _modbus_backend {
unsigned int header_length;
unsigned int checksum_length;
unsigned int max_adu_length;
int (*set_slave) (modbus_t *ctx, int slave);
int (*build_request_basis) (modbus_t *ctx, int function, int addr,
int nb, uint8_t *req);
int (*build_response_basis) (sft_t *sft, uint8_t *rsp);
int (*prepare_response_tid) (const uint8_t *req, int *req_length);
int (*send_msg_pre) (uint8_t *req, int req_length);
ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);
int (*receive) (modbus_t *ctx, uint8_t *req);
ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length);
int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
const int msg_length);
int (*pre_check_confirmation) (modbus_t *ctx, const uint8_t *req,
const uint8_t *rsp, int rsp_length);
int (*connect) (modbus_t *ctx);
void (*close) (modbus_t *ctx);
int (*flush) (modbus_t *ctx);
int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
void (*free) (modbus_t *ctx);
int (*set_slave)(modbus_t *ctx, int slave);
int (*build_request_basis)(
modbus_t *ctx, int function, int addr, int nb, uint8_t *req);
int (*build_response_basis)(sft_t *sft, uint8_t *rsp);
int (*prepare_response_tid)(const uint8_t *req, int *req_length);
int (*send_msg_pre)(uint8_t *req, int req_length);
ssize_t (*send)(modbus_t *ctx, const uint8_t *req, int req_length);
int (*receive)(modbus_t *ctx, uint8_t *req);
ssize_t (*recv)(modbus_t *ctx, uint8_t *rsp, int rsp_length);
int (*check_integrity)(modbus_t *ctx, uint8_t *msg, const int msg_length);
int (*pre_check_confirmation)(modbus_t *ctx,
const uint8_t *req,
const uint8_t *rsp,
int rsp_length);
int (*connect)(modbus_t *ctx);
void (*close)(modbus_t *ctx);
int (*flush)(modbus_t *ctx);
int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
void (*free)(modbus_t *ctx);
} modbus_backend_t;

struct _modbus {
Expand Down Expand Up @@ -114,4 +117,4 @@ size_t strlcpy(char *dest, const char *src, size_t dest_size);

MODBUS_END_DECLS

#endif /* MODBUS_PRIVATE_H */
#endif /* MODBUS_PRIVATE_H */
11 changes: 6 additions & 5 deletions src/modbus-rtu-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#include <termios.h>
#endif

#define _MODBUS_RTU_HEADER_LENGTH 1
#define _MODBUS_RTU_PRESET_REQ_LENGTH 6
#define _MODBUS_RTU_PRESET_RSP_LENGTH 2
#define _MODBUS_RTU_HEADER_LENGTH 1
#define _MODBUS_RTU_PRESET_REQ_LENGTH 6
#define _MODBUS_RTU_PRESET_RSP_LENGTH 2

#define _MODBUS_RTU_CHECKSUM_LENGTH 2
#define _MODBUS_RTU_CHECKSUM_LENGTH 2

#if defined(_WIN32)
#if !defined(ENOTSUP)
Expand All @@ -32,6 +32,7 @@

/* WIN32: struct containing serial handle and a receive buffer */
#define PY_BUF_SIZE 512

struct win32_ser {
/* File handle */
HANDLE fd;
Expand Down Expand Up @@ -67,7 +68,7 @@ typedef struct _modbus_rtu {
int rts;
int rts_delay;
int onebyte_time;
void (*set_rts) (modbus_t *ctx, int on);
void (*set_rts)(modbus_t *ctx, int on);
#endif
/* To handle many slaves on the same link */
int confirmation_to_ignore;
Expand Down
Loading

0 comments on commit dd45f19

Please sign in to comment.