Skip to content

Commit

Permalink
WIP: Implement CoAP over TCP logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Feb 18, 2023
1 parent 564a379 commit bce421f
Show file tree
Hide file tree
Showing 7 changed files with 703 additions and 35 deletions.
47 changes: 47 additions & 0 deletions example/tcp_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ignore_for_file: avoid_print

import 'dart:core';
import 'dart:io';

import 'package:coap/coap.dart';

Future<void> startServer() async {
final server = await ServerSocket.bind(InternetAddress.anyIPv4, 5683);
server.listen((final connection) async {
await connection.forEach((final frame) {
print(frame);

const responseCode = (2 << 5) + 5;

const tokenLength = 8;
const tokenOffset = 2;
final token = frame.sublist(tokenOffset, tokenOffset + tokenLength);

final response = [tokenLength, responseCode, ...token];

connection.add(response);
});
});
}

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

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

final response = await coapClient.get(
'test',
options: [ContentFormatOption(40)],
);
// TODO(JKRhb): Responses can't be matched at the moment, as the current
// implementation requires a message ID which is not defined in
// CoAP over TCP.
print(response);

coapClient.close();
}
13 changes: 2 additions & 11 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 Down Expand Up @@ -657,18 +658,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);
}
Loading

0 comments on commit bce421f

Please sign in to comment.