Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit fd4fdb0

Browse files
vkolotovpetreeftime
authored andcommitted
Adding support for setting RSSI discovery filter
Signed-off-by: Vlad Kolotov <[email protected]>
1 parent 898f6ac commit fd4fdb0

File tree

7 files changed

+292
-2
lines changed

7 files changed

+292
-2
lines changed

api/tinyb/BluetoothAdapter.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#pragma once
2626
#include "BluetoothObject.hpp"
2727
#include "BluetoothManager.hpp"
28+
#include "BluetoothUUID.hpp"
2829
#include <vector>
2930

3031
/* Forward declaration of types */
@@ -33,6 +34,12 @@ typedef struct _Object Object;
3334
struct _Adapter1;
3435
typedef struct _Adapter1 Adapter1;
3536

37+
enum class TransportType {
38+
AUTO,
39+
BREDR,
40+
LE
41+
};
42+
3643
/**
3744
* Provides access to Bluetooth adapters. Follows the BlueZ adapter API
3845
* available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/adapter-api.txt
@@ -109,6 +116,15 @@ friend class tinyb::BluetoothNotificationHandler;
109116
bool stop_discovery (
110117
);
111118

119+
/** Sets the device discovery filter for the caller. If all fields are empty,
120+
* filter is removed.
121+
* @param uuids Vector of UUIDs to filter on
122+
* @param rssi RSSI low bounded threshold
123+
* @param pathloss Pathloss threshold value
124+
* @param transport Type of scan to run
125+
*/
126+
bool set_discovery_filter (std::vector<BluetoothUUID> uuids,
127+
int16_t rssi, uint16_t pathloss, const TransportType &transport);
112128

113129
/** Returns a list of BluetoothDevices visible from this adapter.
114130
* @return A list of BluetoothDevices visible on this adapter,

include/generated-code.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ struct _Adapter1Iface
4242
Adapter1 *object,
4343
GDBusMethodInvocation *invocation);
4444

45+
gboolean (*handle_set_discovery_filter) (
46+
Adapter1 *object,
47+
GDBusMethodInvocation *invocation,
48+
GVariant *arg_filter);
49+
4550
const gchar * (*get_address) (Adapter1 *object);
4651

4752
const gchar * (*get_alias) (Adapter1 *object);
@@ -140,6 +145,23 @@ gboolean adapter1_call_remove_device_sync (
140145
GCancellable *cancellable,
141146
GError **error);
142147

148+
void adapter1_call_set_discovery_filter (
149+
Adapter1 *proxy,
150+
GVariant *arg_filter,
151+
GCancellable *cancellable,
152+
GAsyncReadyCallback callback,
153+
gpointer user_data);
154+
155+
gboolean adapter1_call_set_discovery_filter_finish (
156+
Adapter1 *proxy,
157+
GAsyncResult *res,
158+
GError **error);
159+
160+
gboolean adapter1_call_set_discovery_filter_sync (
161+
Adapter1 *proxy,
162+
GVariant *arg_filter,
163+
GCancellable *cancellable,
164+
GError **error);
143165

144166

145167
/* D-Bus property accessors: */

java/BluetoothAdapter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,13 @@ public BluetoothDevice find(String name, String address) {
238238
* @return The local ID of the adapter.
239239
*/
240240
public native String getModalias();
241-
241+
242+
/** This method sets RSSI device discovery filter for the caller. When this method is called
243+
* with 0, filter is removed.
244+
* @param rssi a rssi value
245+
*/
246+
public native void setRssiDiscoveryFilter(int rssi);
247+
242248
/** Returns the interface name of the adapter.
243249
* @return The interface name of the adapter.
244250
*/

java/jni/BluetoothAdapter.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,24 @@ void Java_tinyb_BluetoothAdapter_delete(JNIEnv *env, jobject obj)
787787
}
788788
}
789789

790+
void Java_tinyb_BluetoothAdapter_setRssiDiscoveryFilter(JNIEnv *env, jobject obj, jint rssi)
791+
{
792+
try {
793+
BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
794+
795+
std::vector<BluetoothUUID> native_uuids;
796+
native_uuids.reserve(0);
797+
798+
obj_adapter->set_discovery_filter(native_uuids, (int16_t) rssi, 0, TransportType::AUTO);
799+
} catch (std::bad_alloc &e) {
800+
raise_java_oom_exception(env, e);
801+
} catch (BluetoothException &e) {
802+
raise_java_bluetooth_exception(env, e);
803+
} catch (std::runtime_error &e) {
804+
raise_java_runtime_exception(env, e);
805+
} catch (std::invalid_argument &e) {
806+
raise_java_invalid_arg_exception(env, e);
807+
} catch (std::exception &e) {
808+
raise_java_exception(env, e);
809+
}
810+
}

