Free all userspace library objects on releasing master.
authorFlorian Pose <fp@igh-essen.com>
Thu, 16 Sep 2010 08:35:21 +0200
changeset 1959 656f114153c2
parent 1946 55a6e3294dab
child 1960 6c4269dca1c2
Free all userspace library objects on releasing master.
TODO
lib/common.c
lib/domain.c
lib/domain.h
lib/master.c
lib/master.h
lib/sdo_request.c
lib/sdo_request.h
lib/slave_config.c
lib/slave_config.h
lib/voe_handler.c
lib/voe_handler.h
--- a/TODO	Sun Jun 06 23:21:09 2010 +0200
+++ b/TODO	Thu Sep 16 08:35:21 2010 +0200
@@ -13,8 +13,6 @@
 * Ethernet drivers:
     - Fix link detection in generic driver.
     - Add native drivers from 2.6.24 up to 2.6.31.
-* Finish library implementation.
-  - Remove stdio uses?
 * Rescan command.
 * Change SDO index at runtime for SDO request.
 * Output skipped datagrams again.
@@ -86,6 +84,7 @@
 * Simplify master fsm by introducing a common request state to handle external
   requests (replace write_sii, sdo_request, etc).
 * Write PDO mapping/assignment by default?
+* Remove stdio uses in userspace library?
 
 Smaller issues:
 
--- a/lib/common.c	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/common.c	Thu Sep 16 08:35:21 2010 +0200
@@ -55,9 +55,9 @@
     ec_master_t *master = ecrt_open_master(master_index);
     if (master) {
         if (ecrt_master_reserve(master) < 0) {
-            close(master->fd);
+            ec_master_clear(master);
             free(master);
-            master = 0;
+            master = NULL;
         }
     }
 
@@ -82,19 +82,21 @@
 
     master->process_data = NULL;
     master->process_data_size = 0;
+    master->first_domain = NULL;
+    master->first_config = NULL;
 
     snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
 
     master->fd = open(path, O_RDWR);
     if (master->fd == -1) {
         fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
-        goto out_free;
+        goto out_clear;
     }
 
     if (ioctl(master->fd, EC_IOCTL_MODULE, &module_data) < 0) {
         fprintf(stderr, "Failed to get module information from %s: %s\n",
                 path, strerror(errno));
-        goto out_close;
+        goto out_clear;
     }
 
     if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
@@ -102,14 +104,13 @@
                 " %s: %u, libethercat: %u.\n",
                 path, module_data.ioctl_version_magic,
                 EC_IOCTL_VERSION_MAGIC);
-        goto out_close;
+        goto out_clear;
     }
 
     return master;
 
-out_close:
-    close(master->fd);
-out_free:
+out_clear:
+    ec_master_clear(master);
     free(master);
     return 0;
 }
@@ -118,11 +119,7 @@
 
 void ecrt_release_master(ec_master_t *master)
 {
-    if (master->process_data)  {
-        munmap(master->process_data, master->process_data_size);
-    }
-
-    close(master->fd);
+    ec_master_clear(master);
     free(master);
 }
 
--- a/lib/domain.c	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/domain.c	Thu Sep 16 08:35:21 2010 +0200
@@ -47,6 +47,13 @@
 
 /*****************************************************************************/
 
+void ec_domain_clear(ec_domain_t *domain)
+{
+    // nothing to do
+}
+
+/*****************************************************************************/
+
 int ecrt_domain_reg_pdo_entry_list(ec_domain_t *domain,
         const ec_pdo_entry_reg_t *regs)
 {
--- a/lib/domain.h	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/domain.h	Thu Sep 16 08:35:21 2010 +0200
@@ -33,9 +33,14 @@
 /*****************************************************************************/
 
 struct ec_domain {
+    ec_domain_t *next;
     unsigned int index;
     ec_master_t *master;
     uint8_t *process_data;
 };
 
 /*****************************************************************************/
+
+void ec_domain_clear(ec_domain_t *);
+
+/*****************************************************************************/
--- a/lib/master.c	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/master.c	Thu Sep 16 08:35:21 2010 +0200
@@ -53,6 +53,51 @@
 
 /*****************************************************************************/
 
+void ec_master_clear(ec_master_t *master)
+{
+    ec_domain_t *d, *next_d;
+    ec_slave_config_t *c, *next_c;
+
+    if (master->process_data)  {
+        munmap(master->process_data, master->process_data_size);
+    }
+
+    d = master->first_domain;
+    while (d) {
+        next_d = d->next;
+        ec_domain_clear(d);
+        d = next_d;
+    }
+
+    c = master->first_config;
+    while (c) {
+        next_c = c->next;
+        ec_slave_config_clear(c);
+        c = next_c;
+    }
+
+    if (master->fd != -1) {
+        close(master->fd);
+    }
+}
+
+/*****************************************************************************/
+
+void ec_master_add_domain(ec_master_t *master, ec_domain_t *domain)
+{
+    if (master->first_domain) {
+        ec_domain_t *d = master->first_domain;
+        while (d->next) {
+            d = d->next;
+        }
+        d->next = domain;
+    } else {
+        master->first_domain = domain;
+    }
+}
+
+/*****************************************************************************/
+
 ec_domain_t *ecrt_master_create_domain(ec_master_t *master)
 {
     ec_domain_t *domain;
@@ -71,14 +116,33 @@
         return 0; 
     }
 
+    domain->next = NULL;
     domain->index = (unsigned int) index;
     domain->master = master;
     domain->process_data = NULL;
+
+    ec_master_add_domain(master, domain);
+
     return domain;
 }
 
 /*****************************************************************************/
 
