lib/common.c
author Florian Pose <fp@igh-essen.com>
Thu, 02 Oct 2008 07:14:00 +0000
changeset 1243 13cb18b91627
parent 1242 632a6b91f8e4
child 1244 0b70040d3daa
permissions -rw-r--r--
Added changes since 1.4.0-rc2.
1242
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  and/or modify it under the terms of the GNU General Public License
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  The IgH EtherCAT Master is distributed in the hope that it will be
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
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
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <sys/types.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <sys/stat.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <fcntl.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <stdio.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#include <stdlib.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
#include <errno.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
#include <string.h>
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include "include/ecrt.h"
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include "master/ioctl.h"
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
struct ec_master {
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
    int fd;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
    void *handle;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
};
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
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
#define MAX_PATH_LEN 64
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
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
    55
{
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    char path[MAX_PATH_LEN];
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
    ec_master_t *master;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    ec_ioctl_request_t data;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
    master = malloc(sizeof(ec_master_t));
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    if (!master) {
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
        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
    63
        return 0;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
    }
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    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
    67
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
    master->fd = open(path, O_RDWR);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
    if (master->fd == -1) {
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
        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
    71
        free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
        return 0;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
    }
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
    if (ioctl(master->fd, EC_IOCTL_REQUEST, &data) == -1) {
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        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
    77
                master_index, strerror(errno));
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
        close(master->fd);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
        free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
        return 0; 
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    }
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
    master->handle = data.handle;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    return master;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
}
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
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
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
    90
{
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
    close(master->fd);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    free(master);
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
}
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
/*****************************************************************************/
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
unsigned int ecrt_version_magic(void)
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
{
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    return ECRT_VERSION_MAGIC;
632a6b91f8e4 First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
}
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
/*****************************************************************************/