ccollins476ad closed pull request #1501: Misc. battery fixes
URL: https://github.com/apache/mynewt-core/pull/1501
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/battery/src/battery.c b/hw/battery/src/battery.c
index dd9efd20a3..c8d4fc0a70 100644
--- a/hw/battery/src/battery.c
+++ b/hw/battery/src/battery.c
@@ -150,23 +150,6 @@ battery_pkg_init(void)
 #endif
 }
 
-int
-battery_mgr_register(struct battery *battery)
-{
-    int i;
-
-    os_mutex_pend(&battery_manager.bm_lock, OS_WAIT_FOREVER);
-
-    for (i = 0; i < BATTERY_MAX_COUNT; ++i) {
-        if (battery_manager.bm_batteries[i] == NULL) {
-            battery_manager.bm_batteries[i] = battery;
-        }
-    }
-    os_mutex_release(&battery_manager.bm_lock);
-
-    return 0;
-}
-
 int
 battery_mgr_get_battery_count(void)
 {
@@ -808,11 +791,14 @@ battery_init(struct os_dev *dev, void *arg)
 
     OS_DEV_SETHANDLERS(dev, battery_open, battery_close);
 
+    os_mutex_pend(&battery_manager.bm_lock, OS_WAIT_FOREVER);
     for (i = 0; i < BATTERY_MAX_COUNT; ++i) {
         if (battery_manager.bm_batteries[i] == NULL) {
             break;
         }
     }
+    os_mutex_release(&battery_manager.bm_lock);
+
     assert(i < BATTERY_MAX_COUNT);
     battery_manager.bm_batteries[i] = bat;
 
diff --git a/hw/battery/src/battery_shell.c b/hw/battery/src/battery_shell.c
index 58acc1d816..6fb411af29 100644
--- a/hw/battery/src/battery_shell.c
+++ b/hw/battery/src/battery_shell.c
@@ -29,8 +29,6 @@
 
 static int bat_compat_cmd(int argc, char **argv);
 
-struct os_dev *bat;
-
 #if MYNEWT_VAL(SHELL_CMD_HELP)
 #define HELP(a) &(a)
 
@@ -183,15 +181,38 @@ static void print_property(const struct battery_property 
*prop)
     }
 }
 
+static struct os_dev *
+battery_shell_open_dev(void)
+{
+    struct os_dev *bat;
+
+    bat = os_dev_open("battery_0", 0, NULL);
+    if (bat == NULL) {
+        console_printf("Failed to open battery device\n");
+    }
+
+    return bat;
+}
+
 static int cmd_bat_read(int argc, char **argv)
 {
-    int rc = 0;
+    int rc;
     int maxp;
     int i;
     struct battery_property *prop;
+    struct os_dev *bat;
+
+    bat = NULL;
 
     if (argc < 2) {
         console_printf("Invalid number of arguments, use read <prop>\n");
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    bat = battery_shell_open_dev();
+    if (bat == NULL) {
+        rc = SYS_ENODEV;
         goto err;
     }
 
@@ -202,6 +223,7 @@ static int cmd_bat_read(int argc, char **argv)
             battery_prop_get_value(prop);
             if (!prop->bp_valid) {
                 console_printf("Error reading property\n");
+                rc = SYS_EIO;
                 goto err;
             }
             print_property(prop);
@@ -211,17 +233,25 @@ static int cmd_bat_read(int argc, char **argv)
             prop = battery_find_property_by_name(bat, argv[i]);
             if (prop == NULL) {
                 console_printf("Invalid property name %s\n", argv[i]);
+                rc = SYS_EINVAL;
                 goto err;
             }
             battery_prop_get_value(prop);
             if (!prop->bp_valid) {
                 console_printf("Error reading property\n");
+                rc = SYS_EIO;
                 goto err;
             }
             print_property(prop);
         }
     }
+
+    rc = 0;
+
 err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
     return rc;
 }
 