src/BluetoothAdapter.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,57 @@ bool BluetoothAdapter::remove_device (
211211
return result;
212212
}
213213

214+
bool BluetoothAdapter::set_discovery_filter (std::vector<BluetoothUUID> uuids,
215+
int16_t rssi, uint16_t pathloss, const TransportType &transport)
216+
{
217+
GError *error = NULL;
218+
bool result = true;
219+
GVariantDict dict;
220+
g_variant_dict_init(&dict, NULL);
221+
222+
if (uuids.size() > 0)
223+
{
224+
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
225+
226+
for (std::vector<BluetoothUUID>::iterator i = uuids.begin(); i != uuids.end(); ++i)
227+
g_variant_builder_add(builder, "(s)", (*i).get_string().c_str());
228+
229+
GVariant *uuids_variant = g_variant_new("a(s)", builder);
230+
g_variant_builder_unref(builder);
231+
g_variant_dict_insert_value(&dict, "UUIDs", uuids_variant);
232+
}
233+
234+
if (rssi != 0)
235+
g_variant_dict_insert_value(&dict, "RSSI", g_variant_new_int16(rssi));
236+
237+
if (pathloss != 0)
238+
g_variant_dict_insert_value(&dict, "Pathloss", g_variant_new_uint16(pathloss));
239+
240+
std::string transport_str;
241+
242+
if (transport == TransportType::AUTO)
243+
transport_str = "auto";
244+
else if (transport == TransportType::BREDR)
245+
transport_str = "bredr";
246+
else if (transport == TransportType::LE)
247+
transport_str = "le";
248+
249+
if (!transport_str.empty())
250+
g_variant_dict_insert_value(&dict, "Transport", g_variant_new_string(transport_str.c_str()));
251+
252+
GVariant *variant = g_variant_dict_end(&dict);
253+
254+
result = adapter1_call_set_discovery_filter_sync(
255+
object,
256+
variant,
257+
NULL,
258+
&error
259+
);
260+
261+
handle_error(error);
262+
return result;
263+
}
264+
214265
/* D-Bus property accessors: */
215266
std::string BluetoothAdapter::get_address ()
216267
{

src/generated-code.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,42 @@ static const _ExtendedGDBusMethodInfo _adapter1_method_info_remove_device =
219219
FALSE
220220
};
221221

222+
static const _ExtendedGDBusArgInfo _adapter1_method_info_set_discovery_filter_IN_ARG_filter =
223+
{
224+
{
225+
-1,
226+
(gchar *) "filter",
227+
(gchar *) "a{sv}",
228+
NULL
229+
},
230+
FALSE
231+
};
232+
233+
static const _ExtendedGDBusArgInfo * const _adapter1_method_info_set_discovery_filter_IN_ARG_pointers[] =
234+
{
235+
&_adapter1_method_info_set_discovery_filter_IN_ARG_filter,
236+
NULL
237+
};
238+
239+
static const _ExtendedGDBusMethodInfo _adapter1_method_info_set_discovery_filter =
240+
{
241+
{
242+
-1,
243+
(gchar *) "SetDiscoveryFilter",
244+
(GDBusArgInfo **) &_adapter1_method_info_set_discovery_filter_IN_ARG_pointers,
245+
NULL,
246+
NULL
247+
},
248+
"handle-set-discovery-filter",
249+
FALSE
250+
};
251+
222252
static const _ExtendedGDBusMethodInfo * const _adapter1_method_info_pointers[] =
223253
{
224254
&_adapter1_method_info_start_discovery,
225255
&_adapter1_method_info_stop_discovery,
226256
&_adapter1_method_info_remove_device,
257+
&_adapter1_method_info_set_discovery_filter,
227258
NULL
228259
};
229260

@@ -467,6 +498,7 @@ adapter1_override_properties (GObjectClass *klass, guint property_id_begin)
467498
* Adapter1Iface:
468499
* @parent_iface: The parent interface.
469500
* @handle_remove_device: Handler for the #Adapter1::handle-remove-device signal.
501+
* @handle_set_discovery_filter: Handler for the #Adapter1::handle-set-discovery-filter signal.
470502
* @handle_start_discovery: Handler for the #Adapter1::handle-start-discovery signal.
471503
* @handle_stop_discovery: Handler for the #Adapter1::handle-stop-discovery signal.
472504
* @get_address: Getter for the #Adapter1:address property.
@@ -559,6 +591,29 @@ adapter1_default_init (Adapter1Iface *iface)
559591
2,
560592
G_TYPE_DBUS_METHOD_INVOCATION, G_TYPE_STRING);
561593

594+
/**
595+
* Adapter1::handle-set-discovery-filter:
596+
* @object: A #Adapter1.
597+
* @invocation: A #GDBusMethodInvocation.
598+
* @arg_filter: Argument passed by remote caller.
599+
*
600+
* Signal emitted when a remote caller is invoking the <link linkend="gdbus-method-org-bluez-Adapter1.SetDiscoveryFilter">SetDiscoveryFilter()</link> D-Bus method.
601+
*
602+
* If a signal handler returns %TRUE, it means the signal handler will handle the invocation (e.g. take a reference to @invocation and eventually call adapter1_complete_set_discovery_filter() or e.g. g_dbus_method_invocation_return_error() on it) and no order signal handlers will run. If no signal handler handles the invocation, the %G_DBUS_ERROR_UNKNOWN_METHOD error is returned.
603+
*
604+
* Returns: %TRUE if the invocation was handled, %FALSE to let other signal handlers run.
605+
*/
606+
g_signal_new ("handle-set-discovery-filter",
607+
G_TYPE_FROM_INTERFACE (iface),
608+
G_SIGNAL_RUN_LAST,
609+
G_STRUCT_OFFSET (Adapter1Iface, handle_set_discovery_filter),
610+
g_signal_accumulator_true_handled,
611+
NULL,
612+
g_cclosure_marshal_generic,
613+
G_TYPE_BOOLEAN,
614+
2,
615+
G_TYPE_DBUS_METHOD_INVOCATION, G_TYPE_VARIANT);
616+
562617
/* GObject properties for D-Bus properties: */
563618
/**
564619
* Adapter1:address:
@@ -1424,6 +1479,104 @@ adapter1_call_remove_device_sync (
14241479
return _ret != NULL;
14251480
}
14261481

1482+
/**
1483+
* adapter1_call_set_discovery_filter:
1484+
* @proxy: A #Adapter1Proxy.
1485+
* @arg_filter: Argument to pass with the method invocation.
1486+
* @cancellable: (allow-none): A #GCancellable or %NULL.
1487+
* @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL.
1488+
* @user_data: User data to pass to @callback.
1489+
*
1490+
* Asynchronously invokes the <link linkend="gdbus-method-org-bluez-Adapter1.SetDiscoveryFilter">SetDiscoveryFilter()</link> D-Bus method on @proxy.
1491+
* When the operation is finished, @callback will be invoked in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link> of the thread you are calling this method from.
1492+
* You can then call adapter1_call_set_discovery_filter_finish() to get the result of the operation.
1493+
*
1494+
* See adapter1_call_set_discovery_filter_sync() for the synchronous, blocking version of this method.
1495+
*/
1496+
void
1497+
adapter1_call_set_discovery_filter (
1498+
Adapter1 *proxy,
1499+
GVariant *arg_filter,
1500+
GCancellable *cancellable,
1501+
GAsyncReadyCallback callback,
1502+
gpointer user_data)
1503+
{
1504+
g_dbus_proxy_call (G_DBUS_PROXY (proxy),
1505+
"SetDiscoveryFilter",
1506+
g_variant_new ("(@a{sv})",
1507+
arg_filter),
1508+
G_DBUS_CALL_FLAGS_NONE,
1509+
-1,
1510+
cancellable,
1511+
callback,
1512+
user_data);
1513+
}
1514+
1515+
/**
1516+
* adapter1_call_set_discovery_filter_finish:
1517+
* @proxy: A #Adapter1Proxy.
1518+
* @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to adapter1_call_set_discovery_filter().
1519+
* @error: Return location for error or %NULL.
1520+
*
1521+
* Finishes an operation started with adapter1_call_set_discovery_filter().
1522+
*
1523+
* Returns: (skip): %TRUE if the call succeded, %FALSE if @error is set.
1524+
*/
1525+
gboolean
1526+
adapter1_call_set_discovery_filter_finish (
1527+
Adapter1 *proxy,
1528+
GAsyncResult *res,
1529+
GError **error)
1530+
{
1531+
GVariant *_ret;
1532+
_ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (proxy), res, error);
1533+
if (_ret == NULL)
1534+
goto _out;
1535+
g_variant_get (_ret,
1536+
"()");
1537+
g_variant_unref (_ret);
1538+
_out:
1539+
return _ret != NULL;
1540+
}
1541+
1542+
/**
1543+
* adapter1_call_set_discovery_filter_sync:
1544+
* @proxy: A #Adapter1Proxy.
1545+
* @arg_filter: Argument to pass with the method invocation.
1546+
* @cancellable: (allow-none): A #GCancellable or %NULL.
1547+
* @error: Return location for error or %NULL.
1548+
*
1549+
* Synchronously invokes the <link linkend="gdbus-method-org-bluez-Adapter1.SetDiscoveryFilter">SetDiscoveryFilter()</link> D-Bus method on @proxy. The calling thread is blocked until a reply is received.
1550+
*
1551+
* See adapter1_call_set_discovery_filter() for the asynchronous version of this method.
1552+
*
1553+
* Returns: (skip): %TRUE if the call succeded, %FALSE if @error is set.
1554+
*/
1555+
gboolean
1556+
adapter1_call_set_discovery_filter_sync (
1557+
Adapter1 *proxy,
1558+
GVariant *arg_filter,
1559+
GCancellable *cancellable,
1560+
GError **error)
1561+
{
1562+
GVariant *_ret;
1563+
_ret = g_dbus_proxy_call_sync (G_DBUS_PROXY (proxy),
1564+
"SetDiscoveryFilter",
1565+
g_variant_new ("(@a{sv})",
1566+
arg_filter),
1567+
G_DBUS_CALL_FLAGS_NONE,
1568+
-1,
1569+
cancellable,
1570+
error);
1571+
if (_ret == NULL)
1572+
goto _out;
1573+
g_variant_get (_ret,
1574+
"()");
1575+
g_variant_unref (_ret);
1576+
_out:
1577+
return _ret != NULL;
1578+
}
1579+
14271580
/**
14281581
* adapter1_complete_start_discovery:
14291582
* @object: A #Adapter1.
@@ -1478,6 +1631,24 @@ adapter1_complete_remove_device (
14781631
g_variant_new ("()"));
14791632
}
14801633

1634+
/**
1635+
* adapter1_complete_set_discovery_filter:
1636+
* @object: A #Adapter1.
1637+
* @invocation: (transfer full): A #GDBusMethodInvocation.
1638+
*
1639+
* Helper function used in service implementations to finish handling invocations of the <link linkend="gdbus-method-org-bluez-Adapter1.SetDiscoveryFilter">SetDiscoveryFilter()</link> D-Bus method. If you instead want to finish handling an invocation by returning an error, use g_dbus_method_invocation_return_error() or similar.
1640+
*
1641+
* This method will free @invocation, you cannot use it afterwards.
1642+
*/
1643+
void
1644+
adapter1_complete_set_discovery_filter (
1645+
Adapter1 *object,
1646+
GDBusMethodInvocation *invocation)
1647+
{
1648+
g_dbus_method_invocation_return_value (invocation,
1649+
g_variant_new ("()"));
1650+
}
1651+
14811652
/* ------------------------------------------------------------------------ */
14821653

14831654
/**

src/org.bluez.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ THE SOFTWARE.
2828
<method name="RemoveDevice">
2929
<arg name="device" type="o" direction="in"/>
3030
</method>
31+
<method name="SetDiscoveryFilter">
32+
<arg name="filter" type="a{sv}" direction="in"/>
33+
</method>
3134
<property name="Address" type="s" access="read"/>
3235
<property name="Name" type="s" access="read"/>
3336
<property name="Alias" type="s" access="readwrite"/>
@@ -111,4 +114,4 @@ THE SOFTWARE.
111114
<property name="Characteristic" type="o" access="read"/>
112115
<property name="Value" type="ay" access="read"/>
113116
</interface>
114-
</node>
117+
</node>

0 commit comments

Comments
 (0)