Kai Ren / client

Files at this revision

API Documentation at this revision

Comitter:
krenbluetoothsig
Date:
Wed May 16 03:38:02 2018 +0000
Parent:
6:8ee39d23b0f3
Child:
8:4ed917660ce8
Child:
15:ebdbdd25ac60
Commit message:
1. Final version before Bluetooth Asia, it supports persistent storage.

Changed in this revision

prj_bbc_microbit.txt Show annotated file Show diff for this revision Revisions of this file
src/main.c Show annotated file Show diff for this revision Revisions of this file
--- a/prj_bbc_microbit.txt	Mon May 07 23:56:34 2018 +0000
+++ b/prj_bbc_microbit.txt	Wed May 16 03:38:02 2018 +0000
@@ -4,8 +4,6 @@
 CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
 CONFIG_SYS_CLOCK_TICKS_PER_SEC=250
 CONFIG_GPIO=y
-CONFIG_FLASH=y
-CONFIG_SOC_FLASH_NRF5=y
 CONFIG_MICROBIT_DISPLAY=y
 CONFIG_PWM=y
 CONFIG_PWM_NRF5_SW=y
@@ -15,7 +13,6 @@
 CONFIG_BT_CTLR_DUP_FILTER_LEN=0
 CONFIG_BT_OBSERVER=y
 CONFIG_BT_BROADCASTER=y
-CONFIG_BT_PRIVACY=n
 
 CONFIG_BT_MESH=y
 CONFIG_BT_MESH_RELAY=y
@@ -27,10 +24,20 @@
 CONFIG_BT_MESH_PB_ADV=n
 CONFIG_BT_MESH_CFG_CLI=y
 
-CONFIG_BT_DEBUG_LOG=y
-CONFIG_BT_MESH_DEBUG=y
+CONFIG_BT_SETTINGS=y
+CONFIG_FLASH=y
+CONFIG_FLASH_PAGE_LAYOUT=y
+CONFIG_FLASH_MAP=y
+CONFIG_FCB=y
+CONFIG_SETTINGS=y
+CONFIG_SETTINGS_FCB=y
+CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600
+
+#CONFIG_BT_DEBUG_LOG=y
+#CONFIG_BT_MESH_DEBUG=y
+#CONFIG_BT_MESH_DEBUG_SETTINGS=y
 #CONFIG_BT_MESH_DEBUG_NET=y
-CONFIG_BT_MESH_DEBUG_MODEL=y
+#CONFIG_BT_MESH_DEBUG_MODEL=y
 #CONFIG_BT_MESH_DEBUG_TRANS=y
 #CONFIG_BT_MESH_SELF_TEST=y
 #CONFIG_BT_MESH_IV_UPDATE_TEST=y
--- a/src/main.c	Mon May 07 23:56:34 2018 +0000
+++ b/src/main.c	Wed May 16 03:38:02 2018 +0000
@@ -12,6 +12,7 @@
 #include <board.h>
 #include <soc.h>
 #include <misc/printk.h>
+#include <settings/settings.h>
 #include <ctype.h>
 #include <flash.h>
 #include <gpio.h>
@@ -50,11 +51,6 @@
 #define BUZZER_PIN     EXT_P0_GPIO_PIN
 #define BEEP_DURATION  K_MSEC(60)
 
-/* NVM offset */
-#define SEQ_PER_BIT  976
-#define SEQ_PAGE     (NRF_FICR->CODEPAGESIZE * (NRF_FICR->CODESIZE - 1))
-#define SEQ_MAX      (NRF_FICR->CODEPAGESIZE * 8 * SEQ_PER_BIT)
-
 /* Key definition, it's pre-configured, not need to do provisioning. */
 static const u8_t net_key[16] = {
     0xf1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
@@ -91,7 +87,6 @@
 u8_t flags;
 u16_t addr = NODE_ADDR;
 u16_t groupAddress = GROUP_ADDR;
-static u32_t seq;
 
 /* Transaction ID for Generic OnOff Set */
 static u8_t trans_ID = 0;
@@ -136,84 +131,6 @@
     return groupAddress;
 }
 
