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-mcumgr.git

commit 0738644e1bfa4fa762e013c12d9e080e2bde542f
Author: Christopher Collins <ccoll...@apache.org>
AuthorDate: Fri Jan 26 15:52:34 2018 -0800

    Add log_mgmt support to smp_svr sample app.
---
 samples/smp_svr/README.md             | 30 +++++++++++
 samples/smp_svr/zephyr/CMakeLists.txt |  7 ++-
 samples/smp_svr/zephyr/prj.conf       | 41 ++++++++++++---
 samples/smp_svr/zephyr/src/main.c     | 97 +++++++++++++++++++++++++++++++++--
 4 files changed, 162 insertions(+), 13 deletions(-)

diff --git a/samples/smp_svr/README.md b/samples/smp_svr/README.md
index cb9d3e7..6ff9dcd 100644
--- a/samples/smp_svr/README.md
+++ b/samples/smp_svr/README.md
@@ -8,6 +8,7 @@ transports:
 `smp_svr` enables support for the following command groups:
     * fs_mgmt
     * img_mgmt
+    * log_mgmt
     * os_mgmt
 
 ## Mynewt
@@ -52,6 +53,35 @@ setting in `prj.conf` accordingly.
 In addition, the MCUBoot boot loader (https://github.com/runtimeco/mcuboot) is
 required for img_mgmt to function properly.
 
+The smp_svr app logs reboots to a flash circular buffer (FCB) backed log.  The
+flash map for the nRF52 only allocates flash for either the NFFS file system or
+the FCB, but not both.  By default, this application uses the FCB log, not the
+file system.  You can enable the NFFS file system and disable the FCB as 
follows-
+
+1. In `zephyr/prj.conf`, uncomment the `FILE_SYSTEM` settings and comment out
+the `FLASH` and `FCB` settings:
+
+```
+    # Enable the NFFS file system.
+    CONFIG_FILE_SYSTEM=y
+    CONFIG_FILE_SYSTEM_NFFS=y
+
+     # Enable the flash circular buffer (FCB) for the reboot log.
+    #CONFIG_FLASH_PAGE_LAYOUT=y
+    #CONFIG_FLASH_MAP=y
+    #CONFIG_FCB=y
+```
+
+2. Link in the NFFS library by uncommenting the `NFFS` line in
+`zephyr/CMakeLists.txt`:
+
+```
+    zephyr_link_libraries(
+        MCUMGR
+        NFFS
+    )
+```
+
 ### Building
 
 The Zephyr port of `smp_svr` can be built using the usual procedure:
diff --git a/samples/smp_svr/zephyr/CMakeLists.txt 
b/samples/smp_svr/zephyr/CMakeLists.txt
index 30a4ebd..ec1fced 100644
--- a/samples/smp_svr/zephyr/CMakeLists.txt
+++ b/samples/smp_svr/zephyr/CMakeLists.txt
@@ -39,4 +39,9 @@ target_sources(app PRIVATE
     src/main.c
 )
 
-zephyr_link_libraries(MCUMGR NFFS)
+zephyr_link_libraries(
+    MCUMGR
+
+    ### Uncomment this line to use the NFFS file system.
+    # NFFS
+)
diff --git a/samples/smp_svr/zephyr/prj.conf b/samples/smp_svr/zephyr/prj.conf
index 50e8ff1..60ccd65 100644
--- a/samples/smp_svr/zephyr/prj.conf
+++ b/samples/smp_svr/zephyr/prj.conf
@@ -1,11 +1,3 @@
-# Required by file management command handlers.
-CONFIG_FILE_SYSTEM=y
-CONFIG_FILE_SYSTEM_NFFS=y
-CONFIG_FS_NFFS_FLASH_DEV_NAME="NRF5_FLASH"
-
-# Required by tinycbor (math.h).
-CONFIG_NEWLIB_LIBC=y
-
 # Some command handlers require a large stack.
 CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
 
@@ -24,7 +16,40 @@ CONFIG_BT_RX_BUF_LEN=260
 CONFIG_MCUMGR_SMP_BT=y
 CONFIG_MCUMGR_SMP_SHELL=y
 
+# Enable the NFFS file system.
+#CONFIG_FILE_SYSTEM=y
+#CONFIG_FILE_SYSTEM_NFFS=y
+
+# Enable the flash circular buffer (FCB) for the reboot log.
+CONFIG_FLASH_PAGE_LAYOUT=y
+CONFIG_FLASH_MAP=y
+CONFIG_FCB=y
+
+# Enable the managed log (mdlog).
+CONFIG_MDLOG=y
+CONFIG_MDLOG_CONSOLE=y
+CONFIG_MDLOG_FCB=y
+
+# Required by the `taskstat` command.
+CONFIG_THREAD_MONITOR=y
+
+# Enable the reboot log.
+CONFIG_REBOOT_LOG=y
+
+# Enable assertions.  Required for crash logging.
+CONFIG_ASSERT=y
+
 # Enable all core commands.
 CONFIG_MCUMGR_CMD_FS_MGMT=y
 CONFIG_MCUMGR_CMD_IMG_MGMT=y
+CONFIG_MCUMGR_CMD_LOG_MGMT=y
 CONFIG_MCUMGR_CMD_OS_MGMT=y
+
+### nRF5 specific settings
+
+# Disable flash-radio sync.  This is necessary for writing to the reboot log
+# with an interrupt handler.
+CONFIG_SOC_FLASH_NRF5_RADIO_SYNC=n
+
+# Specify the location of the NFFS file system.
+CONFIG_FS_NFFS_FLASH_DEV_NAME="NRF5_FLASH"
diff --git a/samples/smp_svr/zephyr/src/main.c 
b/samples/smp_svr/zephyr/src/main.c
index 757725f..fad6fbc 100644
--- a/samples/smp_svr/zephyr/src/main.c
+++ b/samples/smp_svr/zephyr/src/main.c
@@ -11,15 +11,44 @@
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/conn.h>
 #include <bluetooth/gatt.h>
+#include "logging/mdlog.h"
+#include "logging/reboot_log.h"
+#include "fcb.h"
+#include "mgmt/smp_bt.h"
+#include "mgmt/buf.h"
+
+#if defined CONFIG_FCB && defined CONFIG_FILE_SYSTEM_NFFS
+#error Both CONFIG_FCB and CONFIG_FILE_SYSTEM_NFFS are defined; smp_svr \
+       application only supports one at a time.
+#endif
+
+#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
 #include "fs_mgmt/fs_mgmt.h"
+#endif
+#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
 #include "os_mgmt/os_mgmt.h"
+#endif
+#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
 #include "img_mgmt/img_mgmt.h"
-#include "mgmt/smp_bt.h"
-#include "mgmt/buf.h"
- 
+#endif
+#ifdef CONFIG_MCUMGR_CMD_LOG_MGMT
+#include "log_mgmt/log_mgmt.h"
+#endif
+
 #define DEVICE_NAME         CONFIG_BT_DEVICE_NAME
 #define DEVICE_NAME_LEN     (sizeof(DEVICE_NAME) - 1)
 
+struct fcb smp_svr_fcb;
+struct mdlog smp_svr_log;
+
+/* smp_svr uses the first "peruser" log module. */
+#define SMP_SVR_MDLOG_MODULE  (MDLOG_MODULE_PERUSER + 0)
+
+/* Convenience macro for logging to the smp_svr module. */
+#define SMP_SVR_MDLOG(lvl, ...) \
+    MDLOG_ ## lvl(&smp_svr_log, SMP_SVR_MDLOG_MODULE, __VA_ARGS__)
+
+
 static const struct bt_data ad[] = {
     BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
     BT_DATA_BYTES(BT_DATA_UUID128_ALL,
@@ -79,14 +108,71 @@ static void bt_ready(int err)
     advertise();
 }
 
+static int init_fcb(void)
+{
+    /* If the user has enabled FCB support, assume there is an FCB in the NFFS
+     * area.
+     */
+#ifndef CONFIG_FCB
+    return 0;
+#endif
+
+    static struct flash_sector sectors[128];
+
+    uint32_t sector_cnt;
+    int rc;
+
+    sector_cnt = sizeof sectors / sizeof sectors[0];
+    rc = flash_area_get_sectors(4, &sector_cnt, sectors);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (sector_cnt <= 1) {
+        return -EINVAL;
+    }
+
+    /* Initialize the FCB in the NFFS flash area (area 4) with one scratch
+     * sector.
+     */
+    smp_svr_fcb = (struct fcb) {
+        .f_magic = 0x12345678,
+        .f_version = MDLOG_VERSION,
+        .f_sector_cnt = sector_cnt - 1,
+        .f_scratch_cnt = 1,
+        .f_area_id = 4,
+        .f_sectors = sectors,
+    };
+
+    rc = fcb_init(&smp_svr_fcb);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
 void main(void)
 {
     int rc;
 
+    rc = init_fcb();
+    assert(rc == 0);
+    reboot_log_configure(&smp_svr_log);
+
     /* Register the built-in mcumgr command handlers. */
+#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
+    fs_mgmt_register_group();
+#endif
+#ifdef CONFIG_MCUMGR_CMD_OS_MGMT
     os_mgmt_register_group();
+#endif
+#ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
     img_mgmt_register_group();
-    fs_mgmt_register_group();
+#endif
+#ifdef CONFIG_MCUMGR_CMD_LOG_MGMT
+    log_mgmt_register_group();
+#endif
 
     /* Enable Bluetooth. */
     rc = bt_enable(bt_ready);
@@ -99,6 +185,9 @@ void main(void)
     /* Initialize the Bluetooth mcumgr transport. */
     smp_bt_register();
 
+    mdlog_register("smp_svr", &smp_svr_log, &mdlog_fcb_handler, &smp_svr_fcb,
+                   MDLOG_LEVEL_DEBUG);
+
     /* The system work queue handles all incoming mcumgr requests.  Let the
      * main thread idle while the mcumgr server runs.
      */

-- 
To stop receiving notification emails like this one, please contact
ccoll...@apache.org.

Reply via email to