Publishing for Bluetooth Asia 2018 developer session: mesh session. This repo is for GenericOnOff client side firmware.

EULA

PLEASE READ MESH_DEMO_TUTORIAL_EULA.TXT BEFORE START DEVELOPMENT.

Overview

This sample demonstrates Bluetooth Mesh functionality on micro:bit. It doesn't need provisioning because all the credential material (DevKey, NetKey, AppKey, Key Index, IVI and Unicast address) is pre-configured. This sample work as a GenericOnOff client:

  • Pressing Button A on micro:bit will send GenericOnOffSet access message with on or off state alternatively and specific group address;
  • Pressing Button B on micro:bit will switch the group address from 0xC000 to 0xC003 and back to 0xC000 again like round robin;

Requirements

micro:bit board

Building and Running

0. Download source code, it's zip file, the link is on this page, Download repository.

1. Following below link to set the development environment up on your Windows computer. http://docs.zephyrproject.org/getting_started/installation_win.html . Please make sure git checkout this commit, commit: 88dfd399f480b1593a8e13f5a68d512921a55502 , the detail is here, https://github.com/zephyrproject-rtos/zephyr/commit/88dfd399f480b1593a8e13f5a68d512921a55502

2. copy zip file into ./zephyr/sample/ in the Zephyr tree.

3. unzip the file by "Extract Here".

3. access into the extracted folder and rename prj_bbc_microbit.txt to prj_bbc_microbit.conf

4. if adopting Windows Command Prompt, use it to access into the unzip folder and type following commands on the console:

mkdir build & cd build

cmake -GNinja -DBOARD=bbc_microbit ..

ninja

if adopting MSYS2, use it to access into the unzip folder and type following commands on the console:

mkdir build & cd build

cmake -GNinja -DBOARD=bbc_microbit ..

ninja

5. connect micro:bit with your computer by USB cable, the board will be enumerated as a massive storage device;

6. drag the hex file (which is in ./zephyr/sample/[unzip folder]/build/zephyr/zephyr.hex) into Microbit massive storage device to flash the firmware;

7. micro:bit is ready to work as a GenericOnOff client.

Revision:
7:6512e150f5f6
Parent:
1:fb4893064dd4
Child:
9:ba6d8314dbb0
Child:
16:64e0ae95d4f1
--- 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);