lib/sdo_request.c
changeset 2589 2b9c78543663
parent 1959 656f114153c2
--- a/lib/sdo_request.c	Thu Sep 06 14:21:02 2012 +0200
+++ b/lib/sdo_request.c	Mon Nov 03 15:20:05 2014 +0100
@@ -2,10 +2,10 @@
  *
  *  $Id$
  *
- *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
- *  
+ *  Copyright (C) 2006-2012  Florian Pose, Ingenieurgemeinschaft IgH
+ *
  *  This file is part of the IgH EtherCAT master userspace library.
- *  
+ *
  *  The IgH EtherCAT master userspace library is free software; you can
  *  redistribute it and/or modify it under the terms of the GNU Lesser General
  *  Public License as published by the Free Software Foundation; version 2.1
@@ -19,9 +19,9 @@
  *  You should have received a copy of the GNU Lesser General Public License
  *  along with the IgH EtherCAT master userspace library. If not, see
  *  <http://www.gnu.org/licenses/>.
- *  
+ *
  *  ---
- *  
+ *
  *  The license mentioned above concerns the source code only. Using the
  *  EtherCAT technology and brand is only permitted in compliance with the
  *  industrial property and similar rights of Beckhoff Automation GmbH.
@@ -35,11 +35,10 @@
 /*****************************************************************************/
 
 #include <stdio.h>
-#include <errno.h>
 #include <string.h>
 
+#include "ioctl.h"
 #include "sdo_request.h"
-#include "master/ioctl.h"
 #include "slave_config.h"
 #include "master.h"
 
@@ -56,18 +55,40 @@
  * Application interface.
  ****************************************************************************/
 
+void ecrt_sdo_request_index(ec_sdo_request_t *req, uint16_t index,
+        uint8_t subindex)
+{
+    ec_ioctl_sdo_request_t data;
+    int ret;
+
+    data.config_index = req->config->index;
+    data.request_index = req->index;
+    data.sdo_index = index;
+    data.sdo_subindex = subindex;
+
+    ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_INDEX, &data);
+    if (EC_IOCTL_IS_ERROR(ret)) {
+        fprintf(stderr, "Failed to set SDO request index/subindex: %s\n",
+                strerror(EC_IOCTL_ERRNO(ret)));
+    }
+}
+
+/*****************************************************************************/
+
 void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
 {
     ec_ioctl_sdo_request_t data;
+    int ret;
 
     data.config_index = req->config->index;
     data.request_index = req->index;
     data.timeout = timeout;
 
-    if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_TIMEOUT,
-                &data) == -1)
+    ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_TIMEOUT, &data);
+    if (EC_IOCTL_IS_ERROR(ret)) {
         fprintf(stderr, "Failed to set SDO request timeout: %s\n",
-                strerror(errno));
+                strerror(EC_IOCTL_ERRNO(ret)));
+    }
 }
 
 /*****************************************************************************/
@@ -89,34 +110,32 @@
 ec_request_state_t ecrt_sdo_request_state(ec_sdo_request_t *req)
 {
     ec_ioctl_sdo_request_t data;
+    int ret;
 
     data.config_index = req->config->index;
     data.request_index = req->index;
 
-    if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_STATE,
-                &data) == -1)
+    ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_STATE, &data);
+    if (EC_IOCTL_IS_ERROR(ret)) {
         fprintf(stderr, "Failed to get SDO request state: %s\n",
-                strerror(errno));
+                strerror(EC_IOCTL_ERRNO(ret)));
+        return EC_REQUEST_ERROR;
+    }
 
     if (data.size) { // new data waiting to be copied
         if (req->mem_size < data.size) {
-            if (req->data)
-                free(req->data);
-            req->data = malloc(data.size);
-            if (!req->data) {
-                req->mem_size = 0;
-                fprintf(stderr, "Failed to allocate %u bytes of SDO data"
-                        " memory!\n", data.size);
-                return EC_REQUEST_ERROR;
-            }
-            req->mem_size = data.size;
+            fprintf(stderr, "Received %zu bytes do not fit info SDO data"
+                    " memory (%zu bytes)!\n", data.size, req->mem_size);
+            return EC_REQUEST_ERROR;
         }
 
         data.data = req->data;
 
-        if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_DATA,
-                    &data) == -1) {
-            fprintf(stderr, "Failed to get SDO data: %s\n", strerror(errno));
+        ret = ioctl(req->config->master->fd,
+                EC_IOCTL_SDO_REQUEST_DATA, &data);
+        if (EC_IOCTL_IS_ERROR(ret)) {
+            fprintf(stderr, "Failed to get SDO data: %s\n",
+                    strerror(EC_IOCTL_ERRNO(ret)));
             return EC_REQUEST_ERROR;
         }
         req->data_size = data.size;
@@ -130,14 +149,16 @@
 void ecrt_sdo_request_read(ec_sdo_request_t *req)
 {
     ec_ioctl_sdo_request_t data;
+    int ret;
 
     data.config_index = req->config->index;
     data.request_index = req->index;
 
-    if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_READ,
-                &data) == -1)
+    ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_READ, &data);
+    if (EC_IOCTL_IS_ERROR(ret)) {
         fprintf(stderr, "Failed to command an SDO read operation : %s\n",
-                strerror(errno));
+                strerror(EC_IOCTL_ERRNO(ret)));
+    }
 }
 
 /*****************************************************************************/
@@ -145,16 +166,18 @@
 void ecrt_sdo_request_write(ec_sdo_request_t *req)
 {
     ec_ioctl_sdo_request_t data;
+    int ret;
 
     data.config_index = req->config->index;
     data.request_index = req->index;
     data.data = req->data;
     data.size = req->data_size;
 
-    if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_WRITE,
-                &data) == -1)
+    ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_WRITE, &data);
+    if (EC_IOCTL_IS_ERROR(ret)) {
         fprintf(stderr, "Failed to command an SDO write operation : %s\n",
-                strerror(errno));
+                strerror(EC_IOCTL_ERRNO(ret)));
+    }
 }
 
 /*****************************************************************************/