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