-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluetoothServer.cpp
313 lines (261 loc) · 11.2 KB
/
BluetoothServer.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "tbluetooth/BluetoothServer.h"
#include <iostream>
// Define the interface VTable to link method names with callbacks
static const gchar *agent_introspection_xml =
"<node>"
" <interface name='org.bluez.Agent1'>"
" <method name='Release' />"
" <method name='RequestPinCode'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='s' name='pincode' direction='out'/>"
" </method>"
" <method name='DisplayPinCode'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='s' name='pincode' direction='in'/>"
" </method>"
" <method name='RequestPasskey'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='u' name='passkey' direction='out'/>"
" </method>"
" <method name='DisplayPasskey'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='u' name='passkey' direction='in'/>"
" <arg type='q' name='entered' direction='in'/>"
" </method>"
" <method name='RequestConfirmation'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='u' name='passkey' direction='in'/>"
" </method>"
" <method name='RequestAuthorization'>"
" <arg type='o' name='device' direction='in'/>"
" </method>"
" <method name='AuthorizeService'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='s' name='uuid' direction='in'/>"
" </method>"
" <method name='Cancel' />"
" </interface>"
"</node>";
#define AGENT_PATH "/org/bluez/AutoPinAgent"
GDBusNodeInfo* BluetoothServer::introspection_data = nullptr;
GDBusInterfaceVTable BluetoothServer::interface_vtable = {
// Initialize with your callbacks
onMethodCall, // Method call callback
onGetProperty, // Property get callback, or nullptr if not needed
onSetProperty, // Property set callback, or nullptr if not needed
nullptr, // No need for padding in user code
};
BluetoothServer::BluetoothServer() {
// Initialize GDBus connection
GError* error = nullptr;
connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
if (!connection) {
std::cerr << "Error connecting to D-Bus: " << error->message << std::endl;
g_error_free(error);
exit(EXIT_FAILURE);
}
}
BluetoothServer::~BluetoothServer() {
// Cleanup GDBus connection
if (connection) {
g_object_unref(connection);
}
}
void BluetoothServer::onMethodCall(GDBusConnection* connection,
const gchar* sender,
const gchar* object_path,
const gchar* interface_name,
const gchar* method_name,
GVariant* parameters,
GDBusMethodInvocation* invocation,
gpointer user_data) {
BluetoothServer *server = static_cast<BluetoothServer*>(user_data);
if (g_strcmp0(method_name, "RequestPinCode") == 0) {
server->handleRequestPinCode(invocation, parameters);
} else if (g_strcmp0(method_name, "RequestPasskey") == 0) {
server->handleRequestPasskey(invocation, parameters);
} else if (g_strcmp0(method_name, "RequestConfirmation") == 0) {
server->handleRequestConfirmation(invocation, parameters);
} else {
// Method not recognized or not implemented
g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Method '%s' not supported.", method_name);
}
}
GVariant *BluetoothServer::onGetProperty(GDBusConnection *connection, const gchar *sender, const gchar *object_path,
const gchar *interface_name, const gchar *property_name, GError **error,
gpointer user_data) {
// Handle get property calls here
return nullptr;
}
gboolean BluetoothServer::onSetProperty(GDBusConnection *connection, const gchar *sender, const gchar *object_path,
const gchar *interface_name, const gchar *property_name, GVariant *value,
GError **error, gpointer user_data) {
// Handle set property calls here
return FALSE;
}
int BluetoothServer::bluezAdapterSetProperty(const char *prop, GVariant *value)
{
GVariant *result;
GError *error = NULL;
GVariant* params = g_variant_new("(ssv)", "org.bluez.Adapter1", prop, value);
result = g_dbus_connection_call_sync(this->connection,
"org.bluez",
"/org/bluez/hci0",
"org.freedesktop.DBus.Properties",
"Set",
params, // Correctly formatted parameters
NULL, // Expecting no return value
G_DBUS_CALL_FLAGS_NONE,
-1, // No timeout
NULL, // No cancellable
&error);
if (error) {
std::cerr << "Error setting Bluetooth alias: " << error->message << std::endl;
g_error_free(error); // Clean up error
return 1;
}
// Only unref result if it's not NULL
g_variant_unref(result);
return 0;
}
int BluetoothServer::bluezAgentCallMethod(const gchar *method, GVariant *param)
{
GVariant *result;
GError *error = NULL;
result = g_dbus_connection_call_sync(this->connection,
"org.bluez",
"/org/bluez",
"org.bluez.AgentManager1",
method,
param,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if(error != NULL) {
g_print("Register %s: %s\n", AGENT_PATH, error->message);
return 1;
}
g_variant_unref(result);
return 0;
}
void BluetoothServer::setAlias(const std::string& alias) {
int err;
err = this->bluezAdapterSetProperty("Alias", g_variant_new("s", alias.c_str()));
if(err) {
g_print("Not able to enable the adapter\n");
} else {
g_print("Bluetooth alias set successfully.\n");
}
}
void BluetoothServer::powerOnAdapter() {
int err;
err = this->bluezAdapterSetProperty("Powered", g_variant_new("b", TRUE));
if(err) {
g_print("Not able to enable the adapter\n");
} else {
g_print("Adapter enabled successfully\n");
}
}
void BluetoothServer::makeDiscoverable() {
int err;
err = this->bluezAdapterSetProperty("Discoverable", g_variant_new("b", TRUE));
if(err) {
g_print("Not able to Discoverable the adapter\n");
} else {
g_print("Adapter Discoverable successfully\n");
}
}
void BluetoothServer::makePairable() {
// Placeholder for making device pairable
int err;
err = this->bluezAdapterSetProperty("Pairable", g_variant_new("b", TRUE));
if(err) {
g_print("Not able to Pairable the adapter\n");
} else {
g_print("Adapter Pairable successfully\n");
}
}
void BluetoothServer::registerAgent() {
GError *error = NULL;
int err;
BluetoothServer::introspection_data = g_dbus_node_info_new_for_xml(agent_introspection_xml, NULL);
if (introspection_data == nullptr) {
std::cerr << "Failed to parse introspection XML" << std::endl;
return;
}
//const GDBusInterfaceInfo *interface_info = g_dbus_node_info_lookup_interface(introspection_data, "org.bluez.Agent1");
guint registration_id = g_dbus_connection_register_object(
this->connection,
AGENT_PATH, // The D-Bus object path of your agent
BluetoothServer::introspection_data->interfaces[0],
&BluetoothServer::interface_vtable, // Your GDBusInterfaceVTable with callbacks
this, // User data passed to callbacks
NULL, // User data free function
&error // GError for error handling
);
if (error) {
std::cerr << "Error registering object: " << error->message << std::endl;
g_error_free(error);
return;
}
// Agent registration
err = this->bluezAgentCallMethod("RegisterAgent", g_variant_new("(os)", AGENT_PATH, "NoInputNoOutput"));
if (err) {
std::cerr << "Error registering agent: " << std::endl;
}
std::cout << "Agent registered succesfully";
// Set the agent as the default agent
err = this->bluezAgentCallMethod("RequestDefaultAgent", g_variant_new("(o)", AGENT_PATH));
if (err) {
std::cerr << "Error requesting default agent: " << std::endl;
this->bluezAgentCallMethod("UnregisterAgent", g_variant_new("(o)", AGENT_PATH));
}
std::cout << "Agent registered and set as default successfully." << std::endl;
}
void BluetoothServer::handleRequestPinCode(GDBusMethodInvocation* invocation, GVariant* parameters) {
// For NoInputNoOutput, we cannot show a pin code. Auto-confirm if needed or log.
std::cout << "Pin code request received, automatically confirmed due to NoInputNoOutput capability." << std::endl;
// Normally, you would not return a pin code here, but for the sake of completeness:
g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", ""));
}
void BluetoothServer::handleRequestPasskey(GDBusMethodInvocation* invocation, GVariant* parameters) {
// For NoInputNoOutput, we cannot display or input a passkey. Auto-confirm if needed or log.
std::cout << "Passkey request received, automatically confirmed due to NoInputNoOutput capability." << std::endl;
// Normally, you would not return a passkey here, but for completeness:
g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", 0));
}
void BluetoothServer::handleRequestConfirmation(GDBusMethodInvocation* invocation, GVariant* parameters) {
// Since there's no user interface to confirm the passkey, auto-confirm the request.
std::cout << "Pairing confirmation request received, automatically confirmed due to NoInputNoOutput capability." << std::endl;
g_dbus_method_invocation_return_value(invocation, NULL);
}
void BluetoothServer::startAdvertising() {
std::cout << "Start Advertising." << std::endl;
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
// If you have specific advertisement data, add it here. Otherwise, it's an empty array.
// Example:
g_variant_builder_add(&builder, "{sv}", "Key", g_variant_new_int32(1));
GVariant* advData = g_variant_builder_end(&builder); // Finalize the builder
GError* error = nullptr;
g_dbus_connection_call_sync(
connection,
"org.bluez",
"/org/bluez/hci0",
"org.bluez.LEAdvertisingManager1",
"RegisterAdvertisement",
g_variant_new("(oa{sv})", "/com/example/ble/advertisement", advData),
NULL, // No return type expected
G_DBUS_CALL_FLAGS_NONE,
-1, // No timeout
NULL, // No cancellable
&error);
if (error) {
std::cerr << "Failed to start advertising: " << error->message << std::endl;
g_error_free(error);
} else {
std::cout << "Advertising started successfully." << std::endl;
}
}