diff --git a/master/master.c b/master/master.c index fdf9d88a53d44f2d16aa81db1a6b6b4b3f3b698a..327c6cc430fd0045ef691c6934a512c6fb823a9e 100644 --- a/master/master.c +++ b/master/master.c @@ -1023,6 +1023,29 @@ void ec_master_attach_slave_configs( /*****************************************************************************/ +/** Common implementation for ec_master_find_slave() + * and ec_master_find_slave_const(). + */ +#define EC_FIND_SLAVE \ + do { \ + if (alias) { \ + for (; slave < master->slaves + master->slave_count; \ + slave++) { \ + if (slave->sii.alias == alias) \ + break; \ + } \ + if (slave == master->slaves + master->slave_count) \ + return NULL; \ + } \ + \ + slave += position; \ + if (slave < master->slaves + master->slave_count) { \ + return slave; \ + } else { \ + return NULL; \ + } \ + } while (0) + /** Finds a slave in the bus, given the alias and position. */ ec_slave_t *ec_master_find_slave( @@ -1032,24 +1055,21 @@ ec_slave_t *ec_master_find_slave( ) { ec_slave_t *slave = master->slaves; + EC_FIND_SLAVE; +} - if (alias) { - // find slave with the given alias - for (; slave < master->slaves + master->slave_count; - slave++) { - if (slave->sii.alias == alias) - break; - } - if (slave == master->slaves + master->slave_count) - return NULL; - } - - slave += position; - if (slave < master->slaves + master->slave_count) { - return slave; - } else { - return NULL; - } +/** Finds a slave in the bus, given the alias and position. + * + * Const version. + */ +const ec_slave_t *ec_master_find_slave_const( + const ec_master_t *master, /**< EtherCAT master. */ + uint16_t alias, /**< Slave alias. */ + uint16_t position /**< Slave position. */ + ) +{ + const ec_slave_t *slave = master->slaves; + EC_FIND_SLAVE; } /*****************************************************************************/ diff --git a/master/master.h b/master/master.h index 7d24d765ae9e16fa68454311a32ccd1c65c1abcc..d5ccc6c578d5d9fcd28874dcc95346dfe35ad8ee 100644 --- a/master/master.h +++ b/master/master.h @@ -190,6 +190,8 @@ void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *); // misc. void ec_master_attach_slave_configs(ec_master_t *); ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t); +const ec_slave_t *ec_master_find_slave_const(const ec_master_t *, uint16_t, + uint16_t); void ec_master_output_stats(ec_master_t *); #ifdef EC_EOE void ec_master_clear_eoe_handlers(ec_master_t *);