tools/Master.cpp
changeset 960 36e460ffbb5e
parent 957 ed5ac2e83495
child 965 1aee4aa1def3
--- a/tools/Master.cpp	Thu Jun 05 15:58:25 2008 +0000
+++ b/tools/Master.cpp	Fri Jun 06 08:12:34 2008 +0000
@@ -13,6 +13,7 @@
 #include <iostream>
 #include <iomanip>
 #include <sstream>
+#include <cctype> // toupper()
 using namespace std;
 
 #include "Master.h"
@@ -223,6 +224,49 @@
 
 /****************************************************************************/
 
+void Master::requestStates(
+        int slavePosition,
+        const vector<string> &commandArgs
+        )
+{
+    string stateStr;
+    uint8_t state;
+    
+    if (commandArgs.size() != 1) {
+        stringstream err;
+        err << "'state' takes exactly one argument!";
+        throw MasterException(err.str());
+    }
+
+    stateStr = commandArgs[0];
+    transform(stateStr.begin(), stateStr.end(),
+            stateStr.begin(), (int (*) (int)) std::toupper);
+
+    if (stateStr == "INIT") {
+        state = 0x01;
+    } else if (stateStr == "PREOP") {
+        state = 0x02;
+    } else if (stateStr == "SAFEOP") {
+        state = 0x04;
+    } else if (stateStr == "OP") {
+        state = 0x08;
+    } else {
+        stringstream err;
+        err << "Invalid state '" << commandArgs[0] << "'!";
+        throw MasterException(err.str());
+    }
+
+    if (slavePosition == -1) {
+        unsigned int i, numSlaves = slaveCount();
+        for (i = 0; i < numSlaves; i++)
+            requestState(i, state);
+    } else {
+        requestState(slavePosition, state);
+    }
+}
+
+/****************************************************************************/
+
 void Master::generateXml(int slavePosition)
 {
     if (slavePosition == -1) {
@@ -673,6 +717,29 @@
 
 /****************************************************************************/
 
+void Master::requestState(
+        uint16_t slavePosition,
+        uint8_t state
+        )
+{
+    ec_ioctl_slave_state_t data;
+
+    data.slave_position = slavePosition;
+    data.requested_state = state;
+    
+    if (ioctl(fd, EC_IOCTL_SLAVE_STATE, &data)) {
+        stringstream err;
+        err << "Failed to request slave state: ";
+        if (errno == EINVAL)
+            err << "Slave " << slavePosition << " does not exist!";
+        else
+            err << strerror(errno);
+        throw MasterException(err.str());
+    }
+}
+
+/****************************************************************************/
+
 string Master::slaveState(uint8_t state)
 {
     switch (state) {