Publishing for Bluetooth Asia 2018 developer session: mesh session. This repo is for GenericOnOff server side firmware.
EULA
PLEASE READ MESH_DEMO_TUTORIAL_EULA.TXT BEFORE START DEVELOPMENT.
Overview
This sample demonstrates Bluetooth Mesh functionality on micro:bit, www.microbit.org. 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 server:
- Pressing Button B on micro:bit will subscribe to 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: 60b01f3f5489975098657caa514133c89a472d19 , the detail is here, https://github.com/zephyrproject-rtos/zephyr/commit/60b01f3f5489975098657caa514133c89a472d19
2. copy zip file into ./zephyr/samples/
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 server.
src/main.c@4:a52949a1cf72, 2018-05-16 (annotated)
- Committer:
- krenbluetoothsig
- Date:
- Wed May 16 03:43:25 2018 +0000
- Revision:
- 4:a52949a1cf72
- Parent:
- 0:3ed424a8870a
1. final version before Bluetooth Asia 2018.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krenbluetoothsig | 0:3ed424a8870a | 1 | /* |
krenbluetoothsig | 0:3ed424a8870a | 2 | * Copyright (c) 2018 Bluetooth SIG |
krenbluetoothsig | 0:3ed424a8870a | 3 | * |
krenbluetoothsig | 0:3ed424a8870a | 4 | * SPDX-License-Identifier: Apache-2.0 |
krenbluetoothsig | 0:3ed424a8870a | 5 | */ |
krenbluetoothsig | 0:3ed424a8870a | 6 | |
krenbluetoothsig | 0:3ed424a8870a | 7 | #include <misc/printk.h> |
krenbluetoothsig | 0:3ed424a8870a | 8 | |
krenbluetoothsig | 0:3ed424a8870a | 9 | #include <bluetooth/bluetooth.h> |
krenbluetoothsig | 0:3ed424a8870a | 10 | #include <bluetooth/mesh.h> |
krenbluetoothsig | 0:3ed424a8870a | 11 | #include <gpio.h> |
krenbluetoothsig | 0:3ed424a8870a | 12 | #include <board.h> |
krenbluetoothsig | 0:3ed424a8870a | 13 | #include <soc.h> |
krenbluetoothsig | 0:3ed424a8870a | 14 | #include <misc/printk.h> |
krenbluetoothsig | 0:3ed424a8870a | 15 | #include <ctype.h> |
krenbluetoothsig | 0:3ed424a8870a | 16 | #include <flash.h> |
krenbluetoothsig | 0:3ed424a8870a | 17 | #include <gpio.h> |
krenbluetoothsig | 0:3ed424a8870a | 18 | #include <pwm.h> |
krenbluetoothsig | 0:3ed424a8870a | 19 | |
krenbluetoothsig | 0:3ed424a8870a | 20 | #include <display/mb_display.h> |
krenbluetoothsig | 0:3ed424a8870a | 21 | |
krenbluetoothsig | 0:3ed424a8870a | 22 | #include <bluetooth/mesh.h> |
krenbluetoothsig | 0:3ed424a8870a | 23 | |
krenbluetoothsig | 0:3ed424a8870a | 24 | #define GROUP_ADDR 0xc000 |
krenbluetoothsig | 0:3ed424a8870a | 25 | #define PUBLISHER_ADDR 0x000f |
krenbluetoothsig | 0:3ed424a8870a | 26 | #if !defined(NODE_ADDR) |
krenbluetoothsig | 0:3ed424a8870a | 27 | #define NODE_ADDR 0x0b02 |
krenbluetoothsig | 0:3ed424a8870a | 28 | #endif |
krenbluetoothsig | 0:3ed424a8870a | 29 | |
krenbluetoothsig | 0:3ed424a8870a | 30 | /* Model Operation Codes */ |
krenbluetoothsig | 0:3ed424a8870a | 31 | #define BT_MESH_MODEL_OP_GEN_ONOFF_GET BT_MESH_MODEL_OP_2(0x82, 0x01) |
krenbluetoothsig | 0:3ed424a8870a | 32 | #define BT_MESH_MODEL_OP_GEN_ONOFF_SET BT_MESH_MODEL_OP_2(0x82, 0x02) |
krenbluetoothsig | 0:3ed424a8870a | 33 | #define BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x03) |
krenbluetoothsig | 0:3ed424a8870a | 34 | #define BT_MESH_MODEL_OP_GEN_ONOFF_STATUS BT_MESH_MODEL_OP_2(0x82, 0x04) |
krenbluetoothsig | 0:3ed424a8870a | 35 | |
krenbluetoothsig | 0:3ed424a8870a | 36 | |
krenbluetoothsig | 0:3ed424a8870a | 37 | /* Button debounce timeout */ |
krenbluetoothsig | 0:3ed424a8870a | 38 | #define BUTTON_DEBOUNCE_DELAY_MS 250 |
krenbluetoothsig | 0:3ed424a8870a | 39 | |
krenbluetoothsig | 0:3ed424a8870a | 40 | /* LED matrix scroll speed */ |
krenbluetoothsig | 0:3ed424a8870a | 41 | #define SCROLL_SPEED K_MSEC(500) |
krenbluetoothsig | 0:3ed424a8870a | 42 | |
krenbluetoothsig | 0:3ed424a8870a | 43 | /* LED matrix scroll speed */ |
krenbluetoothsig | 0:3ed424a8870a | 44 | #define BUZZER_PIN EXT_P0_GPIO_PIN |
krenbluetoothsig | 0:3ed424a8870a | 45 | #define BEEP_DURATION K_MSEC(60) |
krenbluetoothsig | 0:3ed424a8870a | 46 | |
krenbluetoothsig | 0:3ed424a8870a | 47 | /* NVM offset */ |
krenbluetoothsig | 0:3ed424a8870a | 48 | #define SEQ_PER_BIT 976 |
krenbluetoothsig | 0:3ed424a8870a | 49 | #define SEQ_PAGE (NRF_FICR->CODEPAGESIZE * (NRF_FICR->CODESIZE - 1)) |
krenbluetoothsig | 0:3ed424a8870a | 50 | #define SEQ_MAX (NRF_FICR->CODEPAGESIZE * 8 * SEQ_PER_BIT) |
krenbluetoothsig | 0:3ed424a8870a | 51 | |
krenbluetoothsig | 0:3ed424a8870a | 52 | /* Key definition, it's pre-configured, not need to do provisioning. */ |
krenbluetoothsig | 0:3ed424a8870a | 53 | static const u8_t net_key[16] = { |
krenbluetoothsig | 0:3ed424a8870a | 54 | 0xf1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
krenbluetoothsig | 0:3ed424a8870a | 55 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
krenbluetoothsig | 0:3ed424a8870a | 56 | }; |
krenbluetoothsig | 0:3ed424a8870a | 57 | static const u8_t dev_key[16] = { |
krenbluetoothsig | 0:3ed424a8870a | 58 | 0xf1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
krenbluetoothsig | 0:3ed424a8870a | 59 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
krenbluetoothsig | 0:3ed424a8870a | 60 | }; |
krenbluetoothsig | 0:3ed424a8870a | 61 | static const u8_t app_key[16] = { |
krenbluetoothsig | 0:3ed424a8870a | 62 | 0xf1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
krenbluetoothsig | 0:3ed424a8870a | 63 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
krenbluetoothsig | 0:3ed424a8870a | 64 | }; |
krenbluetoothsig | 0:3ed424a8870a | 65 | |
krenbluetoothsig | 0:3ed424a8870a | 66 | /* group address string. */ |
krenbluetoothsig | 0:3ed424a8870a | 67 | const char *groupAddress2String[] = { |
krenbluetoothsig | 0:3ed424a8870a | 68 | "Bedroom", |
krenbluetoothsig | 0:3ed424a8870a | 69 | "Living", |
krenbluetoothsig | 0:3ed424a8870a | 70 | "Dining", |
krenbluetoothsig | 0:3ed424a8870a | 71 | "Garage" |
krenbluetoothsig | 0:3ed424a8870a | 72 | }; |
krenbluetoothsig | 0:3ed424a8870a | 73 | |
krenbluetoothsig | 0:3ed424a8870a | 74 | static struct device *gpio; |
krenbluetoothsig | 0:3ed424a8870a | 75 | static struct device *nvm; |
krenbluetoothsig | 0:3ed424a8870a | 76 | static struct device *pwm; |
krenbluetoothsig | 0:3ed424a8870a | 77 | |
krenbluetoothsig | 0:3ed424a8870a | 78 | static struct k_work button_work; |
krenbluetoothsig | 0:3ed424a8870a | 79 | static struct k_work subscribe_work; |
krenbluetoothsig | 0:3ed424a8870a | 80 | static u32_t time, last_time; |
krenbluetoothsig | 0:3ed424a8870a | 81 | |
krenbluetoothsig | 0:3ed424a8870a | 82 | const u16_t net_idx; |
krenbluetoothsig | 0:3ed424a8870a | 83 | const u16_t app_idx; |
krenbluetoothsig | 0:3ed424a8870a | 84 | const u32_t iv_index; |
krenbluetoothsig | 0:3ed424a8870a | 85 | u8_t flags; |
krenbluetoothsig | 0:3ed424a8870a | 86 | u16_t addr; |
krenbluetoothsig | 0:3ed424a8870a | 87 | static u32_t seq; |
krenbluetoothsig | 0:3ed424a8870a | 88 | |
krenbluetoothsig | 0:3ed424a8870a | 89 | /* group address, initialized */ |
krenbluetoothsig | 0:3ed424a8870a | 90 | u16_t groupAddress = GROUP_ADDR; |
krenbluetoothsig | 0:3ed424a8870a | 91 | |
krenbluetoothsig | 0:3ed424a8870a | 92 | u16_t board_set_target(void) |
krenbluetoothsig | 0:3ed424a8870a | 93 | { |
krenbluetoothsig | 0:3ed424a8870a | 94 | switch (groupAddress) { |
krenbluetoothsig | 0:3ed424a8870a | 95 | case GROUP_ADDR: |
krenbluetoothsig | 0:3ed424a8870a | 96 | groupAddress++ ; |
krenbluetoothsig | 0:3ed424a8870a | 97 | break; |
krenbluetoothsig | 0:3ed424a8870a | 98 | case (GROUP_ADDR + 3): |
krenbluetoothsig | 0:3ed424a8870a | 99 | groupAddress = GROUP_ADDR; |
krenbluetoothsig | 0:3ed424a8870a | 100 | break; |
krenbluetoothsig | 0:3ed424a8870a | 101 | default: |
krenbluetoothsig | 0:3ed424a8870a | 102 | groupAddress++; |
krenbluetoothsig | 0:3ed424a8870a | 103 | break; |
krenbluetoothsig | 0:3ed424a8870a | 104 | } |
krenbluetoothsig | 0:3ed424a8870a | 105 | |
krenbluetoothsig | 0:3ed424a8870a | 106 | return groupAddress; |
krenbluetoothsig | 0:3ed424a8870a | 107 | } |
krenbluetoothsig | 0:3ed424a8870a | 108 | |
krenbluetoothsig | 0:3ed424a8870a | 109 | void board_seq_update(u32_t seq) |
krenbluetoothsig | 0:3ed424a8870a | 110 | { |
krenbluetoothsig | 0:3ed424a8870a | 111 | u32_t loc, seq_map; |
krenbluetoothsig | 0:3ed424a8870a | 112 | int err; |
krenbluetoothsig | 0:3ed424a8870a | 113 | |
krenbluetoothsig | 0:3ed424a8870a | 114 | if (seq % SEQ_PER_BIT) { |
krenbluetoothsig | 0:3ed424a8870a | 115 | return; |
krenbluetoothsig | 0:3ed424a8870a | 116 | } |
krenbluetoothsig | 0:3ed424a8870a | 117 | |
krenbluetoothsig | 0:3ed424a8870a | 118 | loc = (SEQ_PAGE + ((seq / SEQ_PER_BIT) / 32)); |
krenbluetoothsig | 0:3ed424a8870a | 119 | |
krenbluetoothsig | 0:3ed424a8870a | 120 | err = flash_read(nvm, loc, &seq_map, sizeof(seq_map)); |
krenbluetoothsig | 0:3ed424a8870a | 121 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 122 | printk("flash_read err %d\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 123 | return; |
krenbluetoothsig | 0:3ed424a8870a | 124 | } |
krenbluetoothsig | 0:3ed424a8870a | 125 | |
krenbluetoothsig | 0:3ed424a8870a | 126 | seq_map >>= 1; |
krenbluetoothsig | 0:3ed424a8870a | 127 | |
krenbluetoothsig | 0:3ed424a8870a | 128 | flash_write_protection_set(nvm, false); |
krenbluetoothsig | 0:3ed424a8870a | 129 | err = flash_write(nvm, loc, &seq_map, sizeof(seq_map)); |
krenbluetoothsig | 0:3ed424a8870a | 130 | flash_write_protection_set(nvm, true); |
krenbluetoothsig | 0:3ed424a8870a | 131 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 132 | printk("flash_write err %d\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 133 | } |
krenbluetoothsig | 0:3ed424a8870a | 134 | } |
krenbluetoothsig | 0:3ed424a8870a | 135 | |
krenbluetoothsig | 0:3ed424a8870a | 136 | static u32_t get_seq(void) |
krenbluetoothsig | 0:3ed424a8870a | 137 | { |
krenbluetoothsig | 0:3ed424a8870a | 138 | u32_t seq_map, seq = 0; |
krenbluetoothsig | 0:3ed424a8870a | 139 | int err, i; |
krenbluetoothsig | 0:3ed424a8870a | 140 | |
krenbluetoothsig | 0:3ed424a8870a | 141 | for (i = 0; i < NRF_FICR->CODEPAGESIZE / sizeof(seq_map); i++) { |
krenbluetoothsig | 0:3ed424a8870a | 142 | err = flash_read(nvm, SEQ_PAGE + (i * sizeof(seq_map)), |
krenbluetoothsig | 0:3ed424a8870a | 143 | &seq_map, sizeof(seq_map)); |
krenbluetoothsig | 0:3ed424a8870a | 144 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 145 | printk("flash_read err %d\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 146 | return seq; |
krenbluetoothsig | 0:3ed424a8870a | 147 | } |
krenbluetoothsig | 0:3ed424a8870a | 148 | |
krenbluetoothsig | 0:3ed424a8870a | 149 | printk("seq_map 0x%08x\n", seq_map); |
krenbluetoothsig | 0:3ed424a8870a | 150 | |
krenbluetoothsig | 0:3ed424a8870a | 151 | if (seq_map) { |
krenbluetoothsig | 0:3ed424a8870a | 152 | seq = ((i * 32) + |
krenbluetoothsig | 0:3ed424a8870a | 153 | (32 - popcount(seq_map))) * SEQ_PER_BIT; |
krenbluetoothsig | 0:3ed424a8870a | 154 | if (!seq) { |
krenbluetoothsig | 0:3ed424a8870a | 155 | return 0; |
krenbluetoothsig | 0:3ed424a8870a | 156 | } |
krenbluetoothsig | 0:3ed424a8870a | 157 | |
krenbluetoothsig | 0:3ed424a8870a | 158 | break; |
krenbluetoothsig | 0:3ed424a8870a | 159 | } |
krenbluetoothsig | 0:3ed424a8870a | 160 | } |
krenbluetoothsig | 0:3ed424a8870a | 161 | |
krenbluetoothsig | 0:3ed424a8870a | 162 | seq += SEQ_PER_BIT; |
krenbluetoothsig | 0:3ed424a8870a | 163 | if (seq >= SEQ_MAX) { |
krenbluetoothsig | 0:3ed424a8870a | 164 | seq = 0; |
krenbluetoothsig | 0:3ed424a8870a | 165 | } |
krenbluetoothsig | 0:3ed424a8870a | 166 | |
krenbluetoothsig | 0:3ed424a8870a | 167 | if (seq) { |
krenbluetoothsig | 0:3ed424a8870a | 168 | seq_map >>= 1; |
krenbluetoothsig | 0:3ed424a8870a | 169 | flash_write_protection_set(nvm, false); |
krenbluetoothsig | 0:3ed424a8870a | 170 | err = flash_write(nvm, SEQ_PAGE + (i * sizeof(seq_map)), |
krenbluetoothsig | 0:3ed424a8870a | 171 | &seq_map, sizeof(seq_map)); |
krenbluetoothsig | 0:3ed424a8870a | 172 | flash_write_protection_set(nvm, true); |
krenbluetoothsig | 0:3ed424a8870a | 173 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 174 | printk("flash_write err %d\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 175 | } |
krenbluetoothsig | 0:3ed424a8870a | 176 | } else { |
krenbluetoothsig | 0:3ed424a8870a | 177 | printk("Performing flash erase of page 0x%08x\n", SEQ_PAGE); |
krenbluetoothsig | 0:3ed424a8870a | 178 | err = flash_erase(nvm, SEQ_PAGE, NRF_FICR->CODEPAGESIZE); |
krenbluetoothsig | 0:3ed424a8870a | 179 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 180 | printk("flash_erase err %d\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 181 | } |
krenbluetoothsig | 0:3ed424a8870a | 182 | } |
krenbluetoothsig | 0:3ed424a8870a | 183 | |
krenbluetoothsig | 0:3ed424a8870a | 184 | return seq; |
krenbluetoothsig | 0:3ed424a8870a | 185 | } |
krenbluetoothsig | 0:3ed424a8870a | 186 | |
krenbluetoothsig | 0:3ed424a8870a | 187 | static void buttonA_pressed(struct k_work *work) |
krenbluetoothsig | 0:3ed424a8870a | 188 | { |
krenbluetoothsig | 0:3ed424a8870a | 189 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 190 | |
krenbluetoothsig | 0:3ed424a8870a | 191 | printk("buttonA_pressed\n"); |
krenbluetoothsig | 0:3ed424a8870a | 192 | mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, SCROLL_SPEED, "%s", |
krenbluetoothsig | 0:3ed424a8870a | 193 | groupAddress2String[groupAddress - GROUP_ADDR]); |
krenbluetoothsig | 0:3ed424a8870a | 194 | } |
krenbluetoothsig | 0:3ed424a8870a | 195 | |
krenbluetoothsig | 0:3ed424a8870a | 196 | static void buttonB_resubscribe(struct k_work *work) |
krenbluetoothsig | 0:3ed424a8870a | 197 | { |
krenbluetoothsig | 0:3ed424a8870a | 198 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 199 | |
krenbluetoothsig | 0:3ed424a8870a | 200 | printk("change new group address to 0x%04x\n", board_set_target()); |
krenbluetoothsig | 0:3ed424a8870a | 201 | |
krenbluetoothsig | 0:3ed424a8870a | 202 | mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, SCROLL_SPEED, "%s", |
krenbluetoothsig | 0:3ed424a8870a | 203 | groupAddress2String[groupAddress - GROUP_ADDR]); |
krenbluetoothsig | 0:3ed424a8870a | 204 | |
krenbluetoothsig | 0:3ed424a8870a | 205 | // change model subscription |
krenbluetoothsig | 0:3ed424a8870a | 206 | bt_mesh_cfg_mod_sub_overwrite(net_idx, addr, addr, groupAddress, |
krenbluetoothsig | 0:3ed424a8870a | 207 | BT_MESH_MODEL_ID_GEN_ONOFF_SRV, NULL); |
krenbluetoothsig | 0:3ed424a8870a | 208 | |
krenbluetoothsig | 0:3ed424a8870a | 209 | { |
krenbluetoothsig | 0:3ed424a8870a | 210 | struct bt_mesh_cfg_hb_sub sub = { |
krenbluetoothsig | 0:3ed424a8870a | 211 | .src = PUBLISHER_ADDR, |
krenbluetoothsig | 0:3ed424a8870a | 212 | .dst = groupAddress, |
krenbluetoothsig | 0:3ed424a8870a | 213 | .period = 0x10, |
krenbluetoothsig | 0:3ed424a8870a | 214 | }; |
krenbluetoothsig | 0:3ed424a8870a | 215 | |
krenbluetoothsig | 0:3ed424a8870a | 216 | bt_mesh_cfg_hb_sub_set(net_idx, addr, &sub, NULL /*&status*/); |
krenbluetoothsig | 0:3ed424a8870a | 217 | } |
krenbluetoothsig | 0:3ed424a8870a | 218 | |
krenbluetoothsig | 0:3ed424a8870a | 219 | } |
krenbluetoothsig | 0:3ed424a8870a | 220 | |
krenbluetoothsig | 0:3ed424a8870a | 221 | static void button_pressed(struct device *dev, struct gpio_callback *cb, |
krenbluetoothsig | 0:3ed424a8870a | 222 | uint32_t pins) |
krenbluetoothsig | 0:3ed424a8870a | 223 | { |
krenbluetoothsig | 0:3ed424a8870a | 224 | |
krenbluetoothsig | 0:3ed424a8870a | 225 | /* |
krenbluetoothsig | 0:3ed424a8870a | 226 | * One button press within a 1 second interval sends an on message |
krenbluetoothsig | 0:3ed424a8870a | 227 | * More than one button press sends an off message |
krenbluetoothsig | 0:3ed424a8870a | 228 | */ |
krenbluetoothsig | 0:3ed424a8870a | 229 | time = k_uptime_get_32(); |
krenbluetoothsig | 0:3ed424a8870a | 230 | |
krenbluetoothsig | 0:3ed424a8870a | 231 | /* debounce the switch */ |
krenbluetoothsig | 0:3ed424a8870a | 232 | if (time < last_time + BUTTON_DEBOUNCE_DELAY_MS) { |
krenbluetoothsig | 0:3ed424a8870a | 233 | last_time = time; |
krenbluetoothsig | 0:3ed424a8870a | 234 | return; |
krenbluetoothsig | 0:3ed424a8870a | 235 | } |
krenbluetoothsig | 0:3ed424a8870a | 236 | |
krenbluetoothsig | 0:3ed424a8870a | 237 | if (pins & BIT(SW0_GPIO_PIN)) { |
krenbluetoothsig | 0:3ed424a8870a | 238 | k_work_submit(&button_work); |
krenbluetoothsig | 0:3ed424a8870a | 239 | } |
krenbluetoothsig | 0:3ed424a8870a | 240 | else { |
krenbluetoothsig | 0:3ed424a8870a | 241 | k_work_submit(&subscribe_work); |
krenbluetoothsig | 0:3ed424a8870a | 242 | } |
krenbluetoothsig | 0:3ed424a8870a | 243 | |
krenbluetoothsig | 0:3ed424a8870a | 244 | last_time = time; |
krenbluetoothsig | 0:3ed424a8870a | 245 | |
krenbluetoothsig | 0:3ed424a8870a | 246 | } |
krenbluetoothsig | 0:3ed424a8870a | 247 | |
krenbluetoothsig | 0:3ed424a8870a | 248 | static const struct { |
krenbluetoothsig | 0:3ed424a8870a | 249 | char note; |
krenbluetoothsig | 0:3ed424a8870a | 250 | u32_t period; |
krenbluetoothsig | 0:3ed424a8870a | 251 | u32_t sharp; |
krenbluetoothsig | 0:3ed424a8870a | 252 | } period_map[] = { |
krenbluetoothsig | 0:3ed424a8870a | 253 | { 'C', 3822, 3608 }, |
krenbluetoothsig | 0:3ed424a8870a | 254 | { 'D', 3405, 3214 }, |
krenbluetoothsig | 0:3ed424a8870a | 255 | { 'E', 3034, 3034 }, |
krenbluetoothsig | 0:3ed424a8870a | 256 | { 'F', 2863, 2703 }, |
krenbluetoothsig | 0:3ed424a8870a | 257 | { 'G', 2551, 2407 }, |
krenbluetoothsig | 0:3ed424a8870a | 258 | { 'A', 2273, 2145 }, |
krenbluetoothsig | 0:3ed424a8870a | 259 | { 'B', 2025, 2025 }, |
krenbluetoothsig | 0:3ed424a8870a | 260 | }; |
krenbluetoothsig | 0:3ed424a8870a | 261 | |
krenbluetoothsig | 0:3ed424a8870a | 262 | static u32_t get_period(char note, bool sharp) |
krenbluetoothsig | 0:3ed424a8870a | 263 | { |
krenbluetoothsig | 0:3ed424a8870a | 264 | int i; |
krenbluetoothsig | 0:3ed424a8870a | 265 | |
krenbluetoothsig | 0:3ed424a8870a | 266 | if (note == ' ') { |
krenbluetoothsig | 0:3ed424a8870a | 267 | return 0; |
krenbluetoothsig | 0:3ed424a8870a | 268 | } |
krenbluetoothsig | 0:3ed424a8870a | 269 | |
krenbluetoothsig | 0:3ed424a8870a | 270 | for (i = 0; i < ARRAY_SIZE(period_map); i++) { |
krenbluetoothsig | 0:3ed424a8870a | 271 | if (period_map[i].note != note) { |
krenbluetoothsig | 0:3ed424a8870a | 272 | continue; |
krenbluetoothsig | 0:3ed424a8870a | 273 | } |
krenbluetoothsig | 0:3ed424a8870a | 274 | |
krenbluetoothsig | 0:3ed424a8870a | 275 | if (sharp) { |
krenbluetoothsig | 0:3ed424a8870a | 276 | return period_map[i].sharp; |
krenbluetoothsig | 0:3ed424a8870a | 277 | } else { |
krenbluetoothsig | 0:3ed424a8870a | 278 | return period_map[i].period; |
krenbluetoothsig | 0:3ed424a8870a | 279 | } |
krenbluetoothsig | 0:3ed424a8870a | 280 | } |
krenbluetoothsig | 0:3ed424a8870a | 281 | |
krenbluetoothsig | 0:3ed424a8870a | 282 | return 1500; |
krenbluetoothsig | 0:3ed424a8870a | 283 | } |
krenbluetoothsig | 0:3ed424a8870a | 284 | |
krenbluetoothsig | 0:3ed424a8870a | 285 | void board_heartbeat(u8_t hops, u16_t feat) |
krenbluetoothsig | 0:3ed424a8870a | 286 | { |
krenbluetoothsig | 0:3ed424a8870a | 287 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 288 | const struct mb_image hops_img[] = { |
krenbluetoothsig | 0:3ed424a8870a | 289 | MB_IMAGE({ 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 290 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 291 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 292 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 293 | { 1, 1, 1, 1, 1 }), |
krenbluetoothsig | 0:3ed424a8870a | 294 | MB_IMAGE({ 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 295 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 296 | { 1, 1, 0, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 297 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 298 | { 1, 1, 1, 1, 1 }), |
krenbluetoothsig | 0:3ed424a8870a | 299 | MB_IMAGE({ 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 300 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 301 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 302 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 303 | { 1, 1, 1, 1, 1 }), |
krenbluetoothsig | 0:3ed424a8870a | 304 | MB_IMAGE({ 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 305 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 306 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 307 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 308 | { 1, 1, 1, 1, 1 }), |
krenbluetoothsig | 0:3ed424a8870a | 309 | MB_IMAGE({ 1, 0, 1, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 310 | { 0, 0, 0, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 311 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 312 | { 0, 0, 0, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 313 | { 1, 0, 1, 0, 1 }) |
krenbluetoothsig | 0:3ed424a8870a | 314 | }; |
krenbluetoothsig | 0:3ed424a8870a | 315 | |
krenbluetoothsig | 0:3ed424a8870a | 316 | printk("%u hops\n", hops); |
krenbluetoothsig | 0:3ed424a8870a | 317 | |
krenbluetoothsig | 0:3ed424a8870a | 318 | if (hops) { |
krenbluetoothsig | 0:3ed424a8870a | 319 | hops = min(hops, ARRAY_SIZE(hops_img)); |
krenbluetoothsig | 0:3ed424a8870a | 320 | mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2), |
krenbluetoothsig | 0:3ed424a8870a | 321 | &hops_img[hops - 1], 1); |
krenbluetoothsig | 0:3ed424a8870a | 322 | } |
krenbluetoothsig | 0:3ed424a8870a | 323 | } |
krenbluetoothsig | 0:3ed424a8870a | 324 | |
krenbluetoothsig | 0:3ed424a8870a | 325 | void board_other_dev_pressed(u16_t addr) |
krenbluetoothsig | 0:3ed424a8870a | 326 | { |
krenbluetoothsig | 0:3ed424a8870a | 327 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 328 | |
krenbluetoothsig | 0:3ed424a8870a | 329 | printk("board_other_dev_pressed(0x%04x)\n", addr); |
krenbluetoothsig | 0:3ed424a8870a | 330 | |
krenbluetoothsig | 0:3ed424a8870a | 331 | mb_display_print(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2), |
krenbluetoothsig | 0:3ed424a8870a | 332 | "%c", groupAddress2String[groupAddress - GROUP_ADDR][0]); |
krenbluetoothsig | 0:3ed424a8870a | 333 | } |
krenbluetoothsig | 0:3ed424a8870a | 334 | |
krenbluetoothsig | 0:3ed424a8870a | 335 | void board_attention(bool attention) |
krenbluetoothsig | 0:3ed424a8870a | 336 | { |
krenbluetoothsig | 0:3ed424a8870a | 337 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 338 | static const struct mb_image attn_img[] = { |
krenbluetoothsig | 0:3ed424a8870a | 339 | MB_IMAGE({ 0, 0, 0, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 340 | { 0, 0, 0, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 341 | { 0, 0, 1, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 342 | { 0, 0, 0, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 343 | { 0, 0, 0, 0, 0 }), |
krenbluetoothsig | 0:3ed424a8870a | 344 | MB_IMAGE({ 0, 0, 0, 0, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 345 | { 0, 1, 1, 1, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 346 | { 0, 1, 1, 1, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 347 | { 0, 1, 1, 1, 0 }, |
krenbluetoothsig | 0:3ed424a8870a | 348 | { 0, 0, 0, 0, 0 }), |
krenbluetoothsig | 0:3ed424a8870a | 349 | MB_IMAGE({ 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 350 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 351 | { 1, 1, 0, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 352 | { 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 353 | { 1, 1, 1, 1, 1 }), |
krenbluetoothsig | 0:3ed424a8870a | 354 | MB_IMAGE({ 1, 1, 1, 1, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 355 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 356 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 357 | { 1, 0, 0, 0, 1 }, |
krenbluetoothsig | 0:3ed424a8870a | 358 | { 1, 1, 1, 1, 1 }), |
krenbluetoothsig | 0:3ed424a8870a | 359 | }; |
krenbluetoothsig | 0:3ed424a8870a | 360 | |
krenbluetoothsig | 0:3ed424a8870a | 361 | if (attention) { |
krenbluetoothsig | 0:3ed424a8870a | 362 | mb_display_image(disp, |
krenbluetoothsig | 0:3ed424a8870a | 363 | MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP, |
krenbluetoothsig | 0:3ed424a8870a | 364 | K_MSEC(150), attn_img, ARRAY_SIZE(attn_img)); |
krenbluetoothsig | 0:3ed424a8870a | 365 | } else { |
krenbluetoothsig | 0:3ed424a8870a | 366 | mb_display_stop(disp); |
krenbluetoothsig | 0:3ed424a8870a | 367 | } |
krenbluetoothsig | 0:3ed424a8870a | 368 | } |
krenbluetoothsig | 0:3ed424a8870a | 369 | |
krenbluetoothsig | 0:3ed424a8870a | 370 | static void configure_button(void) |
krenbluetoothsig | 0:3ed424a8870a | 371 | { |
krenbluetoothsig | 0:3ed424a8870a | 372 | static struct gpio_callback button_cb; |
krenbluetoothsig | 0:3ed424a8870a | 373 | |
krenbluetoothsig | 0:3ed424a8870a | 374 | /* Initialize the button debouncer */ |
krenbluetoothsig | 0:3ed424a8870a | 375 | last_time = k_uptime_get_32(); |
krenbluetoothsig | 0:3ed424a8870a | 376 | |
krenbluetoothsig | 0:3ed424a8870a | 377 | k_work_init(&button_work, buttonA_pressed); |
krenbluetoothsig | 0:3ed424a8870a | 378 | k_work_init(&subscribe_work, buttonB_resubscribe); |
krenbluetoothsig | 0:3ed424a8870a | 379 | |
krenbluetoothsig | 0:3ed424a8870a | 380 | gpio = device_get_binding(SW0_GPIO_NAME); |
krenbluetoothsig | 0:3ed424a8870a | 381 | |
krenbluetoothsig | 0:3ed424a8870a | 382 | gpio_pin_configure(gpio, SW0_GPIO_PIN, |
krenbluetoothsig | 0:3ed424a8870a | 383 | (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | |
krenbluetoothsig | 0:3ed424a8870a | 384 | GPIO_INT_ACTIVE_LOW)); |
krenbluetoothsig | 0:3ed424a8870a | 385 | gpio_pin_configure(gpio, SW1_GPIO_PIN, |
krenbluetoothsig | 0:3ed424a8870a | 386 | (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | |
krenbluetoothsig | 0:3ed424a8870a | 387 | GPIO_INT_ACTIVE_LOW)); |
krenbluetoothsig | 0:3ed424a8870a | 388 | |
krenbluetoothsig | 0:3ed424a8870a | 389 | gpio_init_callback(&button_cb, button_pressed, |
krenbluetoothsig | 0:3ed424a8870a | 390 | BIT(SW0_GPIO_PIN) | BIT(SW1_GPIO_PIN)); |
krenbluetoothsig | 0:3ed424a8870a | 391 | gpio_add_callback(gpio, &button_cb); |
krenbluetoothsig | 0:3ed424a8870a | 392 | |
krenbluetoothsig | 0:3ed424a8870a | 393 | gpio_pin_enable_callback(gpio, SW0_GPIO_PIN); |
krenbluetoothsig | 0:3ed424a8870a | 394 | gpio_pin_enable_callback(gpio, SW1_GPIO_PIN); |
krenbluetoothsig | 0:3ed424a8870a | 395 | } |
krenbluetoothsig | 0:3ed424a8870a | 396 | |
krenbluetoothsig | 0:3ed424a8870a | 397 | void board_init(u16_t *addr, u32_t *seq) |
krenbluetoothsig | 0:3ed424a8870a | 398 | { |
krenbluetoothsig | 0:3ed424a8870a | 399 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 400 | |
krenbluetoothsig | 0:3ed424a8870a | 401 | printk("SEQ_PAGE 0x%08x\n", SEQ_PAGE); |
krenbluetoothsig | 0:3ed424a8870a | 402 | |
krenbluetoothsig | 0:3ed424a8870a | 403 | nvm = device_get_binding(FLASH_DEV_NAME); |
krenbluetoothsig | 0:3ed424a8870a | 404 | pwm = device_get_binding(CONFIG_PWM_NRF5_SW_0_DEV_NAME); |
krenbluetoothsig | 0:3ed424a8870a | 405 | |
krenbluetoothsig | 0:3ed424a8870a | 406 | *addr = ( (u16_t)NRF_FICR->DEVICEADDR[0] ) & 0x7fff; |
krenbluetoothsig | 0:3ed424a8870a | 407 | |
krenbluetoothsig | 0:3ed424a8870a | 408 | printk("FICR 0x%02x\n", *addr); |
krenbluetoothsig | 0:3ed424a8870a | 409 | |
krenbluetoothsig | 0:3ed424a8870a | 410 | *seq = get_seq(); |
krenbluetoothsig | 0:3ed424a8870a | 411 | |
krenbluetoothsig | 0:3ed424a8870a | 412 | mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, SCROLL_SPEED, |
krenbluetoothsig | 0:3ed424a8870a | 413 | "%s %s", "Server", groupAddress2String[groupAddress - GROUP_ADDR]); |
krenbluetoothsig | 0:3ed424a8870a | 414 | |
krenbluetoothsig | 0:3ed424a8870a | 415 | configure_button(); |
krenbluetoothsig | 0:3ed424a8870a | 416 | } |
krenbluetoothsig | 0:3ed424a8870a | 417 | |
krenbluetoothsig | 0:3ed424a8870a | 418 | static void heartbeat(u8_t hops, u16_t feat) |
krenbluetoothsig | 0:3ed424a8870a | 419 | { |
krenbluetoothsig | 0:3ed424a8870a | 420 | board_heartbeat(hops, feat); |
krenbluetoothsig | 0:3ed424a8870a | 421 | } |
krenbluetoothsig | 0:3ed424a8870a | 422 | |
krenbluetoothsig | 0:3ed424a8870a | 423 | static struct bt_mesh_cfg_srv cfg_srv = { |
krenbluetoothsig | 4:a52949a1cf72 | 424 | //#if defined(CONFIG_BOARD_BBC_MICROBIT) |
krenbluetoothsig | 4:a52949a1cf72 | 425 | // .relay = BT_MESH_RELAY_ENABLED, |
krenbluetoothsig | 4:a52949a1cf72 | 426 | // //.relay = BT_MESH_RELAY_DISABLED, |
krenbluetoothsig | 4:a52949a1cf72 | 427 | // .beacon = BT_MESH_BEACON_DISABLED, |
krenbluetoothsig | 4:a52949a1cf72 | 428 | //#else |
krenbluetoothsig | 4:a52949a1cf72 | 429 | .relay = BT_MESH_RELAY_DISABLED, |
krenbluetoothsig | 0:3ed424a8870a | 430 | .beacon = BT_MESH_BEACON_DISABLED, |
krenbluetoothsig | 4:a52949a1cf72 | 431 | //#endif |
krenbluetoothsig | 0:3ed424a8870a | 432 | .frnd = BT_MESH_FRIEND_NOT_SUPPORTED, |
krenbluetoothsig | 0:3ed424a8870a | 433 | .default_ttl = 7, |
krenbluetoothsig | 0:3ed424a8870a | 434 | |
krenbluetoothsig | 0:3ed424a8870a | 435 | /* 3 transmissions with 20ms interval */ |
krenbluetoothsig | 0:3ed424a8870a | 436 | .net_transmit = BT_MESH_TRANSMIT(2, 20), |
krenbluetoothsig | 0:3ed424a8870a | 437 | .relay_retransmit = BT_MESH_TRANSMIT(3, 20), |
krenbluetoothsig | 0:3ed424a8870a | 438 | |
krenbluetoothsig | 0:3ed424a8870a | 439 | .hb_sub.func = heartbeat, |
krenbluetoothsig | 0:3ed424a8870a | 440 | }; |
krenbluetoothsig | 0:3ed424a8870a | 441 | |
krenbluetoothsig | 0:3ed424a8870a | 442 | static struct bt_mesh_cfg_cli cfg_cli = { |
krenbluetoothsig | 0:3ed424a8870a | 443 | }; |
krenbluetoothsig | 0:3ed424a8870a | 444 | |
krenbluetoothsig | 0:3ed424a8870a | 445 | static void attention_on(struct bt_mesh_model *model) |
krenbluetoothsig | 0:3ed424a8870a | 446 | { |
krenbluetoothsig | 0:3ed424a8870a | 447 | printk("attention_on()\n"); |
krenbluetoothsig | 0:3ed424a8870a | 448 | board_attention(true); |
krenbluetoothsig | 0:3ed424a8870a | 449 | } |
krenbluetoothsig | 0:3ed424a8870a | 450 | |
krenbluetoothsig | 0:3ed424a8870a | 451 | static void attention_off(struct bt_mesh_model *model) |
krenbluetoothsig | 0:3ed424a8870a | 452 | { |
krenbluetoothsig | 0:3ed424a8870a | 453 | printk("attention_off()\n"); |
krenbluetoothsig | 0:3ed424a8870a | 454 | board_attention(false); |
krenbluetoothsig | 0:3ed424a8870a | 455 | } |
krenbluetoothsig | 0:3ed424a8870a | 456 | |
krenbluetoothsig | 0:3ed424a8870a | 457 | static const struct bt_mesh_health_srv_cb health_srv_cb = { |
krenbluetoothsig | 0:3ed424a8870a | 458 | .attn_on = attention_on, |
krenbluetoothsig | 0:3ed424a8870a | 459 | .attn_off = attention_off, |
krenbluetoothsig | 0:3ed424a8870a | 460 | }; |
krenbluetoothsig | 0:3ed424a8870a | 461 | |
krenbluetoothsig | 0:3ed424a8870a | 462 | static struct bt_mesh_health_srv health_srv = { |
krenbluetoothsig | 0:3ed424a8870a | 463 | .cb = &health_srv_cb, |
krenbluetoothsig | 0:3ed424a8870a | 464 | }; |
krenbluetoothsig | 0:3ed424a8870a | 465 | |
krenbluetoothsig | 0:3ed424a8870a | 466 | BT_MESH_HEALTH_PUB_DEFINE(health_pub, 0); |
krenbluetoothsig | 0:3ed424a8870a | 467 | BT_MESH_MODEL_PUB_DEFINE(gen_onoff_pub_srv, NULL, 2 + 2); |
krenbluetoothsig | 0:3ed424a8870a | 468 | BT_MESH_MODEL_PUB_DEFINE(gen_onoff_pub_cli, NULL, 2 + 2); |
krenbluetoothsig | 0:3ed424a8870a | 469 | |
krenbluetoothsig | 0:3ed424a8870a | 470 | |
krenbluetoothsig | 0:3ed424a8870a | 471 | static void gen_onoff_set(struct bt_mesh_model *model, |
krenbluetoothsig | 0:3ed424a8870a | 472 | struct bt_mesh_msg_ctx *ctx, |
krenbluetoothsig | 0:3ed424a8870a | 473 | struct net_buf_simple *buf) |
krenbluetoothsig | 0:3ed424a8870a | 474 | { |
krenbluetoothsig | 0:3ed424a8870a | 475 | struct mb_display *disp = mb_display_get(); |
krenbluetoothsig | 0:3ed424a8870a | 476 | uint8_t value, trans; |
krenbluetoothsig | 0:3ed424a8870a | 477 | value = *buf->data++; |
krenbluetoothsig | 0:3ed424a8870a | 478 | trans = *buf->data++; |
krenbluetoothsig | 0:3ed424a8870a | 479 | |
krenbluetoothsig | 0:3ed424a8870a | 480 | printk("gen_onoff_set model 0x%04x, src 0x%04x, value:0x%02x, trans: 0x%02x\n", |
krenbluetoothsig | 0:3ed424a8870a | 481 | model->id, ctx->addr, value, trans); |
krenbluetoothsig | 0:3ed424a8870a | 482 | if(value){ |
krenbluetoothsig | 0:3ed424a8870a | 483 | mb_display_print(disp, MB_DISPLAY_MODE_SINGLE, -1, |
krenbluetoothsig | 0:3ed424a8870a | 484 | "%c", groupAddress2String[groupAddress - GROUP_ADDR][0]); |
krenbluetoothsig | 0:3ed424a8870a | 485 | } |
krenbluetoothsig | 0:3ed424a8870a | 486 | else{ |
krenbluetoothsig | 0:3ed424a8870a | 487 | mb_display_stop(disp); |
krenbluetoothsig | 0:3ed424a8870a | 488 | } |
krenbluetoothsig | 0:3ed424a8870a | 489 | |
krenbluetoothsig | 0:3ed424a8870a | 490 | } |
krenbluetoothsig | 0:3ed424a8870a | 491 | |
krenbluetoothsig | 0:3ed424a8870a | 492 | static void gen_onoff_status(struct bt_mesh_model *model, |
krenbluetoothsig | 0:3ed424a8870a | 493 | struct bt_mesh_msg_ctx *ctx, |
krenbluetoothsig | 0:3ed424a8870a | 494 | struct net_buf_simple *buf) |
krenbluetoothsig | 0:3ed424a8870a | 495 | { |
krenbluetoothsig | 0:3ed424a8870a | 496 | printk("gen_onoff_status modelId 0x%04x, src 0x%04x\n", model->id, ctx->addr); |
krenbluetoothsig | 0:3ed424a8870a | 497 | } |
krenbluetoothsig | 0:3ed424a8870a | 498 | |
krenbluetoothsig | 0:3ed424a8870a | 499 | /* |
krenbluetoothsig | 0:3ed424a8870a | 500 | * OnOff Model Server Op Dispatch Table |
krenbluetoothsig | 0:3ed424a8870a | 501 | * |
krenbluetoothsig | 0:3ed424a8870a | 502 | */ |
krenbluetoothsig | 0:3ed424a8870a | 503 | |
krenbluetoothsig | 0:3ed424a8870a | 504 | static const struct bt_mesh_model_op gen_onoff_srv_op[] = { |
krenbluetoothsig | 0:3ed424a8870a | 505 | //{ BT_MESH_MODEL_OP_GEN_ONOFF_GET, 0, gen_onoff_get }, |
krenbluetoothsig | 0:3ed424a8870a | 506 | { BT_MESH_MODEL_OP_GEN_ONOFF_SET, 2, gen_onoff_set }, |
krenbluetoothsig | 0:3ed424a8870a | 507 | //{ BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2, gen_onoff_set_unack }, |
krenbluetoothsig | 0:3ed424a8870a | 508 | BT_MESH_MODEL_OP_END, |
krenbluetoothsig | 0:3ed424a8870a | 509 | }; |
krenbluetoothsig | 0:3ed424a8870a | 510 | |
krenbluetoothsig | 0:3ed424a8870a | 511 | static struct bt_mesh_model root_models[] = { |
krenbluetoothsig | 0:3ed424a8870a | 512 | BT_MESH_MODEL_CFG_SRV(&cfg_srv), |
krenbluetoothsig | 0:3ed424a8870a | 513 | BT_MESH_MODEL_CFG_CLI(&cfg_cli), |
krenbluetoothsig | 0:3ed424a8870a | 514 | BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub), |
krenbluetoothsig | 0:3ed424a8870a | 515 | BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_ONOFF_SRV, gen_onoff_srv_op, |
krenbluetoothsig | 0:3ed424a8870a | 516 | &gen_onoff_pub_srv, NULL), |
krenbluetoothsig | 0:3ed424a8870a | 517 | }; |
krenbluetoothsig | 0:3ed424a8870a | 518 | |
krenbluetoothsig | 0:3ed424a8870a | 519 | static struct bt_mesh_elem elements[] = { |
krenbluetoothsig | 0:3ed424a8870a | 520 | BT_MESH_ELEM(0, root_models, BT_MESH_MODEL_NONE), |
krenbluetoothsig | 0:3ed424a8870a | 521 | //BT_MESH_ELEM(0, vnd_models, BT_MESH_MODEL_NONE), |
krenbluetoothsig | 0:3ed424a8870a | 522 | }; |
krenbluetoothsig | 0:3ed424a8870a | 523 | |
krenbluetoothsig | 0:3ed424a8870a | 524 | static const struct bt_mesh_comp comp = { |
krenbluetoothsig | 0:3ed424a8870a | 525 | .cid = BT_COMP_ID_LF, |
krenbluetoothsig | 0:3ed424a8870a | 526 | .elem = elements, |
krenbluetoothsig | 0:3ed424a8870a | 527 | .elem_count = ARRAY_SIZE(elements), |
krenbluetoothsig | 0:3ed424a8870a | 528 | }; |
krenbluetoothsig | 0:3ed424a8870a | 529 | |
krenbluetoothsig | 0:3ed424a8870a | 530 | |
krenbluetoothsig | 0:3ed424a8870a | 531 | static void configure(void) |
krenbluetoothsig | 0:3ed424a8870a | 532 | { |
krenbluetoothsig | 0:3ed424a8870a | 533 | printk("Configuring...\n"); |
krenbluetoothsig | 0:3ed424a8870a | 534 | |
krenbluetoothsig | 0:3ed424a8870a | 535 | /* Add Application Key */ |
krenbluetoothsig | 0:3ed424a8870a | 536 | bt_mesh_cfg_app_key_add(net_idx, addr, net_idx, app_idx, app_key, NULL); |
krenbluetoothsig | 0:3ed424a8870a | 537 | |
krenbluetoothsig | 0:3ed424a8870a | 538 | /* Bind to vendor model */ |
krenbluetoothsig | 0:3ed424a8870a | 539 | bt_mesh_cfg_mod_app_bind(net_idx, addr, addr, app_idx, |
krenbluetoothsig | 0:3ed424a8870a | 540 | BT_MESH_MODEL_ID_GEN_ONOFF_SRV, NULL); |
krenbluetoothsig | 0:3ed424a8870a | 541 | |
krenbluetoothsig | 0:3ed424a8870a | 542 | /* Bind to Health model */ |
krenbluetoothsig | 0:3ed424a8870a | 543 | bt_mesh_cfg_mod_app_bind(net_idx, addr, addr, app_idx, |
krenbluetoothsig | 0:3ed424a8870a | 544 | BT_MESH_MODEL_ID_HEALTH_SRV, NULL); |
krenbluetoothsig | 0:3ed424a8870a | 545 | |
krenbluetoothsig | 0:3ed424a8870a | 546 | bt_mesh_cfg_mod_sub_add(net_idx, addr, addr, groupAddress, |
krenbluetoothsig | 0:3ed424a8870a | 547 | BT_MESH_MODEL_ID_GEN_ONOFF_SRV, NULL); |
krenbluetoothsig | 0:3ed424a8870a | 548 | |
krenbluetoothsig | 0:3ed424a8870a | 549 | { |
krenbluetoothsig | 0:3ed424a8870a | 550 | struct bt_mesh_cfg_hb_sub sub = { |
krenbluetoothsig | 0:3ed424a8870a | 551 | .src = PUBLISHER_ADDR, |
krenbluetoothsig | 0:3ed424a8870a | 552 | .dst = groupAddress, |
krenbluetoothsig | 0:3ed424a8870a | 553 | .period = 0x10, |
krenbluetoothsig | 0:3ed424a8870a | 554 | }; |
krenbluetoothsig | 0:3ed424a8870a | 555 | |
krenbluetoothsig | 0:3ed424a8870a | 556 | bt_mesh_cfg_hb_sub_set(net_idx, addr, &sub, NULL); |
krenbluetoothsig | 0:3ed424a8870a | 557 | printk("Subscribing to heartbeat messages, group address: 0x%04x, %s\n", |
krenbluetoothsig | 0:3ed424a8870a | 558 | groupAddress, groupAddress2String[groupAddress - GROUP_ADDR]); |
krenbluetoothsig | 0:3ed424a8870a | 559 | } |
krenbluetoothsig | 0:3ed424a8870a | 560 | |
krenbluetoothsig | 0:3ed424a8870a | 561 | printk("Configuration complete\n"); |
krenbluetoothsig | 0:3ed424a8870a | 562 | |
krenbluetoothsig | 0:3ed424a8870a | 563 | } |
krenbluetoothsig | 0:3ed424a8870a | 564 | |
krenbluetoothsig | 0:3ed424a8870a | 565 | static const u8_t dev_uuid[16] = { 0xdd, 0xdd }; |
krenbluetoothsig | 0:3ed424a8870a | 566 | |
krenbluetoothsig | 0:3ed424a8870a | 567 | static const struct bt_mesh_prov prov = { |
krenbluetoothsig | 0:3ed424a8870a | 568 | .uuid = dev_uuid, |
krenbluetoothsig | 0:3ed424a8870a | 569 | }; |
krenbluetoothsig | 0:3ed424a8870a | 570 | |
krenbluetoothsig | 0:3ed424a8870a | 571 | static void bt_ready(int err) |
krenbluetoothsig | 0:3ed424a8870a | 572 | { |
krenbluetoothsig | 0:3ed424a8870a | 573 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 574 | printk("Bluetooth init failed (err %d)\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 575 | return; |
krenbluetoothsig | 0:3ed424a8870a | 576 | } |
krenbluetoothsig | 0:3ed424a8870a | 577 | |
krenbluetoothsig | 0:3ed424a8870a | 578 | printk("Bluetooth initialized\n"); |
krenbluetoothsig | 0:3ed424a8870a | 579 | |
krenbluetoothsig | 0:3ed424a8870a | 580 | err = bt_mesh_init(&prov, &comp); |
krenbluetoothsig | 0:3ed424a8870a | 581 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 582 | printk("Initializing mesh failed (err %d)\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 583 | return; |
krenbluetoothsig | 0:3ed424a8870a | 584 | } |
krenbluetoothsig | 0:3ed424a8870a | 585 | |
krenbluetoothsig | 0:3ed424a8870a | 586 | printk("Mesh initialized\n"); |
krenbluetoothsig | 0:3ed424a8870a | 587 | |
krenbluetoothsig | 0:3ed424a8870a | 588 | err = bt_mesh_provision(net_key, net_idx, flags, iv_index, seq, addr, |
krenbluetoothsig | 0:3ed424a8870a | 589 | dev_key); |
krenbluetoothsig | 0:3ed424a8870a | 590 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 591 | printk("Provisioning failed (err %d)\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 592 | return; |
krenbluetoothsig | 0:3ed424a8870a | 593 | } |
krenbluetoothsig | 0:3ed424a8870a | 594 | |
krenbluetoothsig | 0:3ed424a8870a | 595 | printk("Provisioning completed\n"); |
krenbluetoothsig | 0:3ed424a8870a | 596 | |
krenbluetoothsig | 0:3ed424a8870a | 597 | configure(); |
krenbluetoothsig | 0:3ed424a8870a | 598 | } |
krenbluetoothsig | 0:3ed424a8870a | 599 | |
krenbluetoothsig | 0:3ed424a8870a | 600 | void main(void) |
krenbluetoothsig | 0:3ed424a8870a | 601 | { |
krenbluetoothsig | 0:3ed424a8870a | 602 | int err; |
krenbluetoothsig | 0:3ed424a8870a | 603 | |
krenbluetoothsig | 0:3ed424a8870a | 604 | printk("Initializing...\n"); |
krenbluetoothsig | 0:3ed424a8870a | 605 | |
krenbluetoothsig | 0:3ed424a8870a | 606 | board_init(&addr, &seq); |
krenbluetoothsig | 0:3ed424a8870a | 607 | |
krenbluetoothsig | 0:3ed424a8870a | 608 | printk("Unicast address: 0x%04x, seq 0x%06x\n", addr, seq); |
krenbluetoothsig | 0:3ed424a8870a | 609 | |
krenbluetoothsig | 0:3ed424a8870a | 610 | /* Initialize the Bluetooth Subsystem */ |
krenbluetoothsig | 0:3ed424a8870a | 611 | err = bt_enable(bt_ready); |
krenbluetoothsig | 0:3ed424a8870a | 612 | if (err) { |
krenbluetoothsig | 0:3ed424a8870a | 613 | printk("Bluetooth init failed (err %d)\n", err); |
krenbluetoothsig | 0:3ed424a8870a | 614 | } |
krenbluetoothsig | 0:3ed424a8870a | 615 | |
krenbluetoothsig | 0:3ed424a8870a | 616 | while (1) { |
krenbluetoothsig | 0:3ed424a8870a | 617 | } |
krenbluetoothsig | 0:3ed424a8870a | 618 | |
krenbluetoothsig | 0:3ed424a8870a | 619 | } |