lib/common.c
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 14 Sep 2018 17:59:33 +0200
branchstable-1.5
changeset 2710 05359dee3578
parent 2703 045624f7f4c3
permissions -rw-r--r--
rtdmnet module should follow same (un)loading rules as generic module
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
2383
572948e8b2ed Removed whitespace.
Florian Pose <fp@igh-essen.com>
parents: 1975
diff changeset
     2
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
     3
 *  $Id$
2383
572948e8b2ed Removed whitespace.
Florian Pose <fp@igh-essen.com>
parents: 1975
diff changeset
     4
 *
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
     5
 *  Copyright (C) 2006-2012  Florian Pose, Ingenieurgemeinschaft IgH
2383
572948e8b2ed Removed whitespace.
Florian Pose <fp@igh-essen.com>
parents: 1975
diff changeset
     6
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
     7
 *  This file is part of the IgH EtherCAT master userspace library.
2383
572948e8b2ed Removed whitespace.
Florian Pose <fp@igh-essen.com>
parents: 1975
diff changeset
     8
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
     9
 *  The IgH EtherCAT master userspace library is free software; you can
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    10
 *  redistribute it and/or modify it under the terms of the GNU Lesser General
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    11
 *  Public License as published by the Free Software Foundation; version 2.1
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    12
 *  of the License.
1287
cc7b679c74e9 Improved LGPL headers.
Florian Pose <fp@igh-essen.com>
parents: 1266
diff changeset
    13
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    14
 *  The IgH EtherCAT master userspace library is distributed in the hope that
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    15
 *  it will be useful, but WITHOUT ANY WARRANTY; without even the implied
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    16
 *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    17
 *  GNU Lesser General Public License for more details.
1287
cc7b679c74e9 Improved LGPL headers.
Florian Pose <fp@igh-essen.com>
parents: 1266
diff changeset
    18
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    19
 *  You should have received a copy of the GNU Lesser General Public License
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    20
 *  along with the IgH EtherCAT master userspace library. If not, see
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1287
diff changeset
    21
 *  <http://www.gnu.org/licenses/>.
2383
572948e8b2ed Removed whitespace.
Florian Pose <fp@igh-essen.com>
parents: 1975
diff changeset
    22
 *
1363
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
    23
 *  ---
2383
572948e8b2ed Removed whitespace.
Florian Pose <fp@igh-essen.com>
parents: 1975
diff changeset
    24
 *
1363
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
    25
 *  The license mentioned above concerns the source code only. Using the
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
    26
 *  EtherCAT technology and brand is only permitted in compliance with the
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
    27
 *  industrial property and similar rights of Beckhoff Automation GmbH.
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#include <sys/types.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include <sys/stat.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include <fcntl.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <stdio.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <stdlib.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <string.h>
1258
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    37
#include <sys/mman.h>
1975
8e173dddd183 Improved compiling on 2.6.34 (thanks to Malcolm Lewis).
Florian Pose <fp@igh-essen.com>
parents: 1959
diff changeset
    38
#include <unistd.h>
8e173dddd183 Improved compiling on 2.6.34 (thanks to Malcolm Lewis).
Florian Pose <fp@igh-essen.com>
parents: 1959
diff changeset
    39
#include <sys/socket.h>
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    41
#include "ioctl.h"
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    42
#include "master.h"
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    44
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    45
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    46
unsigned int ecrt_version_magic(void)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    47
{
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    48
    return ECRT_VERSION_MAGIC;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    49
}
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
/*****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
1497
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    53
ec_master_t *ecrt_request_master(unsigned int master_index)
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    54
{
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    55
    ec_master_t *master = ecrt_open_master(master_index);
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    56
    if (master) {
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    57
        if (ecrt_master_reserve(master) < 0) {
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
    58
            ec_master_clear(master);
1497
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    59
            free(master);
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
    60
            master = NULL;
1497
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    61
        }
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    62
    }
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    63
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    64
    return master;
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    65
}
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    66
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    67
/*****************************************************************************/
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    68
1495
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    69
#define MAX_PATH_LEN 64
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    70
1497
c9308eb34c0e Separated master requesting into open and reserve for userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
    71
ec_master_t *ecrt_open_master(unsigned int master_index)
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
{
1495
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    73
    char path[MAX_PATH_LEN];
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
    74
    ec_master_t *master = NULL;
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
    75
    ec_ioctl_module_t module_data;
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    76
    int ret;
1495
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    77
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    78
    master = malloc(sizeof(ec_master_t));
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    79
    if (!master) {
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    80
        fprintf(stderr, "Failed to allocate memory.\n");
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    81
        return 0;
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    82
    }
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    83
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    84
    master->process_data = NULL;
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    85
    master->process_data_size = 0;
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
    86
    master->first_domain = NULL;
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
    87
    master->first_config = NULL;
1495
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    88
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    89
    snprintf(path, MAX_PATH_LEN - 1,
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    90
            "/dev/EtherCAT%u",
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    91
            master_index);
1495
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    92
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
    93
    master->fd = open(path, O_RDWR);
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    94
    if (EC_IOCTL_IS_ERROR(master->fd)) {
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    95
        fprintf(stderr, "Failed to open %s: %s\n", path,
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
    96
                strerror(EC_IOCTL_ERRNO(master->fd)));
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
    97
        goto out_clear;
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
    98
    }
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
    99
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
   100
    ret = ioctl(master->fd, EC_IOCTL_MODULE, &module_data);
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
   101
    if (EC_IOCTL_IS_ERROR(ret)) {
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   102
        fprintf(stderr, "Failed to get module information from %s: %s\n",
2433
3bdd7a747fae Re-worked and seamlessly integrated RTDM interface.
Florian Pose <fp@igh-essen.com>
parents: 2383
diff changeset
   103
                path, strerror(EC_IOCTL_ERRNO(ret)));
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
   104
        goto out_clear;
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   105
    }
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   106
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   107
    if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   108
        fprintf(stderr, "ioctl() version magic is differing:"
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   109
                " %s: %u, libethercat: %u.\n",
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   110
                path, module_data.ioctl_version_magic,
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   111
                EC_IOCTL_VERSION_MAGIC);
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
   112
        goto out_clear;
1495
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
   113
    }
88f242b722ce Reverted accidential checkin.
Florian Pose <fp@igh-essen.com>
parents: 1494
diff changeset
   114
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
    return master;
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   116
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
   117
out_clear:
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
   118
    ec_master_clear(master);
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   119
    free(master);
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1497
diff changeset
   120
    return 0;
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
}
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
/*****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
void ecrt_release_master(ec_master_t *master)
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
{
1959
656f114153c2 Free all userspace library objects on releasing master.
Florian Pose <fp@igh-essen.com>
parents: 1826
diff changeset
   127
    ec_master_clear(master);
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
}
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
/*****************************************************************************/