From 7194a43da13d12aadf44921140b78bc8088b9011 Mon Sep 17 00:00:00 2001 From: Florian Pose Date: Thu, 5 Jun 2008 14:33:36 +0000 Subject: [PATCH] Implemented 'ethercat debug' command. --- master/cdev.c | 6 +++++ master/ioctl.h | 3 ++- master/master.c | 65 +++++++++++++++--------------------------------- master/master.h | 4 ++- tools/Master.cpp | 29 +++++++++++++++++++++ tools/Master.h | 3 +++ tools/main.cpp | 7 ++++++ 7 files changed, 70 insertions(+), 47 deletions(-) diff --git a/master/cdev.c b/master/cdev.c index e764794b..ea469dfc 100644 --- a/master/cdev.c +++ b/master/cdev.c @@ -449,6 +449,12 @@ long eccdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) break; } + case EC_IOCTL_DEBUG_LEVEL: + if (ec_master_debug_level(master, (unsigned int) arg)) { + retval = -EINVAL; + } + break; + default: retval = -ENOIOCTLCMD; } diff --git a/master/ioctl.h b/master/ioctl.h index 68f3770e..ca2818a8 100644 --- a/master/ioctl.h +++ b/master/ioctl.h @@ -52,7 +52,8 @@ enum { EC_IOCTL_DOMAIN_COUNT, EC_IOCTL_DOMAIN, EC_IOCTL_DOMAIN_FMMU, - EC_IOCTL_DATA + EC_IOCTL_DATA, + EC_IOCTL_DEBUG_LEVEL, }; /*****************************************************************************/ diff --git a/master/master.c b/master/master.c index 50c07084..42ed2161 100644 --- a/master/master.c +++ b/master/master.c @@ -65,25 +65,20 @@ static int ec_master_operation_thread(ec_master_t *); void ec_master_eoe_run(unsigned long); #endif ssize_t ec_show_master_attribute(struct kobject *, struct attribute *, char *); -ssize_t ec_store_master_attribute(struct kobject *, struct attribute *, - const char *, size_t); /*****************************************************************************/ /** \cond */ EC_SYSFS_READ_ATTR(info); -EC_SYSFS_READ_WRITE_ATTR(debug_level); static struct attribute *ec_def_attrs[] = { &attr_info, - &attr_debug_level, NULL, }; static struct sysfs_ops ec_sysfs_ops = { .show = &ec_show_master_attribute, - .store = ec_store_master_attribute }; static struct kobj_type ktype_ec_master = { @@ -1085,52 +1080,12 @@ ssize_t ec_show_master_attribute(struct kobject *kobj, /**< kobject */ if (attr == &attr_info) { return ec_master_info(master, buffer); } - else if (attr == &attr_debug_level) { - return sprintf(buffer, "%i\n", master->debug_level); - } return 0; } /*****************************************************************************/ -/** - Formats attribute data for SysFS write access. - \return number of bytes processed, or negative error code -*/ - -ssize_t ec_store_master_attribute(struct kobject *kobj, /**< slave's kobject */ - struct attribute *attr, /**< attribute */ - const char *buffer, /**< memory with data */ - size_t size /**< size of data to store */ - ) -{ - ec_master_t *master = container_of(kobj, ec_master_t, kobj); - - if (attr == &attr_debug_level) { - if (!strcmp(buffer, "0\n")) { - master->debug_level = 0; - } - else if (!strcmp(buffer, "1\n")) { - master->debug_level = 1; - } - else if (!strcmp(buffer, "2\n")) { - master->debug_level = 2; - } - else { - EC_ERR("Invalid debug level value!\n"); - return -EINVAL; - } - - EC_INFO("Master debug level set to %i.\n", master->debug_level); - return size; - } - - return -EINVAL; -} - -/*****************************************************************************/ - #ifdef EC_EOE /** Starts Ethernet-over-EtherCAT processing on demand. @@ -1339,6 +1294,26 @@ ec_domain_t *ec_master_find_domain( return NULL; } +/*****************************************************************************/ + +int ec_master_debug_level( + ec_master_t *master, + int level + ) +{ + if (level < 0 || level > 2) { + EC_ERR("Invalid debug level %i!\n", level); + return -1; + } + + if (level != master->debug_level) { + master->debug_level = level; + EC_INFO("Master debug level set to %i.\n", master->debug_level); + } + + return 0; +} + /****************************************************************************** * Realtime interface *****************************************************************************/ diff --git a/master/master.h b/master/master.h index e3d271ab..8be5d3f9 100644 --- a/master/master.h +++ b/master/master.h @@ -126,7 +126,7 @@ struct ec_master { struct list_head domains; /**< list of domains */ - int debug_level; /**< master debug level */ + int debug_level; /**< Master debug level. */ ec_stats_t stats; /**< cyclic statistics */ unsigned int frames_timed_out; /**< there were frame timeouts in the last call to ecrt_master_receive() */ @@ -200,6 +200,8 @@ void ec_master_destroy_slaves(ec_master_t *); unsigned int ec_master_domain_count(const ec_master_t *); ec_domain_t *ec_master_find_domain(ec_master_t *, unsigned int); +int ec_master_debug_level(ec_master_t *, int); + /*****************************************************************************/ #endif diff --git a/tools/Master.cpp b/tools/Master.cpp index 5cf554b5..6ee44a4c 100644 --- a/tools/Master.cpp +++ b/tools/Master.cpp @@ -77,6 +77,35 @@ void Master::outputData(int domainIndex) /****************************************************************************/ +void Master::setDebug(const vector &commandArgs) +{ + stringstream str; + int debugLevel; + + if (commandArgs.size() != 1) { + stringstream err; + err << "'debug' takes exactly one argument!"; + throw MasterException(err.str()); + } + + str << commandArgs[0]; + str >> debugLevel; + + if (str.fail()) { + stringstream err; + err << "Invalid debug level '" << commandArgs[0] << "'!"; + throw MasterException(err.str()); + } + + if (ioctl(fd, EC_IOCTL_DEBUG_LEVEL, debugLevel) < 0) { + stringstream err; + err << "Failed to set debug level: " << strerror(errno); + throw MasterException(err.str()); + } +} + +/****************************************************************************/ + void Master::showDomains(int domainIndex) { if (domainIndex == -1) { diff --git a/tools/Master.h b/tools/Master.h index 50101535..eef78cad 100644 --- a/tools/Master.h +++ b/tools/Master.h @@ -8,6 +8,8 @@ #define __EC_MASTER_H__ #include +#include +#include using namespace std; #include "../master/ioctl.h" @@ -41,6 +43,7 @@ class Master void close(); void outputData(int); + void setDebug(const vector &); void showDomains(int); void listSlaves(); void listPdos(int); diff --git a/tools/main.cpp b/tools/main.cpp index 270e6788..e41183b1 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -8,6 +8,7 @@ #include #include +#include using namespace std; #include "Master.h" @@ -23,6 +24,7 @@ static unsigned int masterIndex = DEFAULT_MASTER; static int slavePosition = DEFAULT_SLAVEPOSITION; static int domainIndex = DEFAULT_DOMAININDEX; static string command = DEFAULT_COMMAND; +vector commandArgs; /*****************************************************************************/ @@ -32,6 +34,7 @@ void printUsage() << "Usage: ethercat [OPTIONS]" << endl << "Commands:" << endl << " data Output binary domain process data." << endl + << " debug Set the master debug level." << endl << " domain Show domain information." << endl << " list (ls, slaves) List all slaves (former 'lsec')." << endl << " pdos List Pdo mapping of given slaves." << endl @@ -131,6 +134,8 @@ void getOptions(int argc, char **argv) } command = argv[optind]; + while (++optind < argc) + commandArgs.push_back(string(argv[optind])); } /****************************************************************************/ @@ -146,6 +151,8 @@ int main(int argc, char **argv) if (command == "data") { master.outputData(domainIndex); + } else if (command == "debug") { + master.setDebug(commandArgs); } else if (command == "domain") { master.showDomains(domainIndex); } else if (command == "list" || command == "ls" || command == "slaves") {