-void board_seq_update(u32_t seq)
-{
-    u32_t loc, seq_map;
-    int err;
-
-    if (seq % SEQ_PER_BIT) {
-        return;
-    }
-
-    loc = (SEQ_PAGE + ((seq / SEQ_PER_BIT) / 32));
-
-    err = flash_read(nvm, loc, &seq_map, sizeof(seq_map));
-    if (err) {
-        printk("flash_read err %d\n", err);
-        return;
-    }
-
-    seq_map >>= 1;
-
-    flash_write_protection_set(nvm, false);
-    err = flash_write(nvm, loc, &seq_map, sizeof(seq_map));
-    flash_write_protection_set(nvm, true);
-    if (err) {
-        printk("flash_write err %d\n", err);
-    }
-}
-
-static u32_t get_seq(void)
-{
-    u32_t seq_map, seq = 0;
-    int err, i;
-
-    for (i = 0; i < NRF_FICR->CODEPAGESIZE / sizeof(seq_map); i++) {
-        err = flash_read(nvm, SEQ_PAGE + (i * sizeof(seq_map)),
-                 &seq_map, sizeof(seq_map));
-        if (err) {
-            printk("flash_read err %d\n", err);
-            return seq;
-        }
-
-        printk("seq_map 0x%08x\n", seq_map);
-
-        if (seq_map) {
-            seq = ((i * 32) +
-                   (32 - popcount(seq_map))) * SEQ_PER_BIT;
-            if (!seq) {
-                return 0;
-            }
-
-            break;
-        }
-    }
-
-    seq += SEQ_PER_BIT;
-    if (seq >= SEQ_MAX) {
-        seq = 0;
-    }
-
-    if (seq) {
-        seq_map >>= 1;
-        flash_write_protection_set(nvm, false);
-        err = flash_write(nvm, SEQ_PAGE + (i * sizeof(seq_map)),
-                  &seq_map, sizeof(seq_map));
-        flash_write_protection_set(nvm, true);
-        if (err) {
-            printk("flash_write err %d\n", err);
-        }
-    } else {
-        printk("Performing flash erase of page 0x%08x\n", SEQ_PAGE);
-        err = flash_erase(nvm, SEQ_PAGE, NRF_FICR->CODEPAGESIZE);
-        if (err) {
-            printk("flash_erase err %d\n", err);
-        }
-    }
-
-    return seq;
-}
-
 /* Callback function for button B pressed, it's scheduled by OS, out of interrupt routine
  * it's safe to stay here longer. */
 static void button_send_pressed(struct k_work *work)
@@ -402,20 +319,16 @@
     gpio_pin_enable_callback(gpio, SW1_GPIO_PIN);
 }
 
-void board_init(u16_t *addr, u32_t *seq)
+void board_init(u16_t *addr)
 {
     struct mb_display *disp = mb_display_get();
 
-    printk("SEQ_PAGE 0x%08x\n", SEQ_PAGE);
-
     nvm = device_get_binding(FLASH_DEV_NAME);
     pwm = device_get_binding(CONFIG_PWM_NRF5_SW_0_DEV_NAME);
 
     *addr = ( (u16_t)NRF_FICR->DEVICEADDR[0] ) & 0x7fff;
     printk("FICR 0x%02x\n", *addr);
 
-    *seq = get_seq();
-
     mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, SCROLL_SPEED,
              "%s %s", "Client", groupAddress2String[groupAddress - GROUP_ADDR]);
 
@@ -591,11 +504,22 @@
 
     printk("Mesh initialized\n");
 
-    err = bt_mesh_provision(net_key, net_idx, flags, iv_index, seq, addr,
+    if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
+        printk("Loading stored settings\n");
+        settings_load();
+    }
+
+    err = bt_mesh_provision(net_key, net_idx, flags, iv_index, addr,
                 dev_key);
-    if (err) {
+
+    if (err == -EALREADY) {
+        printk("Using stored settings\n");
+    } else if (err) {
         printk("Provisioning failed (err %d)\n", err);
         return;
+    } else {
+        printk("Provisioning completed\n");
+        configure();
     }
 
     printk("Provisioning completed\n");
@@ -633,9 +557,9 @@
 
     printk("Initializing...\n");
 
-    board_init(&addr, &seq);
+    board_init(&addr);
 
-    printk("Unicast address: 0x%04x, name: %s, seq 0x%06x\n", addr, groupAddress2String[groupAddress - GROUP_ADDR], seq);
+    printk("Unicast address: 0x%04x, name: %s\n", addr, groupAddress2String[groupAddress - GROUP_ADDR]);
 
     /* Initialize the Bluetooth Subsystem */
     err = bt_enable(bt_ready);