aboutsummaryrefslogtreecommitdiff
path: root/src/GattInterface.cpp
blob: bb80481424f8d813ec68e0f09fad858b8a9288ac (plain)
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
// Copyright 2017 Paul Nettle. 
// 
// This file is part of Gobbledegook. 
// 
// Gobbledegook is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
// 
// Gobbledegook is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
// 
// You should have received a copy of the GNU General Public License 
// along with Gobbledegook.  If not, see <http://www.gnu.org/licenses/>. 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// 
// >> 
// >>>  INSIDE THIS FILE 
// >> 
// 
// This is our abstraction layer for GATT interfaces, used by GattService, GattCharacteristic & GattDescriptor 
// 
// >> 
// >>>  DISCUSSION 
// >> 
// 
// This class is intended to be used within the server description. For an explanation of how this class is used, see the detailed 
// description in Server.cpp. 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
#include "GattInterface.h"
#include "GattProperty.h"
#include "DBusObject.h"
#include "Logger.h"
 
// 
// Standard constructor 
// 
GattInterface::GattInterface(DBusObject &owner, const std::string &name)
: DBusInterface(owner, name)
{
}
 
GattInterface::~GattInterface()
{
}
 
// 
// GATT Characteristic properties 
// 
 
// Returns the list of GATT properties 
const std::list<GattProperty> &GattInterface::getProperties() const
{
return properties;
}
 
// When responding to a method, we need to return a GVariant value wrapped in a tuple. This method will simplify this slightly by 
// wrapping a GVariant of the type "ay" and wrapping it in a tuple before sending it off as the method response. 
// 
// This is the generalized form that accepts a GVariant *. There is a templated helper method (`methodReturnValue()`) that accepts 
// common types. 
void GattInterface::methodReturnVariant(GDBusMethodInvocation *pInvocation, GVariant *pVariant, bool wrapInTuple) const
{
if (wrapInTuple)
{
pVariant = g_variant_new_tuple(&pVariant, 1);
}
g_dbus_method_invocation_return_value(pInvocation, pVariant);
}
 
// Locates a `GattProperty` within the interface 
// 
// This method returns a pointer to the property or nullptr if not found 
const GattProperty *GattInterface::findProperty(const std::string &name) const
{
for (const GattProperty &property : properties)
{
if (property.getName() == name)
{
return &property;
}
}
 
return nullptr;
}
 
// Internal method used to generate introspection XML used to describe our services on D-Bus 
std::string GattInterface::generateIntrospectionXML(int depth) const
{
std::string prefix;
prefix.insert(0, depth * 2' ');
 
std::string xml = std::string();
 
if (methods.size() && getProperties().empty())
{
xml += prefix + "<interface name='" + getName() + "' />\n";
}
else
{
xml += prefix + "<interface name='" + getName() + "'>\n";
 
// Describe our methods 
for (const DBusMethod &method : methods)
{
xml += method.generateIntrospectionXML(depth + 1);
}
 
// Describe our properties 
for (const GattProperty &property : getProperties())
{
xml += property.generateIntrospectionXML(depth + 1);
}
 
xml += prefix + "</interface>\n";
}
 
return xml;
}