Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / src / platform / Zephyr / GenericBLEManagerImpl_Zephyr.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  *    @file
20  *          Provides an implementation of the BLEManager singleton object
21  *          for the Zephyr platforms.
22  */
23
24 #pragma once
25
26 #include <bluetooth/bluetooth.h>
27 #include <bluetooth/conn.h>
28 #include <bluetooth/gatt.h>
29
30 #include <support/logging/CHIPLogging.h>
31
32 namespace chip {
33 namespace DeviceLayer {
34 namespace Internal {
35
36 using namespace chip::Ble;
37
38 /**
39  * Template implementation of the GenericBLEManagerImpl_Zephyr singleton object for the Zephyr platforms.
40  */
41 template <class ImplClass>
42 class GenericBLEManagerImpl_Zephyr : public BLEManager,
43                                      private BleLayer,
44                                      private BlePlatformDelegate,
45                                      private BleApplicationDelegate
46 {
47     // Allow the BLEManager interface class to delegate method calls to
48     // the implementation methods provided by this class.
49     friend BLEManager;
50
51 private:
52     // ===== Members that implement the BLEManager internal interface.
53
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);
67
68     // ===== Members that implement virtual methods on BlePlatformDelegate.
69
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);
82
83     // ===== Members that implement virtual methods on BleApplicationDelegate.
84
85     void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId);
86
87     // ===== Private members reserved for use by this class only.
88
89     enum
90     {
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. */
97     };
98
99     struct ServiceData;
100
101     uint16_t mFlags;
102     uint16_t mGAPConns;
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;
107
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();
121
122     static void DriveBLEState(intptr_t arg);
123
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);
129
130 public:
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,
133                                  uint8_t flags);
134     static ssize_t HandleTXCCCWrite(bt_conn * conn, const bt_gatt_attr * attr, uint16_t value);
135 };
136
137 template <class ImplClass>
138 inline BleLayer * GenericBLEManagerImpl_Zephyr<ImplClass>::_GetBleLayer()
139 {
140     return this;
141 }
142
143 template <class ImplClass>
144 inline BLEManager::CHIPoBLEServiceMode GenericBLEManagerImpl_Zephyr<ImplClass>::_GetCHIPoBLEServiceMode(void)
145 {
146     return mServiceMode;
147 }
148
149 template <class ImplClass>
150 inline bool GenericBLEManagerImpl_Zephyr<ImplClass>::_IsAdvertisingEnabled(void)
151 {
152     return GetFlag(mFlags, kFlag_AdvertisingEnabled);
153 }
154
155 template <class ImplClass>
156 inline bool GenericBLEManagerImpl_Zephyr<ImplClass>::_IsFastAdvertisingEnabled(void)
157 {
158     return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
159 }
160
161 template <class ImplClass>
162 inline bool GenericBLEManagerImpl_Zephyr<ImplClass>::_IsAdvertising(void)
163 {
164     return GetFlag(mFlags, kFlag_Advertising);
165 }
166
167 } // namespace Internal
168 } // namespace DeviceLayer
169 } // namespace chip