+void ec_master_add_slave_config(ec_master_t *master, ec_slave_config_t *sc)
+{
+    if (master->first_config) {
+        ec_slave_config_t *c = master->first_config;
+        while (c->next) {
+            c = c->next;
+        }
+        c->next = sc;
+    } else {
+        master->first_config = sc;
+    }
+}
+
+/*****************************************************************************/
+
 ec_slave_config_t *ecrt_master_slave_config(ec_master_t *master,
         uint16_t alias, uint16_t position, uint32_t vendor_id,
         uint32_t product_code)
@@ -105,10 +169,16 @@
         return 0; 
     }
 
+    sc->next = NULL;
     sc->master = master;
     sc->index = data.config_index;
     sc->alias = alias;
     sc->position = position;
+    sc->first_sdo_request = NULL;
+    sc->first_voe_handler = NULL;
+
+    ec_master_add_slave_config(master, sc);
+
     return sc;
 }
 
--- a/lib/master.h	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/master.h	Thu Sep 16 08:35:21 2010 +0200
@@ -36,6 +36,13 @@
     int fd;
     uint8_t *process_data;
     size_t process_data_size;
+
+    ec_domain_t *first_domain;
+    ec_slave_config_t *first_config;
 };
 
 /*****************************************************************************/
+
+void ec_master_clear(ec_master_t *);
+
+/*****************************************************************************/
--- a/lib/sdo_request.c	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/sdo_request.c	Thu Sep 16 08:35:21 2010 +0200
@@ -43,8 +43,17 @@
 #include "slave_config.h"
 #include "master.h"
 
+/*****************************************************************************/
+
+void ec_sdo_request_clear(ec_sdo_request_t *req)
+{
+    if (req->data) {
+        free(req->data);
+    }
+}
+
 /*****************************************************************************
- * Realtime interface.
+ * Application interface.
  ****************************************************************************/
 
 void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
--- a/lib/sdo_request.h	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/sdo_request.h	Thu Sep 16 08:35:21 2010 +0200
@@ -33,6 +33,7 @@
 /*****************************************************************************/
 
 struct ec_sdo_request {
+    ec_sdo_request_t *next; /**< List header. */
     ec_slave_config_t *config; /**< Parent slave configuration. */
     unsigned int index; /**< Request index (identifier). */
     uint16_t sdo_index; /**< SDO index. */
@@ -43,3 +44,7 @@
 };
 
 /*****************************************************************************/
+
+void ec_sdo_request_clear(ec_sdo_request_t *);
+
+/*****************************************************************************/
--- a/lib/slave_config.c	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/slave_config.c	Thu Sep 16 08:35:21 2010 +0200
@@ -43,6 +43,29 @@
 
 /*****************************************************************************/
 
