Removed global variables.
authorFlorian Pose <fp@igh-essen.com>
Mon, 28 Jul 2008 14:28:43 +0000
changeset 1166 006244d53f68
parent 1165 c5d6e28eec91
child 1167 9e0ebb38e301
Removed global variables.
TODO
tool/Command.cpp
tool/Command.h
tool/CommandAlias.cpp
tool/CommandData.cpp
tool/CommandData.h
tool/CommandDomains.cpp
tool/CommandDomains.h
tool/CommandDownload.cpp
tool/CommandMaster.cpp
tool/CommandSiiWrite.cpp
tool/CommandUpload.cpp
tool/MasterDevice.h
tool/main.cpp
--- a/TODO	Mon Jul 28 14:06:03 2008 +0000
+++ b/TODO	Mon Jul 28 14:28:43 2008 +0000
@@ -13,8 +13,6 @@
 * Update documentation.
 * Get original driver for r8169.
 * Race in jiffies frame timeout?
-* ethercat tool:
-    - Check for options, remove global variables.
 
 Future issues:
 
@@ -36,6 +34,7 @@
 * ethercat tool:
     - Data type abbreviations.
     - Add a -n (numeric) switch.
+	- Check for unwanted options.
 
 Smaller issues:
 
--- a/tool/Command.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/Command.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -42,6 +42,27 @@
 	position = p;
 };
 
+/*****************************************************************************/
+
+void Command::setDomain(int d)
+{
+	domain = d;
+};
+
+/*****************************************************************************/
+
+void Command::setDataType(const string &t)
+{
+	dataType = t;
+};
+
+/*****************************************************************************/
+
+void Command::setForce(bool f)
+{
+	force = f;
+};
+
 /****************************************************************************/
 
 bool Command::matchesSubstr(const string &cmd) const
@@ -226,6 +247,31 @@
 
 /****************************************************************************/
 
