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.
20 * Provides an implementation of the BLEManager singleton object
21 * for the Zephyr platforms.
26 #include <bluetooth/bluetooth.h>
27 #include <bluetooth/conn.h>
28 #include <bluetooth/gatt.h>
30 #include <support/logging/CHIPLogging.h>
33 namespace DeviceLayer {
36 using namespace chip::Ble;
39 * Template implementation of the GenericBLEManagerImpl_Zephyr singleton object for the Zephyr platforms.
41 template <class ImplClass>
42 class GenericBLEManagerImpl_Zephyr : public BLEManager,
44 private BlePlatformDelegate,
45 private BleApplicationDelegate
47 // Allow the BLEManager interface class to delegate method calls to
48 // the implementation methods provided by this class.
52 // ===== Members that implement the BLEManager internal interface.
54 CHIP_ERROR _Init(void);
55 CHIPoBLEServiceMode _GetCHIPoBLEServiceMode(void);
56 CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
57 bool _IsAdvertisingEnabled(void);
58 CHIP_ERROR _SetAdvertisingEnabled(bool val);
59 bool _IsFastAdvertisingEnabled(void);
60 CHIP_ERROR _SetFastAdvertisingEnabled(bool val);
61 bool _IsAdvertising(void);
62 CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
63 CHIP_ERROR _SetDeviceName(const char * deviceName);
64 uint16_t _NumConnections(void);
65 void _OnPlatformEvent(const ChipDeviceEvent * event);
66 BleLayer * _GetBleLayer(void);
68 // ===== Members that implement virtual methods on BlePlatformDelegate.
70 bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId);
71 bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId);
72 bool CloseConnection(BLE_CONNECTION_OBJECT conId);
73 uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const;
74 bool SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
75 PacketBufferHandle pBuf);
76 bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
77 PacketBufferHandle pBuf);
78 bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
79 PacketBufferHandle pBuf);
80 bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const ChipBleUUID * svcId,
81 const ChipBleUUID * charId);
83 // ===== Members that implement virtual methods on BleApplicationDelegate.
85 void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId);
87 // ===== Private members reserved for use by this class only.
91 kFlag_AsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
92 kFlag_AdvertisingEnabled = 0x0002, /**< The application has enabled CHIPoBLE advertising. */
93 kFlag_FastAdvertisingEnabled = 0x0004, /**< The application has enabled fast advertising. */
94 kFlag_Advertising = 0x0008, /**< The system is currently CHIPoBLE advertising. */
95 kFlag_AdvertisingRefreshNeeded =
96 0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */
103 CHIPoBLEServiceMode mServiceMode;
104 bool mSubscribedConns[CONFIG_BT_MAX_CONN];
105 bt_gatt_indicate_params mIndicateParams[CONFIG_BT_MAX_CONN];
106 bt_conn_cb mConnCallbacks;
108 void DriveBLEState(void);
109 CHIP_ERROR ConfigureAdvertising(void);
110 CHIP_ERROR StartAdvertising(void);
111 CHIP_ERROR StopAdvertising(void);
112 CHIP_ERROR HandleGAPConnect(const ChipDeviceEvent * event);
113 CHIP_ERROR HandleGAPDisconnect(const ChipDeviceEvent * event);
114 CHIP_ERROR HandleRXCharWrite(const ChipDeviceEvent * event);
115 CHIP_ERROR HandleTXCharCCCDWrite(const ChipDeviceEvent * event);
116 CHIP_ERROR HandleTXComplete(const ChipDeviceEvent * event);
117 bool IsSubscribed(bt_conn * conn);
118 bool SetSubscribed(bt_conn * conn);
119 bool UnsetSubscribed(bt_conn * conn);
120 uint32_t GetAdvertisingInterval();
122 static void DriveBLEState(intptr_t arg);
124 // Below callbacks run from the system workqueue context and have a limited stack capacity.
125 static void HandleTXIndicated(bt_conn * conn, const bt_gatt_attr * attr, uint8_t err);
126 static void HandleConnect(bt_conn * conn, uint8_t err);
127 static void HandleDisconnect(bt_conn * conn, uint8_t reason);
128 static void HandleBLEAdvertisementTimeout(System::Layer * layer, void * param, System::Error error);
131 // Below callbacks are public in order to be visible from the global scope.
132 static ssize_t HandleRXWrite(bt_conn * conn, const bt_gatt_attr * attr, const void * buf, uint16_t len, uint16_t offset,
134 static ssize_t HandleTXCCCWrite(bt_conn * conn, const bt_gatt_attr * attr, uint16_t value);
137 template <class ImplClass>
138 inline BleLayer * GenericBLEManagerImpl_Zephyr<ImplClass>::_GetBleLayer()
143 template <class ImplClass>
144 inline BLEManager::CHIPoBLEServiceMode GenericBLEManagerImpl_Zephyr<ImplClass>::_GetCHIPoBLEServiceMode(void)
149 template <class ImplClass>
150 inline bool GenericBLEManagerImpl_Zephyr<ImplClass>::_IsAdvertisingEnabled(void)
152 return GetFlag(mFlags, kFlag_AdvertisingEnabled);
155 template <class ImplClass>
156 inline bool GenericBLEManagerImpl_Zephyr<ImplClass>::_IsFastAdvertisingEnabled(void)
158 return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
161 template <class ImplClass>
162 inline bool GenericBLEManagerImpl_Zephyr<ImplClass>::_IsAdvertising(void)
164 return GetFlag(mFlags, kFlag_Advertising);
167 } // namespace Internal
168 } // namespace DeviceLayer