From a1034a205f05adf2bb2fa151dfca07575913dd15 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Tue, 15 Dec 2015 10:51:01 +0100 Subject: [PATCH] Bluetooth: Add DIS peripheral sample application This adds very simple peripheral application that supports Device Information Service only and doesn't support pairing. Change-Id: Iaec1317a8894db11388408d3f65af8a38d1618c3 Signed-off-by: Szymon Janc --- samples/bluetooth/peripheral_dis/Makefile | 7 + samples/bluetooth/peripheral_dis/prj.mdef | 5 + samples/bluetooth/peripheral_dis/prj_arm.conf | 3 + samples/bluetooth/peripheral_dis/prj_x86.conf | 3 + samples/bluetooth/peripheral_dis/src/Makefile | 1 + samples/bluetooth/peripheral_dis/src/main.c | 139 ++++++++++++++++++ samples/bluetooth/peripheral_dis/testcase.ini | 11 ++ 7 files changed, 169 insertions(+) create mode 100644 samples/bluetooth/peripheral_dis/Makefile create mode 100644 samples/bluetooth/peripheral_dis/prj.mdef create mode 100644 samples/bluetooth/peripheral_dis/prj_arm.conf create mode 100644 samples/bluetooth/peripheral_dis/prj_x86.conf create mode 100644 samples/bluetooth/peripheral_dis/src/Makefile create mode 100644 samples/bluetooth/peripheral_dis/src/main.c create mode 100644 samples/bluetooth/peripheral_dis/testcase.ini diff --git a/samples/bluetooth/peripheral_dis/Makefile b/samples/bluetooth/peripheral_dis/Makefile new file mode 100644 index 00000000000..19a3b3694df --- /dev/null +++ b/samples/bluetooth/peripheral_dis/Makefile @@ -0,0 +1,7 @@ +PLATFORM_CONFIG ?= qemu_x86 +MDEF_FILE = prj.mdef +KERNEL_TYPE = micro +CONF_FILE = prj_$(ARCH).conf +QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr + +include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/bluetooth/peripheral_dis/prj.mdef b/samples/bluetooth/peripheral_dis/prj.mdef new file mode 100644 index 00000000000..438ceccd459 --- /dev/null +++ b/samples/bluetooth/peripheral_dis/prj.mdef @@ -0,0 +1,5 @@ +% Application : Bluetooth peripheral Device Information Service sample + +% TASK NAME PRIO ENTRY STACK GROUPS +% =================================================== + TASK MAIN 7 mainloop 2048 [EXE] diff --git a/samples/bluetooth/peripheral_dis/prj_arm.conf b/samples/bluetooth/peripheral_dis/prj_arm.conf new file mode 100644 index 00000000000..8e286c6696c --- /dev/null +++ b/samples/bluetooth/peripheral_dis/prj_arm.conf @@ -0,0 +1,3 @@ +CONFIG_BLUETOOTH=y +CONFIG_BLUETOOTH_LE=y +CONFIG_BLUETOOTH_PERIPHERAL=y diff --git a/samples/bluetooth/peripheral_dis/prj_x86.conf b/samples/bluetooth/peripheral_dis/prj_x86.conf new file mode 100644 index 00000000000..8e286c6696c --- /dev/null +++ b/samples/bluetooth/peripheral_dis/prj_x86.conf @@ -0,0 +1,3 @@ +CONFIG_BLUETOOTH=y +CONFIG_BLUETOOTH_LE=y +CONFIG_BLUETOOTH_PERIPHERAL=y diff --git a/samples/bluetooth/peripheral_dis/src/Makefile b/samples/bluetooth/peripheral_dis/src/Makefile new file mode 100644 index 00000000000..00066e15678 --- /dev/null +++ b/samples/bluetooth/peripheral_dis/src/Makefile @@ -0,0 +1 @@ +obj-y = main.o diff --git a/samples/bluetooth/peripheral_dis/src/main.c b/samples/bluetooth/peripheral_dis/src/main.c new file mode 100644 index 00000000000..07933b738b2 --- /dev/null +++ b/samples/bluetooth/peripheral_dis/src/main.c @@ -0,0 +1,139 @@ +/* main.c - Application main entry point */ + +/* + * Copyright (c) 2015 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#define DEVICE_NAME "DIS peripheral" +#define APPEARANCE 0x0000 + +static int read_appearance(struct bt_conn *conn, + const struct bt_gatt_attr *attr, void *buf, + uint16_t len, uint16_t offset) +{ + uint16_t appearance = sys_cpu_to_le16(APPEARANCE); + + return bt_gatt_attr_read(conn, attr, buf, len, offset, &appearance, + sizeof(appearance)); +} + +static int read_string(struct bt_conn *conn, const struct bt_gatt_attr *attr, + void *buf, uint16_t len, uint16_t offset) +{ + const char *value = attr->user_data; + + return bt_gatt_attr_read(conn, attr, buf, len, offset, value, + strlen(value)); +} + +static struct bt_gatt_attr attrs[] = { + BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP), + BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ), + BT_GATT_DESCRIPTOR(BT_UUID_GAP_DEVICE_NAME, BT_GATT_PERM_READ, + read_string, NULL, DEVICE_NAME), + BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ), + BT_GATT_DESCRIPTOR(BT_UUID_GAP_APPEARANCE, BT_GATT_PERM_READ, + read_appearance, NULL, NULL), + /* Device Information Service Declaration */ + BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), + BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), + BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, + read_string, NULL, CONFIG_PLATFORM), + BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, + BT_GATT_CHRC_READ), + BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, + read_string, NULL, "Manufacturer"), +}; + +static const struct bt_eir ad[] = { + { + .len = 2, + .type = BT_EIR_FLAGS, + .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, + }, + { + .len = 3, + .type = BT_EIR_UUID16_ALL, + .data = { 0x0a, 0x18 }, + }, + { } +}; + +static const struct bt_eir sd[] = { + { + .len = 15, + .type = BT_EIR_NAME_COMPLETE, + .data = DEVICE_NAME, + }, + { } +}; + +static void connected(struct bt_conn *conn) +{ + printk("Connected\n"); +} + +static void disconnected(struct bt_conn *conn) +{ + printk("Disconnected\n"); +} + +static struct bt_conn_cb conn_callbacks = { + .connected = connected, + .disconnected = disconnected, +}; + +#ifdef CONFIG_MICROKERNEL +void mainloop(void) +#else +void main(void) +#endif +{ + int err; + + err = bt_enable(NULL); + if (err) { + printk("Bluetooth init failed (err %d)\n", err); + return; + } + + printk("Bluetooth initialized\n"); + + bt_gatt_register(attrs, ARRAY_SIZE(attrs)); + + bt_conn_cb_register(&conn_callbacks); + + err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, sd); + if (err) { + printk("Advertising failed to start (err %d)\n", err); + return; + } + + printk("Advertising successfully started\n"); +} diff --git a/samples/bluetooth/peripheral_dis/testcase.ini b/samples/bluetooth/peripheral_dis/testcase.ini new file mode 100644 index 00000000000..22c1cc1bb63 --- /dev/null +++ b/samples/bluetooth/peripheral_dis/testcase.ini @@ -0,0 +1,11 @@ +[test_x86] +tags = bluetooth +build_only = true +arch_whitelist = x86 +# FIXME Doesn't work for ia32_pci +config_whitelist = CONFIG_PLATFORM="ia32" + +[test_arm] +tags = bluetooth +build_only = true +arch_whitelist = arm