Skip to content

Add CoAP protocol binding for WoT consumer with Block2 support and enhanced error messages#12

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/add-coap-support-to-wot-api
Draft

Add CoAP protocol binding for WoT consumer with Block2 support and enhanced error messages#12
Copilot wants to merge 7 commits intomainfrom
copilot/add-coap-support-to-wot-api

Conversation

Copy link

Copilot AI commented Oct 28, 2025

Implements CoAP (Constrained Application Protocol) support per WoT Scripting API spec, enabling consumption of CoAP Things alongside existing HTTP/HTTPS support.

Implementation

New binding package (WoT.Net.Binding.CoAP):

  • WotCoapClient: IProtocolClient implementation with RFC 7252 compliant message encoding/decoding
  • CoapClientFactory: Factory for creating CoAP client instances
  • CoapClientConfig: Configuration for timeout, retransmit, ACK parameters

CoAP protocol support:

  • GET, POST, PUT, DELETE over UDP
  • Option delta calculation with 1-byte extended encoding (delta/length 13-268)
  • Uri-Path and Content-Format options
  • IDisposable pattern with proper cancellation token handling
  • Block2 (RFC 7959) support for blockwise transfer of large responses
  • Meaningful error messages with RFC 7252 response code descriptions

Block2 implementation:

  • Automatically detects when server uses blockwise transfer
  • Requests subsequent blocks until complete payload is received
  • Assembles blocks transparently into complete response
  • Supports block sizes from 16 to 1024 bytes
  • Enables fetching large Thing Descriptions that exceed basic CoAP message size limits

Enhanced error handling:

  • Maps CoAP response codes to human-readable descriptions (e.g., 4.00 → "Bad Request - Invalid request syntax or parameters")
  • Extracts and includes server error details from response payload when available
  • Provides actionable guidance for common error scenarios
  • Example: CoAP request failed with code 5.00 (Internal Server Error - Server encountered an error (check request parameters and payload)) - Invalid value according to DataSchema

Not implemented (throws NotImplementedException):

  • CoAP Observe for event subscriptions
  • Block1 (blockwise transfer for requests)
  • DTLS/CoAPS security
  • 2-byte extended option encoding (>= 269)

Usage

Consumer consumer = new();
consumer.AddClientFactory(new CoapClientFactory(new CoapClientConfig()));
consumer.Start();

// Fetches Thing Description via CoAP (supports large TDs with Block2)
ThingDescription td = await consumer.RequestThingDescription("coap://example.com:5683/thing");
ConsumedThing thing = (ConsumedThing)consumer.Consume(td);

// Read/write properties and invoke actions via CoAP
// Now with meaningful error messages when operations fail
try {
    int temp = await (await thing.ReadProperty<int>("temperature")).Value();
    await thing.WriteProperty("setpoint", 22);
    await thing.InvokeAction("toggle");
} catch (Exception ex) {
    // Error messages now include code descriptions and server error details
    Console.WriteLine(ex.Message);
}

Notes

  • Follows same architecture pattern as existing HTTP binding
  • .NET Standard 2.0 compatible
  • No breaking changes to existing code
  • Block2 support fixes JSON parsing errors for large Thing Descriptions
  • Enhanced error messages provide actionable debugging information
Original prompt

