|
| 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 | +} |
0 commit comments