fp@359: /****************************************************************************** fp@359: * fp@359: * $Id$ fp@359: * fp@359: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@359: * fp@359: * This file is part of the IgH EtherCAT Master. fp@359: * fp@359: * The IgH EtherCAT Master is free software; you can redistribute it fp@359: * and/or modify it under the terms of the GNU General Public License fp@359: * as published by the Free Software Foundation; either version 2 of the fp@359: * License, or (at your option) any later version. fp@359: * fp@359: * The IgH EtherCAT Master is distributed in the hope that it will be fp@359: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@359: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@359: * GNU General Public License for more details. fp@359: * fp@359: * You should have received a copy of the GNU General Public License fp@359: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@359: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@359: * fp@359: * The right to use EtherCAT Technology is granted and comes free of fp@359: * charge under condition of compatibility of product made by fp@359: * Licensee. People intending to distribute/sell products based on the fp@359: * code, have to sign an agreement to guarantee that products using fp@359: * software based on IgH EtherCAT master stay compatible with the actual fp@359: * EtherCAT specification (which are released themselves as an open fp@359: * standard) as the (only) precondition to have the right to use EtherCAT fp@359: * Technology, IP and trade marks. fp@359: * fp@359: *****************************************************************************/ fp@359: fp@359: /** fp@359: \file fp@359: EtherCAT XML Daemon. fp@359: */ fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: #include fp@359: #include fp@359: #include fp@359: #include fp@359: fp@359: #include fp@359: fp@359: #include fp@359: #include fp@359: using namespace std; fp@359: fp@359: #include "slave_device.hpp" fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: unsigned int sig_int_term = 0; fp@359: unsigned int sig_hangup = 0; fp@359: string xml_dir; fp@359: bool become_daemon = true; fp@359: fp@359: list slaveDevices; fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void parse_xml_file(const char *); fp@359: void parse_info(xmlDocPtr, xmlNodePtr); fp@359: void parse_descriptions(xmlDocPtr, xmlNodePtr); fp@359: void parse_devices(xmlDocPtr, xmlNodePtr); fp@359: fp@359: void read_xml_dir(); fp@359: void set_signal_handlers(); fp@359: void get_options(int, char *[]); fp@359: void print_usage(); fp@359: void signal_handler(int); fp@359: void init_daemon(); fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: int main(int argc, char *argv[]) fp@359: { fp@359: set_signal_handlers(); fp@359: get_options(argc, argv); fp@359: fp@359: read_xml_dir(); fp@359: fp@359: if (become_daemon) init_daemon(); fp@359: fp@359: openlog("ecxmld", LOG_PID, LOG_DAEMON); fp@359: syslog(LOG_INFO, "EtherCAT XML daemon starting up."); fp@359: fp@359: return 0; fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void read_xml_dir() fp@359: { fp@359: DIR *dir; fp@359: struct dirent *dir_ent; fp@359: string entry; fp@359: fp@359: if (!(dir = opendir(xml_dir.c_str()))) { fp@359: cerr << "ERROR: Failed to open XML directory \"" << xml_dir << "\"!" fp@359: << endl; fp@359: exit(1); fp@359: } fp@359: fp@359: while ((dir_ent = readdir(dir))) { fp@359: entry = dir_ent->d_name; fp@359: if (entry.size() < 4 fp@359: || entry.substr(entry.size() - 4) != ".xml") continue; fp@359: fp@359: parse_xml_file((xml_dir + "/" + entry).c_str()); fp@359: } fp@359: fp@359: // Verzeichnis schliessen fp@359: closedir(dir); fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void parse_xml_file(const char *xml_file) fp@359: { fp@359: xmlDocPtr doc; fp@359: xmlNodePtr cur; fp@359: fp@359: cout << xml_file << endl; fp@359: fp@359: if (!(doc = xmlParseFile(xml_file))) { fp@359: cerr << "ERROR: Parse error in document!" << endl; fp@359: return; fp@359: } fp@359: fp@359: if (!(cur = xmlDocGetRootElement(doc))) { fp@359: cout << "Empty document!" << endl; fp@359: xmlFreeDoc(doc); fp@359: return; fp@359: } fp@359: fp@359: if (xmlStrcmp(cur->name, (const xmlChar *) "EtherCATInfo")) { fp@359: cerr << "Document of the wrong type!" << endl; fp@359: xmlFreeDoc(doc); fp@359: return; fp@359: } fp@359: fp@359: parse_info(doc, cur); fp@359: xmlFreeDoc(doc); fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void parse_info(xmlDocPtr doc, xmlNodePtr cur) fp@359: { fp@359: cout << "info" << endl; fp@359: cur = cur->xmlChildrenNode; fp@359: fp@359: while (cur) { fp@359: if ((!xmlStrcmp(cur->name, (const xmlChar *) "Descriptions"))) { fp@359: parse_descriptions(doc, cur); fp@359: } fp@359: cur = cur->next; fp@359: } fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void parse_descriptions(xmlDocPtr doc, xmlNodePtr cur) fp@359: { fp@359: cout << "desc" << endl; fp@359: cur = cur->xmlChildrenNode; fp@359: fp@359: while (cur) { fp@359: if ((!xmlStrcmp(cur->name, (const xmlChar *) "Devices"))) { fp@359: parse_devices(doc, cur); fp@359: } fp@359: cur = cur->next; fp@359: } fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void parse_devices(xmlDocPtr doc, xmlNodePtr cur) fp@359: { fp@359: cout << "devices" << endl; fp@359: cur = cur->xmlChildrenNode; fp@359: fp@359: while (cur) { fp@359: if ((!xmlStrcmp(cur->name, (const xmlChar *) "Device"))) { fp@359: slaveDevices.push_back(SlaveDevice()); fp@359: slaveDevices.back().fromXml(doc, cur); fp@359: } fp@359: cur = cur->next; fp@359: } fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void get_options(int argc, char *argv[]) fp@359: { fp@359: int c; fp@359: fp@359: while (1) { fp@359: if ((c = getopt(argc, argv, "d:kh")) == -1) break; fp@359: fp@359: switch (c) { fp@359: case 'd': fp@359: xml_dir = optarg; fp@359: break; fp@359: fp@359: case 'k': fp@359: become_daemon = false; fp@359: break; fp@359: fp@359: case 'h': fp@359: print_usage(); fp@359: exit(0); fp@359: fp@359: default: fp@359: print_usage(); fp@359: exit(1); fp@359: } fp@359: } fp@359: fp@359: if (optind < argc) { fp@359: cerr << "ERROR: Too many arguments!" << endl; fp@359: print_usage(); fp@359: exit(1); fp@359: } fp@359: fp@359: if (xml_dir == "") { fp@359: cerr << "ERROR: XML directory not set!" << endl; fp@359: print_usage(); fp@359: exit(1); fp@359: } fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void print_usage() fp@359: { fp@359: cout << "Usage: ecxmld [OPTIONS]" << endl fp@359: << " -d DIR Set XML directory (MANDATORY)." << endl fp@359: << " -k Do not detach from console." << endl fp@359: << " -h Show this help." << endl; fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void signal_handler(int sig) fp@359: { fp@359: if (sig == SIGHUP) { fp@359: sig_hangup++; fp@359: } fp@359: else if (sig == SIGINT || sig == SIGTERM) { fp@359: sig_int_term++; fp@359: } fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void set_signal_handlers() fp@359: { fp@359: struct sigaction action; fp@359: fp@359: action.sa_handler = signal_handler; fp@359: sigemptyset(&action.sa_mask); fp@359: action.sa_flags = 0; fp@359: fp@359: sigaction(SIGHUP, &action, 0); fp@359: sigaction(SIGINT, &action, 0); fp@359: sigaction(SIGTERM, &action, 0); fp@359: } fp@359: fp@359: /*****************************************************************************/ fp@359: fp@359: void init_daemon() fp@359: { fp@359: pid_t pid; fp@359: fp@359: if ((pid = fork()) < 0) { fp@359: cerr << endl << "ERROR: fork() failed!" << endl << endl; fp@359: exit(1); fp@359: } fp@359: fp@359: if (pid) exit(0); fp@359: fp@359: if (setsid() == -1) { fp@359: cerr << "ERROR: Failed to become session leader!" << endl; fp@359: exit(1); fp@359: } fp@359: fp@359: if (chdir("/") < 0) { fp@359: cerr << "ERROR: Failed to change to file root!" << endl; fp@359: exit(1); fp@359: } fp@359: fp@359: umask(0); fp@359: fp@359: if (close(0) < 0) { fp@359: cerr << "WARNING: Failed to close STDIN!" << endl; fp@359: } fp@359: fp@359: if (close(1) < 0) { fp@359: cerr << "WARNING: Failed to close STDOUT!" << endl; fp@359: } fp@359: fp@359: if (close(2) < 0) { fp@359: cerr << "WARNING: Failed to close STDERR!" << endl; fp@359: } fp@359: } fp@359: fp@359: /*****************************************************************************/