Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * This file defines the CHIP Connection object that maintains a BLE connection.
22 : *
23 : */
24 :
25 : #pragma once
26 :
27 : #include <utility>
28 :
29 : #include <ble/Ble.h>
30 : #include <lib/core/CHIPCore.h>
31 : #include <lib/support/DLLUtil.h>
32 : #include <system/SystemPacketBuffer.h>
33 : #include <transport/raw/Base.h>
34 :
35 : namespace chip {
36 : namespace Transport {
37 :
38 : /** Defines listening parameters for setting up a BLE transport */
39 : class BleListenParameters
40 : {
41 : public:
42 1 : explicit BleListenParameters(Ble::BleLayer * layer) : mLayer(layer) {}
43 : BleListenParameters(const BleListenParameters &) = default;
44 : BleListenParameters(BleListenParameters &&) = default;
45 :
46 1 : Ble::BleLayer * GetBleLayer() const { return mLayer; }
47 :
48 : /**
49 : * PreserveExistingBleLayerTransport controls whether the BleBase transport
50 : * initialized with these parameters should update the global BleLayer transport
51 : * if it is already set.
52 : *
53 : * This is relevant when there is more than one TransportMgr,
54 : * for example, when a device is both a commissioner (has a CHIPDeviceController)
55 : * and a commissionee (has a Server) since each TransportMgr will have a BleBase
56 : * which can override the global BleLayer transport to point to itself.
57 : *
58 : * The default value is true - don't override the global BleLayer transport if it is
59 : * already set. In other words, the first BleBase to initialize (eg. Server) will be
60 : * the active BleBase transport, and the second BleBase to initialize (eg. CHIPDeviceController)
61 : * will need to call BleBase.SetBleLayerTransportToSelf if it needs to commission using BLE.
62 : *
63 : * Call SetPreserveExistingBleLayerTransport(false) to set the global
64 : * BleLayer transport to the BleBase created with these parameters, even if it is already
65 : * set to another BleBase.
66 : *
67 : * Use the BleBase.IsBleLayerTransportSetToSelf() and BleBase.SetBleLayerTransportToSelf
68 : * methods to toggle between BleBase transports when there is more than one.
69 : */
70 0 : bool PreserveExistingBleLayerTransport() const { return mPreserveExistingBleLayerTransport; }
71 : BleListenParameters & SetPreserveExistingBleLayerTransport(bool preserveExistingBleLayerTransport)
72 : {
73 : mPreserveExistingBleLayerTransport = preserveExistingBleLayerTransport;
74 :
75 : return *this;
76 : }
77 :
78 : private:
79 : Ble::BleLayer * mLayer;
80 : bool mPreserveExistingBleLayerTransport = true;
81 : };
82 :
83 : /** Implements a transport using BLE.
84 : *
85 : * TODO: BLE transport currently only allow one BLE connection, neet to clearify if we should support multiple BLE connections.
86 : */
87 : class DLL_EXPORT BLEBase : public Base, public Ble::BleLayerDelegate
88 : {
89 : /**
90 : * The State of the BLE connection
91 : *
92 : */
93 : enum class State
94 : {
95 : kNotReady = 0, /**< State before initialization. */
96 : kInitialized = 1, /**< State after class is connected and ready. */
97 : kConnected = 2, /**< Endpoint connected. */
98 : };
99 :
100 : public:
101 1 : BLEBase(System::PacketBufferHandle * packetBuffers, size_t packetBuffersSize) :
102 1 : mPendingPackets(packetBuffers), mPendingPacketsSize(packetBuffersSize)
103 1 : {}
104 : ~BLEBase() override;
105 :
106 : /**
107 : * Initialize a BLE transport to a given peripheral or a given device name.
108 : *
109 : * @param param BLE configuration parameters for this transport
110 : */
111 : CHIP_ERROR Init(const BleListenParameters & param);
112 :
113 : CHIP_ERROR SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf) override;
114 :
115 0 : bool CanSendToPeer(const Transport::PeerAddress & address) override
116 : {
117 0 : return (mState != State::kNotReady) && (address.GetTransportType() == Type::kBle);
118 : }
119 :
120 : CHIP_ERROR SetEndPoint(Ble::BLEEndPoint * endPoint) override;
121 :
122 : /**
123 : * Change BLE transport to this
124 : *
125 : * This is relevant when there is more than one TransportMgr,
126 : * for example, when a device is both a commissioner (has a CHIPDeviceController)
127 : * and a commissionee (has a Server) since each TransportMgr will set
128 : * the global BleLayer transport to point to itself.
129 : *
130 : * In this scenario, the device will need the ability to toggle between a
131 : * BleLayer transport for commissioner functionality and one for commissionee functionality.
132 : */
133 : void SetBleLayerTransportToSelf() { mBleLayer->mBleTransport = this; }
134 : bool IsBleLayerTransportSetToSelf() { return mBleLayer->mBleTransport == this; }
135 :
136 : private:
137 : void ClearState();
138 :
139 : /**
140 : * Sends the specified message once a connection has been established.
141 : *
142 : * @param msg - what buffer to send once a connection has been established.
143 : *
144 : * Ownership of msg is taken over and will be freed at some unspecified time
145 : * in the future (once connection succeeds/fails).
146 : */
147 : CHIP_ERROR SendAfterConnect(System::PacketBufferHandle && msg);
148 :
149 : // Those functions are BLEConnectionDelegate callbacks used when the connection
150 : // parameters used a name instead of a BLE_CONNECTION_OBJECT.
151 : void OnBleConnectionComplete(Ble::BLEEndPoint * endPoint) override;
152 : void OnBleConnectionError(CHIP_ERROR err) override;
153 :
154 : void ClearPendingPackets();
155 :
156 : // Those functions are BLEEndPoint callbacks
157 : void OnEndPointMessageReceived(Ble::BLEEndPoint * endPoint, System::PacketBufferHandle && buffer) override;
158 : void OnEndPointConnectComplete(Ble::BLEEndPoint * endPoint, CHIP_ERROR err) override;
159 : void OnEndPointConnectionClosed(Ble::BLEEndPoint * endPoint, CHIP_ERROR err) override;
160 :
161 : Ble::BleLayer * mBleLayer = nullptr; ///< Associated ble layer
162 : State mState = State::kNotReady; ///< State of the BLE transport
163 : Ble::BLEEndPoint * mBleEndPoint = nullptr; ///< BLE endpoint used by transport
164 :
165 : // Data to be sent when connections succeed
166 : System::PacketBufferHandle * mPendingPackets;
167 : const size_t mPendingPacketsSize;
168 : };
169 :
170 : template <size_t kPendingPacketSize>
171 : class BLE : public BLEBase
172 : {
173 : public:
174 2 : BLE() : BLEBase(mPendingPackets, kPendingPacketSize) {}
175 :
176 : private:
177 : System::PacketBufferHandle mPendingPackets[kPendingPacketSize];
178 : };
179 :
180 : } // namespace Transport
181 : } // namespace chip
|