From c66929079986d7071a52be82c5ac918ba665852e Mon Sep 17 00:00:00 2001
From: Florian Pose <fp@igh-essen.com>
Date: Fri, 30 May 2008 10:46:14 +0000
Subject: [PATCH] Introduced ec_master_find_slave().

---
 master/master.c       | 29 +++++++++++++++++++++++++++++
 master/master.h       |  1 +
 master/slave_config.c | 23 ++++++-----------------
 3 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/master/master.c b/master/master.c
index 22e896e6..33677da1 100644
--- a/master/master.c
+++ b/master/master.c
@@ -1276,6 +1276,35 @@ int ec_master_attach_slave_configs(
     return errors ? -1 : 0;
 }
 
+/*****************************************************************************/
+
+/** Finds a slave in the bus, given the alias and position.
+ */
+ec_slave_t *ec_master_find_slave(
+        ec_master_t *master, /**< EtherCAT master. */
+        uint16_t alias, /**< Slave alias. */
+        uint16_t position /**< Slave position. */
+        )
+{
+    ec_slave_t *slave;
+    unsigned int alias_found = 0, relative_position = 0;
+
+	list_for_each_entry(slave, &master->slaves, list) {
+        if (!alias_found) {
+			if (alias && slave->sii.alias != alias)
+				continue;
+			alias_found = 1;
+			relative_position = 0;
+		}
+		if (relative_position == position) {
+            return slave;
+        }
+		relative_position++;
+	}
+
+    return NULL;
+}
+
 /******************************************************************************
  *  Realtime interface
  *****************************************************************************/
diff --git a/master/master.h b/master/master.h
index bc6b8952..29ea1b34 100644
--- a/master/master.h
+++ b/master/master.h
@@ -190,6 +190,7 @@ void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *);
 
 // misc.
 int ec_master_attach_slave_configs(ec_master_t *);
+ec_slave_t *ec_master_find_slave(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 *);
diff --git a/master/slave_config.c b/master/slave_config.c
index 52350ffd..005a1215 100644
--- a/master/slave_config.c
+++ b/master/slave_config.c
@@ -333,28 +333,17 @@ int ec_slave_config_attach(
         )
 {
 	ec_slave_t *slave;
-	unsigned int alias_found = 0, relative_position = 0;
 
 	if (sc->slave)
 		return 0; // already attached
 
-	list_for_each_entry(slave, &sc->master->slaves, list) {
-		if (!alias_found) {
-			if (sc->alias && slave->sii.alias != sc->alias)
-				continue;
-			alias_found = 1;
-			relative_position = 0;
-		}
-		if (relative_position == sc->position)
-			goto found;
-		relative_position++;
-	}
-
-	EC_WARN("Failed to find slave for configuration %u:%u.\n",
-			sc->alias, sc->position);
-	return -1;
+    if (!(slave = ec_master_find_slave(
+                    sc->master, sc->alias, sc->position))) {
+        EC_WARN("Failed to find slave for configuration %u:%u.\n",
+                sc->alias, sc->position);
+        return -1;
+    }
 
-found:
 	if (slave->config) {
 		EC_ERR("Failed to attach slave configuration %u:%u. Slave %u"
 				" already has a configuration!\n", sc->alias,
-- 
GitLab