+Command::DomainList Command::selectedDomains(MasterDevice &m)
+{
+	ec_ioctl_domain_t d;
+	DomainList list;
+
+    if (domain == -1) {
+		ec_ioctl_master_t master;
+        unsigned int i;
+
+        m.getMaster(&master);
+
+        for (i = 0; i < master.domain_count; i++) {
+			m.getDomain(&d, i);
+			list.push_back(d);
+        }
+    } else {
+		m.getDomain(&d, domain);
+		list.push_back(d);
+    }
+
+	return list;
+}
+
+/****************************************************************************/
+
 string Command::alStateString(uint8_t state)
 {
     switch (state) {
--- a/tool/Command.h	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/Command.h	Mon Jul 28 14:28:43 2008 +0000
@@ -14,13 +14,6 @@
 
 #include "MasterDevice.h"
 
-/*****************************************************************************/
-
-extern unsigned int masterIndex;
-extern int domainIndex;
-extern string dataTypeStr;
-extern bool force;
-
 /****************************************************************************/
 
 class InvalidUsageException:
@@ -71,6 +64,12 @@
         int getAlias() const;
         void setPosition(int);
         int getPosition() const;
+        void setDomain(int);
+        int getDomain() const;
+        void setDataType(const string &);
+        const string &getDataType() const;
+		void setForce(bool);
+		bool getForce() const;
 
         bool matchesSubstr(const string &) const;
         bool matchesAbbrev(const string &) const;
@@ -93,6 +92,8 @@
         SlaveList selectedSlaves(MasterDevice &);
         typedef list<ec_ioctl_config_t> ConfigList;
         ConfigList selectedConfigs(MasterDevice &);
+        typedef list<ec_ioctl_domain_t> DomainList;
+        DomainList selectedDomains(MasterDevice &);
 
         static string alStateString(uint8_t);
 
@@ -102,6 +103,9 @@
         Verbosity verbosity;
         int alias;
         int position;
+		int domain;
+		string dataType;
+		bool force;
 
         Command();
 };
@@ -143,4 +147,25 @@
 
 /****************************************************************************/
 
+inline int Command::getDomain() const
+{
+    return domain;
+}
+
+/****************************************************************************/
+
+inline const string &Command::getDataType() const
+{
+    return dataType;
+}
+
+/****************************************************************************/
+
+inline bool Command::getForce() const
+{
+    return force;
+}
+
+/****************************************************************************/
+
 #endif
--- a/tool/CommandAlias.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandAlias.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -79,7 +79,7 @@
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     
-    if (slaves.size() > 1 && !force) {
+    if (slaves.size() > 1 && !getForce()) {
         err << "This will write the alias addresses of "
             << slaves.size() << " slaves to " << alias
             << "! Please specify --force to proceed.";
--- a/tool/CommandData.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandData.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -41,40 +41,35 @@
 
 void CommandData::execute(MasterDevice &m, const StringVector &args)
 {
+	DomainList domains;
+	DomainList::const_iterator di;
+	
     m.open(MasterDevice::Read);
+	domains = selectedDomains(m);
 
-    if (domainIndex == -1) {
-        unsigned int i;
-        ec_ioctl_master_t master;
-
-        m.getMaster(&master);
-
-        for (i = 0; i < master.domain_count; i++) {
-            outputDomainData(m, i);
-        }
-    } else {
-        outputDomainData(m, domainIndex);
-    }
+	for (di = domains.begin(); di != domains.end(); di++) {
+		outputDomainData(m, *di);
+	}
 }
 
 /****************************************************************************/
 
-void CommandData::outputDomainData(MasterDevice &m, unsigned int domainIndex)
+void CommandData::outputDomainData(
+		MasterDevice &m,
+		const ec_ioctl_domain_t &domain
+		)
 {
-    ec_ioctl_domain_t domain;
     ec_ioctl_domain_data_t data;
     unsigned char *processData;
     unsigned int i;
     
-    m.getDomain(&domain, domainIndex);
-
     if (!domain.data_size)
         return;
 
     processData = new unsigned char[domain.data_size];
 
     try {
-        m.getData(&data, domainIndex, domain.data_size, processData);
+        m.getData(&data, domain.index, domain.data_size, processData);
     } catch (MasterDeviceException &e) {
         delete [] processData;
         throw e;
--- a/tool/CommandData.h	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandData.h	Mon Jul 28 14:28:43 2008 +0000
@@ -21,7 +21,7 @@
         void execute(MasterDevice &, const StringVector &);
 
     protected:
-		void outputDomainData(MasterDevice &, unsigned int);
+		void outputDomainData(MasterDevice &, const ec_ioctl_domain_t &);
 };
 
 /****************************************************************************/
--- a/tool/CommandDomains.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandDomains.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -66,36 +66,31 @@
 
 void CommandDomains::execute(MasterDevice &m, const StringVector &args)
 {
+	DomainList domains;
+	DomainList::const_iterator di;
+	
     m.open(MasterDevice::Read);
+	domains = selectedDomains(m);
 
-    if (domainIndex == -1) {
-        unsigned int i;
-        ec_ioctl_master_t master;
-
-        m.getMaster(&master);
-
-        for (i = 0; i < master.domain_count; i++) {
-            showDomain(m, i);
-        }
-    } else {
-        showDomain(m, domainIndex);
-    }
+	for (di = domains.begin(); di != domains.end(); di++) {
+		showDomain(m, *di);
+	}
 }
 
 /****************************************************************************/
 
-void CommandDomains::showDomain(MasterDevice &m, unsigned int domainIndex)
+void CommandDomains::showDomain(
+		MasterDevice &m,
+		const ec_ioctl_domain_t &domain
+		)
 {
-    ec_ioctl_domain_t domain;
     unsigned char *processData;
     ec_ioctl_domain_data_t data;
     unsigned int i, j;
     ec_ioctl_domain_fmmu_t fmmu;
     unsigned int dataOffset;
     
-    m.getDomain(&domain, domainIndex);
-
-	cout << "Domain" << dec << domainIndex << ":"
+	cout << "Domain" << dec << domain.index << ":"
 		<< " LogBaseAddr 0x"
 		<< hex << setfill('0')
         << setw(8) << domain.logical_base_address
@@ -111,14 +106,14 @@
     processData = new unsigned char[domain.data_size];
 
     try {
-        m.getData(&data, domainIndex, domain.data_size, processData);
+        m.getData(&data, domain.index, domain.data_size, processData);
     } catch (MasterDeviceException &e) {
         delete [] processData;
         throw e;
     }
 
     for (i = 0; i < domain.fmmu_count; i++) {
-        m.getFmmu(&fmmu, domainIndex, i);
+        m.getFmmu(&fmmu, domain.index, i);
 
         cout << "  SlaveConfig "
             << dec << fmmu.slave_config_alias
--- a/tool/CommandDomains.h	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandDomains.h	Mon Jul 28 14:28:43 2008 +0000
@@ -21,7 +21,7 @@
         void execute(MasterDevice &, const StringVector &);
 
     protected:
-		void showDomain(MasterDevice &, unsigned int);
+		void showDomain(MasterDevice &, const ec_ioctl_domain_t &);
 };
 
 /****************************************************************************/
--- a/tool/CommandDownload.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandDownload.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -100,9 +100,9 @@
     }
     data.slave_position = slaves.front().position;
 
-    if (dataTypeStr != "") { // data type specified
-        if (!(dataType = findDataType(dataTypeStr))) {
-            err << "Invalid data type '" << dataTypeStr << "'!";
+    if (!getDataType().empty()) { // data type specified
+        if (!(dataType = findDataType(getDataType()))) {
+            err << "Invalid data type '" << getDataType() << "'!";
             throwInvalidUsageException(err);
         }
     } else { // no data type specified: fetch from dictionary
--- a/tool/CommandMaster.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandMaster.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -47,7 +47,7 @@
     m.getMaster(&data);
 
     cout
-        << "Master" << masterIndex << endl
+        << "Master" << m.getIndex() << endl
         << "  Phase: ";
 
     switch (data.phase) {
--- a/tool/CommandSiiWrite.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandSiiWrite.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -77,7 +77,7 @@
         file.close();
     }
 
-    if (!force) {
+    if (!getForce()) {
         try {
             checkSiiData(&data);
         } catch (CommandException &e) {
--- a/tool/CommandUpload.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/CommandUpload.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -99,9 +99,9 @@
     }
     data.slave_position = slaves.front().position;
 
-    if (dataTypeStr != "") { // data type specified
-        if (!(dataType = findDataType(dataTypeStr))) {
-            err << "Invalid data type '" << dataTypeStr << "'!";
+    if (!getDataType().empty()) { // data type specified
+        if (!(dataType = findDataType(getDataType()))) {
+            err << "Invalid data type '" << getDataType() << "'!";
             throwInvalidUsageException(err);
         }
     } else { // no data type specified: fetch from dictionary
--- a/tool/MasterDevice.h	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/MasterDevice.h	Mon Jul 28 14:28:43 2008 +0000
@@ -37,6 +37,7 @@
         ~MasterDevice();
 
         void setIndex(unsigned int);
+		unsigned int getIndex() const;
 
         enum Permissions {Read, ReadWrite};
         void open(Permissions);
@@ -74,4 +75,11 @@
 
 /****************************************************************************/
 
+inline unsigned int MasterDevice::getIndex() const
+{
+	return index;
+}
+
+/****************************************************************************/
+
 #endif
--- a/tool/main.cpp	Mon Jul 28 14:06:03 2008 +0000
+++ b/tool/main.cpp	Mon Jul 28 14:28:43 2008 +0000
@@ -285,6 +285,9 @@
                     cmd->setVerbosity(verbosity);
                     cmd->setAlias(slaveAlias);
                     cmd->setPosition(slavePosition);
+                    cmd->setDomain(domainIndex);
+                    cmd->setDataType(dataTypeStr);
+                    cmd->setForce(force);
                     cmd->execute(masterDev, commandArgs);
                 } catch (InvalidUsageException &e) {
                     cerr << e.what() << endl << endl;