简体   繁体   中英

How to add extra options to CoAP request?

I know that CoAP defines some options which can be included in the sending request and each option has their own number. The structure of the CoAP uri request looks like:

coap-URI = "coap:" "//" host [ ":" port ] path-abempty [ "?" query ]

where inside they include some options like: Uri-Host, Uri-Port, Uri-Path, and Uri-Query, and each of them has their own number, ex: 3 for Uri-Host, 11 for Uri Path.... . And I would like to add some more extra options to this CoAP request, for example some options number 256, 257...How can I do that?

Thank you in advanced

Son

I've managed to pass the Option Number 256.

CoapClient client = new CoapClient(...);
Request request = new Request(CoAP.Code.GET, CoAP.Type.NON);
OptionSet optionSet = new OptionSet();
optionSet.addOption(new Option(256, "admin:admin"));
request.setOptions(optionSet);
client.advanced(request); // or async version
client.shutdown();

At resource:

@Override
public void handleGET(CoapExchange exchange) {
    OptionSet optionSet = exchange.advanced().getRequest().getOptions();
    List<Option> options = optionSet.asSortedList();
    options.stream()
            .filter(o -> o.getNumber() == 256)
            .findFirst()
            .ifPresent(o -> System.err.println(o.getNumber() + " " + o.getStringValue()));
}

Output:

256 admin:admin

However, Option Number 256 may not be a proper choice in general:

RFC 7252 The Constrained Application Protocol (CoAP). 12.2. CoAP Option Numbers Registry

The IANA policy for future additions to this sub-registry is split into three tiers as follows. The range of 0..255 is reserved for options defined by the IETF (IETF Review or IESG Approval). The range of 256..2047 is reserved for commonly used options with public specifications (Specification Required). The range of 2048..64999 is for all other options including private or vendor-specific ones, which undergo a Designated Expert review to help ensure that the option semantics are defined correctly. The option numbers between 65000 and 65535 inclusive are reserved for experiments. They are not meant for vendor-specific use of any kind and MUST NOT be used in operational deployments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM