lib/voe_handler.c
changeset 1264 e7882f246d7a
child 1266 dd1f501c4070
equal deleted inserted replaced
1263:f44720defbc5 1264:e7882f246d7a
       
     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 /** \file
       
    35  * Vendor-specific-over-EtherCAT protocol handler functions.
       
    36  */
       
    37 
       
    38 /*****************************************************************************/
       
    39 
       
    40 #include <stdio.h>
       
    41 
       
    42 #include "voe_handler.h"
       
    43 #include "slave_config.h"
       
    44 #include "master.h"
       
    45 #include "master/ioctl.h"
       
    46 
       
    47 /*****************************************************************************/
       
    48 
       
    49 void ecrt_voe_handler_send_header(ec_voe_handler_t *voe, uint32_t vendor_id,
       
    50         uint16_t vendor_type)
       
    51 {
       
    52     ec_ioctl_voe_t data;
       
    53 
       
    54     data.config_index = voe->config->index;
       
    55     data.voe_index = voe->index;
       
    56     data.vendor_id = &vendor_id;
       
    57     data.vendor_type = &vendor_type;
       
    58 
       
    59     if (ioctl(voe->config->master->fd, EC_IOCTL_VOE_SEND_HEADER, &data) == -1) {
       
    60         fprintf(stderr, "Failed to set VoE send header.\n");
       
    61     }
       
    62 }
       
    63 
       
    64 /*****************************************************************************/
       
    65 
       
    66 void ecrt_voe_handler_received_header(const ec_voe_handler_t *voe,
       
    67         uint32_t *vendor_id, uint16_t *vendor_type)
       
    68 {
       
    69     ec_ioctl_voe_t data;
       
    70 
       
    71     data.config_index = voe->config->index;
       
    72     data.voe_index = voe->index;
       
    73     data.vendor_id = vendor_id;
       
    74     data.vendor_type = vendor_type;
       
    75 
       
    76     if (ioctl(voe->config->master->fd, EC_IOCTL_VOE_REC_HEADER, &data) == -1) {
       
    77         fprintf(stderr, "Failed to get received VoE header.\n");
       
    78     }
       
    79 }
       
    80 
       
    81 /*****************************************************************************/
       
    82 
       
    83 uint8_t *ecrt_voe_handler_data(ec_voe_handler_t *voe)
       
    84 {
       
    85     return voe->data;
       
    86 }
       
    87 
       
    88 /*****************************************************************************/
       
    89 
       
    90 size_t ecrt_voe_handler_data_size(const ec_voe_handler_t *voe)
       
    91 {
       
    92     return voe->data_size;
       
    93 }
       
    94 
       
    95 /*****************************************************************************/
       
    96 
       
    97 void ecrt_voe_handler_read(ec_voe_handler_t *voe)
       
    98 {
       
    99     ec_ioctl_voe_t data;
       
   100 
       
   101     data.config_index = voe->config->index;
       
   102     data.voe_index = voe->index;
       
   103 
       
   104     if (ioctl(voe->config->master->fd, EC_IOCTL_VOE_READ, &data) == -1) {
       
   105         fprintf(stderr, "Failed to initiate VoE reading.\n");
       
   106     }
       
   107 }
       
   108 
       
   109 /*****************************************************************************/
       
   110 
       
   111 void ecrt_voe_handler_write(ec_voe_handler_t *voe, size_t size)
       
   112 {
       
   113     ec_ioctl_voe_t data;
       
   114 
       
   115     data.config_index = voe->config->index;
       
   116     data.voe_index = voe->index;
       
   117     data.size = size;
       
   118     data.data = voe->data;
       
   119 
       
   120     if (ioctl(voe->config->master->fd, EC_IOCTL_VOE_WRITE, &data) == -1) {
       
   121         fprintf(stderr, "Failed to initiate VoE reading.\n");
       
   122     }
       
   123 }
       
   124 
       
   125 /*****************************************************************************/
       
   126 
       
   127 ec_request_state_t ecrt_voe_handler_execute(ec_voe_handler_t *voe)
       
   128 {
       
   129     ec_ioctl_voe_t data;
       
   130 
       
   131     data.config_index = voe->config->index;
       
   132     data.voe_index = voe->index;
       
   133     data.size = 0;
       
   134 
       
   135     if (ioctl(voe->config->master->fd, EC_IOCTL_VOE_EXEC, &data) == -1) {
       
   136         fprintf(stderr, "Failed to initiate VoE reading.\n");
       
   137         return EC_REQUEST_ERROR;
       
   138     }
       
   139 
       
   140     if (data.size) { // new data waiting to be copied
       
   141         if (voe->mem_size < data.size) {
       
   142             if (voe->data)
       
   143                 free(voe->data);
       
   144             voe->data = malloc(data.size);
       
   145             if (!voe->data) {
       
   146                 voe->mem_size = 0;
       
   147                 fprintf(stderr, "Failed to allocate VoE data memory!");
       
   148                 return EC_REQUEST_ERROR;
       
   149             }
       
   150             voe->mem_size = data.size;
       
   151         }
       
   152 
       
   153         if (ioctl(voe->config->master->fd, EC_IOCTL_VOE_DATA, &data) == -1) {
       
   154             fprintf(stderr, "Failed to get VoE data!\n");
       
   155             return EC_REQUEST_ERROR;
       
   156         }
       
   157         voe->data_size = data.size;
       
   158     }
       
   159 
       
   160     return data.state;
       
   161 }
       
   162 
       
   163 /*****************************************************************************/