From ba8b7f30c00644bfcd0adfa7417f30b9528239f4 Mon Sep 17 00:00:00 2001 From: Bartjan van der Meer Date: Mon, 3 Jun 2024 13:46:11 +0200 Subject: [PATCH] run code format on simple_ng.c --- test/simple_ng/simple_ng.c | 158 +++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 60 deletions(-) diff --git a/test/simple_ng/simple_ng.c b/test/simple_ng/simple_ng.c index 8926b932..45c990a5 100644 --- a/test/simple_ng/simple_ng.c +++ b/test/simple_ng/simple_ng.c @@ -12,33 +12,32 @@ #include #include - -typedef struct { - ecx_contextt context; - char * iface; - uint8 group; - int roundtrip_time; +typedef struct +{ + ecx_contextt context; + char *iface; + uint8 group; + int roundtrip_time; /* Used by the context */ - uint8 map[4096]; - ecx_portt port; - ec_slavet slavelist[EC_MAXSLAVE]; - int slavecount; - ec_groupt grouplist[EC_MAXGROUP]; - uint8 esibuf[EC_MAXEEPBUF]; - uint32 esimap[EC_MAXEEPBITMAP]; - ec_eringt elist; - ec_idxstackT idxstack; - boolean ecaterror; - int64 DCtime; - ec_SMcommtypet SMcommtype[EC_MAX_MAPT]; - ec_PDOassignt PDOassign[EC_MAX_MAPT]; - ec_PDOdesct PDOdesc[EC_MAX_MAPT]; - ec_eepromSMt eepSM; - ec_eepromFMMUt eepFMMU; + uint8 map[4096]; + ecx_portt port; + ec_slavet slavelist[EC_MAXSLAVE]; + int slavecount; + ec_groupt grouplist[EC_MAXGROUP]; + uint8 esibuf[EC_MAXEEPBUF]; + uint32 esimap[EC_MAXEEPBITMAP]; + ec_eringt elist; + ec_idxstackT idxstack; + boolean ecaterror; + int64 DCtime; + ec_SMcommtypet SMcommtype[EC_MAX_MAPT]; + ec_PDOassignt PDOassign[EC_MAX_MAPT]; + ec_PDOdesct PDOdesc[EC_MAX_MAPT]; + ec_eepromSMt eepSM; + ec_eepromFMMUt eepFMMU; } Fieldbus; - static void fieldbus_initialize(Fieldbus *fieldbus, char *iface) { @@ -108,14 +107,16 @@ fieldbus_start(Fieldbus *fieldbus) grp = fieldbus->grouplist + fieldbus->group; printf("Initializing SOEM on '%s'... ", fieldbus->iface); - if (! ecx_init(context, fieldbus->iface)) { + if (!ecx_init(context, fieldbus->iface)) + { printf("no socket connection\n"); return FALSE; } printf("done\n"); printf("Finding autoconfig slaves... "); - if (ecx_config_init(context, FALSE) <= 0) { + if (ecx_config_init(context, FALSE) <= 0) + { printf("no slaves found\n"); return FALSE; } @@ -125,9 +126,11 @@ fieldbus_start(Fieldbus *fieldbus) ecx_config_map_group(context, fieldbus->map, fieldbus->group); printf("mapped %dO+%dI bytes from %d segments", grp->Obytes, grp->Ibytes, grp->nsegments); - if (grp->nsegments > 1) { + if (grp->nsegments > 1) + { /* Show how slaves are distrubuted */ - for (i = 0; i < grp->nsegments; ++i) { + for (i = 0; i < grp->nsegments; ++i) + { printf("%s%d", i == 0 ? " (" : "+", grp->IOsegment[i]); } printf(" slaves)"); @@ -152,11 +155,13 @@ fieldbus_start(Fieldbus *fieldbus) slave->state = EC_STATE_OPERATIONAL; ecx_writestate(context, 0); /* Poll the result ten times before giving up */ - for (i = 0; i < 10; ++i) { + for (i = 0; i < 10; ++i) + { printf("."); fieldbus_roundtrip(fieldbus); ecx_statecheck(context, 0, EC_STATE_OPERATIONAL, EC_TIMEOUTSTATE / 10); - if (slave->state == EC_STATE_OPERATIONAL) { + if (slave->state == EC_STATE_OPERATIONAL) + { printf(" all slaves are now operational\n"); return TRUE; } @@ -164,12 +169,14 @@ fieldbus_start(Fieldbus *fieldbus) printf(" failed,"); ecx_readstate(context); - for (i = 1; i <= fieldbus->slavecount; ++i) { + for (i = 1; i <= fieldbus->slavecount; ++i) + { slave = fieldbus->slavelist + i; - if (slave->state != EC_STATE_OPERATIONAL) { + if (slave->state != EC_STATE_OPERATIONAL) + { printf(" slave %d is 0x%04X (AL-status=0x%04X %s)", - i, slave->state, slave->ALstatuscode, - ec_ALstatuscode2string(slave->ALstatuscode)); + i, slave->state, slave->ALstatuscode, + ec_ALstatuscode2string(slave->ALstatuscode)); } } printf("\n"); @@ -209,20 +216,23 @@ fieldbus_dump(Fieldbus *fieldbus) wkc = fieldbus_roundtrip(fieldbus); expected_wkc = grp->outputsWKC * 2 + grp->inputsWKC; printf("%6d usec WKC %d", fieldbus->roundtrip_time, wkc); - if (wkc < expected_wkc) { + if (wkc < expected_wkc) + { printf(" wrong (expected %d)\n", expected_wkc); return FALSE; } printf(" O:"); - for (n = 0; n < grp->Obytes; ++n) { + for (n = 0; n < grp->Obytes; ++n) + { printf(" %02X", grp->outputs[n]); } printf(" I:"); - for (n = 0; n < grp->Ibytes; ++n) { + for (n = 0; n < grp->Ibytes; ++n) + { printf(" %02X", grp->inputs[n]); } - printf(" T: %lld\n", (long long) fieldbus->DCtime); + printf(" T: %lld\n", (long long)fieldbus->DCtime); return TRUE; } @@ -238,55 +248,74 @@ fieldbus_check_state(Fieldbus *fieldbus) grp = context->grouplist + fieldbus->group; grp->docheckstate = FALSE; ecx_readstate(context); - for (i = 1; i <= fieldbus->slavecount; ++i) { + for (i = 1; i <= fieldbus->slavecount; ++i) + { slave = context->slavelist + i; - if (slave->group != fieldbus->group) { + if (slave->group != fieldbus->group) + { /* This slave is part of another group: do nothing */ - } else if (slave->state != EC_STATE_OPERATIONAL) { + } + else if (slave->state != EC_STATE_OPERATIONAL) + { grp->docheckstate = TRUE; - if (slave->state == EC_STATE_SAFE_OP + EC_STATE_ERROR) { + if (slave->state == EC_STATE_SAFE_OP + EC_STATE_ERROR) + { printf("* Slave %d is in SAFE_OP+ERROR, attempting ACK\n", i); slave->state = EC_STATE_SAFE_OP + EC_STATE_ACK; ecx_writestate(context, i); - } else if(slave->state == EC_STATE_SAFE_OP) { + } + else if (slave->state == EC_STATE_SAFE_OP) + { printf("* Slave %d is in SAFE_OP, change to OPERATIONAL\n", i); slave->state = EC_STATE_OPERATIONAL; ecx_writestate(context, i); - } else if(slave->state > EC_STATE_NONE) { - if (ecx_reconfig_slave(context, i, EC_TIMEOUTRET)) { + } + else if (slave->state > EC_STATE_NONE) + { + if (ecx_reconfig_slave(context, i, EC_TIMEOUTRET)) + { slave->islost = FALSE; printf("* Slave %d reconfigured\n", i); } - } else if(! slave->islost) { + } + else if (!slave->islost) + { ecx_statecheck(context, i, EC_STATE_OPERATIONAL, EC_TIMEOUTRET); - if (slave->state == EC_STATE_NONE) { + if (slave->state == EC_STATE_NONE) + { slave->islost = TRUE; printf("* Slave %d lost\n", i); } } - } else if (slave->islost) { - if(slave->state != EC_STATE_NONE) { + } + else if (slave->islost) + { + if (slave->state != EC_STATE_NONE) + { slave->islost = FALSE; printf("* Slave %d found\n", i); - } else if (ecx_recover_slave(context, i, EC_TIMEOUTRET)) { + } + else if (ecx_recover_slave(context, i, EC_TIMEOUTRET)) + { slave->islost = FALSE; printf("* Slave %d recovered\n", i); } } } - if (! grp->docheckstate) { + if (!grp->docheckstate) + { printf("All slaves resumed OPERATIONAL\n"); } } -int -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { Fieldbus fieldbus; - if (argc != 2) { - ec_adaptert * adapter = NULL; + if (argc != 2) + { + ec_adaptert *adapter = NULL; printf("Usage: simple_ng IFNAME1\n" "IFNAME1 is the NIC interface name, e.g. 'eth0'\n"); @@ -302,18 +331,27 @@ main(int argc, char *argv[]) } fieldbus_initialize(&fieldbus, argv[1]); - if (fieldbus_start(&fieldbus)) { + if (fieldbus_start(&fieldbus)) + { int i, min_time, max_time; min_time = max_time = 0; - for (i = 1; i <= 10000; ++i) { + for (i = 1; i <= 10000; ++i) + { printf("Iteration %4d:", i); - if (! fieldbus_dump(&fieldbus)) { + if (!fieldbus_dump(&fieldbus)) + { fieldbus_check_state(&fieldbus); - } else if (i == 1) { + } + else if (i == 1) + { min_time = max_time = fieldbus.roundtrip_time; - } else if (fieldbus.roundtrip_time < min_time) { + } + else if (fieldbus.roundtrip_time < min_time) + { min_time = fieldbus.roundtrip_time; - } else if (fieldbus.roundtrip_time > max_time) { + } + else if (fieldbus.roundtrip_time > max_time) + { max_time = fieldbus.roundtrip_time; } osal_usleep(5000);