Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitsangwan01 committed Sep 24, 2024
1 parent f98ee29 commit d4a88db
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 103 deletions.
4 changes: 2 additions & 2 deletions lib/src/bluez_advertisement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BlueZAdvertisingManager {

_nextAdvertId += 1;

await _client.bus.registerObject(advert);
await _client.registerObject(advert);

await _object.callMethod(_advertInterfaceName, 'RegisterAdvertisement',
[advert.path, DBusDict.stringVariant({})],
Expand All @@ -80,7 +80,7 @@ class BlueZAdvertisingManager {
_advertInterfaceName, 'UnregisterAdvertisement', [advert.path],
replySignature: DBusSignature(''));

await _client.bus.unregisterObject(advert);
await _client.unregisterObject(advert);
}
}

Expand Down
89 changes: 89 additions & 0 deletions lib/src/bluez_agent_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'package:bluez/src/bluez_agent.dart';
import 'package:bluez/src/bluez_client.dart';
import 'package:bluez/src/bluez_uuid.dart';
import 'package:dbus/dbus.dart';

class BlueZAgentObject extends DBusObject {
final BlueZClient bluezClient;
final BlueZAgent agent;

BlueZAgentObject(this.bluezClient, this.agent, DBusObjectPath path)
: super(path);

@override
Future<DBusMethodResponse> handleMethodCall(DBusMethodCall methodCall) async {
if (methodCall.interface != 'org.bluez.Agent1') {
return DBusMethodErrorResponse.unknownInterface();
}

if (methodCall.name == 'Release') {
if (methodCall.signature != DBusSignature('')) {
return DBusMethodErrorResponse.invalidArgs();
}
await agent.release();
return DBusMethodSuccessResponse();
} else if (methodCall.name == 'RequestPinCode') {
if (methodCall.signature != DBusSignature('o')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestPinCode(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!))
.response;
} else if (methodCall.name == 'DisplayPinCode') {
if (methodCall.signature != DBusSignature('os')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.displayPinCode(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!,
methodCall.values[1].asString()))
.response;
} else if (methodCall.name == 'RequestPasskey') {
if (methodCall.signature != DBusSignature('o')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestPasskey(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!))
.response;
} else if (methodCall.name == 'DisplayPasskey') {
if (methodCall.signature != DBusSignature('ouq')) {
return DBusMethodErrorResponse.invalidArgs();
}
await agent.displayPasskey(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!,
methodCall.values[1].asUint32(),
methodCall.values[2].asUint16());
return DBusMethodSuccessResponse();
} else if (methodCall.name == 'RequestConfirmation') {
if (methodCall.signature != DBusSignature('ou')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestConfirmation(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!,
methodCall.values[1].asUint32()))
.response;
} else if (methodCall.name == 'RequestAuthorization') {
if (methodCall.signature != DBusSignature('o')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestAuthorization(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!))
.response;
} else if (methodCall.name == 'AuthorizeService') {
if (methodCall.signature != DBusSignature('os')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.authorizeService(
bluezClient.getDevice(methodCall.values[0].asObjectPath())!,
BlueZUUID.fromString(methodCall.values[1].asString())))
.response;
} else if (methodCall.name == 'Cancel') {
if (methodCall.signature != DBusSignature('')) {
return DBusMethodErrorResponse.invalidArgs();
}
await agent.cancel();
return DBusMethodSuccessResponse();
} else {
return DBusMethodErrorResponse.unknownMethod();
}
}
}
8 changes: 4 additions & 4 deletions lib/src/bluez_battery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BlueZBatteryProviderManager {
DBusObjectPath('/org/bluez/battery/provider$_nextBatteryProviderId'));
_nextBatteryProviderId += 1;

await _client.bus.registerObject(provider);
await _client.registerObject(provider);

await _object.callMethod(_batteryProviderManagerInterfaceName,
'RegisterBatteryProvider', [provider.path],
Expand All @@ -37,7 +37,7 @@ class BlueZBatteryProviderManager {
'UnregisterBatteryProvider', [provider.path],
replySignature: DBusSignature(''));

await _client.bus.unregisterObject(provider);
await _client.unregisterObject(provider);
}
}

Expand All @@ -59,13 +59,13 @@ class BlueZBatteryProvider extends DBusObject {
percentage: percentage, source: source);
_nextBatteryId += 1;

await _client.bus.registerObject(battery);
await _client.registerObject(battery);
return battery;
}

/// Removes a [battery] previously added with [addBattery].
Future<void> removeBattery(BlueZBattery battery) async {
await _client.bus.unregisterObject(battery);
await _client.unregisterObject(battery);
}
}

Expand Down
110 changes: 13 additions & 97 deletions lib/src/bluez_client.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'dart:async';

