style: Apply clang-format to C/C++ source files

Applied consistent code formatting using clang-format to all C/C++ source
and header files in the 'software/' directory.
This commit is contained in:
Eduard Iten 2025-07-10 23:33:50 +02:00
parent 8f89713866
commit bd8a7a766c
10 changed files with 580 additions and 428 deletions

View File

@ -1,5 +1,142 @@
# .clang-format
BasedOnStyle: Google
#IndentWidth: 4
#ColumnLimit: 100
#AllowShortFunctionsOnASingleLine: None
# Zephyr Project .clang-format configuration
# Based on Linux kernel style with Zephyr-specific adaptations
# Use LLVM as the base style and customize from there
BasedOnStyle: LLVM
# Language settings
Language: Cpp
# Indentation settings
IndentWidth: 8
TabWidth: 8
UseTab: ForIndentation
# Line length
ColumnLimit: 100
# Brace settings
BreakBeforeBraces: Linux
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
# Always add braces for control statements (Zephyr requirement)
RemoveBracesLLVM: false
# Control statement settings
SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
# Function settings
AllowShortFunctionsOnASingleLine: None
AllowShortBlocksOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
# Pointer and reference alignment
PointerAlignment: Right
ReferenceAlignment: Right
# Spacing settings
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInSquareBrackets: false
# Alignment settings
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: false
# Breaking settings
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
# Penalties (used for line breaking decisions)
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
# Comment settings
ReflowComments: true
CommentPragmas: '^ IWYU pragma:'
# Sorting settings
SortIncludes: true
SortUsingDeclarations: true
# Preprocessor settings
IndentPPDirectives: None
MacroBlockBegin: ''
MacroBlockEnd: ''
# Misc settings
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros: ['LISTIFY', 'FOR_EACH', 'FOR_EACH_FIXED_ARG', 'FOR_EACH_IDX', 'FOR_EACH_IDX_FIXED_ARG', 'FOR_EACH_NONEMPTY_TERM', 'Z_FOR_EACH', 'Z_FOR_EACH_FIXED_ARG', 'Z_FOR_EACH_IDX', 'Z_FOR_EACH_IDX_FIXED_ARG']
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<zephyr/.*\.h>'
Priority: 1
- Regex: '^<.*\.h>'
Priority: 2
- Regex: '^<.*'
Priority: 3
- Regex: '.*'
Priority: 4
IndentCaseLabels: false
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true

View File