Backend WoT Scripting API Implementation (https://www.w3.org/TR/wot-scripting-api/), similar to node-wot (@eclipse-thingweb/node-wot), but for .Net.
Should work on the basis of: @tum-esi/WoT.Net but work for other protocols and also as server.

First allow the current implementation to also work with CoAP.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits October 28, 2025 12:35
Co-authored-by: BinkertR <65028874+BinkertR@users.noreply.github.com>
Co-authored-by: BinkertR <65028874+BinkertR@users.noreply.github.com>
Co-authored-by: BinkertR <65028874+BinkertR@users.noreply.github.com>
Copilot AI changed the title [WIP] Add CoAP support to WoT Scripting API implementation Add CoAP protocol binding for WoT consumer Oct 28, 2025
Copilot AI requested a review from BinkertR October 28, 2025 12:48
@BinkertR BinkertR marked this pull request as ready for review October 28, 2025 14:13
@BinkertR BinkertR marked this pull request as draft October 28, 2025 15:11
@BinkertR
Copy link

@copilot:

Implement Block2 (recommended for robustness)
fix this error when fetching a TD

Info: Fetched TD from coap://localhost:5683/ac successfully
Info: Parsing TD
Unhandled exception. Newtonsoft.Json.JsonReaderException: Unterminated string. Expected delimiter: ". Path 'properties.temperature.forms[4].op[0]', line 1, position 1024.
at WoT.Core.Implementation.Codecs.JsonCodec.BytesToValue[T](Byte[] bytes, IDataSchema schema, Dictionary`2 parameters) in C:\Git\unity\WoT.Net\WoT\Core\Implementation\Codecs\JsonCodec.cs:line 41
at WoT.Core.Definitions.ContentSerdes.ContentToValue[T](ReadContent content, IDataSchema schema) in C:\Git\unity\WoT.Net\WoT\Core\Definitions\ContentSerdes.cs:line 177
at WoT.Core.Implementation.Consumer.RequestThingDescription(Uri uri) in C:\Git\unity\WoT.Net\WoT\Core\Implementation\Consumer.cs:line 64
at Program.

$(String[] args) in C:\Git\unity\WoT.Net\Tester\Tester.cs:line 22
at Program.(String[] args)

Co-authored-by: BinkertR <65028874+BinkertR@users.noreply.github.com>
Copy link
Author

Copilot AI commented Oct 28, 2025

@copilot:

Implement Block2 (recommended for robustness)
...

Implemented Block2 (RFC 7959) support in commit f4fb6ce. The CoAP client now:

  • Automatically detects when the server uses blockwise transfer
  • Requests subsequent blocks until the complete payload is received
  • Assembles all blocks transparently into the complete response

This fixes the "Unterminated string" error when fetching large Thing Descriptions. The JSON parsing error at position 1024 was caused by the payload being truncated to a single block. With Block2 support, large TDs (and any payloads exceeding ~1KB) are now fully retrieved.

Copilot AI changed the title Add CoAP protocol binding for WoT consumer Add CoAP protocol binding for WoT consumer with Block2 support Oct 28, 2025
@BinkertR
Copy link

@copilot add more meaningful error messages when something fails.

e.g. if an action requires and input, this is the current error message "coap://10.5.0.2:5683/ac/actions/incrementInvoke 'increment' failed: CoAP request failed with code 5.00" But should be sth. like "Invalid value according to DataSchema"

…scriptions

Co-authored-by: BinkertR <65028874+BinkertR@users.noreply.github.com>
Copy link
Author

Copilot AI commented Oct 28, 2025

@copilot add more meaningful error messages when something fails.

e.g. if an action requires and input, this is the current error message "coap://10.5.0.2:5683/ac/actions/incrementInvoke 'increment...

Enhanced error messages in commit 5cee6f3. CoAP failures now include:

  • Human-readable descriptions of response codes per RFC 7252
  • Server error details extracted from response payload when available
  • Actionable guidance for common issues

Example improvements:

  • 5.00 now shows: "Internal Server Error - Server encountered an error (check request parameters and payload)"
  • 4.00 now shows: "Bad Request - Invalid request syntax or parameters"
  • If server includes error text in payload (like "Invalid value according to DataSchema"), it's appended to the message

The error for missing action input will now display as:
CoAP request failed with code 5.00 (Internal Server Error - Server encountered an error (check request parameters and payload)) - Invalid value according to DataSchema

Copilot AI changed the title Add CoAP protocol binding for WoT consumer with Block2 support Add CoAP protocol binding for WoT consumer with Block2 support and enhanced error messages Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants