MCPcopy Index your code
hub / github.com/PelionIoT/java-coap

github.com/PelionIoT/java-coap @5.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 5.0.0 ↗ · + Follow
2,074 symbols 11,381 edges 223 files 336 documented · 16%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

mbed CoAP

CircleCI License codecov

Introduction

This library makes it easy to integrate a Java SE enabled device with coap based services like mbed Cloud. It can also help to emulate an embedded device for prototyping and testing purposes.

The following features are supported by the library:

  • Complete CoAP support
    • The Constrained Application Protocol RFC 7252
    • Observing Resources in the Constrained Application Protocol RFC 7641
    • Block-Wise Transfers in the Constrained Application Protocol RFC 7959
  • CoRE Link Format processing API
    • Constrained RESTful Environments (CoRE) Link Format RFC 6690
  • CoAP server mode
  • CoAP client mode
  • Coap over tcp, tls RFC 8323
    • excluding: websockets, observations with BERT blocks
  • Network transports:
    • plain text UDP
    • TLS
  • LwM2M TLV and JSON data formats

Requirements

Runtime:

  • JRE v1.8

Development:

  • JDK v1.8
  • maven 3.x

Using the Library

Add repository to build file:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add dependency into your pom.xml:

<dependency>
    <groupId>com.mbed.java-coap</groupId>
    <artifactId>coap-core</artifactId>
    <version>{VERSION}</version>
</dependency>

Creating a Server

Initializing, starting and stopping the server

To initialize a server, you must at minimum define the port number. You must set the server parameters before starting a server.

CoapServer server = CoapServer.builder().transport(5683).build();
server.start();

To stop a server, use the stop() method.

server.stop();

Adding request handlers

You can add handlers before or while the server is running. There can be several URI paths assigned to the same handler. You can also remove a handler at any time.

CoapHandler handler = new ReadOnlyCoapResource("24");
server.addRequestHandler("/temperature", handler);

server.removeRequestHandler(handler);

Creating CoAP resources

To create a CoAP resource, you must implement a CoapHandler. There is one abstract helper class CoapResource that can be extended. At minimum, implement the get() method.

The following example overrides get() and put() and creates a simple CoAP resource:

public class SimpleCoapResource extends CoapResource {
    private String body="Hello World";

    @Override
    public void get(CoapExchange ex) throws CoapCodeException {
        ex.setResponseBody("Hello World");
        ex.setResponseCode(Code.C205_CONTENT);
        ex.sendResponse();
    }

    @Override
    public void put(CoapExchange ex) throws CoapCodeException {
      body = ex.getRequestBodyString();        
        ex.setResponseCode(Code.C204_CHANGED);
        ex.sendResponse();
    }
}

Creating a client

To make a CoAP request, use the class CoapClient. It uses fluent API. The following is a simple example on the usage:

CoapClient client = CoapClientBuilder.newBuilder(new InetSocketAddress("localhost",5683)).build();

CoapPacket coapResp = client.resource("/s/temp").sync().get();

coapResp = client.resource("/a/relay").payload("1", MediaTypes.CT_TEXT_PLAIN).sync().put();

//it is important to close connection in order to release socket
client.close();

Example client

This example client demonstrates how to build coap client.

Development

Build

mvn clean install

Build with all checks enabled

mvn clean install -P ci

Update license header

mvn com.mycila:license-maven-plugin:format

Contributions

All contributions are Apache 2.0. Only submit contributions where you have authored all of the code. If you do this on work time make sure your employer is OK with this.

License

Unless specifically indicated otherwise in a file, files are licensed under the Apache 2.0 license, as can be found in: LICENSE

Extension points exported contracts — how you extend this code

TransmissionTimeout (Interface)
@author szymon [6 implementers]
coap-core/src/main/java/com/mbed/coap/transmission/TransmissionTimeout.java
RequestCallback (Interface)
Created by szymon [5 implementers]
coap-core/src/main/java/com/mbed/coap/utils/RequestCallback.java
ObservationHandler (Interface)
@author szymon [5 implementers]
coap-core/src/main/java/com/mbed/coap/server/ObservationHandler.java
CoapHandler (Interface)
Interface for handling CoAP requests. @author szymon [4 implementers]
coap-core/src/main/java/com/mbed/coap/server/CoapHandler.java
CoapRequestHandler (Interface)
Created by olesmi01 on 15.08.2017. CoAP requests handler interface which is called by CoAP protocol servers impl (messag [4 …
coap-core/src/main/java/com/mbed/coap/server/internal/CoapRequestHandler.java

Core symbols most depended-on inside this repo

get
called by 456
coap-core/src/main/java/com/mbed/coap/transport/TransportContext.java
headers
called by 300
coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java
put
called by 176
coap-core/src/main/java/com/mbed/coap/server/CoapTcpCSMStorage.java
resource
called by 133
coap-core/src/main/java/com/mbed/coap/client/CoapClient.java
getPayloadString
called by 107
coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java
transport
called by 94
coap-core/src/main/java/com/mbed/coap/client/CoapClientBuilder.java
from
called by 92
lwm2m/src/main/java/com/mbed/lwm2m/LWM2MID.java
toByteArray
called by 90
coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java

Shape

Method 1,809
Class 238
Interface 19
Enum 8

Languages

Java100%

Modules by API surface

coap-core/src/main/java/com/mbed/coap/packet/BasicHeaderOptions.java50 symbols
coap-core/src/main/java/com/mbed/coap/linkformat/LinkFormat.java48 symbols
coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java43 symbols
coap-core/src/test/java/com/mbed/coap/linkformat/LinkFormatTest.java41 symbols
coap-core/src/test/java/com/mbed/coap/server/internal/CoapUdpMessagingTest.java34 symbols
coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java33 symbols
coap-core/src/main/java/com/mbed/coap/server/CoapServer.java31 symbols
coap-core/src/test/java/protocolTests/utils/CoapPacketBuilder.java29 symbols
coap-core/src/test/java/protocolTests/ClientServerTest.java29 symbols
coap-core/src/test/java/com/mbed/coap/packet/HeaderOptionTest.java29 symbols
coap-core/src/main/java/com/mbed/coap/server/internal/CoapUdpMessaging.java29 symbols
coap-core/src/test/java/protocolTests/ClientServerWithBlocksTest.java27 symbols

For agents

$ claude mcp add java-coap \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact