diff options
author | Vlad Kolotov <[email protected]> | 2017-10-13 22:49:52 +1300 |
---|---|---|
committer | petreeftime <[email protected]> | 2017-10-14 17:18:12 +0300 |
commit | fd4fdb0854d328f44bbf0f3ce2493c30e8587849 (patch) | |
tree | f50d40e5a0f25c8b57e28a171b3f9220620271fe | |
parent | 898f6ac604d6266292f74ce95d28ae8b7a7a3a2a (diff) |
Adding support for setting RSSI discovery filter
Signed-off-by: Vlad Kolotov <[email protected]>
-rw-r--r-- | api/tinyb/BluetoothAdapter.hpp | 16 | ||||
-rw-r--r-- | include/generated-code.h | 22 | ||||
-rw-r--r-- | java/BluetoothAdapter.java | 8 | ||||
-rw-r--r-- | java/jni/BluetoothAdapter.cxx | 21 | ||||
-rw-r--r-- | src/BluetoothAdapter.cpp | 51 | ||||
-rw-r--r-- | src/generated-code.c | 171 | ||||
-rw-r--r-- | src/org.bluez.xml | 5 |
7 files changed, 292 insertions, 2 deletions
diff --git a/api/tinyb/BluetoothAdapter.hpp b/api/tinyb/BluetoothAdapter.hpp index c16f8e42..b38cd636 100644 --- a/api/tinyb/BluetoothAdapter.hpp +++ b/api/tinyb/BluetoothAdapter.hpp @@ -25,6 +25,7 @@ #pragma once #include "BluetoothObject.hpp" #include "BluetoothManager.hpp" +#include "BluetoothUUID.hpp" #include <vector> /* Forward declaration of types */ @@ -33,6 +34,12 @@ typedef struct _Object Object; struct _Adapter1; typedef struct _Adapter1 Adapter1; +enum class TransportType { + AUTO, + BREDR, + LE +}; + /** * Provides access to Bluetooth adapters. Follows the BlueZ adapter API * available at: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/adapter-api.txt @@ -109,6 +116,15 @@ public: bool stop_discovery ( ); + /** Sets the device discovery filter for the caller. If all fields are empty, + * filter is removed. + * @param uuids Vector of UUIDs to filter on + * @param rssi RSSI low bounded threshold + * @param pathloss Pathloss threshold value + * @param transport Type of scan to run + */ + bool set_discovery_filter (std::vector<BluetoothUUID> uuids, + int16_t rssi, uint16_t pathloss, const TransportType &transport); /** Returns a list of BluetoothDevices visible from this adapter. * @return A list of BluetoothDevices visible on this adapter, diff --git a/include/generated-code.h b/include/generated-code.h index 69eb0432..6f9d83a3 100644 --- a/include/generated-code.h +++ b/include/generated-code.h @@ -42,6 +42,11 @@ struct _Adapter1Iface Adapter1 *object, GDBusMethodInvocation *invocation); + gboolean (*handle_set_discovery_filter) ( + Adapter1 *object, + GDBusMethodInvocation *invocation, + GVariant *arg_filter); + const gchar * (*get_address) (Adapter1 *object); const gchar * (*get_alias) (Adapter1 *object); @@ -140,6 +145,23 @@ gboolean adapter1_call_remove_device_sync ( GCancellable *cancellable, GError **error); +void adapter1_call_set_discovery_filter ( + Adapter1 *proxy, + GVariant *arg_filter, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +gboolean adapter1_call_set_discovery_filter_finish ( + Adapter1 *proxy, + GAsyncResult *res, + GError **error); + +gboolean adapter1_call_set_discovery_filter_sync ( + Adapter1 *proxy, + GVariant *arg_filter, + GCancellable *cancellable, + GError **error); /* D-Bus property accessors: */ diff --git a/java/BluetoothAdapter.java b/java/BluetoothAdapter.java index e48909bc..0bbaecde 100644 --- a/java/BluetoothAdapter.java +++ b/java/BluetoothAdapter.java @@ -238,7 +238,13 @@ public class BluetoothAdapter extends BluetoothObject * @return The local ID of the adapter. */ public native String getModalias(); - + + /** This method sets RSSI device discovery filter for the caller. When this method is called + * with 0, filter is removed. + * @param rssi a rssi value + */ + public native void setRssiDiscoveryFilter(int rssi); + /** Returns the interface name of the adapter. * @return The interface name of the adapter. */ diff --git a/java/jni/BluetoothAdapter.cxx b/java/jni/BluetoothAdapter.cxx index 1775da4e..0a15a4e1 100644 --- a/java/jni/BluetoothAdapter.cxx +++ b/java/jni/BluetoothAdapter.cxx @@ -787,3 +787,24 @@ void Java_tinyb_BluetoothAdapter_delete(JNIEnv *env, jobject obj) } } +void Java_tinyb_BluetoothAdapter_setRssiDiscoveryFilter(JNIEnv *env, jobject obj, jint rssi) +{ + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + std::vector<BluetoothUUID> native_uuids; + native_uuids.reserve(0); + + obj_adapter->set_discovery_filter(native_uuids, (int16_t) rssi, 0, TransportType::AUTO); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (BluetoothException &e) { + raise_java_bluetooth_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } +}
\ No newline at end of file diff --git a/src/BluetoothAdapter.cpp b/src/BluetoothAdapter.cpp index 35c8865c..c3844320 100644 --- a/src/BluetoothAdapter.cpp +++ b/src/BluetoothAdapter.cpp @@ -211,6 +211,57 @@ bool BluetoothAdapter::remove_device ( return result; } +bool BluetoothAdapter::set_discovery_filter (std::vector<BluetoothUUID> uuids, + int16_t rssi, uint16_t pathloss, const TransportType &transport) +{ + GError *error = NULL; + bool result = true; + GVariantDict dict; + g_variant_dict_init(&dict, NULL); + + if (uuids.size() > 0) + { + GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)")); + + for (std::vector<BluetoothUUID>::iterator i = uuids.begin(); i != uuids.end(); ++i) + g_variant_builder_add(builder, "(s)", (*i).get_string().c_str()); + + GVariant *uuids_variant = g_variant_new("a(s)", builder); + g_variant_builder_unref(builder); + g_variant_dict_insert_value(&dict, "UUIDs", uuids_variant); + } + + if (rssi != 0) + g_variant_dict_insert_value(&dict, "RSSI", g_variant_new_int16(rssi)); + + if (pathloss != 0) + g_variant_dict_insert_value(&dict, "Pathloss", g_variant_new_uint16(pathloss)); + + std::string transport_str; + + if (transport == TransportType::AUTO) + transport_str = "auto"; + else if (transport == TransportType::BREDR) + transport_str = "bredr"; + else if (transport == TransportType::LE) + transport_str = "le"; + + if (!transport_str.empty()) + g_variant_dict_insert_value(&dict, "Transport", g_variant_new_string(transport_str.c_str())); + + GVariant *variant = g_variant_dict_end(&dict); + + result = adapter1_call_set_discovery_filter_sync( + object, + variant, + NULL, + &error + ); + + handle_error(error); + return result; +} + /* D-Bus property accessors: */ std::string BluetoothAdapter::get_address () { diff --git a/src/generated-code.c b/src/generated-code.c index 350e09da..e8fb54e5 100644 --- a/src/generated-code.c +++ b/src/generated-code.c @@ -219,11 +219,42 @@ static const _ExtendedGDBusMethodInfo _adapter1_method_info_remove_device = FALSE }; +static const _ExtendedGDBusArgInfo _adapter1_method_info_set_discovery_filter_IN_ARG_filter = +{ + { + -1, + (gchar *) "filter", + (gchar *) "a{sv}", + NULL + }, + FALSE +}; + +static const _ExtendedGDBusArgInfo * const _adapter1_method_info_set_discovery_filter_IN_ARG_pointers[] = +{ + &_adapter1_method_info_set_discovery_filter_IN_ARG_filter, + NULL +}; + +static const _ExtendedGDBusMethodInfo _adapter1_method_info_set_discovery_filter = +{ + { + -1, + (gchar *) "SetDiscoveryFilter", + (GDBusArgInfo **) &_adapter1_method_info_set_discovery_filter_IN_ARG_pointers, + NULL, + NULL + }, + "handle-set-discovery-filter", + FALSE +}; + static const _ExtendedGDBusMethodInfo * const _adapter1_method_info_pointers[] = { &_adapter1_method_info_start_discovery, &_adapter1_method_info_stop_discovery, &_adapter1_method_info_remove_device, + &_adapter1_method_info_set_discovery_filter, NULL }; @@ -467,6 +498,7 @@ adapter1_override_properties (GObjectClass *klass, guint property_id_begin) * Adapter1Iface: * @parent_iface: The parent interface. * @handle_remove_device: Handler for the #Adapter1::handle-remove-device signal. + * @handle_set_discovery_filter: Handler for the #Adapter1::handle-set-discovery-filter signal. * @handle_start_discovery: Handler for the #Adapter1::handle-start-discovery signal. * @handle_stop_discovery: Handler for the #Adapter1::handle-stop-discovery signal. * @get_address: Getter for the #Adapter1:address property. @@ -559,6 +591,29 @@ adapter1_default_init (Adapter1Iface *iface) 2, G_TYPE_DBUS_METHOD_INVOCATION, G_TYPE_STRING); + /** + * Adapter1::handle-set-discovery-filter: + * @object: A #Adapter1. + * @invocation: A #GDBusMethodInvocation. + * @arg_filter: Argument passed by remote caller. + * + * Signal emitted when a remote caller is invoking the <link linkend="gdbus-method-org-bluez-Adapter1.SetDiscoveryFilter">SetDiscoveryFilter()</link> D-Bus method. + * + * 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. + * + * Returns: %TRUE if the invocation was handled, %FALSE to let other signal handlers run. + */ + g_signal_new ("handle-set-discovery-filter", + G_TYPE_FROM_INTERFACE (iface), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (Adapter1Iface, handle_set_discovery_filter), + g_signal_accumulator_true_handled, + NULL, + g_cclosure_marshal_generic, + G_TYPE_BOOLEAN, + 2, + G_TYPE_DBUS_METHOD_INVOCATION, G_TYPE_VARIANT); + /* GObject properties for D-Bus properties: */ /** * Adapter1:address: @@ -1425,6 +1480,104 @@ _out: } /** + * adapter1_call_set_discovery_filter: + * @proxy: A #Adapter1Proxy. + * @arg_filter: Argument to pass with the method invocation. + * @cancellable: (allow-none): A #GCancellable or %NULL. + * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL. + * @user_data: User data to pass to @callback. + * + * Asynchronously invokes the <link linkend="gdbus-method-org-bluez-Adapter1.SetDiscoveryFilter">SetDiscoveryFilter()</link> D-Bus method on @proxy. + * 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. + * You can then call adapter1_call_set_discovery_filter_finish() to get the result of the operation. + * + * See adapter1_call_set_discovery_filter_sync() for the synchronous, blocking version of this method. + */ +void +adapter1_call_set_discovery_filter ( + Adapter1 *proxy, + GVariant *arg_filter, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_dbus_proxy_call (G_DBUS_PROXY (proxy), + "SetDiscoveryFilter", + g_variant_new ("(@a{sv})", + arg_filter), + G_DBUS_CALL_FLAGS_NONE, + -1, + cancellable, + callback, + user_data); +} + +/** + * adapter1_call_set_discovery_filter_finish: + * @proxy: A #Adapter1Proxy. + * @res: The #GAsyncResult obtained from the #GAsyncReadyCallback passed to adapter1_call_set_discovery_filter(). + * @error: Return location for error or %NULL. + * + * Finishes an operation started with adapter1_call_set_discovery_filter(). + * + * Returns: (skip): %TRUE if the call succeded, %FALSE if @error is set. + */ +gboolean +adapter1_call_set_discovery_filter_finish ( + Adapter1 *proxy, + GAsyncResult *res, + GError **error) +{ + GVariant *_ret; + _ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (proxy), res, error); + if (_ret == NULL) + goto _out; + g_variant_get (_ret, + "()"); + g_variant_unref (_ret); +_out: + return _ret != NULL; +} + +/** + * adapter1_call_set_discovery_filter_sync: + * @proxy: A #Adapter1Proxy. + * @arg_filter: Argument to pass with the method invocation. + * @cancellable: (allow-none): A #GCancellable or %NULL. + * @error: Return location for error or %NULL. + * + * 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. + * + * See adapter1_call_set_discovery_filter() for the asynchronous version of this method. + * + * Returns: (skip): %TRUE if the call succeded, %FALSE if @error is set. + */ +gboolean +adapter1_call_set_discovery_filter_sync ( + Adapter1 *proxy, + GVariant *arg_filter, + GCancellable *cancellable, + GError **error) +{ + GVariant *_ret; + _ret = g_dbus_proxy_call_sync (G_DBUS_PROXY (proxy), + "SetDiscoveryFilter", + g_variant_new ("(@a{sv})", + arg_filter), + G_DBUS_CALL_FLAGS_NONE, + -1, + cancellable, + error); + if (_ret == NULL) + goto _out; + g_variant_get (_ret, + "()"); + g_variant_unref (_ret); +_out: + return _ret != NULL; +} + +/** * adapter1_complete_start_discovery: * @object: A #Adapter1. * @invocation: (transfer full): A #GDBusMethodInvocation. @@ -1478,6 +1631,24 @@ adapter1_complete_remove_device ( g_variant_new ("()")); } +/** + * adapter1_complete_set_discovery_filter: + * @object: A #Adapter1. + * @invocation: (transfer full): A #GDBusMethodInvocation. + * + * 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. + * + * This method will free @invocation, you cannot use it afterwards. + */ +void +adapter1_complete_set_discovery_filter ( + Adapter1 *object, + GDBusMethodInvocation *invocation) +{ + g_dbus_method_invocation_return_value (invocation, + g_variant_new ("()")); +} + /* ------------------------------------------------------------------------ */ /** diff --git a/src/org.bluez.xml b/src/org.bluez.xml index 0c3b99bb..6b36d091 100644 --- a/src/org.bluez.xml +++ b/src/org.bluez.xml @@ -28,6 +28,9 @@ THE SOFTWARE. <method name="RemoveDevice"> <arg name="device" type="o" direction="in"/> </method> + <method name="SetDiscoveryFilter"> + <arg name="filter" type="a{sv}" direction="in"/> + </method> <property name="Address" type="s" access="read"/> <property name="Name" type="s" access="read"/> <property name="Alias" type="s" access="readwrite"/> @@ -111,4 +114,4 @@ THE SOFTWARE. <property name="Characteristic" type="o" access="read"/> <property name="Value" type="ay" access="read"/> </interface> -</node> +</node>
\ No newline at end of file |