Skip to content

Commit

Permalink
feat!: update coap dependency, fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 29, 2023
1 parent b3322b7 commit 4928baa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 47 deletions.
7 changes: 5 additions & 2 deletions example/coaps_readproperty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ PskCredentials? _pskCredentialsCallback(
}

Future<void> main(List<String> args) async {
final CoapClientFactory coapClientFactory =
CoapClientFactory(CoapConfig(useTinyDtls: true));
final CoapClientFactory coapClientFactory = CoapClientFactory(
CoapConfig(
dtlsCiphers: 'PSK-AES128-CCM8',
),
);
final securityProvider =
ClientSecurityProvider(pskCredentialsCallback: _pskCredentialsCallback);
final servient = Servient(clientSecurityProvider: securityProvider)
Expand Down
43 changes: 18 additions & 25 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: BSD-3-Clause

import 'dart:async';
import 'dart:typed_data';

import 'package:coap/coap.dart' as coap;
import 'package:coap/config/coap_config_default.dart';
Expand All @@ -26,35 +27,28 @@ import 'coap_extensions.dart';
import 'coap_subscription.dart';

class _InternalCoapConfig extends CoapConfigDefault {
_InternalCoapConfig(CoapConfig coapConfig, this._form)
_InternalCoapConfig(CoapConfig coapConfig)
: preferredBlockSize =
coapConfig.blocksize ?? coap.CoapConstants.preferredBlockSize {
if (!_dtlsNeeded) {
return;
}

final form = _form;

if (form == null) {
return;
}
coapConfig.blocksize ?? coap.CoapConstants.preferredBlockSize,
dtlsCiphers = coapConfig.dtlsCiphers,
dtlsVerify = coapConfig.dtlsVerify,
dtlsWithTrustedRoots = coapConfig.dtlsWithTrustedRoots,
rootCertificates = coapConfig.rootCertificates;

if (form.usesPskScheme && coapConfig.useTinyDtls) {
dtlsBackend = coap.DtlsBackend.TinyDtls;
} else if (coapConfig.useOpenSsl) {
dtlsBackend = coap.DtlsBackend.OpenSsl;
}
}
@override
final int preferredBlockSize;

@override
int preferredBlockSize;
final String? dtlsCiphers;

@override
coap.DtlsBackend? dtlsBackend;
final bool dtlsVerify;

final Form? _form;
@override
final bool dtlsWithTrustedRoots;

bool get _dtlsNeeded => _form?.resolvedHref.scheme == 'coaps';
@override
final List<Uint8List> rootCertificates;
}

coap.PskCredentialsCallback? _createPskCallback(
Expand Down Expand Up @@ -165,7 +159,7 @@ class CoapClient extends ProtocolClient {
}) async {
final coapClient = coap.CoapClient(
uri,
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
pskCredentialsCallback:
_createPskCallback(uri, form, _clientSecurityProvider),
);
Expand Down Expand Up @@ -248,7 +242,7 @@ class CoapClient extends ProtocolClient {

final coapClient = coap.CoapClient(
creationHintUri,
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
);

final response = await coapClient.send(request);
Expand Down Expand Up @@ -322,7 +316,6 @@ class CoapClient extends ProtocolClient {

final client = coap.CoapClient(
request.uri.replace(scheme: 'coaps'),
config: coap.CoapConfigTinydtls(),
pskCredentialsCallback: (identityHint) => pskCredentials,
);

Expand Down Expand Up @@ -434,7 +427,7 @@ class CoapClient extends ProtocolClient {

final coapClient = coap.CoapClient(
form.resolvedHref,
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
);

if (subprotocol == CoapSubprotocol.observe) {
Expand Down
26 changes: 18 additions & 8 deletions lib/src/binding_coap/coap_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import 'dart:typed_data';

/// Allows for configuring the behavior of CoAP clients and servers.
class CoapConfig {
/// Creates a new [CoapConfig] object.
CoapConfig({
this.port = 5683,
this.securePort = 5684,
this.blocksize,
this.useTinyDtls = false,
this.useOpenSsl = false,
this.allowMulticastDiscovery = false,
this.multicastDiscoveryTimeout = const Duration(minutes: 60),
this.dtlsCiphers,
this.rootCertificates = const [],
this.dtlsWithTrustedRoots = true,
this.dtlsVerify = true,
});

/// Whether certificates should be verified by OpenSSL.
final bool dtlsVerify;

/// Whether OpenSSL should be used with trusted Root Certificates.
final bool dtlsWithTrustedRoots;

/// Can be used to specify the Ciphers that should be used by OpenSSL.
final String? dtlsCiphers;

/// List of custom root certificates to use with OpenSSL.
final List<Uint8List> rootCertificates;

/// The port number used by a client or server. Defaults to 5683.
final int port;

Expand All @@ -26,12 +42,6 @@ class CoapConfig {
/// The preferred block size for blockwise transfer.
final int? blocksize;

/// Indicates if tinydtls is available as a DTLS backend.
final bool useTinyDtls;

/// Indicates if openSSL is available as a DTLS backend.
final bool useOpenSsl;

/// Indicates if multicast should be available for discovery.
///
/// Defaults to false for security reasons, as multicast can lead to
Expand Down
12 changes: 2 additions & 10 deletions lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,7 @@ extension OperationTypeExtension on OperationType {

/// Extension for easily extracting the [content] from a [CoapResponse].
extension ResponseExtension on CoapResponse {
Stream<List<int>> get _payloadStream {
final payload = this.payload;
if (payload != null) {
return Stream.value(payload);
} else {
return const Stream.empty();
}
}
Stream<List<int>> get _payloadStream => Stream.value(payload);

String get _contentType =>
contentFormat?.contentType.toString() ?? 'application/json';
Expand Down Expand Up @@ -238,8 +231,7 @@ extension ResponseExtension on CoapResponse {

final responsePayload = payload;

if (responsePayload != null &&
contentFormat == CoapMediaType.applicationAceCbor &&
if (contentFormat == CoapMediaType.applicationAceCbor &&
unauthorizedAceCodes.contains(contentFormat)) {
return AuthServerRequestCreationHint.fromSerialized(
responsePayload.toList(),
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=2.17.0 <3.0.0'

dev_dependencies:
build_runner: ^2.1.7
build_runner: ^2.3.0
coverage: ^1.0.4
dart_code_metrics: ^5.4.0
lint: ^2.0.1
Expand All @@ -17,7 +17,7 @@ dev_dependencies:

dependencies:
cbor: ^5.1.2
coap: ^7.0.0
coap: ^8.0.0
collection: ^1.16.0
curie: ^0.1.0
dcaf: ^0.1.0
Expand Down

0 comments on commit 4928baa

Please sign in to comment.