+void ec_slave_config_clear(ec_slave_config_t *sc)
+{
+    ec_sdo_request_t *r, *next_r;
+    ec_voe_handler_t *v, *next_v;
+
+    r = sc->first_sdo_request;
+    while (r) {
+        next_r = r->next;
+        ec_sdo_request_clear(r);
+        r = next_r;
+    }
+
+
+    v = sc->first_voe_handler;
+    while (v) {
+        next_v = v->next;
+        ec_voe_handler_clear(v);
+        v = next_v;
+    }
+}
+
+/*****************************************************************************/
+
 int ecrt_slave_config_sync_manager(ec_slave_config_t *sc, uint8_t sync_index,
         ec_direction_t dir, ec_watchdog_mode_t watchdog_mode)
 {
@@ -361,6 +384,22 @@
 
 /*****************************************************************************/
 
+void ec_slave_config_add_sdo_request(ec_slave_config_t *sc,
+        ec_sdo_request_t *req)
+{
+    if (sc->first_sdo_request) {
+        ec_sdo_request_t *r = sc->first_sdo_request;
+        while (r->next) {
+            r = r->next;
+        }
+        r->next = req;
+    } else {
+        sc->first_sdo_request = req;
+    }
+}
+
+/*****************************************************************************/
+
 ec_sdo_request_t *ecrt_slave_config_create_sdo_request(ec_slave_config_t *sc,
         uint16_t index, uint8_t subindex, size_t size)
 {
@@ -393,23 +432,42 @@
     if (ioctl(sc->master->fd, EC_IOCTL_SC_SDO_REQUEST, &data) == -1) {
         fprintf(stderr, "Failed to create SDO request: %s\n",
                 strerror(errno));
-        if (req->data)
-            free(req->data);
+        ec_sdo_request_clear(req);
         free(req);
         return NULL; 
     }
 
+    req->next = NULL;
     req->config = sc;
     req->index = data.request_index;
     req->sdo_index = data.sdo_index;
     req->sdo_subindex = data.sdo_subindex;
     req->data_size = size;
     req->mem_size = size;
+
+    ec_slave_config_add_sdo_request(sc, req);
+
     return req;
 }
 
 /*****************************************************************************/
 
+void ec_slave_config_add_voe_handler(ec_slave_config_t *sc,
+        ec_voe_handler_t *voe)
+{
+    if (sc->first_voe_handler) {
+        ec_voe_handler_t *v = sc->first_voe_handler;
+        while (v->next) {
+            v = v->next;
+        }
+        v->next = voe;
+    } else {
+        sc->first_voe_handler = voe;
+    }
+}
+
+/*****************************************************************************/
+
 ec_voe_handler_t *ecrt_slave_config_create_voe_handler(ec_slave_config_t *sc,
         size_t size)
 {
@@ -441,16 +499,19 @@
     if (ioctl(sc->master->fd, EC_IOCTL_SC_VOE, &data) == -1) {
         fprintf(stderr, "Failed to create VoE handler: %s\n",
                 strerror(errno));
-        if (voe->data)
-            free(voe->data);
+        ec_voe_handler_clear(voe);
         free(voe);
         return NULL; 
     }
 
+    voe->next = NULL;
     voe->config = sc;
     voe->index = data.voe_index;
     voe->data_size = size;
     voe->mem_size = size;
+
+    ec_slave_config_add_voe_handler(sc, voe);
+
     return voe;
 }
 
--- a/lib/slave_config.h	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/slave_config.h	Thu Sep 16 08:35:21 2010 +0200
@@ -33,10 +33,17 @@
 /*****************************************************************************/
 
 struct ec_slave_config {
+    ec_slave_config_t *next;
     ec_master_t *master;
     unsigned int index;
     uint16_t alias;
     uint16_t position;
+    ec_sdo_request_t *first_sdo_request;
+    ec_voe_handler_t *first_voe_handler;
 };
 
 /*****************************************************************************/
+
+void ec_slave_config_clear(ec_slave_config_t *);
+
+/*****************************************************************************/
--- a/lib/voe_handler.c	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/voe_handler.c	Thu Sep 16 08:35:21 2010 +0200
@@ -47,6 +47,14 @@
 
 /*****************************************************************************/
 
+void ec_voe_handler_clear(ec_voe_handler_t *voe)
+{
+    if (voe->data)
+        free(voe->data);
+}
+
+/*****************************************************************************/
+
 void ecrt_voe_handler_send_header(ec_voe_handler_t *voe, uint32_t vendor_id,
         uint16_t vendor_type)
 {
--- a/lib/voe_handler.h	Sun Jun 06 23:21:09 2010 +0200
+++ b/lib/voe_handler.h	Thu Sep 16 08:35:21 2010 +0200
@@ -33,6 +33,7 @@
 /*****************************************************************************/
 
 struct ec_voe_handler {
+    ec_voe_handler_t *next;
     ec_slave_config_t *config;
     unsigned int index;
     size_t data_size;
@@ -41,3 +42,7 @@
 };
 
 /*****************************************************************************/
+
+void ec_voe_handler_clear(ec_voe_handler_t *);
+
+/*****************************************************************************/