Implemented domain command.
authorFlorian Pose <fp@igh-essen.com>
Wed, 04 Jun 2008 10:13:07 +0000
changeset 948 1cfab7161491
parent 947 6ca9c5fb56d5
child 949 e424c0074c67
Implemented domain command.
master/cdev.c
master/ioctl.h
tools/Master.cpp
tools/Master.h
tools/main.cpp
--- a/master/cdev.c	Wed Jun 04 10:10:50 2008 +0000
+++ b/master/cdev.c	Wed Jun 04 10:13:07 2008 +0000
@@ -347,6 +347,38 @@
                 break;
             }
 
+        case EC_IOCTL_DOMAIN_COUNT:
+            retval = ec_master_domain_count(master);
+            break;
+
+        case EC_IOCTL_DOMAIN:
+            {
+                ec_ioctl_domain_t data;
+                const ec_domain_t *domain;
+
+                if (copy_from_user(&data, (void __user *) arg, sizeof(data))) {
+                    retval = -EFAULT;
+                    break;
+                }
+                
+                if (!(domain = ec_master_find_domain(master, data.index))) {
+                    EC_ERR("Domain %u does not exist!\n", data.index);
+                    retval = -EINVAL;
+                    break;
+                }
+
+                data.data_size = domain->data_size;
+                data.logical_base_address = domain->logical_base_address;
+                data.working_counter = domain->working_counter;
+                data.expected_working_counter = domain->expected_working_counter;
+
+                if (copy_to_user((void __user *) arg, &data, sizeof(data))) {
+                    retval = -EFAULT;
+                    break;
+                }
+                break;
+            }
+
         default:
             retval = -ENOIOCTLCMD;
     }
--- a/master/ioctl.h	Wed Jun 04 10:10:50 2008 +0000
+++ b/master/ioctl.h	Wed Jun 04 10:13:07 2008 +0000
@@ -49,6 +49,8 @@
     EC_IOCTL_SYNC,
     EC_IOCTL_PDO,
     EC_IOCTL_PDO_ENTRY,
+	EC_IOCTL_DOMAIN_COUNT,
+	EC_IOCTL_DOMAIN
 };
 
 /*****************************************************************************/
@@ -123,4 +125,17 @@
 
 /*****************************************************************************/
 
+typedef struct {
+    // inputs
+	unsigned int index;
+
+    // outputs
+	unsigned int data_size;
+	uint32_t logical_base_address;
+	uint16_t working_counter;
+	uint16_t expected_working_counter;
+} ec_ioctl_domain_t;
+
+/*****************************************************************************/
+
 #endif
--- a/tools/Master.cpp	Wed Jun 04 10:10:50 2008 +0000
+++ b/tools/Master.cpp	Wed Jun 04 10:13:07 2008 +0000
@@ -62,6 +62,21 @@
 
 /****************************************************************************/
 