@ -6,7 +6,8 @@
#include <zephyr/kernel.h>
int main(void) {
int main(void)
{
printk("Hello from Gateway!\n");
return 0;
}

View File

@ -1,13 +1,14 @@
#include <lib/fwu.h>
#include <lib/modbus_server.h>
#include <lib/valve.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>
#include <lib/fwu.h>
#include <lib/modbus_server.h>
#include <lib/valve.h>
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
int main(void) {
int main(void)
{
LOG_INF("Starting Irrigation System Slave Node");
if (settings_subsys_init() || settings_load()) {

View File

@ -1,8 +1,8 @@
#ifndef VALVE_H
#define VALVE_H
#include <stdint.h>
#include <zephyr/drivers/gpio.h>
#include <stdint.h>
/**
* @file valve.h

View File

@ -8,31 +8,33 @@
* the update process. The actual writing to flash is simulated.
*/
#include <lib/fwu.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/crc.h>
#include <lib/fwu.h>
LOG_MODULE_REGISTER(fwu, LOG_LEVEL_INF);
#define FWU_BUFFER_SIZE 256
static uint8_t fwu_buffer[FWU_BUFFER_SIZE]; // Buffer to store incoming
// firmware data chunks
static uint32_t fwu_chunk_offset =
0; // Offset for the current firmware chunk in the overall image
static uint32_t fwu_chunk_offset = 0; // Offset for the current firmware chunk in the overall image
static uint16_t fwu_chunk_size = 0; // Size of the current firmware chunk
static uint16_t fwu_last_chunk_crc =
0; // CRC16 of the last received firmware chunk
static uint16_t fwu_last_chunk_crc = 0; // CRC16 of the last received firmware chunk
void fwu_init(void) {}
void fwu_init(void)
{
}
void fwu_handler(uint16_t addr, uint16_t reg) {
void fwu_handler(uint16_t addr, uint16_t reg)
{
// This is a simplified handler. In a real scenario, you would have a proper
// mapping between register addresses and actions.
if (addr == 0x0100) { // FWU_COMMAND
if (reg == 1) {
LOG_INF("FWU: Chunk at offset %u (size %u) verified.", fwu_chunk_offset,
LOG_INF("FWU: Chunk at offset %u (size %u) verified.",
fwu_chunk_offset,
fwu_chunk_size);
} else if (reg == 2) {
LOG_INF("FWU: Finalize command received. Rebooting (simulated).");
@ -48,11 +50,15 @@ void fwu_handler(uint16_t addr, uint16_t reg) {
if (index < sizeof(fwu_buffer)) {
sys_put_be16(reg, &fwu_buffer[index]);
if (index + 2 >= fwu_chunk_size) {
fwu_last_chunk_crc = crc16_ccitt(0xffff, fwu_buffer, fwu_chunk_size);
fwu_last_chunk_crc =
crc16_ccitt(0xffff, fwu_buffer, fwu_chunk_size);
LOG_INF("FWU: Chunk received, CRC is 0x%04X", fwu_last_chunk_crc);
}
}
}
}
uint16_t fwu_get_last_chunk_crc(void) { return fwu_last_chunk_crc; }
uint16_t fwu_get_last_chunk_crc(void)
{
return fwu_last_chunk_crc;
}

View File

@ -7,10 +7,6 @@
* libraries like valve control, ADC sensors, and firmware updates.
*/
#include <app_version.h>
#include <lib/fwu.h>
#include <lib/modbus_server.h>
#include <lib/valve.h>
#include <zephyr/device.h>
#include <zephyr/drivers/misc/vnd7050aj/vnd7050aj.h>
#include <zephyr/drivers/uart.h>
@ -20,6 +16,10 @@
#include <zephyr/settings/settings.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/usb/usb_device.h>
#include <app_version.h>
#include <lib/fwu.h>
#include <lib/modbus_server.h>
#include <lib/valve.h>
LOG_MODULE_REGISTER(modbus_server, LOG_LEVEL_INF);
@ -42,7 +42,8 @@ static struct k_timer watchdog_timer;
*
* @param timer_id Pointer to the timer instance.
*/
static void watchdog_timer_handler(struct k_timer *timer_id) {
static void watchdog_timer_handler(struct k_timer *timer_id)
{
LOG_WRN("Modbus watchdog expired! Closing valve as a fail-safe.");
valve_close();
}
@ -53,7 +54,8 @@ static void watchdog_timer_handler(struct k_timer *timer_id) {
* This function should be called upon receiving any valid Modbus request
* to prevent the watchdog from expiring.
*/
static inline void reset_watchdog(void) {
static inline void reset_watchdog(void)
{
if (watchdog_timeout_s > 0) {
k_timer_start(&watchdog_timer, K_SECONDS(watchdog_timeout_s), K_NO_WAIT);
}
@ -66,7 +68,8 @@ static inline void reset_watchdog(void) {
* @param reg Pointer to store the read value.
* @return 0 on success.
*/
static int holding_reg_rd(uint16_t addr, uint16_t *reg) {
static int holding_reg_rd(uint16_t addr, uint16_t *reg)
{
reset_watchdog();
switch (addr) {
case REG_HOLDING_MAX_OPENING_TIME_S:
@ -92,7 +95,8 @@ static int holding_reg_rd(uint16_t addr, uint16_t *reg) {
* @param reg Value to write.
* @return 0 on success.
*/
static int holding_reg_wr(uint16_t addr, uint16_t reg) {
static int holding_reg_wr(uint16_t addr, uint16_t reg)
{
reset_watchdog();
switch (addr) {
case REG_HOLDING_VALVE_COMMAND:
@ -140,7 +144,8 @@ static int holding_reg_wr(uint16_t addr, uint16_t reg) {
* @param reg Pointer to store the read value.
* @return 0 on success.
*/
static int input_reg_rd(uint16_t addr, uint16_t *reg) {
static int input_reg_rd(uint16_t addr, uint16_t *reg)
{
reset_watchdog();
uint32_t uptime_s = k_uptime_get_32() / 1000;
switch (addr) {
@ -187,7 +192,8 @@ static struct modbus_user_callbacks mbs_cbs = {
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)
int modbus_server_init(void) {
int modbus_server_init(void)
{
k_timer_init(&watchdog_timer, watchdog_timer_handler, NULL);
// Load saved settings
@ -222,12 +228,12 @@ int modbus_server_init(void) {
}
server_param.server.user_cb = &mbs_cbs;
LOG_INF("Starting Modbus server: baudrate=%u, unit_id=%u", saved_baudrate,
saved_unit_id);
LOG_INF("Starting Modbus server: baudrate=%u, unit_id=%u", saved_baudrate, saved_unit_id);
return modbus_init_server(modbus_iface, server_param);
}
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id) {
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id)
{
// Update parameters
server_param.serial.baud = baudrate;
server_param.server.unit_id = unit_id;
@ -247,8 +253,7 @@ int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id) {
settings_save_one("modbus/baudrate", &baudrate, sizeof(baudrate));
settings_save_one("modbus/unit_id", &unit_id, sizeof(unit_id));
LOG_INF(
"Settings saved. Type 'reset' to restart the device and apply the "
LOG_INF("Settings saved. Type 'reset' to restart the device and apply the "
"change.");
return 0; // Return success since settings are saved
}
@ -256,5 +261,11 @@ int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id) {
return ret;
}
uint32_t modbus_get_baudrate(void) { return server_param.serial.baud; }
uint8_t modbus_get_unit_id(void) { return server_param.server.unit_id; }
uint32_t modbus_get_baudrate(void)
{
return server_param.serial.baud;
}
uint8_t modbus_get_unit_id(void)
{
return server_param.server.unit_id;
}

View File

@ -8,10 +8,10 @@
* storage.
*/
#include <zephyr/shell/shell.h>
#include <lib/modbus_server.h>
#include <lib/valve.h>
#include <stdlib.h>
#include <zephyr/shell/shell.h>
/**
* @brief Shell command to set the Modbus baudrate.
@ -21,16 +21,15 @@
* @param argv Argument values.
* @return 0 on success, -EINVAL on error.
*/
static int cmd_modbus_set_baud(const struct shell *sh, size_t argc,
char **argv) {
static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_error(sh, "Usage: set_baud <baudrate>");
return -EINVAL;
}
uint32_t new_baud = (uint32_t)strtoul(argv[1], NULL, 10);
const uint32_t valid_baud_rates[] = {1200, 2400, 4800, 9600,
19200, 38400, 57600, 115200};
const uint32_t valid_baud_rates[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
bool is_valid = false;
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
@ -42,10 +41,12 @@ static int cmd_modbus_set_baud(const struct shell *sh, size_t argc,
if (!is_valid) {
char error_msg[128];
int offset = snprintf(error_msg, sizeof(error_msg),
"Invalid baudrate. Valid rates are: ");
int offset =
snprintf(error_msg, sizeof(error_msg), "Invalid baudrate. Valid rates are: ");
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
offset += snprintf(error_msg + offset, sizeof(error_msg) - offset, "%u ",
offset += snprintf(error_msg + offset,
sizeof(error_msg) - offset,
"%u ",
valid_baud_rates[i]);
}
shell_error(sh, "%s", error_msg);
@ -69,7 +70,8 @@ static int cmd_modbus_set_baud(const struct shell *sh, size_t argc,
* @param argv Argument values.
* @return 0 on success, -EINVAL on error.
*/
static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) {
static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_error(sh, "Usage: set_id <slave_id>");
return -EINVAL;
@ -77,8 +79,7 @@ static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) {
uint32_t new_id_u32 = (uint32_t)strtoul(argv[1], NULL, 10);
if (new_id_u32 == 0 || new_id_u32 > 247) {
shell_error(sh, "Invalid slave ID: %s. Must be between 1 and 247.",
argv[1]);
shell_error(sh, "Invalid slave ID: %s. Must be between 1 and 247.", argv[1]);
return -EINVAL;
}
uint8_t new_id = (uint8_t)new_id_u32;
@ -100,8 +101,8 @@ static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) {
* @param argv Argument values.
* @return 0 on success, -EINVAL on error.
*/
static int cmd_valve_set_open_time(const struct shell *sh, size_t argc,
char **argv) {
static int cmd_valve_set_open_time(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_error(sh, "Usage: set_open_time <seconds>");
return -EINVAL;
@ -122,8 +123,8 @@ static int cmd_valve_set_open_time(const struct shell *sh, size_t argc,
* @param argv Argument values.
* @return 0 on success, -EINVAL on error.
*/
static int cmd_valve_set_close_time(const struct shell *sh, size_t argc,
char **argv) {
static int cmd_valve_set_close_time(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_error(sh, "Usage: set_close_time <seconds>");
return -EINVAL;
@ -144,7 +145,8 @@ static int cmd_valve_set_close_time(const struct shell *sh, size_t argc,
* @param argv Argument values.
* @return 0 on success.
*/
static int cmd_config_show(const struct shell *sh, size_t argc, char **argv) {
static int cmd_config_show(const struct shell *sh, size_t argc, char **argv)
{
shell_print(sh, "Current Modbus Configuration:");
shell_print(sh, " Baudrate: %u", modbus_get_baudrate());
shell_print(sh, " Slave ID: %u", modbus_get_unit_id());
@ -155,22 +157,15 @@ static int cmd_config_show(const struct shell *sh, size_t argc, char **argv) {
}
SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds,
SHELL_CMD(set_baud, NULL, "Set Modbus baudrate",
cmd_modbus_set_baud),
SHELL_CMD(set_id, NULL, "Set Modbus slave ID",
cmd_modbus_set_id),
SHELL_CMD(set_baud, NULL, "Set Modbus baudrate", cmd_modbus_set_baud),
SHELL_CMD(set_id, NULL, "Set Modbus slave ID", cmd_modbus_set_id),
SHELL_SUBCMD_SET_END);
SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_cmds,
SHELL_CMD(set_open_time, NULL,
"Set max valve opening time",
cmd_valve_set_open_time),
SHELL_CMD(set_close_time, NULL,
"Set max valve closing time",
cmd_valve_set_close_time),
SHELL_CMD(set_open_time, NULL, "Set max valve opening time", cmd_valve_set_open_time),
SHELL_CMD(set_close_time, NULL, "Set max valve closing time", cmd_valve_set_close_time),
SHELL_SUBCMD_SET_END);
SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus configuration", NULL);
SHELL_CMD_REGISTER(valve, &sub_valve_cmds, "Valve configuration", NULL);
SHELL_CMD_REGISTER(show_config, NULL, "Show all configurations",
cmd_config_show);
SHELL_CMD_REGISTER(show_config, NULL, "Show all configurations", cmd_config_show);

View File

@ -20,7 +20,8 @@
* @param argv Argument values.
* @return 0 on success.
*/
static int cmd_reset(const struct shell *sh, size_t argc, char **argv) {
static int cmd_reset(const struct shell *sh, size_t argc, char **argv)
{
shell_print(sh, "Rebooting system...");
k_sleep(K_MSEC(100)); // Allow the shell to print the message
sys_reboot(SYS_REBOOT_WARM);