3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * Copyright (c) 2016-2019, The OpenThread Authors.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. Neither the name of the copyright holder nor the
30 * names of its contributors may be used to endorse or promote products
31 * derived from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
37 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 * POSSIBILITY OF SUCH DAMAGE.
48 * Provides Bluez dbus implementation for BLE
53 #include "BLEManagerImpl.h"
56 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
58 #include "ble/CHIPBleServiceData.h"
59 #include "platform/Linux/dbus/bluez/DbusBluez.h"
62 namespace DeviceLayer {
65 #define BLUEZ_ADDRESS_SIZE 6 ///< BLE address size (in bytes)
66 #define BLUEZ_PATH "/org/bluez"
67 #define BLUEZ_INTERFACE "org.bluez"
68 #define ADAPTER_INTERFACE BLUEZ_INTERFACE ".Adapter1"
69 #define PROFILE_INTERFACE BLUEZ_INTERFACE ".GattManager1"
70 #define ADVERTISING_MANAGER_INTERFACE BLUEZ_INTERFACE ".LEAdvertisingManager1"
71 #define SERVICE_INTERFACE BLUEZ_INTERFACE ".GattService1"
72 #define CHARACTERISTIC_INTERFACE BLUEZ_INTERFACE ".GattCharacteristic1"
73 #define ADVERTISING_INTERFACE BLUEZ_INTERFACE ".LEAdvertisement1"
74 #define DEVICE_INTERFACE BLUEZ_INTERFACE ".Device1"
76 #define CHIP_PLAT_BLE_UUID_C1_STRING "18ee2ef5-263d-4559-959f-4f9c429f9d11"
77 #define CHIP_PLAT_BLE_UUID_C2_STRING "18ee2ef5-263d-4559-959f-4f9c429f9d12"
79 #define CHIP_BLE_BASE_SERVICE_UUID_STRING "-0000-1000-8000-00805f9b34fb"
80 #define CHIP_BLE_SERVICE_PREFIX_LENGTH 8
81 #define CHIP_BLE_BASE_SERVICE_PREFIX "0000"
82 #define CHIP_BLE_UUID_SERVICE_SHORT_STRING "fffb"
84 #define CHIP_BLE_UUID_SERVICE_STRING \
85 CHIP_BLE_BASE_SERVICE_PREFIX CHIP_BLE_UUID_SERVICE_SHORT_STRING CHIP_BLE_BASE_SERVICE_UUID_STRING
87 #define BLUEZ_ADV_TYPE_FLAGS 0x01
88 #define BLUEZ_ADV_TYPE_SERVICE_DATA 0x16
90 #define BLUEZ_ADV_FLAGS_LE_LIMITED (1 << 0)
91 #define BLUEZ_ADV_FLAGS_LE_DISCOVERABLE (1 << 1)
92 #define BLUEZ_ADV_FLAGS_EDR_UNSUPPORTED (1 << 2)
93 #define BLUEZ_ADV_FLAGS_LE_EDR_CONTROLLER (1 << 3)
94 #define BLUEZ_ADV_FLAGS_LE_EDR_HOST (1 << 4)
96 #define CHAR_TO_BLUEZ(c) (static_cast<uint8_t>(((c) <= '9') ? (c) - '0' : tolower((c)) - 'a' + 10))
100 BLUEZ_ADDRESS_TYPE_PUBLIC = 0, ///< Bluetooth public device address.
101 BLUEZ_ADDRESS_TYPE_RANDOM_STATIC = 1, ///< Bluetooth random static address.
102 BLUEZ_ADDRESS_TYPE_RANDOM_PRIVATE_RESOLVABLE = 2, ///< Bluetooth random private resolvable address.
103 BLUEZ_ADDRESS_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = 3, ///< Bluetooth random private non-resolvable address.
108 BluezAddressType mType; ///< Bluetooth device address type.
109 uint8_t mAddress[BLUEZ_ADDRESS_SIZE]; ///< A 48-bit address of Bluetooth device in LSB format.
114 GIOChannel * mpChannel;
125 uint8_t mPairingStatus;
126 } __attribute__((packed));
128 struct CHIPServiceData
130 uint8_t mDataBlock0Len;
131 uint8_t mDataBlock0Type;
133 } __attribute__((packed));
135 struct BluezDiscoveryRequest
137 uint16_t mDiscriminator;
143 char * mpOwningName; // Bus owning name
145 // Adapter properties
146 char * mpAdapterName;
147 char * mpAdapterAddr;
149 // Paths for objects published by this service
152 char * mpServicePath;
154 // Objects (interfaces) subscibed to by this service
155 GDBusObjectManager * mpObjMgr;
156 BluezAdapter1 * mpAdapter;
157 BluezDevice1 * mpDevice;
159 // Objects (interfaces) published by this service
160 GDBusObjectManagerServer * mpRoot;
161 BluezGattService1 * mpService;
162 BluezGattCharacteristic1 * mpC1;
163 BluezGattCharacteristic1 * mpC2;
165 // map device path to the connection
166 GHashTable * mpConnMap;
170 char * mpAdvertisingUUID;
171 chip::Ble::ChipBLEDeviceIdentificationInfo mDeviceIdInfo;
172 ChipAdvType mType; ///< Advertisement type.
173 uint16_t mDuration; ///< Advertisement interval (in ms).
175 char * mpPeerDevicePath;
177 // Discovery settings
178 BluezDiscoveryRequest mDiscoveryRequest = {};
181 struct BluezConnection
183 char * mpPeerAddress;
184 BluezDevice1 * mpDevice;
185 BluezGattService1 * mpService;
186 BluezGattCharacteristic1 * mpC1;
187 BluezGattCharacteristic1 * mpC2;
190 struct IOChannel mC1Channel;
191 struct IOChannel mC2Channel;
192 BluezEndpoint * mpEndpoint;
195 struct ConnectionDataBundle
197 BluezConnection * mpConn;
201 CHIP_ERROR InitBluezBleLayer(bool aIsCentral, char * apBleAddr, BLEAdvConfig & aBleAdvConfig, void *& apEndpoint);
202 bool BluezRunOnBluezThread(int (*aCallback)(void *), void * apClosure);
203 bool SendBluezIndication(BLE_CONNECTION_OBJECT apConn, chip::System::PacketBufferHandle apBuf);
204 bool CloseBluezConnection(BLE_CONNECTION_OBJECT apConn);
205 CHIP_ERROR StartBluezAdv(BluezEndpoint * apEndpoint);
206 CHIP_ERROR StopBluezAdv(BluezEndpoint * apEndpoint);
207 CHIP_ERROR BluezGattsAppRegister(BluezEndpoint * apEndpoint);
208 CHIP_ERROR BluezAdvertisementSetup(BluezEndpoint * apEndpoint);
210 CHIP_ERROR StartDiscovery(BluezEndpoint * apEndpoint, BluezDiscoveryRequest aRequest = {});
211 CHIP_ERROR StopDiscovery(BluezEndpoint * apEndpoint);
213 CHIP_ERROR ConnectDevice(BluezDevice1 * apDevice);
215 } // namespace Internal
216 } // namespace DeviceLayer
219 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE