3 * Copyright (c) 2020 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * This file defines the CHIP Connection object that maintains a BLE connection.
27 #include <ble/BleConfig.h>
31 #include <ble/BleError.h>
32 #include <ble/BleLayer.h>
33 #include <core/CHIPCore.h>
34 #include <support/DLLUtil.h>
35 #include <transport/RendezvousParameters.h>
36 #include <transport/RendezvousSessionDelegate.h>
37 #include <transport/raw/Base.h>
42 /** Implements a transport using BLE.
44 * TODO: BLE transport currently does NOT receive messages as defined
45 * in the Transport::Base (i.e. no header is parsed and processed) and
46 * instead received packets are sent raw via BLE Handler callbacks.
48 class DLL_EXPORT BLE : public Base
51 * The State of the BLE connection
56 kNotReady = 0, /**< State before initialization. */
57 kInitialized = 1, /**< State after class is connected and ready. */
64 * Initialize a BLE transport to a given peripheral or a given device name.
66 * @param delegate the delegate that will receive BLE events
67 * @param params BLE configuration parameters for this transport
69 CHIP_ERROR Init(RendezvousSessionDelegate * delegate, const RendezvousParameters & params);
71 CHIP_ERROR SendMessage(const PacketHeader & header, const Transport::PeerAddress & address,
72 System::PacketBufferHandle msgBuf) override;
74 bool CanSendToPeer(const Transport::PeerAddress & address) override
76 return (mState == State::kInitialized) && (address.GetTransportType() == Type::kBle);
79 CHIP_ERROR SetEndPoint(Ble::BLEEndPoint * endPoint);
82 CHIP_ERROR InitInternal(BLE_CONNECTION_OBJECT connObj);
83 void SetupEvents(Ble::BLEEndPoint * endPoint);
88 * Called when the underlying BleLayer receive a new
91 * @param endPoint The newly opened BLEEndPoint
93 static void OnNewConnection(Ble::BLEEndPoint * endPoint);
95 // Those functions are BLEConnectionDelegate callbacks used when the connection
96 // parameters used a name instead of a BLE_CONNECTION_OBJECT.
97 static void OnBleConnectionComplete(void * appState, BLE_CONNECTION_OBJECT connObj);
98 static void OnBleConnectionError(void * appState, BLE_ERROR err);
100 // Those functions are BLEEndPoint callbacks
101 static void OnBleEndPointReceive(Ble::BLEEndPoint * endPoint, System::PacketBufferHandle buffer);
102 static void OnBleEndPointConnectionComplete(Ble::BLEEndPoint * endPoint, BLE_ERROR err);
103 static void OnBleEndPointConnectionClosed(Ble::BLEEndPoint * endPoint, BLE_ERROR err);
105 Ble::BleLayer * mBleLayer = nullptr; ///< Associated ble layer
106 State mState = State::kNotReady; ///< State of the BLE transport
107 Ble::BLEEndPoint * mBleEndPoint = nullptr; ///< BLE endpoint used by transport
108 RendezvousSessionDelegate * mDelegate = nullptr; ///< BLE events from transport
111 } // namespace Transport