lib/common.c
author Florian Pose <fp@igh-essen.com>
Wed, 22 Oct 2008 08:26:05 +0000
changeset 1292 ea1bcfb8d631
parent 1287 cc7b679c74e9
child 1326 ef907b0b5125
permissions -rw-r--r--
Avouded 'you'.
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
1266
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     2
 *  
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     3
 * $Id$
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     4
 * 
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     5
 * Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     6
 * 
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     7
 * This file is part of the IgH EtherCAT master userspace library.
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
     8
 * 
1287
cc7b679c74e9 Improved LGPL headers.
Florian Pose <fp@igh-essen.com>
parents: 1266
diff changeset
     9
 * The IgH EtherCAT master userspace library is free software; you can
1266
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    10
 * redistribute it and/or modify it under the terms of the GNU Lesser General
1287
cc7b679c74e9 Improved LGPL headers.
Florian Pose <fp@igh-essen.com>
parents: 1266
diff changeset
    11
 * Public License as published by the Free Software Foundation; version 2.1 of
1266
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    12
 * the License.
1287
cc7b679c74e9 Improved LGPL headers.
Florian Pose <fp@igh-essen.com>
parents: 1266
diff changeset
    13
 *
1266
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    14
 * The IgH EtherCAT master userspace library is distributed in the hope that
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    15
 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    16
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
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
 *
1266
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    19
 * You should have received a copy of the GNU Lesser General Public License
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    20
 * along with the IgH EtherCAT master userspace library. If not, see
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    21
 * <http://www.gnu.org/licenses/>.
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    22
 * 
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    23
 * The right to use EtherCAT Technology is granted and comes free of charge
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    24
 * under condition of compatibility of product made by Licensee. People
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    25
 * intending to distribute/sell products based on the code, have to sign an
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    26
 * agreement to guarantee that products using software based on IgH EtherCAT
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    27
 * master stay compatible with the actual EtherCAT specification (which are
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    28
 * released themselves as an open standard) as the (only) precondition to have
dd1f501c4070 Added LGPL licence headers to userspace library.
Florian Pose <fp@igh-essen.com>
parents: 1258
diff changeset
    29
 * the right to use EtherCAT Technology, IP and trade marks.
1242
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
 *****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include <sys/types.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <sys/stat.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <fcntl.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <stdio.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <stdlib.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#include <errno.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
#include <string.h>
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    40
#include <sys/ioctl.h>
1258
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    41
#include <sys/mman.h>
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    43
#include "master.h"
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#include "master/ioctl.h"
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    46
/*****************************************************************************/
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
unsigned int ecrt_version_magic(void)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    49
{
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    50
    return ECRT_VERSION_MAGIC;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    51
}
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
/*****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
#define MAX_PATH_LEN 64
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
ec_master_t *ecrt_request_master(unsigned int master_index)
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
{
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    char path[MAX_PATH_LEN];
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
    ec_master_t *master;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    master = malloc(sizeof(ec_master_t));
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    if (!master) {
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
        fprintf(stderr, "Failed to allocate memory.\n");
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
        return 0;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    }
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
1258
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    68
    master->process_data = NULL;
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    69
    master->process_data_size = 0;
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    70
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
    snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
    master->fd = open(path, O_RDWR);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    if (master->fd == -1) {
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
        return 0;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
    }
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents: 1242
diff changeset
    80
    if (ioctl(master->fd, EC_IOCTL_REQUEST, NULL) == -1) {
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
        fprintf(stderr, "Failed to request master %u: %s\n",
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
                master_index, strerror(errno));
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
        close(master->fd);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
        free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
        return 0; 
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    }
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    return master;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
}
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
/*****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
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
    94
{
1258
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    95
    if (master->process_data)  {
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    96
        munmap(master->process_data, master->process_data_size);
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    97
    }
900f1124e8f8 Memory-mapped process data.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    98
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    close(master->fd);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
    free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
}
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
/*****************************************************************************/