Skip to content

Commit

Permalink
Minor fixes for clangd warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
debevv committed Apr 11, 2024
1 parent a25ee57 commit 363e7f7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
16 changes: 9 additions & 7 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
Checks: >
modernize-*,
performance-*,
readability-*,
bugprone-*,
*,
-altera-*,
-bugprone-easily-swappable-parameters,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,
-google-readability-braces-around-statements,
-hicpp-braces-around-statements,
-hicpp-signed-bitwise,
-llvmlibc-restrict-system-libc-headers,
-readability-braces-around-statements,
-readability-magic-numbers,
-readability-function-cognitive-complexity,
-readability-identifier-length
-readability-identifier-length,
...

WarningsAsErrors: '*'
HeaderFilterRegex: ''
FormatStyle: none
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(nanomodbus C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wswitch-enum -Wcast-qual -Woverflow")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -g0")

include_directories(tests examples/linux .)

Expand Down
9 changes: 5 additions & 4 deletions nanomodbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
MIT License
Copyright (c) 2022 Valerio De Benedetto (@debevv)
Copyright (c) 2024 Valerio De Benedetto (@debevv)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,6 +27,7 @@
#include "nanomodbus.h"

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#ifdef NMBS_DEBUG
Expand Down Expand Up @@ -423,7 +424,7 @@ static nmbs_error recv_res_header(nmbs_t* nmbs) {
uint8_t req_unit_id = nmbs->msg.unit_id;
uint8_t req_fc = nmbs->msg.fc;

bool first_byte_received;
bool first_byte_received = false;
nmbs_error err = recv_msg_header(nmbs, &first_byte_received);
if (err != NMBS_ERROR_NONE)
return err;
Expand Down Expand Up @@ -1710,7 +1711,7 @@ static nmbs_error handle_read_device_identification(nmbs_t* nmbs) {

int16_t str_len = (int16_t) strlen(str);

res_size_left = res_size_left - 2 - str_len;
res_size_left = (int16_t) (res_size_left - 2 - str_len);
if (res_size_left < 0) {
res_more_follows = 0xFF;
res_next_object_id = id;
Expand Down Expand Up @@ -1747,7 +1748,7 @@ static nmbs_error handle_read_device_identification(nmbs_t* nmbs) {
static nmbs_error handle_req_fc(nmbs_t* nmbs) {
NMBS_DEBUG_PRINT("fc %d\t", nmbs->msg.fc);

nmbs_error err;
nmbs_error err = NMBS_ERROR_NONE;
switch (nmbs->msg.fc) {
#ifndef NMBS_SERVER_WRITE_MULTIPLE_COILS_DISABLED
case 1:
Expand Down
11 changes: 5 additions & 6 deletions nanomodbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
MIT License
Copyright (c) 2022 Valerio De Benedetto (@debevv)
Copyright (c) 2024 Valerio De Benedetto (@debevv)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -42,7 +42,6 @@

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -440,25 +439,25 @@ nmbs_error nmbs_read_write_registers(nmbs_t* nmbs, uint16_t read_address, uint16
* @param vendor_name char array where the read VendorName value will be stored
* @param product_code char array where the read ProductCode value will be stored
* @param major_minor_revision char array where the read MajorMinorRevision value will be stored
* @param buffer_length length of every char array
* @param buffers_length length of every char array
*
* @return NMBS_ERROR_NONE if successful, other errors otherwise.
*/
nmbs_error nmbs_read_device_identification_basic(nmbs_t* nmbs, char* vendor_name, char* product_code,
char* major_minor_revision, uint8_t buffer_length);
char* major_minor_revision, uint8_t buffers_length);

/** Send a FC 43 / 14 (0x2B / 0x0E) Read Device Identification to read all Regular Object Id values (Read Device ID code 2)
* @param nmbs pointer to the nmbs_t instance
* @param vendor_url char array where the read VendorUrl value will be stored
* @param product_name char array where the read ProductName value will be stored
* @param model_name char array where the read ModelName value will be stored
* @param user_application_name char array where the read UserApplicationName value will be stored
* @param buffer_length length of every char array
* @param buffers_length length of every char array
*
* @return NMBS_ERROR_NONE if successful, other errors otherwise.
*/
nmbs_error nmbs_read_device_identification_regular(nmbs_t* nmbs, char* vendor_url, char* product_name, char* model_name,
char* user_application_name, uint8_t buffer_length);
char* user_application_name, uint8_t buffers_length);

/** Send a FC 43 / 14 (0x2B / 0x0E) Read Device Identification to read all Extended Object Id values (Read Device ID code 3)
* @param nmbs pointer to the nmbs_t instance
Expand Down
33 changes: 17 additions & 16 deletions tests/nanomodbus_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#undef NDEBUG
#include <assert.h>
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
Expand Down Expand Up @@ -40,14 +41,14 @@ nmbs_t CLIENT, SERVER;
printf("Should %s\n", (s))


uint64_t now_ms() {
uint64_t now_ms(void) {
struct timespec ts = {0, 0};
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return (uint64_t) (ts.tv_sec) * 1000 + (uint64_t) (ts.tv_nsec) / 1000000;
}


void reset_sockets() {
void reset_sockets(void) {
if (sockets[0] != -1)
close(sockets[0]);

Expand All @@ -70,20 +71,20 @@ int32_t read_fd(int fd, uint8_t* buf, uint16_t count, int32_t timeout_ms) {
if (timeout_ms >= 0) {
tv_p = &tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
tv.tv_usec = ((__suseconds_t) timeout_ms % 1000) * 1000;
}

int ret = select(fd + 1, &rfds, NULL, NULL, tv_p);
if (ret == 0) {
return total;
}
else if (ret == 1) {

if (ret == 1) {
ssize_t r = read(fd, buf + total, 1);
if (r <= 0)
return -1;
else {
total += r;
}

total += r;
}
else
return -1;
Expand All @@ -105,20 +106,20 @@ int32_t write_fd(int fd, const uint8_t* buf, uint16_t count, int32_t timeout_ms)
if (timeout_ms >= 0) {
tv_p = &tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
tv.tv_usec = ((__suseconds_t) timeout_ms % 1000) * 1000;
}

int ret = select(fd + 1, NULL, &wfds, NULL, tv_p);
if (ret == 0) {
return 0;
}
else if (ret == 1) {

if (ret == 1) {
ssize_t w = write(fd, buf + total, count);
if (w <= 0)
return -1;
else {
total += w;
}

total += w;
}
else
return -1;
Expand Down Expand Up @@ -170,7 +171,7 @@ nmbs_platform_conf* platform_conf_socket_client(nmbs_transport transport) {
}


bool is_server_listen_thread_stopped() {
bool is_server_listen_thread_stopped(void) {
bool stopped = false;
expect(pthread_mutex_lock(&server_stopped_m) == 0);
stopped = server_stopped;
Expand All @@ -179,7 +180,8 @@ bool is_server_listen_thread_stopped() {
}


void* server_listen_thread() {
void* server_listen_thread(void* arg) {
UNUSED_PARAM(arg);
while (true) {
if (is_server_listen_thread_stopped())
break;
Expand All @@ -190,8 +192,7 @@ void* server_listen_thread() {
return NULL;
}


void stop_client_and_server() {
void stop_client_and_server(void) {
if (!is_server_listen_thread_stopped()) {
expect(pthread_mutex_lock(&server_stopped_m) == 0);
server_stopped = true;
Expand Down

0 comments on commit 363e7f7

Please sign in to comment.