Skip to content

Commit 0212e57

Browse files
robertxiaoxiang781216
robert
authored andcommitted
Added demo for BLE
Summary --------------------------------------------------------------------------------------------------------- Created a minimal demo application for Bluetooth. It works by displaying some data over Bluetooth and the user can connect to the device running it on NuttX and read this data. This can work in 2 scenarios: a real scenario, in which data from the BME680 (temperature, humidity, etc..) is used and the user will be able to read it. The second scenario involves displaying some dummy data (hardcoded values) for testing purposes. This application can serve as an example for users implementing more complex applications using Bluetooth on NuttX. Testing --------------------------------------------------------------------------------------------------------- I used the ESP32-Sparrow (which includes the BME680 sensor) and the ESP32-devkitc boards for testing. The Bluetooth stack starts by default with advertising when it initializes, however I noticed that enabling advertising in the stack does not work (at least on ESP32). However, when scanning is also enabled, the device starts advertising. Therefore, in our application, during the BLE services initialization part, I also enable scanning as a temporary workaround in order to make sure the device advertises and the user can see it and connect to it.
1 parent 3799e09 commit 0212e57

File tree

9 files changed

+802
-0
lines changed

9 files changed

+802
-0
lines changed

examples/ble/CMakeLists.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# ##############################################################################
2+
# apps/examples/ble/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_EXAMPLES_BLE)
22+
nuttx_add_application(
23+
NAME
24+
${CONFIG_EXAMPLES_BLE_PROGNAME}
25+
PRIORITY
26+
${CONFIG_EXAMPLES_BLE_PRIORITY}
27+
STACKSIZE
28+
${CONFIG_EXAMPLES_BLE_STACKSIZE}
29+
MODULE
30+
${CONFIG_EXAMPLES_BLE}
31+
SRCS
32+
ble_main.c)
33+
endif()

examples/ble/Kconfig

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_BLE
7+
tristate "BLE sensor reading example"
8+
default n
9+
depends on WIRELESS_BLUETOOTH
10+
---help---
11+
Enable the BLE example
12+
13+
if EXAMPLES_BLE
14+
15+
config EXAMPLES_BLE_PROGNAME
16+
string "Program name"
17+
default "ble"
18+
---help---
19+
This is the name of the program that will be used when the NSH ELF
20+
program is installed.
21+
22+
config EXAMPLES_BLE_PRIORITY
23+
int "BLE task priority"
24+
default 100
25+
26+
config EXAMPLES_BLE_STACKSIZE
27+
int "BLE stack size"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
config EXAMPLES_BLE_USE_BME680
31+
bool "Use BME680 sensor data"
32+
default n
33+
depends on SENSORS_BME680
34+
---help---
35+
If enabled, use real BME680 sensor data. If disabled, use dummy data.
36+
37+
endif

examples/ble/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/ble/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifneq ($(CONFIG_EXAMPLES_BLE),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/ble
23+
endif

examples/ble/Makefile

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
############################################################################
2+
# apps/examples/ble/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/Make.defs
22+
23+
# BLE Barometer sensor example built-in application info
24+
25+
PROGNAME = $(CONFIG_EXAMPLES_BLE_PROGNAME)
26+
PRIORITY = $(CONFIG_EXAMPLES_BLE_PRIORITY)
27+
STACKSIZE = $(CONFIG_EXAMPLES_BLE_STACKSIZE)
28+
MODULE = $(CONFIG_EXAMPLES_BLE)
29+
30+
CSRCS = ble.c
31+
MAINSRC = ble_main.c
32+
33+
ifeq ($(CONFIG_EXAMPLES_BLE_USE_BME680),y)
34+
CSRCS += sensors.c
35+
else
36+
CSRCS += dummy.c
37+
endif
38+
39+
include $(APPDIR)/Application.mk

examples/ble/ble.c

+248
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/****************************************************************************
2+
* apps/examples/ble/ble.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/wireless/bluetooth/bt_core.h>
26+
#include <nuttx/wireless/bluetooth/bt_gatt.h>
27+
#include <nuttx/wireless/bluetooth/bt_ioctl.h>
28+
#include <netpacket/bluetooth.h>
29+
#include "sensors.h"
30+
31+
/****************************************************************************
32+
* Pre-processor Definitions
33+
****************************************************************************/
34+
35+
#define GASVC 0x0001
36+
#define NAME_CHR (GASVC + 0x0002)
37+
#define NAME_DSC (GASVC + 0x0003)
38+
#define APPEARANCE_CHR (GASVC + 0x0004)
39+
#define APPEARANCE_DSC (GASVC + 0x0005)
40+
#define SENSOR_SVC (APPEARANCE_DSC + 0x0001)
41+
#define TEMP_CHR (SENSOR_SVC + 0x0001)
42+
#define TEMP_DSC (SENSOR_SVC + 0x0002)
43+
#define HUM_CHR (TEMP_DSC + 0x0001)
44+
#define HUM_DSC (TEMP_DSC + 0x0002)
45+
#define GAS_CHR (HUM_DSC + 0x0001)
46+
#define GAS_DSC (HUM_DSC + 0x0002)
47+
#define PRESS_CHR (GAS_DSC + 0x0001)
48+
#define PRESS_DSC (GAS_DSC + 0x0002)
49+
50+
/* Bluetooth UUIDs */
51+
52+
static struct bt_uuid_s g_gap_uuid =
53+
{
54+
.type = BT_UUID_16,
55+
.u.u16 = BT_UUID_GAP,
56+
};
57+
58+
static struct bt_uuid_s g_device_name_uuid =
59+
{
60+
.type = BT_UUID_16,
61+
.u.u16 = BT_UUID_GAP_DEVICE_NAME,
62+
};
63+
64+
static struct bt_uuid_s g_appearance_uuid =
65+
{
66+
.type = BT_UUID_16,
67+
.u.u16 = BT_UUID_GAP_APPEARANCE,
68+
};
69+
70+
static struct bt_uuid_s g_sensor_uuid =
71+
{
72+
.type = BT_UUID_128,
73+
.u.u128 =
74+
{
75+
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
76+
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
77+
}
78+
};
79+
80+
static struct bt_uuid_s g_temp_uuid =
81+
{
82+
.type = BT_UUID_16,
83+
.u.u16 = 0x2a6e, /* Temperature UUID */
84+
};
85+
86+
static struct bt_uuid_s g_hum_uuid =
87+
{
88+
.type = BT_UUID_16,
89+
.u.u16 = 0x2a6f, /* Humidity UUID */
90+
};
91+
92+
static struct bt_uuid_s g_press_uuid =
93+
{
94+
.type = BT_UUID_16,
95+
.u.u16 = 0x2a6d, /* Pressure UUID */
96+
};
97+
98+
/* GATT characteristics */
99+
100+
static struct bt_gatt_chrc_s g_name_chrc =
101+
{
102+
.properties = BT_GATT_CHRC_READ,
103+
.value_handle = NAME_DSC,
104+
.uuid = &g_device_name_uuid,
105+
};
106+
107+
static struct bt_gatt_chrc_s g_appearance_chrc =
108+
{
109+
.properties = BT_GATT_CHRC_READ,
110+
.value_handle = APPEARANCE_DSC,
111+
.uuid = &g_appearance_uuid,
112+
};
113+
114+
static struct bt_gatt_chrc_s g_temp_chrc =
115+
{
116+
.properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
117+
.value_handle = TEMP_DSC,
118+
.uuid = &g_temp_uuid,
119+
};
120+
121+
static struct bt_gatt_chrc_s g_hum_chrc =
122+
{
123+
.properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
124+
.value_handle = HUM_DSC,
125+
.uuid = &g_hum_uuid,
126+
};
127+
128+
static struct bt_gatt_chrc_s g_press_chrc =
129+
{
130+
.properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
131+
.value_handle = PRESS_DSC,
132+
.uuid = &g_press_uuid,
133+
};
134+
135+
static int read_name(FAR struct bt_conn_s *conn,
136+
FAR const struct bt_gatt_attr_s *attr,
137+
FAR void *buf, uint8_t len, uint16_t offset)
138+
{
139+
const char *name = CONFIG_DEVICE_NAME;
140+
return bt_gatt_attr_read(conn, attr, buf, len, offset, name, strlen(name));
141+
}
142+
143+
static int read_appearance(FAR struct bt_conn_s *conn,
144+
FAR const struct bt_gatt_attr_s *attr,
145+
FAR void *buf, uint8_t len, uint16_t offset)
146+
{
147+
uint16_t appearance = 0; /* Set appropriate appearance value */
148+
return bt_gatt_attr_read(conn, attr, buf, len, offset,
149+
&appearance, sizeof(appearance));
150+
}
151+
152+
static int read_temperature(FAR struct bt_conn_s *conn,
153+
FAR const struct bt_gatt_attr_s *attr,
154+
FAR void *buf, uint8_t len, uint16_t offset)
155+
{
156+
uint16_t temp = (uint16_t) (get_temperature() * 100);
157+
return bt_gatt_attr_read(conn, attr, buf, len, offset,
158+
&temp, sizeof(uint16_t));
159+
}
160+
161+
static int read_humidity(FAR struct bt_conn_s *conn,
162+
FAR const struct bt_gatt_attr_s *attr,
163+
FAR void *buf, uint8_t len, uint16_t offset)
164+
{
165+
uint16_t hum = (uint16_t) (get_humidity() * 100);
166+
return bt_gatt_attr_read(conn, attr, buf, len, offset,
167+
&hum, sizeof(uint16_t));
168+
}
169+
170+
static int read_gas(FAR struct bt_conn_s *conn,
171+
FAR const struct bt_gatt_attr_s *attr,
172+
FAR void *buf, uint8_t len, uint16_t offset)
173+
{
174+
uint16_t gas = (uint16_t) (get_gas_resistance() * 100);
175+
return bt_gatt_attr_read(conn, attr, buf, len, offset,
176+
&gas, sizeof(uint16_t));
177+
}
178+
179+
static int read_pressure(FAR struct bt_conn_s *conn,
180+
FAR const struct bt_gatt_attr_s *attr,
181+
FAR void *buf, uint8_t len, uint16_t offset)
182+
{
183+
uint32_t press = (uint32_t) (get_pressure() * 1000);
184+
return bt_gatt_attr_read(conn, attr, buf, len, offset,
185+
&press, sizeof(uint32_t));
186+
}
187+
188+
/* GATT attributes */
189+
190+
static const struct bt_gatt_attr_s attrs[] =
191+
{
192+
BT_GATT_PRIMARY_SERVICE(GASVC, &g_gap_uuid),
193+
BT_GATT_CHARACTERISTIC(NAME_CHR, &g_name_chrc),
194+
BT_GATT_DESCRIPTOR(NAME_DSC, &g_device_name_uuid, BT_GATT_PERM_READ,
195+
read_name, NULL, NULL),
196+
BT_GATT_CHARACTERISTIC(APPEARANCE_CHR, &g_appearance_chrc),
197+
BT_GATT_DESCRIPTOR(APPEARANCE_DSC, &g_appearance_uuid,
198+
BT_GATT_PERM_READ, read_appearance, NULL, NULL),
199+
200+
BT_GATT_PRIMARY_SERVICE(SENSOR_SVC, &g_sensor_uuid),
201+
BT_GATT_CHARACTERISTIC(TEMP_CHR, &g_temp_chrc),
202+
BT_GATT_DESCRIPTOR(TEMP_DSC, &g_temp_uuid, BT_GATT_PERM_READ,
203+
read_temperature, NULL, NULL),
204+
BT_GATT_CHARACTERISTIC(HUM_CHR, &g_hum_chrc),
205+
BT_GATT_DESCRIPTOR(HUM_DSC, &g_hum_uuid, BT_GATT_PERM_READ,
206+
read_humidity, NULL, NULL),
207+
BT_GATT_CHARACTERISTIC(PRESS_CHR, &g_press_chrc),
208+
BT_GATT_DESCRIPTOR(PRESS_DSC, &g_press_uuid, BT_GATT_PERM_READ,
209+
read_pressure, NULL, NULL),
210+
};
211+
212+
static void start_scanning(void)
213+
{
214+
struct btreq_s btreq;
215+
memset(&btreq, 0, sizeof(struct btreq_s));
216+
strlcpy(btreq.btr_name, "bnep0", 16);
217+
btreq.btr_dupenable = false;
218+
219+
int sockfd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
220+
if (sockfd < 0)
221+
{
222+
fprintf(stderr, "ERROR: failed to create socket\n");
223+
return;
224+
}
225+
int ret = ioctl(sockfd, SIOCBTSCANSTART,
226+
(unsigned long)((uintptr_t)&btreq));
227+
if (ret < 0)
228+
{
229+
fprintf(stderr, "ERROR: ioctl(SIOCBTSCANSTART) failed\n");
230+
}
231+
close(sockfd);
232+
}
233+
234+
/****************************************************************************
235+
* Public Functions
236+
****************************************************************************/
237+
238+
/****************************************************************************
239+
* setup_ble
240+
****************************************************************************/
241+
242+
void setup_ble(void)
243+
{
244+
/* Scanning is enabled here to ensure advertising packets are sent. */
245+
246+
start_scanning();
247+
bt_gatt_register(attrs, nitems(attrs));
248+
}

0 commit comments

Comments
 (0)