lib/common.c
changeset 2589 2b9c78543663
parent 2414 f35c7c8e6591
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
     1 /******************************************************************************
     1 /******************************************************************************
     2  *
     2  *
     3  *  $Id$
     3  *  $Id$
     4  *
     4  *
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
     5  *  Copyright (C) 2006-2012  Florian Pose, Ingenieurgemeinschaft IgH
     6  *
     6  *
     7  *  This file is part of the IgH EtherCAT master userspace library.
     7  *  This file is part of the IgH EtherCAT master userspace library.
     8  *
     8  *
     9  *  The IgH EtherCAT master userspace library is free software; you can
     9  *  The IgH EtherCAT master userspace library is free software; you can
    10  *  redistribute it and/or modify it under the terms of the GNU Lesser General
    10  *  redistribute it and/or modify it under the terms of the GNU Lesser General
    31 #include <sys/types.h>
    31 #include <sys/types.h>
    32 #include <sys/stat.h>
    32 #include <sys/stat.h>
    33 #include <fcntl.h>
    33 #include <fcntl.h>
    34 #include <stdio.h>
    34 #include <stdio.h>
    35 #include <stdlib.h>
    35 #include <stdlib.h>
    36 #include <errno.h>
       
    37 #include <string.h>
    36 #include <string.h>
    38 #include <sys/ioctl.h>
       
    39 #include <sys/mman.h>
    37 #include <sys/mman.h>
    40 #include <unistd.h>
    38 #include <unistd.h>
    41 #include <sys/socket.h>
    39 #include <sys/socket.h>
    42 
    40 
       
    41 #include "ioctl.h"
    43 #include "master.h"
    42 #include "master.h"
    44 #include "master/ioctl.h"
       
    45 
    43 
    46 /*****************************************************************************/
    44 /*****************************************************************************/
    47 
    45 
    48 unsigned int ecrt_version_magic(void)
    46 unsigned int ecrt_version_magic(void)
    49 {
    47 {
    73 ec_master_t *ecrt_open_master(unsigned int master_index)
    71 ec_master_t *ecrt_open_master(unsigned int master_index)
    74 {
    72 {
    75     char path[MAX_PATH_LEN];
    73     char path[MAX_PATH_LEN];
    76     ec_master_t *master = NULL;
    74     ec_master_t *master = NULL;
    77     ec_ioctl_module_t module_data;
    75     ec_ioctl_module_t module_data;
       
    76     int ret;
    78 
    77 
    79     master = malloc(sizeof(ec_master_t));
    78     master = malloc(sizeof(ec_master_t));
    80     if (!master) {
    79     if (!master) {
    81         fprintf(stderr, "Failed to allocate memory.\n");
    80         fprintf(stderr, "Failed to allocate memory.\n");
    82         return 0;
    81         return 0;
    85     master->process_data = NULL;
    84     master->process_data = NULL;
    86     master->process_data_size = 0;
    85     master->process_data_size = 0;
    87     master->first_domain = NULL;
    86     master->first_domain = NULL;
    88     master->first_config = NULL;
    87     master->first_config = NULL;
    89 
    88 
    90     snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
    89     snprintf(path, MAX_PATH_LEN - 1,
       
    90 #ifdef USE_RTDM
       
    91             "EtherCAT%u",
       
    92 #else
       
    93             "/dev/EtherCAT%u",
       
    94 #endif
       
    95             master_index);
    91 
    96 
       
    97 #ifdef USE_RTDM
       
    98     master->fd = rt_dev_open(path, O_RDWR);
       
    99 #else
    92     master->fd = open(path, O_RDWR);
   100     master->fd = open(path, O_RDWR);
    93     if (master->fd == -1) {
   101 #endif
    94         fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
   102     if (EC_IOCTL_IS_ERROR(master->fd)) {
       
   103         fprintf(stderr, "Failed to open %s: %s\n", path,
       
   104                 strerror(EC_IOCTL_ERRNO(master->fd)));
    95         goto out_clear;
   105         goto out_clear;
    96     }
   106     }
    97 
   107 
    98     if (ioctl(master->fd, EC_IOCTL_MODULE, &module_data) < 0) {
   108     ret = ioctl(master->fd, EC_IOCTL_MODULE, &module_data);
       
   109     if (EC_IOCTL_IS_ERROR(ret)) {
    99         fprintf(stderr, "Failed to get module information from %s: %s\n",
   110         fprintf(stderr, "Failed to get module information from %s: %s\n",
   100                 path, strerror(errno));
   111                 path, strerror(EC_IOCTL_ERRNO(ret)));
   101         goto out_clear;
   112         goto out_clear;
   102     }
   113     }
   103 
   114 
   104     if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
   115     if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
   105         fprintf(stderr, "ioctl() version magic is differing:"
   116         fprintf(stderr, "ioctl() version magic is differing:"