Introduced ec_master_find_slave().
authorFlorian Pose <fp@igh-essen.com>
Fri, 30 May 2008 10:46:14 +0000
changeset 927 ffb7d1876ce0
parent 926 6bb33c6ec770
child 928 7fbc0b943c65
Introduced ec_master_find_slave().
master/master.c
master/master.h
master/slave_config.c
--- a/master/master.c	Fri May 30 09:39:29 2008 +0000
+++ b/master/master.c	Fri May 30 10:46:14 2008 +0000
@@ -1276,6 +1276,35 @@
     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
  *****************************************************************************/
--- a/master/master.h	Fri May 30 09:39:29 2008 +0000
+++ b/master/master.h	Fri May 30 10:46:14 2008 +0000
@@ -190,6 +190,7 @@
 
 // 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 *);
--- a/master/slave_config.c	Fri May 30 09:39:29 2008 +0000
+++ b/master/slave_config.c	Fri May 30 10:46:14 2008 +0000
@@ -333,28 +333,17 @@
         )
 {
 	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;
-
-found:
+    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;
+    }
+
 	if (slave->config) {
 		EC_ERR("Failed to attach slave configuration %u:%u. Slave %u"
 				" already has a configuration!\n", sc->alias,