@@ -245,81 +275,140 @@ get_min_max(const struct battery_property *prop, long 
long *min, long long *max)
 static int
 cmd_bat_write(int argc, char ** argv)
 {
-    int rc = 0;
+    int rc;
     long long min;
     long long max;
     struct battery_property *prop;
+    struct os_dev *bat;
     long long int val;
 
+    bat = NULL;
+
     if (argc < 3) {
         console_printf("Invalid number of arguments, use write <prop> 
<value>\n");
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    bat = battery_shell_open_dev();
+    if (bat == NULL) {
+        rc = SYS_ENODEV;
         goto err;
     }
 
     prop = battery_find_property_by_name(bat, argv[1]);
     if (prop == NULL) {
         console_printf("Invalid property name %s\n", argv[1]);
+        rc = SYS_EINVAL;
         goto err;
     }
+
     if (get_min_max(prop, &min, &max)) {
         console_printf("Property %s can not be set\n", argv[1]);
+        rc = SYS_EIO;
         goto err;
     }
+
     val = parse_ll_bounds(argv[2], min, max, &rc);
     if (rc) {
         console_printf("Property value not in range <%lld, %lld>\n", min, max);
         rc = 0;
         goto err;
     }
+
     if (prop->bp_type == BATTERY_PROP_VOLTAGE_NOW &&
             (prop->bp_flags & BATTERY_PROPERTY_FLAGS_ALARM_THREASH) != 0) {
-        battery_prop_set_value_uint32(prop, (uint32_t)val);
+        rc = battery_prop_set_value_uint32(prop, (uint32_t)val);
     } else if (prop->bp_type == BATTERY_PROP_TEMP_NOW &&
             (prop->bp_flags & BATTERY_PROPERTY_FLAGS_ALARM_THREASH) != 0) {
-        battery_prop_set_value_float(prop, val);
+        rc = battery_prop_set_value_float(prop, val);
     } else {
         console_printf("Property %s can't be written!\n", argv[1]);
+        rc = SYS_EINVAL;
+        goto err;
     }
     if (!prop->bp_valid) {
         console_printf("Error writing property!\n");
+        rc = SYS_EIO;
         goto err;
     }
+
+    rc = 0;
+
 err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
     return rc;
 }
 
 static int cmd_bat_list(int argc, char **argv)
 {
     int i;
-    int max = battery_get_property_count(bat, NULL);
+    int max;
     struct battery_property *prop;
+    struct os_dev *bat;
     char name[20];
+    int rc;
+
+    bat = NULL;
 
+    bat = battery_shell_open_dev();
+    if (bat == NULL) {
+        rc = SYS_ENODEV;
+        goto err;
+    }
+
+    max = battery_get_property_count(bat, NULL);
     for (i = 0; i < max; ++i) {
         prop = battery_enum_property(bat, NULL, i);
         if (prop) {
             console_printf(" %s\n", battery_prop_get_name(prop, name, 20));
         }
     }
-    return 0;
+
+    rc = 0;
+
+err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
+    return rc;
 }
 
 static int cmd_bat_poll_rate(int argc, char **argv)
 {
     int rc;
     uint32_t rate_in_s;
+    struct os_dev *bat;
 
-    if (argc == 2) {
-        rate_in_s = (uint32_t)parse_ull_bounds(argv[1], 1, 255, &rc);
-        if (rc) {
-            console_printf("Invalid poll rate, use 1..255\n");
-        } else {
-            battery_set_poll_rate_ms(bat, rate_in_s * 1000);
-        }
-    } else {
+    bat = NULL;
+
+    if (argc < 2) {
         console_printf("Missing poll rate argument\n");
+        rc = SYS_EINVAL;
+        goto err;
     }
-    return 0;
+
+    bat = battery_shell_open_dev();
+    if (bat == NULL) {
+        rc = SYS_ENODEV;
+        goto err;
+    }
+
+    rate_in_s = (uint32_t)parse_ull_bounds(argv[1], 1, 255, &rc);
+    if (rc) {
+        console_printf("Invalid poll rate, use 1..255\n");
+        goto err;
+    }
+
+    rc = battery_set_poll_rate_ms(bat, rate_in_s * 1000);
+
+err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
+    return rc;
 }
 
 static int bat_property(struct battery_prop_listener *listener,
@@ -336,47 +425,62 @@ static struct battery_prop_listener listener = {
 
 static int cmd_bat_monitor(int argc, char **argv)
 {
-    int rc = 0;
+    int rc;
     struct battery_property *prop;
+    struct os_dev *bat;
+
+    bat = NULL;
 
     if (argc < 2) {
         console_printf("Invalid number of arguments, use monitor 
<prop_nam>\n");
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    bat = battery_shell_open_dev();
+    if (bat == NULL) {
+        rc = SYS_ENODEV;
         goto err;
     }
 
     prop = battery_find_property_by_name(bat, argv[1]);
     if (prop == NULL) {
         if (strcmp(argv[1], "off") == 0) {
-            battery_prop_poll_unsubscribe(&listener, NULL);
+            rc = battery_prop_poll_unsubscribe(&listener, NULL);
             goto err;
         }
         console_printf("Invalid property name\n");
+        rc = SYS_EINVAL;
         goto err;
     }
 
     if (argc == 3) {
         if (strcmp(argv[2], "off") == 0) {
-            battery_prop_poll_unsubscribe(&listener, prop);
+            rc = battery_prop_poll_unsubscribe(&listener, prop);
             goto err;
         } else if (strcmp(argv[2], "on") != 0) {
             console_printf("Invalid parameter %s\n", argv[2]);
+            rc = SYS_EINVAL;
             goto err;
         }
     }
-    battery_prop_poll_subscribe(&listener, prop);
+    rc = battery_prop_poll_subscribe(&listener, prop);
 
 err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
     return rc;
 }
 
 static const struct shell_cmd bat_cli_commands[] =
 {
-        { "read", cmd_bat_read, HELP(bat_read_help) },
-        { "write", cmd_bat_write, HELP(bat_write_help) },
-        { "list", cmd_bat_list, HELP(bat_list_help) },
-        { "pollrate", cmd_bat_poll_rate, HELP(bat_poll_rate_help) },
-        { "monitor", cmd_bat_monitor, HELP(bat_monitor_help) },
-        { NULL, NULL, NULL }
+    { "read", cmd_bat_read, HELP(bat_read_help) },
+    { "write", cmd_bat_write, HELP(bat_write_help) },
+    { "list", cmd_bat_list, HELP(bat_list_help) },
+    { "pollrate", cmd_bat_poll_rate, HELP(bat_poll_rate_help) },
+    { "monitor", cmd_bat_monitor, HELP(bat_monitor_help) },
+    { NULL, NULL, NULL }
 };
 
 /**
@@ -394,19 +498,26 @@ static int bat_compat_cmd(int argc, char **argv)
     int rc;
     int i;
 
-    for (i = 0; bat_cli_commands[i].sc_cmd; ++i)
+    if (argc < 2)
     {
-        if (strcmp(bat_cli_commands[i].sc_cmd, argv[1]) == 0)
-        {
-            rc = bat_cli_commands[i].sc_cmd_func(argc - 1, argv + 1);
-            break;
-        }
+        rc = SYS_EINVAL;
     }
-    /* No command found */
-    if (bat_cli_commands[i].sc_cmd == NULL)
+    else
     {
-        console_printf("Invalid command.\n");
-        rc = -1;
+        for (i = 0; bat_cli_commands[i].sc_cmd; ++i)
+        {
+            if (strcmp(bat_cli_commands[i].sc_cmd, argv[1]) == 0)
+            {
+                rc = bat_cli_commands[i].sc_cmd_func(argc - 1, argv + 1);
+                break;
+            }
+        }
+        /* No command found */
+        if (bat_cli_commands[i].sc_cmd == NULL)
+        {
+            console_printf("Invalid command.\n");
+            rc = -1;
+        }
     }
 
     /* Print help in case of error */
@@ -426,7 +537,4 @@ battery_shell_register(void)
     rc = shell_register("bat", bat_cli_commands);
     rc = shell_cmd_register(&bat_cli_cmd);
     SYSINIT_PANIC_ASSERT_MSG(rc == 0, "Failed to register battery shell");
-
-    bat = os_dev_open("battery_0", 0, NULL);
-    SYSINIT_PANIC_ASSERT_MSG(bat != NULL, "Failed to open battery device");
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to