03ab1b2d825e6e649705151b8c5e2624552d5289
[platform/upstream/connectedhomeip.git] / src / platform / Linux / CHIPBluezHelper.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /*
19  *  Copyright (c) 2016-2019, The OpenThread Authors.
20  *  All rights reserved.
21  *
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.
32  *
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.
44  */
45
46 /**
47  *    @file
48  *          Provides Bluez dbus implementation for BLE
49  */
50
51 #pragma once
52
53 #include "BLEManagerImpl.h"
54 #include <stdbool.h>
55 #include <stdint.h>
56 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
57
58 #include "ble/CHIPBleServiceData.h"
59 #include "platform/Linux/dbus/bluez/DbusBluez.h"
60
61 namespace chip {
62 namespace DeviceLayer {
63 namespace Internal {
64
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"
75
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"
78
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"
83
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
86
87 #define BLUEZ_ADV_TYPE_FLAGS 0x01
88 #define BLUEZ_ADV_TYPE_SERVICE_DATA 0x16
89
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)
95
96 #define CHAR_TO_BLUEZ(c) (static_cast<uint8_t>(((c) <= '9') ? (c) - '0' : tolower((c)) - 'a' + 10))
97
98 enum BluezAddressType
99 {
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.
104 };
105
106 struct BluezAddress
107 {
108     BluezAddressType mType;               ///< Bluetooth device address type.
109     uint8_t mAddress[BLUEZ_ADDRESS_SIZE]; ///< A 48-bit address of Bluetooth device in LSB format.
110 };
111
112 struct IOChannel
113 {
114     GIOChannel * mpChannel;
115     guint mWatch;
116 };
117
118 struct CHIPIdInfo
119 {
120     uint8_t mMajor;
121     uint8_t mMinor;
122     uint16_t mVendorId;
123     uint16_t mProductId;
124     uint64_t mDeviceId;
125     uint8_t mPairingStatus;
126 } __attribute__((packed));
127
128 struct CHIPServiceData
129 {
130     uint8_t mDataBlock0Len;
131     uint8_t mDataBlock0Type;
132     CHIPIdInfo mIdInfo;
133 } __attribute__((packed));
134
135 struct BluezDiscoveryRequest
136 {
137     uint16_t mDiscriminator;
138     bool mAutoConnect;
139 };
140
141 struct BluezEndpoint
142 {
143     char * mpOwningName; // Bus owning name
144
145     // Adapter properties
146     char * mpAdapterName;
147     char * mpAdapterAddr;
148
149     // Paths for objects published by this service
150     char * mpRootPath;
151     char * mpAdvPath;
152     char * mpServicePath;
153
154     // Objects (interfaces) subscibed to by this service
155     GDBusObjectManager * mpObjMgr;
156     BluezAdapter1 * mpAdapter;
157     BluezDevice1 * mpDevice;
158
159     // Objects (interfaces) published by this service
160     GDBusObjectManagerServer * mpRoot;
161     BluezGattService1 * mpService;
162     BluezGattCharacteristic1 * mpC1;
163     BluezGattCharacteristic1 * mpC2;
164
165     // map device path to the connection
166     GHashTable * mpConnMap;
167     uint32_t mNodeId;
168     bool mIsNotify;
169     bool mIsCentral;
170     char * mpAdvertisingUUID;
171     chip::Ble::ChipBLEDeviceIdentificationInfo mDeviceIdInfo;
172     ChipAdvType mType;  ///< Advertisement type.
173     uint16_t mDuration; ///< Advertisement interval (in ms).
174     bool mIsAdvertising;
175     char * mpPeerDevicePath;
176
177     // Discovery settings
178     BluezDiscoveryRequest mDiscoveryRequest = {};
179 };
180
181 struct BluezConnection
182 {
183     char * mpPeerAddress;
184     BluezDevice1 * mpDevice;
185     BluezGattService1 * mpService;
186     BluezGattCharacteristic1 * mpC1;
187     BluezGattCharacteristic1 * mpC2;
188     bool mIsNotify;
189     uint16_t mMtu;
190     struct IOChannel mC1Channel;
191     struct IOChannel mC2Channel;
192     BluezEndpoint * mpEndpoint;
193 };
194
195 struct ConnectionDataBundle
196 {
197     BluezConnection * mpConn;
198     GVariant * mpVal;
199 };
200
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);
209
210 CHIP_ERROR StartDiscovery(BluezEndpoint * apEndpoint, BluezDiscoveryRequest aRequest = {});
211 CHIP_ERROR StopDiscovery(BluezEndpoint * apEndpoint);
212
213 CHIP_ERROR ConnectDevice(BluezDevice1 * apDevice);
214
215 } // namespace Internal
216 } // namespace DeviceLayer
217 } // namespace chip
218
219 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE