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:10 UTC

[mynewt-core] branch master updated (d1fc183 -> 89c5d96)

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

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


    from d1fc183  sys/log/stub: Add missing stubs
     new f733184  hw/battery: remove `battery_mgr_register()`
     new f36eb4e  hw/battery: Lock mutex in `battery_init()`
     new f4244cf  hw/battery: Return error on failure in CLI cmds
     new 3bf6cb7  hw/battery: Don't open battery device during init
     new 89c5d96  hw/battery: Minor whitespace fix

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 hw/battery/src/battery.c       |  20 +----
 hw/battery/src/battery_shell.c | 188 ++++++++++++++++++++++++++++++++---------
 2 files changed, 151 insertions(+), 57 deletions(-)


[mynewt-core] 05/05: hw/battery: Minor whitespace fix

Posted by cc...@apache.org.
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 89c5d96c57060c1bc83298bc48d2c13df62fb680
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Nov 1 14:55:13 2018 -0700

    hw/battery: Minor whitespace fix
---
 hw/battery/src/battery_shell.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/hw/battery/src/battery_shell.c b/hw/battery/src/battery_shell.c
index c1979d1..6fb411a 100644
--- a/hw/battery/src/battery_shell.c
+++ b/hw/battery/src/battery_shell.c
@@ -475,12 +475,12 @@ err:
 
 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 }
 };
 
 /**


[mynewt-core] 03/05: hw/battery: Return error on failure in CLI cmds

Posted by cc...@apache.org.
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 f4244cf1448dbb29832d44fe60cc79f3e9fd71a2
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Nov 1 14:50:52 2018 -0700

    hw/battery: Return error on failure in CLI cmds
---
 hw/battery/src/battery_shell.c | 88 +++++++++++++++++++++++++++++-------------
 1 file changed, 61 insertions(+), 27 deletions(-)

diff --git a/hw/battery/src/battery_shell.c b/hw/battery/src/battery_shell.c
index 58acc1d..85b5bed 100644
--- a/hw/battery/src/battery_shell.c
+++ b/hw/battery/src/battery_shell.c
@@ -185,13 +185,14 @@ static void print_property(const struct battery_property *prop)
 
 static int cmd_bat_read(int argc, char **argv)
 {
-    int rc = 0;
+    int rc;
     int maxp;
     int i;
     struct battery_property *prop;
 
     if (argc < 2) {
         console_printf("Invalid number of arguments, use read <prop>\n");
+        rc = SYS_EINVAL;
         goto err;
     }
 
@@ -202,6 +203,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,16 +213,21 @@ 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:
     return rc;
 }
@@ -245,7 +252,7 @@ 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;
@@ -253,37 +260,49 @@ cmd_bat_write(int argc, char ** argv)
 
     if (argc < 3) {
         console_printf("Invalid number of arguments, use write <prop> <value>\n");
+        rc = SYS_EINVAL;
         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:
     return rc;
 }
@@ -309,17 +328,22 @@ static int cmd_bat_poll_rate(int argc, char **argv)
     int rc;
     uint32_t rate_in_s;
 
-    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 {
+    if (argc < 2) {
         console_printf("Missing poll rate argument\n");
+        rc = SYS_EINVAL;
+        goto err;
     }
-    return 0;
+
+    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:
+    return rc;
 }
 
 static int bat_property(struct battery_prop_listener *listener,
@@ -336,34 +360,37 @@ static struct battery_prop_listener listener = {
 
 static int cmd_bat_monitor(int argc, char **argv)
 {
-    int rc = 0;
+    int rc;
     struct battery_property *prop;
 
     if (argc < 2) {
         console_printf("Invalid number of arguments, use monitor <prop_nam>\n");
+        rc = SYS_EINVAL;
         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:
     return rc;
@@ -394,19 +421,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 */


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

Posted by cc...@apache.org.
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");
 }


[mynewt-core] 02/05: hw/battery: Lock mutex in `battery_init()`

Posted by cc...@apache.org.
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 f36eb4ea7ef452a40aa287fd819cb8bab79bf876
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Nov 1 14:35:30 2018 -0700

    hw/battery: Lock mutex in `battery_init()`
    
    Prevent race condition when two tasks call `battery_init()`.
---
 hw/battery/src/battery.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/hw/battery/src/battery.c b/hw/battery/src/battery.c
index 4e09570..c8d4fc0 100644
--- a/hw/battery/src/battery.c
+++ b/hw/battery/src/battery.c
@@ -791,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;
 


[mynewt-core] 01/05: hw/battery: remove `battery_mgr_register()`

Posted by cc...@apache.org.
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 f73318415a048ea464f42dd6e1f3af07745ba8ab
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Nov 1 14:34:59 2018 -0700

    hw/battery: remove `battery_mgr_register()`
    
    This function was not called anywhere and it had no prototype.
---
 hw/battery/src/battery.c | 17 -----------------
 1 file changed, 17 deletions(-)

diff --git a/hw/battery/src/battery.c b/hw/battery/src/battery.c
index dd9efd2..4e09570 100644
--- a/hw/battery/src/battery.c
+++ b/hw/battery/src/battery.c
@@ -151,23 +151,6 @@ battery_pkg_init(void)
 }
 
 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)
 {
     int i;