Skip to content

Commit 4928baa

Browse files
committed
feat!: update coap dependency, fix example
1 parent b3322b7 commit 4928baa

File tree

5 files changed

+45
-47
lines changed

5 files changed

+45
-47
lines changed

example/coaps_readproperty.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ PskCredentials? _pskCredentialsCallback(
2929
}
3030

3131
Future<void> main(List<String> args) async {
32-
final CoapClientFactory coapClientFactory =
33-
CoapClientFactory(CoapConfig(useTinyDtls: true));
32+
final CoapClientFactory coapClientFactory = CoapClientFactory(
33+
CoapConfig(
34+
dtlsCiphers: 'PSK-AES128-CCM8',
35+
),
36+
);
3437
final securityProvider =
3538
ClientSecurityProvider(pskCredentialsCallback: _pskCredentialsCallback);
3639
final servient = Servient(clientSecurityProvider: securityProvider)

lib/src/binding_coap/coap_client.dart

+18-25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// SPDX-License-Identifier: BSD-3-Clause
66

77
import 'dart:async';
8+
import 'dart:typed_data';
89

910
import 'package:coap/coap.dart' as coap;
1011
import 'package:coap/config/coap_config_default.dart';
@@ -26,35 +27,28 @@ import 'coap_extensions.dart';
2627
import 'coap_subscription.dart';
2728

2829
class _InternalCoapConfig extends CoapConfigDefault {
29-
_InternalCoapConfig(CoapConfig coapConfig, this._form)
30+
_InternalCoapConfig(CoapConfig coapConfig)
3031
: preferredBlockSize =
31-
coapConfig.blocksize ?? coap.CoapConstants.preferredBlockSize {
32-
if (!_dtlsNeeded) {
33-
return;
34-
}
35-
36-
final form = _form;
37-
38-
if (form == null) {
39-
return;
40-
}
32+
coapConfig.blocksize ?? coap.CoapConstants.preferredBlockSize,
33+
dtlsCiphers = coapConfig.dtlsCiphers,
34+
dtlsVerify = coapConfig.dtlsVerify,
35+
dtlsWithTrustedRoots = coapConfig.dtlsWithTrustedRoots,
36+
rootCertificates = coapConfig.rootCertificates;
4137

42-
if (form.usesPskScheme && coapConfig.useTinyDtls) {
43-
dtlsBackend = coap.DtlsBackend.TinyDtls;
44-
} else if (coapConfig.useOpenSsl) {
45-
dtlsBackend = coap.DtlsBackend.OpenSsl;
46-
}
47-
}
38+
@override
39+
final int preferredBlockSize;
4840

4941
@override
50-
int preferredBlockSize;
42+
final String? dtlsCiphers;
5143

5244
@override
53-
coap.DtlsBackend? dtlsBackend;
45+
final bool dtlsVerify;
5446

55-
final Form? _form;
47+
@override
48+
final bool dtlsWithTrustedRoots;
5649

57-
bool get _dtlsNeeded => _form?.resolvedHref.scheme == 'coaps';
50+
@override
51+
final List<Uint8List> rootCertificates;
5852
}
5953

6054
coap.PskCredentialsCallback? _createPskCallback(
@@ -165,7 +159,7 @@ class CoapClient extends ProtocolClient {
165159
}) async {
166160
final coapClient = coap.CoapClient(
167161
uri,
168-
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
162+
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
169163
pskCredentialsCallback:
170164
_createPskCallback(uri, form, _clientSecurityProvider),
171165
);
@@ -248,7 +242,7 @@ class CoapClient extends ProtocolClient {
248242

249243
final coapClient = coap.CoapClient(
250244
creationHintUri,
251-
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
245+
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
252246
);
253247

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

323317
final client = coap.CoapClient(
324318
request.uri.replace(scheme: 'coaps'),
325-
config: coap.CoapConfigTinydtls(),
326319
pskCredentialsCallback: (identityHint) => pskCredentials,
327320
);
328321

@@ -434,7 +427,7 @@ class CoapClient extends ProtocolClient {
434427

435428
final coapClient = coap.CoapClient(
436429
form.resolvedHref,
437-
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
430+
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
438431
);
439432

440433
if (subprotocol == CoapSubprotocol.observe) {

lib/src/binding_coap/coap_config.dart

+18-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,35 @@
44
//
55
// SPDX-License-Identifier: BSD-3-Clause
66

7+
import 'dart:typed_data';
8+
79
/// Allows for configuring the behavior of CoAP clients and servers.
810
class CoapConfig {
911
/// Creates a new [CoapConfig] object.
1012
CoapConfig({
1113
this.port = 5683,
1214
this.securePort = 5684,
1315
this.blocksize,
14-
this.useTinyDtls = false,
15-
this.useOpenSsl = false,
1616
this.allowMulticastDiscovery = false,
1717
this.multicastDiscoveryTimeout = const Duration(minutes: 60),
18+
this.dtlsCiphers,
19+
this.rootCertificates = const [],
20+
this.dtlsWithTrustedRoots = true,
21+
this.dtlsVerify = true,
1822
});
1923

24+
/// Whether certificates should be verified by OpenSSL.
25+
final bool dtlsVerify;
26+
27+
/// Whether OpenSSL should be used with trusted Root Certificates.
28+
final bool dtlsWithTrustedRoots;
29+
30+
/// Can be used to specify the Ciphers that should be used by OpenSSL.
31+
final String? dtlsCiphers;
32+
33+
/// List of custom root certificates to use with OpenSSL.
34+
final List<Uint8List> rootCertificates;
35+
2036
/// The port number used by a client or server. Defaults to 5683.
2137
final int port;
2238

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

29-
/// Indicates if tinydtls is available as a DTLS backend.
30-
final bool useTinyDtls;
31-
32-
/// Indicates if openSSL is available as a DTLS backend.
33-
final bool useOpenSsl;
34-
3545
/// Indicates if multicast should be available for discovery.
3646
///
3747
/// Defaults to false for security reasons, as multicast can lead to

lib/src/binding_coap/coap_extensions.dart

+2-10
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,7 @@ extension OperationTypeExtension on OperationType {
190190

191191
/// Extension for easily extracting the [content] from a [CoapResponse].
192192
extension ResponseExtension on CoapResponse {
193-
Stream<List<int>> get _payloadStream {
194-
final payload = this.payload;
195-
if (payload != null) {
196-
return Stream.value(payload);
197-
} else {
198-
return const Stream.empty();
199-
}
200-
}
193+
Stream<List<int>> get _payloadStream => Stream.value(payload);
201194

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

239232
final responsePayload = payload;
240233

241-
if (responsePayload != null &&
242-
contentFormat == CoapMediaType.applicationAceCbor &&
234+
if (contentFormat == CoapMediaType.applicationAceCbor &&
243235
unauthorizedAceCodes.contains(contentFormat)) {
244236
return AuthServerRequestCreationHint.fromSerialized(
245237
responsePayload.toList(),

pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ environment:
77
sdk: '>=2.17.0 <3.0.0'
88

99
dev_dependencies:
10-
build_runner: ^2.1.7
10+
build_runner: ^2.3.0
1111
coverage: ^1.0.4
1212
dart_code_metrics: ^5.4.0
1313
lint: ^2.0.1
@@ -17,7 +17,7 @@ dev_dependencies:
1717

1818
dependencies:
1919
cbor: ^5.1.2
20-
coap: ^7.0.0
20+
coap: ^8.0.0
2121
collection: ^1.16.0
2222
curie: ^0.1.0
2323
dcaf: ^0.1.0

0 commit comments

Comments
 (0)