Skip to content

Commit

Permalink
feat: adjust coap binding to new library API
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Aug 12, 2022
1 parent af7e47d commit b1b7214
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 61 deletions.
10 changes: 7 additions & 3 deletions example/coaps_readproperty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uri, PskCredentials> _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);

Expand Down
25 changes: 8 additions & 17 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,14 @@ class CoapClient extends ProtocolClient {
final ClientSecurityProvider? _clientSecurityProvider;

Future<coap.CoapRequest> _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());
Expand All @@ -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<Content> _sendRequestFromForm(
Expand All @@ -145,11 +139,11 @@ class CoapClient extends ProtocolClient {
// limitations of the CoAP library
Future<Content> _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,
Expand All @@ -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;
}

Expand Down
25 changes: 10 additions & 15 deletions lib/src/binding_coap/coap_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
32 changes: 10 additions & 22 deletions lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>) {
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) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/core/codecs/link_format_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class LinkFormatCodec extends ContentCodec {
DataSchema? dataSchema,
Map<String, String>? 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;
Expand Down
6 changes: 4 additions & 2 deletions lib/src/core/credentials/psk_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import 'dart:typed_data';

import '../../definitions/security/psk_security_scheme.dart';
import 'credentials.dart';

Expand All @@ -17,8 +19,8 @@ class PskCredentials extends Credentials<PskSecurityScheme> {
///
/// 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;
}
4 changes: 3 additions & 1 deletion lib/src/core/security_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b1b7214

Please sign in to comment.