import 'package:bluez/src/bluez_adapter.dart';
import 'package:bluez/src/bluez_agent_object.dart';
import 'package:bluez/src/bluez_characteristic.dart';
import 'package:bluez/src/bluez_device.dart';
import 'package:bluez/src/bluez_enums.dart';
import 'package:bluez/src/bluez_gatt_descriptor.dart';
import 'package:bluez/src/bluez_gatt_service.dart';
import 'package:bluez/src/bluez_object.dart';
import 'package:bluez/src/bluez_uuid.dart';
import 'package:dbus/dbus.dart';
import 'package:bluez/src/bluez_agent.dart';

Expand Down Expand Up @@ -50,7 +50,7 @@ class BlueZClient {
StreamController<BlueZDevice>.broadcast();

/// Registered agent.
_BlueZAgentObject? _agent;
BlueZAgentObject? _agent;

/// Creates a new BlueZ client. If [bus] is provided connect to the given D-Bus server.
BlueZClient({DBusClient? bus})
Expand Down Expand Up @@ -165,7 +165,7 @@ class BlueZClient {
throw 'Missing /org/bluez object required for agent registration';
}

_agent = _BlueZAgentObject(
_agent = BlueZAgentObject(
this, agent, path ?? DBusObjectPath('/org/bluez/Agent'));
await _bus.registerObject(_agent!);

Expand Down Expand Up @@ -223,14 +223,6 @@ class BlueZClient {
}
}

BlueZDevice? _getDevice(DBusObjectPath objectPath) {
var object = _objects[objectPath];
if (object == null) {
return null;
}
return BlueZDevice(this, object);
}

bool _isDevice(BlueZObject object) {
return object.interfaces.containsKey('org.bluez.Device1');
}
Expand All @@ -240,8 +232,17 @@ class BlueZClient {
}
}

/// This extension is for internal use within the plugin and is not part of the public API.
extension BluezClientInternalExtension on BlueZClient {
DBusClient get bus => _bus;
Future<void> registerObject(DBusObject object) => _bus.registerObject(object);

Future<void> unregisterObject(DBusObject object) =>
_bus.unregisterObject(object);

BlueZDevice? getDevice(DBusObjectPath objectPath) {
var object = _objects[objectPath];
return object == null ? null : BlueZDevice(this, object);
}

BlueZAdapter? getAdapter(DBusObjectPath objectPath) {
var object = _objects[objectPath];
Expand Down Expand Up @@ -292,88 +293,3 @@ extension BluezClientInternalExtension on BlueZClient {
return object.interfaces.containsKey('org.bluez.GattDescriptor1');
}
}

class _BlueZAgentObject extends DBusObject {
final BlueZClient bluezClient;
final BlueZAgent agent;

_BlueZAgentObject(this.bluezClient, this.agent, DBusObjectPath path)
: super(path);

@override
Future<DBusMethodResponse> handleMethodCall(DBusMethodCall methodCall) async {
if (methodCall.interface != 'org.bluez.Agent1') {
return DBusMethodErrorResponse.unknownInterface();
}

if (methodCall.name == 'Release') {
if (methodCall.signature != DBusSignature('')) {
return DBusMethodErrorResponse.invalidArgs();
}
await agent.release();
return DBusMethodSuccessResponse();
} else if (methodCall.name == 'RequestPinCode') {
if (methodCall.signature != DBusSignature('o')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestPinCode(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!))
.response;
} else if (methodCall.name == 'DisplayPinCode') {
if (methodCall.signature != DBusSignature('os')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.displayPinCode(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!,
methodCall.values[1].asString()))
.response;
} else if (methodCall.name == 'RequestPasskey') {
if (methodCall.signature != DBusSignature('o')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestPasskey(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!))
.response;
} else if (methodCall.name == 'DisplayPasskey') {
if (methodCall.signature != DBusSignature('ouq')) {
return DBusMethodErrorResponse.invalidArgs();
}
await agent.displayPasskey(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!,
methodCall.values[1].asUint32(),
methodCall.values[2].asUint16());
return DBusMethodSuccessResponse();
} else if (methodCall.name == 'RequestConfirmation') {
if (methodCall.signature != DBusSignature('ou')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestConfirmation(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!,
methodCall.values[1].asUint32()))
.response;
} else if (methodCall.name == 'RequestAuthorization') {
if (methodCall.signature != DBusSignature('o')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.requestAuthorization(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!))
.response;
} else if (methodCall.name == 'AuthorizeService') {
if (methodCall.signature != DBusSignature('os')) {
return DBusMethodErrorResponse.invalidArgs();
}
return (await agent.authorizeService(
bluezClient._getDevice(methodCall.values[0].asObjectPath())!,
BlueZUUID.fromString(methodCall.values[1].asString())))
.response;
} else if (methodCall.name == 'Cancel') {
if (methodCall.signature != DBusSignature('')) {
return DBusMethodErrorResponse.invalidArgs();
}
await agent.cancel();
return DBusMethodSuccessResponse();
} else {
return DBusMethodErrorResponse.unknownMethod();
}
}
}

0 comments on commit d4a88db

Please sign in to comment.