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