From b1b72146dfa0b5b2462d1234091bd47385c11e8a Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sat, 6 Aug 2022 12:31:36 +0200 Subject: [PATCH] feat: adjust coap binding to new library API --- example/coaps_readproperty.dart | 10 ++++-- lib/src/binding_coap/coap_client.dart | 25 +++++---------- lib/src/binding_coap/coap_definitions.dart | 25 ++++++--------- lib/src/binding_coap/coap_extensions.dart | 32 ++++++------------- lib/src/core/codecs/link_format_codec.dart | 2 ++ lib/src/core/credentials/psk_credentials.dart | 6 ++-- lib/src/core/security_provider.dart | 4 ++- pubspec.yaml | 2 +- 8 files changed, 45 insertions(+), 61 deletions(-) diff --git a/example/coaps_readproperty.dart b/example/coaps_readproperty.dart index 8d190c11..8c739292 100644 --- a/example/coaps_readproperty.dart +++ b/example/coaps_readproperty.dart @@ -6,18 +6,22 @@ // ignore_for_file: avoid_print +import 'dart:typed_data'; + import 'package:dart_wot/dart_wot.dart'; /// Matches [PskCredentials] by hostname and URI scheme. final Map _pskCredentialsStore = { - Uri(host: 'californium.eclipseprojects.io', scheme: 'coaps'): - PskCredentials(identity: 'Client_identity', preSharedKey: 'secretPSK') + Uri(host: 'californium.eclipseprojects.io', scheme: 'coaps'): PskCredentials( + identity: Uint8List.fromList('Client_identity'.codeUnits), + preSharedKey: Uint8List.fromList('secretPSK'.codeUnits), + ) }; PskCredentials? _pskCredentialsCallback( Uri uri, Form? form, - String? identityHint, + Uint8List? identityHint, ) { final key = Uri(scheme: uri.scheme, host: uri.host); diff --git a/lib/src/binding_coap/coap_client.dart b/lib/src/binding_coap/coap_client.dart index 1bfd4b3d..eaba82ff 100644 --- a/lib/src/binding_coap/coap_client.dart +++ b/lib/src/binding_coap/coap_client.dart @@ -96,20 +96,14 @@ class CoapClient extends ProtocolClient { final ClientSecurityProvider? _clientSecurityProvider; Future _createRequest( - int code, + coap.CoapCode code, Uri uri, { Content? content, - int? format, - int? accept, + coap.CoapMediaType? format, + coap.CoapMediaType? accept, int? block1Size, int? block2Size, }) async { - if (!coap.CoapCode.isRequest(code)) { - throw CoapBindingException( - '$code is not a valid request method code.', - ); - } - final payload = Uint8Buffer(); if (content != null) { payload.addAll((await content.byteBuffer).asUint8List()); @@ -118,8 +112,8 @@ class CoapClient extends ProtocolClient { return coap.CoapRequest(code) ..payload = payload ..uriPath = uri.path - ..accept = accept ?? coap.CoapMediaType.undefined - ..contentFormat = format ?? coap.CoapMediaType.undefined; + ..accept = accept + ..contentFormat = format; } Future _sendRequestFromForm( @@ -145,11 +139,11 @@ class CoapClient extends ProtocolClient { // limitations of the CoAP library Future _sendRequest( Uri uri, - int method, { + coap.CoapCode method, { Content? content, required Form? form, - int? format, - int? accept, + coap.CoapMediaType? format, + coap.CoapMediaType? accept, int? block1Size, int? block2Size, coap.CoapMulticastResponseHandler? multicastResponseHandler, @@ -176,9 +170,6 @@ class CoapClient extends ProtocolClient { onMulticastResponse: multicastResponseHandler, ); coapClient.close(); - if (response == null) { - throw CoapBindingException('Sending CoAP request to $uri failed'); - } return response.content; } diff --git a/lib/src/binding_coap/coap_definitions.dart b/lib/src/binding_coap/coap_definitions.dart index 62ad7c6b..f08270ec 100644 --- a/lib/src/binding_coap/coap_definitions.dart +++ b/lib/src/binding_coap/coap_definitions.dart @@ -18,39 +18,34 @@ final coapPrefixMapping = /// Defines the available CoAP request methods. enum CoapRequestMethod { /// Corresponds with the GET request method. - get(CoapCode.get, 'GET'), + get(CoapCode.get), /// Corresponds with the PUT request method. - put(CoapCode.put, 'PUT'), + put(CoapCode.put), /// Corresponds with the POST request method. - post(CoapCode.post, 'POST'), + post(CoapCode.post), /// Corresponds with the DELETE request method. - delete(CoapCode.delete, 'DELETE'), + delete(CoapCode.delete), /// Corresponds with the FETCH request method. - fetch(CoapCode.notSet), + fetch(CoapCode.fetch), /// Corresponds with the PATCH request method. - patch(CoapCode.notSet), + patch(CoapCode.patch), /// Corresponds with the iPATCH request method. - ipatch(CoapCode.notSet); + ipatch(CoapCode.ipatch); /// Constructor - const CoapRequestMethod(this.code, [this.stringValue]); + const CoapRequestMethod(this.code); /// The numeric code of this [CoapRequestMethod]. - final int code; - - /// The string value of this request method value (e.g., `GET` or `POST`). - final String? stringValue; + final CoapCode code; static final _registry = HashMap.fromEntries( - values - .where((element) => element.stringValue != null) - .map((e) => MapEntry(e.stringValue, e)), + values.map((e) => MapEntry(e.code.description, e)), ); static CoapRequestMethod? _fromString(String stringValue) => diff --git a/lib/src/binding_coap/coap_extensions.dart b/lib/src/binding_coap/coap_extensions.dart index e7ffd2eb..74a4b4eb 100644 --- a/lib/src/binding_coap/coap_extensions.dart +++ b/lib/src/binding_coap/coap_extensions.dart @@ -34,27 +34,21 @@ extension CoapFormExtension on Form { return null; } - int _determineContentFormat(String fieldName) { - final curieString = coapPrefixMapping.expandCurieString(fieldName); - final dynamic formDefinition = additionalFields[curieString]; - if (formDefinition is int) { - return formDefinition; - } else if (formDefinition is List) { - return formDefinition[0]; - } - - return CoapMediaType.parse(contentType) ?? CoapMediaType.textPlain; + CoapMediaType _determineContentFormat(String contentType, String? encoding) { + return CoapMediaType.parse(contentType, encoding) ?? + CoapMediaType.applicationJson; } /// The Content-Format for CoAP request and response payloads. - int get format { - return _determineContentFormat('format'); + CoapMediaType get format { + return _determineContentFormat(contentType, contentCoding); } /// The Content-Format for the Accept option CoAP request and response /// payloads. - int get accept { - return _determineContentFormat('accept'); + CoapMediaType get accept { + // TODO: The algorithm for accept needs to be adjusted + return _determineContentFormat(contentType, contentCoding); } int? _determineBlockSize(String fieldName) { @@ -137,14 +131,8 @@ extension ResponseExtension on CoapResponse { } } - String get _contentType { - // FIXME: Replace once new CoAP library version has been released. - if (contentFormat == CoapMediaType.undefined) { - return 'application/json'; - } - - return CoapMediaType.name(contentFormat); - } + String get _contentType => + contentFormat?.contentType.toString() ?? 'application/json'; /// Extract the [Content] of this [CoapResponse]. Content get content { diff --git a/lib/src/core/codecs/link_format_codec.dart b/lib/src/core/codecs/link_format_codec.dart index 67d97ef7..f7481aaf 100644 --- a/lib/src/core/codecs/link_format_codec.dart +++ b/lib/src/core/codecs/link_format_codec.dart @@ -22,6 +22,8 @@ class LinkFormatCodec extends ContentCodec { DataSchema? dataSchema, Map? parameters, ) { + // TODO(JKRhb): The question which value types are allowed needs to be + // revisited. if (value is CoapIResource) { return Uint8List.fromList(CoapLinkFormat.serialize(value).codeUnits) .buffer; diff --git a/lib/src/core/credentials/psk_credentials.dart b/lib/src/core/credentials/psk_credentials.dart index 808714a5..359b2372 100644 --- a/lib/src/core/credentials/psk_credentials.dart +++ b/lib/src/core/credentials/psk_credentials.dart @@ -4,6 +4,8 @@ // // SPDX-License-Identifier: BSD-3-Clause +import 'dart:typed_data'; + import '../../definitions/security/psk_security_scheme.dart'; import 'credentials.dart'; @@ -17,8 +19,8 @@ class PskCredentials extends Credentials { /// /// May be omitted if the corresponding Security Definition in the TD already /// specifies an identity. - final String identity; + final Uint8List identity; /// The [preSharedKey] associated with these [PskCredentials]. - final String preSharedKey; + final Uint8List preSharedKey; } diff --git a/lib/src/core/security_provider.dart b/lib/src/core/security_provider.dart index f7ce8133..9e88c9fa 100644 --- a/lib/src/core/security_provider.dart +++ b/lib/src/core/security_provider.dart @@ -4,6 +4,8 @@ // // SPDX-License-Identifier: BSD-3-Clause +import 'dart:typed_data'; + import '../definitions/form.dart'; import 'credentials/apikey_credentials.dart'; import 'credentials/basic_credentials.dart'; @@ -26,7 +28,7 @@ import 'credentials/psk_credentials.dart'; typedef ClientPskCallback = PskCredentials? Function( Uri uri, Form? form, - String? identityHint, + Uint8List? identityHint, ); /// Function signature for a synchronous callback for providing client diff --git a/pubspec.yaml b/pubspec.yaml index 5865955a..8b0559d6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ dev_dependencies: dependencies: cbor: ^5.0.0 - coap: 4.1.0 + coap: ^5.0.0 collection: ^1.16.0 curie: ^0.1.0 http: ^0.13.4