Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / transport / BLE.h
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 <ble/BleConfig.h>
28
29 #include <utility>
30
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>
38
39 namespace chip {
40 namespace Transport {
41
42 /** Implements a transport using BLE.
43  *
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.
47  */
48 class DLL_EXPORT BLE : public Base
49 {
50     /**
51      *  The State of the BLE connection
52      *
53      */
54     enum class State
55     {
56         kNotReady    = 0, /**< State before initialization. */
57         kInitialized = 1, /**< State after class is connected and ready. */
58     };
59
60 public:
61     ~BLE() override;
62
63     /**
64      * Initialize a BLE transport to a given peripheral or a given device name.
65      *
66      * @param delegate      the delegate that will receive BLE events
67      * @param params        BLE configuration parameters for this transport
68      */
69     CHIP_ERROR Init(RendezvousSessionDelegate * delegate, const RendezvousParameters & params);
70
71     CHIP_ERROR SendMessage(const PacketHeader & header, const Transport::PeerAddress & address,
72                            System::PacketBufferHandle msgBuf) override;
73
74     bool CanSendToPeer(const Transport::PeerAddress & address) override
75     {
76         return (mState == State::kInitialized) && (address.GetTransportType() == Type::kBle);
77     }
78
79     CHIP_ERROR SetEndPoint(Ble::BLEEndPoint * endPoint);
80
81 private:
82     CHIP_ERROR InitInternal(BLE_CONNECTION_OBJECT connObj);
83     void SetupEvents(Ble::BLEEndPoint * endPoint);
84     void ClearState();
85
86     /**
87      * @brief
88      *  Called when the underlying BleLayer receive a new
89      *  remote connection.
90      *
91      * @param endPoint The newly opened BLEEndPoint
92      */
93     static void OnNewConnection(Ble::BLEEndPoint * endPoint);
94
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);
99
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);
104
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
109 };
110
111 } // namespace Transport
112 } // namespace chip