forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluezConnection.h
138 lines (108 loc) · 4.7 KB
/
BluezConnection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdint.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <lib/core/CHIPError.h>
#include <platform/GLibTypeDeleter.h>
#include <platform/Linux/dbus/bluez/DBusBluez.h>
#include <system/SystemPacketBuffer.h>
#include "Types.h"
namespace chip {
namespace DeviceLayer {
namespace Internal {
class BluezEndpoint;
class BluezConnection
{
public:
BluezConnection(BluezDevice1 & aDevice) : mDevice(reinterpret_cast<BluezDevice1 *>(g_object_ref(&aDevice))) {}
~BluezConnection() = default;
CHIP_ERROR Init(const BluezEndpoint & aEndpoint);
const char * GetPeerAddress() const;
uint16_t GetMTU() const { return mMtu; }
void SetMTU(uint16_t aMtu) { mMtu = aMtu; }
bool IsNotifyAcquired() const { return mNotifyAcquired; }
void SetNotifyAcquired(bool aNotifyAcquired) { mNotifyAcquired = aNotifyAcquired; }
/**
* @brief Setup callback for receiving data from the CHIP TX characteristic on
* the remote peripheral device.
*
* @note This function takes the ownership of the passed file descriptor and
* will close it when the connection is closed.
*/
void SetupWriteHandler(int aSocketFd);
/**
* @brief Setup callback for receiving HUP event on the notification channel.
*
* @note This function takes the ownership of the passed file descriptor and
* will close it when the connection is closed.
*/
void SetupNotifyHandler(int aSocketFd, bool aAdditionalAdvertising = false);
/// Send indication to the CHIP RX characteristic on the remote peripheral device
CHIP_ERROR SendIndication(chip::System::PacketBufferHandle apBuf);
/// Write to the CHIP RX characteristic on the remote peripheral device
CHIP_ERROR SendWriteRequest(chip::System::PacketBufferHandle apBuf);
/// Subscribe to the CHIP TX characteristic on the remote peripheral device
CHIP_ERROR SubscribeCharacteristic();
/// Unsubscribe from the CHIP TX characteristic on the remote peripheral device
CHIP_ERROR UnsubscribeCharacteristic();
CHIP_ERROR CloseConnection();
private:
struct IOChannel
{
IOChannel() = default;
~IOChannel();
GAutoPtr<GIOChannel> mChannel;
GAutoPtr<GSource> mWatchSource;
};
struct ConnectionDataBundle
{
ConnectionDataBundle(const BluezConnection &, const chip::System::PacketBufferHandle &);
~ConnectionDataBundle() = default;
const BluezConnection & mConn;
GAutoPtr<GVariant> mData;
};
static CHIP_ERROR CloseConnectionImpl(BluezConnection * apConn);
static gboolean WriteHandlerCallback(GIOChannel * aChannel, GIOCondition aCond, BluezConnection * apConn);
static gboolean NotifyHandlerCallback(GIOChannel * aChannel, GIOCondition aCond, BluezConnection * apConn);
static CHIP_ERROR SendIndicationImpl(ConnectionDataBundle * data);
static void SendWriteRequestDone(GObject * aObject, GAsyncResult * aResult, gpointer apConn);
static CHIP_ERROR SendWriteRequestImpl(ConnectionDataBundle * data);
static void OnCharacteristicChanged(GDBusProxy * aInterface, GVariant * aChangedProperties,
const gchar * const * aInvalidatedProps, gpointer apConn);
static void SubscribeCharacteristicDone(GObject * aObject, GAsyncResult * aResult, gpointer apConn);
static CHIP_ERROR SubscribeCharacteristicImpl(BluezConnection * apConn);
static void UnsubscribeCharacteristicDone(GObject * aObject, GAsyncResult * aResult, gpointer apConn);
static CHIP_ERROR UnsubscribeCharacteristicImpl(BluezConnection * apConn);
GAutoPtr<BluezDevice1> mDevice;
GAutoPtr<BluezGattService1> mService;
bool mNotifyAcquired = false;
uint16_t mMtu = 0;
GAutoPtr<BluezGattCharacteristic1> mC1;
IOChannel mC1Channel = {};
GAutoPtr<BluezGattCharacteristic1> mC2;
IOChannel mC2Channel = {};
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
GAutoPtr<BluezGattCharacteristic1> mC3;
IOChannel mC3Channel = {};
#endif
};
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip