From ce49ccf0e5afe0349c6b43969459e933fceb0160 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 22 Jun 2024 09:51:10 +0100 Subject: [PATCH] server.cc: Add in alternative resource --- .github/workflows/main.yml | 2 +- README.md | 9 +++++---- server.cc | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dc11395..3602c27 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,7 +21,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - LIBVER: ["v4.3.4", "develop"] + LIBVER: ["develop", "v4.3.4", "v4.3.3", "v4.3.2"] steps: - uses: actions/checkout@v4 - name: setup diff --git a/README.md b/README.md index cab9472..d541996 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ Minimal CoAP Client and Server Examples for libcoap =================================================== -These examples require [libcoap-3](https://github.com/obgm/libcoap) to -work. +These examples require [libcoap-3](https://github.com/obgm/libcoap) with a +minimum version of 4.3.2 to compile and execute. -`client.cc` shows a minimal libcoap client that sends a confirmable +`client.cc` is a minimal libcoap client that sends a confirmable UDP `GET /hello` request to a pre-defined CoAP server ([coap.me](http://coap.me) in this example) and outputs a received response. It supports an optional argument which can be a different @@ -13,7 +13,8 @@ then this is handled by a NON-confirmable request. `server.cc` is a minimal CoAP server that binds on `::5683`, as well as the multicast IPs 224.0.1.187 and ff02::fd. -It has a single resource `/hello`. +It has two defined resources `/hello` and `hello/my` along with the +inbuilt resource `.well-known/core`. Copyright (C) 2018-2024 Olaf Bergmann diff --git a/server.cc b/server.cc index f8659b9..2c97dc9 100644 --- a/server.cc +++ b/server.cc @@ -94,6 +94,20 @@ main(void) { }); coap_add_resource(ctx, resource); + /* Create another resource that the server can respond to with information */ + resource = coap_resource_init(coap_make_str_const("hello/my"), 0); + coap_register_handler(resource, COAP_REQUEST_GET, + [](auto, auto, + const coap_pdu_t *request, + auto, coap_pdu_t *response) { + coap_show_pdu(COAP_LOG_WARN, request); + coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); + coap_add_data(response, 8, + (const uint8_t *)"my world"); + coap_show_pdu(COAP_LOG_WARN, response); + }); + coap_add_resource(ctx, resource); + /* Handle any libcoap I/O requirements */ while (true) { coap_io_process(ctx, COAP_IO_WAIT);