You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2018/11/07 21:15:14 UTC

[mynewt-core] 04/05: hw/battery: Don't open battery device during init

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 3bf6cb7869bd676f6824f8a70e3ee542a842bfdb
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Nov 1 14:38:03 2018 -0700

    hw/battery: Don't open battery device during init
    
    Prior to commit: The battery shell opened the battery device at init
    time.  If the open failed, a sysinit panic was triggered.
    
    After commit: The battery shell opens the battery device as needed, per
    command.  If the open fails, the command fails with a `SYS_ENODEV`
    error.
    
    Rationale: A hardware fault may render the battery device inoperable.
    It is better for the application to decide how to handle this condition
    rather than the shell triggering a crash.
---
 hw/battery/src/battery_shell.c | 88 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 81 insertions(+), 7 deletions(-)

diff --git a/hw/battery/src/battery_shell.c b/hw/battery/src/battery_shell.c
index 85b5bed..c1979d1 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,12 +181,28 @@ 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;
     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");
@@ -196,6 +210,12 @@ static int cmd_bat_read(int argc, char **argv)
         goto err;
     }
 
+    bat = battery_shell_open_dev();
+    if (bat == NULL) {
+        rc = SYS_ENODEV;
+        goto err;
+    }
+
     if (argc == 2 && strcmp("all", argv[1]) == 0) {
         maxp = battery_get_property_count(bat, NULL);
         for (i = 0; i < maxp; ++i) {
@@ -229,6 +249,9 @@ static int cmd_bat_read(int argc, char **argv)
     rc = 0;
 
 err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
     return rc;
 }
 
@@ -256,14 +279,23 @@ cmd_bat_write(int argc, char ** argv)
     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]);
@@ -304,29 +336,53 @@ cmd_bat_write(int argc, char ** argv)
     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;
+
+    bat = NULL;
 
     if (argc < 2) {
         console_printf("Missing poll rate argument\n");
@@ -334,6 +390,12 @@ static int cmd_bat_poll_rate(int argc, char **argv)
         goto err;
     }
 
+    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");
@@ -343,6 +405,9 @@ static int cmd_bat_poll_rate(int argc, char **argv)
     rc = battery_set_poll_rate_ms(bat, rate_in_s * 1000);
 
 err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
     return rc;
 }
 
@@ -362,6 +427,9 @@ static int cmd_bat_monitor(int argc, char **argv)
 {
     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");
@@ -369,6 +437,12 @@ static int cmd_bat_monitor(int argc, char **argv)
         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) {
@@ -393,6 +467,9 @@ static int cmd_bat_monitor(int argc, char **argv)
     rc = battery_prop_poll_subscribe(&listener, prop);
 
 err:
+    if (bat != NULL) {
+        os_dev_close(bat);
+    }
     return rc;
 }
 
@@ -460,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");
 }