+void Master::showDomains(int domainIndex)
+{
+    if (domainIndex == -1) {
+        unsigned int numDomains = domainCount(), i;
+
+        for (i = 0; i < numDomains; i++) {
+            showDomain(i);
+        }
+    } else {
+        showDomain(domainIndex);
+    }
+}
+
+/****************************************************************************/
+
 void Master::listSlaves()
 {
     unsigned int numSlaves = slaveCount(), i;
@@ -127,6 +142,29 @@
 
 /****************************************************************************/
 
+void Master::showDomain(unsigned int domainIndex)
+{
+    ec_ioctl_domain_t data;
+    
+    getDomain(&data, domainIndex);
+
+	unsigned int data_size;
+	uint32_t logical_base_address;
+	uint16_t working_counter;
+	uint16_t expected_working_counter;
+
+	cout << "Domain" << domainIndex << ":"
+		<< " LogBaseAddr 0x"
+		<< hex << setfill('0') << setw(8) << data.logical_base_address
+		<< ", Size " << dec << data.data_size
+		<< ", WorkingCounter "
+		<< dec << data.working_counter
+		<< " of " << data.expected_working_counter
+		<< endl;
+}
+
+/****************************************************************************/
+
 void Master::listSlavePdos(uint16_t slavePosition, bool printSlave)
 {
     ec_ioctl_slave_t slave;
@@ -275,13 +313,28 @@
 
 /****************************************************************************/
 
+unsigned int Master::domainCount()
+{
+    int ret;
+
+    if ((ret = ioctl(fd, EC_IOCTL_DOMAIN_COUNT, 0)) < 0) {
+        stringstream err;
+        err << "Failed to get number of domains: " << strerror(errno);
+        throw MasterException(err.str());
+    }
+
+    return ret;
+}
+
+/****************************************************************************/
+
 unsigned int Master::slaveCount()
 {
     int ret;
 
     if ((ret = ioctl(fd, EC_IOCTL_SLAVE_COUNT, 0)) < 0) {
         stringstream err;
-        err << "Failed to get slave: " << strerror(errno);
+        err << "Failed to get number of slaves: " << strerror(errno);
         throw MasterException(err.str());
     }
 
@@ -290,6 +343,23 @@
 
 /****************************************************************************/
 
+void Master::getDomain(ec_ioctl_domain_t *data, unsigned int index)
+{
+    data->index = index;
+
+    if (ioctl(fd, EC_IOCTL_DOMAIN, data)) {
+        stringstream err;
+        err << "Failed to get domain: ";
+        if (errno == EINVAL)
+            err << "Domain " << index << " does not exist!";
+        else
+            err << strerror(errno);
+        throw MasterException(err.str());
+    }
+}
+
+/****************************************************************************/
+
 void Master::getSlave(ec_ioctl_slave_t *slave, uint16_t slaveIndex)
 {
     slave->position = slaveIndex;
--- a/tools/Master.h	Wed Jun 04 10:10:50 2008 +0000
+++ b/tools/Master.h	Wed Jun 04 10:13:07 2008 +0000
@@ -40,15 +40,19 @@
         void open(unsigned int);
         void close();
 
+        void showDomains(int);
         void listSlaves();
         void listPdos(int);
         void generateXml(int);
 
     protected:
+        void showDomain(unsigned int);
         void listSlavePdos(uint16_t, bool = false);
         void generateSlaveXml(uint16_t);
+        unsigned int domainCount();
         unsigned int slaveCount();
         void slaveSyncs(uint16_t);
+        void getDomain(ec_ioctl_domain_t *, unsigned int);
         void getSlave(ec_ioctl_slave_t *, uint16_t);
         void getSync(ec_ioctl_sync_t *, uint16_t, uint8_t);
         void getPdo(ec_ioctl_pdo_t *, uint16_t, uint8_t, uint8_t);
--- a/tools/main.cpp	Wed Jun 04 10:10:50 2008 +0000
+++ b/tools/main.cpp	Wed Jun 04 10:13:07 2008 +0000
@@ -17,9 +17,11 @@
 #define DEFAULT_MASTER 0
 #define DEFAULT_COMMAND "slaves"
 #define DEFAULT_SLAVEPOSITION -1
+#define DEFAULT_DOMAININDEX -1
 
 static unsigned int masterIndex = DEFAULT_MASTER;
 static int slavePosition = DEFAULT_SLAVEPOSITION;
+static int domainIndex = DEFAULT_DOMAININDEX;
 static string command = DEFAULT_COMMAND;
 
 /*****************************************************************************/
@@ -29,16 +31,21 @@
     cerr
         << "Usage: ethercat <COMMAND> [OPTIONS]" << endl
 		<< "Commands:" << endl
+        << "  domain             Show domain information." << endl
         << "  list (ls, slaves)  List all slaves (former 'lsec')." << endl
         << "  pdos               List Pdo mapping of given slaves." << endl
         << "  xml                Generate slave information xml." << endl
 		<< "Global options:" << endl
         << "  --master  -m <master>  Index of the master to use. Default: "
 		<< DEFAULT_MASTER	<< endl
-        << "  --slave   -s <slave>   Positive numerical ring position,"
+        << "  --slave   -s <index>   Positive numerical ring position,"
         << endl
         << "                         or 'all' for all slaves. Default: 'all'."
         << endl
+        << "  --domain  -d <index>   Positive numerical index,"
+        << endl
+        << "                         or 'all' for all domains. Default: "
+		<< "'all'." << endl
         << "  --help    -h           Show this help." << endl;
 }
 
@@ -53,12 +60,13 @@
         //name,    has_arg,           flag, val
         {"master", required_argument, NULL, 'm'},
         {"slave",  required_argument, NULL, 's'},
+        {"domain", required_argument, NULL, 'd'},
         {"help",   no_argument,       NULL, 'h'},
         {}
     };
 
     do {
-        c = getopt_long(argc, argv, "m:s:h", longOptions, &optionIndex);
+        c = getopt_long(argc, argv, "m:s:d:h", longOptions, &optionIndex);
 
         switch (c) {
             case 'm':
@@ -87,6 +95,21 @@
                 }
                 break;
 
+            case 'd':
+                if (!strcmp(optarg, "all")) {
+                    domainIndex = -1;
+                } else {
+                    number = strtoul(optarg, &remainder, 0);
+                    if (remainder == optarg || *remainder || number < 0) {
+                        cerr << "Invalid domain index "
+							<< optarg << "!" << endl;
+                        printUsage();
+                        exit(1);
+                    }
+                    domainIndex = number;
+                }
+                break;
+
             case 'h':
             case '?':
                 printUsage();
@@ -120,7 +143,9 @@
     try {
         master.open(masterIndex);
 
-        if (command == "list" || command == "ls" || command == "slaves") {
+        if (command == "domain") {
+            master.showDomains(domainIndex);
+		} else if (command == "list" || command == "ls" || command == "slaves") {
             master.listSlaves();
         } else if (command == "pdos") {
             master.listPdos(slavePosition);