Skip to main content

CoAP Driver

The CoAP driver for PlantPulse Edge is a Java implementation wrapping the Eclipse Californium 3.13.0 library. It accesses resource URIs of low-power IoT devices (RFC 7252) via GET / PUT.

Source: plantpulse.driver.protocol.coap.*

ClassResponsibility
CoAPDriverProtocolDriver implementation — connect/read(GET)/write(PUT)/close, per-resource CoapClient cache

Library:

  • lib/californium-core-3.13.0.jar
  • lib/element-connector-3.13.0.jar

Operation Flow​

[connect] CoapClient(baseUri) 생성 → setConnected(true)
↓
[read] resource path → resourceUri = baseUri + "/" + path
→ resourceClients.computeIfAbsent(uri, CoapClient::new)
→ CoapClient.get() (synchronous)
→ resp.isSuccess() ? resp.getResponseText() : ""
↓
[write] resourceClients.computeIfAbsent(uri, CoapClient::new)
→ CoapClient.put(value, MediaTypeRegistry.TEXT_PLAIN /* 0 */)
→ resp.isSuccess()
↓
[close] 모든 CoapClient.shutdown() + resourceClients.clear()

CoAP is UDP-based, making it connectionless — merely creating the client object counts as "connected." Actual device availability is confirmed at the time of the first read.


Options (BasicDriverSource.options)​

This driver has no options. The values used for the connection are just host and port.

Putting dtls · psk-identity · psk-key has no effect

The driver always forms the connection URI as coap://. Even if you put dtls=true in the options or set the port to 5684 (the DTLS standard port), it transmits as plain CoAP — no error is raised, making it easy to mistakenly assume the communication is encrypted.

For devices that require DTLS, place a DTLS termination gateway in front of the device and use this driver to read the gateway's plaintext side.

The request timeout also cannot be specified. The Californium default is applied as-is. For devices that respond slowly, increase the collection period (timecycle) instead of relying on a timeout.


Address (Resource Path) Format​

FormatConversion result
/sensors/temperaturecoap://host:5683/sensors/temperature
sensors/temperaturecoap://host:5683/sensors/temperature (leading / automatically corrected)
/.well-known/coreDevice resource discovery — list of registered resources (CoRE Link Format)

Per-resource CoapClient is cached to allow socket reuse. All are shutdown at close().


Response Handling​

Californium responseDriver handling
resp == nullread = "", write = false
resp.isSuccess() (2.xx)read = resp.getResponseText(), write = true
resp.getCode() error (4.xx / 5.xx)warn log + read = "", write = false

CoAP response text is returned as a String as-is regardless of the mediaType — if the device responds with JSON, the calling side (PlantPulse's JsonExtract) performs the decoding. CBOR responses are displayed as text, so a separate decoder is needed (currently not supported).


Unit Tests​

test/java/plantpulse/driver/protocol/coap/CoAPDriverTest.java — 13 methods:

  • Class loading + initial state
  • Metadata (isWriteSupported / isExternal / getDriverSource)
  • Safe unconnected read / write (no NPE)
  • safe close()
  • baseUri() — explicit / default port
  • resourceUri() — leading / correction
  • getDebugBaseUrl() — coap://host:port

Build-isolated tests with no network calls (./gradlew test green).


References​