Skip to content

Commit 772807c

Browse files
chengkai15xiaoxiang781216
authored andcommitted
bluetooth: add bt_driver_register interface
add bt_driver_register interface, which could handle these cases:bth4 bth5 btbridge btslip and btuart_lowerhalf_s etc. Signed-off-by: chengkai <[email protected]>
1 parent b66f114 commit 772807c

File tree

5 files changed

+164
-2
lines changed

5 files changed

+164
-2
lines changed

drivers/wireless/bluetooth/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
if DRIVERS_BLUETOOTH
77

8+
config BLUETOOTH_DEVICE_ID
9+
int "Bluetooth Device ID, eg: /dev/ttyBT0"
10+
default 0
11+
---help---
12+
Bluetooth Device ID.
13+
814
config BLUETOOTH_UART
915
bool "Bluetooth UART driver"
1016
default n

drivers/wireless/bluetooth/Make.defs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ ifeq ($(CONFIG_DRIVERS_BLUETOOTH),y)
2424

2525
# Include Bluetooth drivers into the build
2626

27+
CSRCS += bt_driver.c
28+
2729
ifeq ($(CONFIG_BLUETOOTH_UART),y)
2830
CSRCS += bt_uart.c
2931
ifeq ($(CONFIG_BLUETOOTH_UART_GENERIC),y)
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/****************************************************************************
2+
* drivers/wireless/bluetooth/bt_driver.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/config.h>
26+
#include <nuttx/wireless/bluetooth/bt_driver.h>
27+
28+
#ifdef CONFIG_UART_BTH4
29+
# include <nuttx/serial/uart_bth4.h>
30+
#endif
31+
32+
#ifdef CONFIG_BLUETOOTH_BRIDGE
33+
# include <nuttx/wireless/bluetooth/bt_bridge.h>
34+
#endif
35+
36+
#ifdef CONFIG_BLUETOOTH_SLIP
37+
# include <nuttx/wireless/bluetooth/bt_slip.h>
38+
#endif
39+
40+
#include <stdio.h>
41+
42+
/****************************************************************************
43+
* Private Functions
44+
****************************************************************************/
45+
46+
static int bt_driver_register_internal(FAR struct bt_driver_s *driver,
47+
int id, bool bt)
48+
{
49+
#ifdef CONFIG_UART_BTH4
50+
char name[32];
51+
52+
if (bt)
53+
{
54+
snprintf(name, sizeof(name), "/dev/ttyBT%d", id);
55+
}
56+
else
57+
{
58+
snprintf(name, sizeof(name), "/dev/ttyBLE%d", id);
59+
}
60+
61+
return uart_bth4_register(name, driver);
62+
#elif defined(CONFIG_NET_BLUETOOTH)
63+
return bt_netdev_register(driver);
64+
#else
65+
# error "Please select CONFIG_UART_BTH4 or CONFIG_NET_BLUETOOTH"
66+
#endif
67+
}
68+
69+
/****************************************************************************
70+
* Public Functions
71+
****************************************************************************/
72+
73+
/****************************************************************************
74+
* Name: bt_driver_register_with_id
75+
*
76+
* Description:
77+
* Register bluetooth driver.
78+
*
79+
* Input Parameters:
80+
* driver - an instance of the bt_driver_s interface
81+
* id - bluetooth device id
82+
*
83+
* Returned Value:
84+
* Zero is returned on success; a negated errno value is returned on any
85+
* failure.
86+
*
87+
****************************************************************************/
88+
89+
int bt_driver_register_with_id(FAR struct bt_driver_s *driver, int id)
90+
{
91+
#ifdef CONFIG_BLUETOOTH_BRIDGE
92+
FAR struct bt_driver_s *btdrv;
93+
FAR struct bt_driver_s *bledrv;
94+
#endif
95+
int ret;
96+
97+
#ifdef CONFIG_BLUETOOTH_SLIP
98+
driver = bt_slip_register(driver);
99+
if (driver == NULL)
100+
{
101+
return -ENOMEM;
102+
}
103+
#endif
104+
105+
#ifdef CONFIG_BLUETOOTH_BRIDGE
106+
ret = bt_bridge_register(driver, &btdrv, &bledrv);
107+
if (ret < 0)
108+
{
109+
return ret;
110+
}
111+
112+
ret = bt_driver_register_internal(btdrv, id, true);
113+
if (ret < 0)
114+
{
115+
return ret;
116+
}
117+
118+
ret = bt_driver_register_internal(bledrv, id, false);
119+
if (ret < 0)
120+
{
121+
return ret;
122+
}
123+
124+
#else
125+
ret = bt_driver_register_internal(driver, id, true);
126+
if (ret < 0)
127+
{
128+
return ret;
129+
}
130+
#endif
131+
132+
return ret;
133+
}

drivers/wireless/bluetooth/bt_uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ int btuart_register(FAR const struct btuart_lowerhalf_s *lower)
353353
return ret;
354354
}
355355

356-
ret = bt_netdev_register(driver);
356+
ret = bt_driver_register(driver);
357357
if (ret < 0)
358358
{
359-
wlerr("ERROR: bt_netdev_register failed: %d\n", ret);
359+
wlerr("ERROR: bt_driver_register failed: %d\n", ret);
360360
kmm_free(driver);
361361
}
362362

include/nuttx/wireless/bluetooth/bt_driver.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#define bt_netdev_receive(btdev, type, data, len) \
5050
(btdev)->receive(btdev, type, data, len)
5151

52+
#define bt_driver_register(btdev) \
53+
bt_driver_register_with_id(btdev, CONFIG_BLUETOOTH_DEVICE_ID)
54+
5255
/****************************************************************************
5356
* Public Types
5457
****************************************************************************/
@@ -136,4 +139,22 @@ int bt_netdev_register(FAR struct bt_driver_s *btdev);
136139

137140
int bt_netdev_unregister(FAR struct bt_driver_s *btdev);
138141

142+
/****************************************************************************
143+
* Name: bt_driver_register_with_id
144+
*
145+
* Description:
146+
* Register bluetooth driver.
147+
*
148+
* Input Parameters:
149+
* driver - an instance of the bt_driver_s interface
150+
* id - bluetooth device id
151+
*
152+
* Returned Value:
153+
* Zero is returned on success; a negated errno value is returned on any
154+
* failure.
155+
*
156+
****************************************************************************/
157+
158+
int bt_driver_register_with_id(FAR struct bt_driver_s *driver, int id);
159+
139160
#endif /* __INCLUDE_NUTTX_WIRELESS_BLUETOOTH_BT_DRIVER_H */

0 commit comments

Comments
 (0)