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