swconfig: Bugfix switch_port uci option parsing
[openwrt/openwrt.git] / package / network / config / swconfig / src / cli.c
index 2eb73bea38dbfc20226a74e8cd27f10f8188da2d..eab6c64742e96d1453c62b07d40b4824db6ed9fb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * swconfig.c: Switch configuration utility
  *
- * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  * Copyright (C) 2010 Martin Mares <mj@ucw.cz>
  *
  * This program is free software; you can redistribute it and/or
@@ -84,9 +84,27 @@ list_attributes(struct switch_dev *dev)
        print_attrs(dev->port_ops);
 }
 
+static const char *
+speed_str(int speed)
+{
+       switch (speed) {
+       case 10:
+               return "10baseT";
+       case 100:
+               return "100baseT";
+       case 1000:
+               return "1000baseT";
+       default:
+               break;
+       }
+
+       return "unknown";
+}
+
 static void
 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
 {
+       struct switch_port_link *link;
        int i;
 
        switch (attr->type) {
@@ -104,6 +122,21 @@ print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
                                 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
                }
                break;
+       case SWITCH_TYPE_LINK:
+               link = val->value.link;
+               if (link->link)
+                       printf("port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
+                               val->port_vlan,
+                               speed_str(link->speed),
+                               link->duplex ? "full" : "half",
+                               link->tx_flow ? "txflow " : "",
+                               link->rx_flow ? "rxflow " : "",
+                               link->eee & SWLIB_LINK_FLAG_EEE_100BASET ? "eee100 " : "",
+                               link->eee & SWLIB_LINK_FLAG_EEE_1000BASET ? "eee1000 " : "",
+                               link->aneg ? "auto" : "");
+               else
+                       printf("port:%d link:down", val->port_vlan);
+               break;
        default:
                printf("?unknown-type?");
        }
@@ -178,7 +211,6 @@ swconfig_load_uci(struct switch_dev *dev, const char *name)
 {
        struct uci_context *ctx;
        struct uci_package *p = NULL;
-       struct uci_element *e;
        int ret = -1;
 
        ctx = uci_alloc_context();
@@ -206,7 +238,6 @@ int main(int argc, char **argv)
        struct switch_dev *dev;
        struct switch_attr *a;
        struct switch_val val;
-       int err;
        int i;
 
        int cmd = CMD_NONE;
@@ -272,7 +303,7 @@ int main(int argc, char **argv)
 
        dev = swlib_connect(cdev);
        if (!dev) {
-               fprintf(stderr, "Failed to connect to the switch\n");
+               fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n");
                return 1;
        }
 
@@ -289,6 +320,7 @@ int main(int argc, char **argv)
                if(!a)
                {
                        fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
+                       retval = -1;
                        goto out;
                }
        }
@@ -303,10 +335,10 @@ int main(int argc, char **argv)
                if(cvlan > -1)
                        cport = cvlan;
 
-               if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
+               retval = swlib_set_attr_string(dev, a, cport, cvalue);
+               if (retval < 0)
                {
-                       fprintf(stderr, "failed\n");
-                       retval = -1;
+                       nl_perror(-retval, "Failed to set attribute");
                        goto out;
                }
                break;
@@ -315,10 +347,10 @@ int main(int argc, char **argv)
                        val.port_vlan = cvlan;
                if(cport > -1)
                        val.port_vlan = cport;
-               if(swlib_get_attr(dev, a, &val) < 0)
+               retval = swlib_get_attr(dev, a, &val);
+               if (retval < 0)
                {
-                       fprintf(stderr, "failed\n");
-                       retval = -1;
+                       nl_perror(-retval, "Failed to get attribute");
                        goto out;
                }
                print_attr_val(a, &val);
@@ -351,5 +383,5 @@ int main(int argc, char **argv)
 
 out:
        swlib_free_all(dev);
-       return 0;
+       return retval;
 }