Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement CoAP over TCP #177

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
26 changes: 26 additions & 0 deletions example/tcp_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ignore_for_file: avoid_print

import 'dart:core';

import 'package:coap/coap.dart';

/// Tests the basic functionality of the TCP network.
/// Will be replaced with a "real" example later.
Future<void> main() async {
await connect();
}

Future<void> connect() async {
final coapClient =
CoapClient(Uri.parse('coap+tcp://californium.eclipseprojects.io'));

final response = await coapClient.post(
Uri(path: 'test'),
format: CoapMediaType.applicationJson,
accept: CoapMediaType.applicationCbor,
payload: 'Hello?',
);
print(response);

coapClient.close();
}
4 changes: 2 additions & 2 deletions lib/src/coap_empty_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class CoapEmptyMessage extends CoapMessage {
..destination = message.source;

CoapEmptyMessage.fromParsed({
required final CoapMessageType type,
required final int id,
required final Uint8Buffer token,
required final List<Option<Object?>> options,
required final Uint8Buffer? payload,
required final bool hasUnknownCriticalOption,
required final bool hasFormatError,
final int? id,
final CoapMessageType? type,
}) : super.fromParsed(
RequestMethod.empty.coapCode,
type,
Expand Down
33 changes: 7 additions & 26 deletions lib/src/coap_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'coap_code.dart';
import 'coap_media_type.dart';
import 'coap_message_type.dart';
import 'coap_response.dart';
import 'codec/tcp/message_encoder.dart';
import 'codec/udp/message_decoder.dart';
import 'codec/udp/message_encoder.dart';
import 'event/coap_event_bus.dart';
Expand All @@ -38,7 +39,7 @@ typedef HookFunction = void Function();
abstract class CoapMessage {
CoapMessage(
this.code,
this._type, {
this.type, {
final Iterable<int>? payload,
final CoapMediaType? contentFormat,
}) : payload = Uint8Buffer()..addAll(payload ?? []) {
Expand All @@ -47,15 +48,14 @@ abstract class CoapMessage {

CoapMessage.fromParsed(
this.code,
this._type, {
required final int id,
this.type, {
required final Uint8Buffer token,
required final List<Option<Object?>> options,
required final Uint8Buffer? payload,
required this.hasUnknownCriticalOption,
required this.hasFormatError,
this.id,
}) : payload = payload ?? Uint8Buffer() {
this.id = id;
this.token = token;
setOptions(options);
}
Expand All @@ -64,26 +64,17 @@ abstract class CoapMessage {

bool hasFormatError = false;

CoapMessageType _type;

@internal
set type(final CoapMessageType type) => _type = type;

/// The type of this CoAP message.
CoapMessageType get type => _type;
CoapMessageType? type;

/// The code of this CoAP message.
final CoapCode code;

/// The codestring
String get codeString => code.toString();

int? _id;

/// The ID of this CoAP message.
int? get id => _id;
@internal
set id(final int? val) => _id = val;
int? id;

final List<Option<Object?>> _options = [];

Expand Down Expand Up @@ -635,18 +626,8 @@ abstract class CoapMessage {
/// Is also used for DTLS.
Uint8Buffer toUdpPayload() => serializeUdpMessage(this);

/// Serializes this CoAP message from the TCP message format.
///
/// Is also used for TLS.
static CoapMessage? fromTcpPayload(final Uint8Buffer data) =>
throw UnimplementedError(
'TCP segment deserialization is not implemented yet.',
);

/// Serializes this CoAP message into the TCP message format.
///
/// Is also used for TLS.
Uint8Buffer toTcpPayload() => throw UnimplementedError(
'TCP segment serialization is not implemented yet.',
);
Uint8Buffer toTcpPayload() => serializeTcpMessage(this);
}
12 changes: 7 additions & 5 deletions lib/src/coap_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CoapRequest extends CoapMessage {
final RequestMethod method;

@override
CoapMessageType get type {
CoapMessageType? get type {
if (super.type == CoapMessageType.con && isMulticast) {
return CoapMessageType.non;
}
Expand Down Expand Up @@ -73,8 +73,10 @@ class CoapRequest extends CoapMessage {
Endpoint? get endpoint => _endpoint;
@internal
set endpoint(final Endpoint? endpoint) {
super.id = endpoint!.nextMessageId;
super.destination = endpoint.destination;
if (['coap', 'coaps'].contains(uri.scheme)) {
super.id = endpoint!.nextMessageId;
}
super.destination = endpoint!.destination;
_endpoint = endpoint;
}

Expand Down Expand Up @@ -199,13 +201,13 @@ class CoapRequest extends CoapMessage {
CoapRequest.fromParsed(
this.uri,
this.method, {
required final CoapMessageType type,
required final int id,
required final Uint8Buffer token,
required final List<Option<Object?>> options,
required final Uint8Buffer? payload,
required final bool hasUnknownCriticalOption,
required final bool hasFormatError,
final CoapMessageType? type,
final int? id,
}) : super.fromParsed(
method.coapCode,
type,
Expand Down
12 changes: 7 additions & 5 deletions lib/src/coap_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CoapResponse extends CoapMessage {
/// Initializes a response message.
CoapResponse(
this.responseCode,
final CoapMessageType type, {
final CoapMessageType? type, {
final Uri? location,
super.payload,
}) : location = location ?? Uri(path: '/'),
Expand All @@ -43,8 +43,10 @@ class CoapResponse extends CoapMessage {
final Uri location;

@override
List<Option<Object?>> getAllOptions() =>
locationToOptions(location)..addAll(super.getAllOptions());
List<Option<Object?>> getAllOptions() => locationToOptions(location)
..addAll(
super.getAllOptions().where((final element) => !element.isLocationOption),
);

/// Status code as a string
String get statusCodeString => code.toString();
Expand Down Expand Up @@ -100,14 +102,14 @@ class CoapResponse extends CoapMessage {

CoapResponse.fromParsed(
this.responseCode, {
required final CoapMessageType type,
required final int id,
required final Uint8Buffer token,
required final List<Option<Object?>> options,
required final Uint8Buffer? payload,
required final bool hasUnknownCriticalOption,
required final bool hasFormatError,
final Uri? location,
final int? id,
final CoapMessageType? type,
}) : location = location ?? Uri(path: '/'),
super.fromParsed(
responseCode.coapCode,
Expand Down
Loading