Contiki-NG
Loading...
Searching...
No Matches
res-edhoc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, RISE Research Institutes of Sweden AB
3 * Copyright (c) 2020, Industrial Systems Institute (ISI), Patras, Greece
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32/**
33 * \file
34 * EDHOC plugtest resource [RFC9528] with CoAP Block-Wise Transfer [RFC7959]
35 * \author
36 * Lidia Pocero <pocero@isi.gr>, Peter A Jonsson, Rikard Höglund
37 */
38
39#include <stdio.h>
40#include <string.h>
41#include "coap-engine.h"
42#include "coap.h"
43#include "edhoc-server.h"
44
45#include "sys/log.h"
46#define LOG_MODULE "res-edhoc"
47#define LOG_LEVEL LOG_LEVEL_EDHOC
48
49/*---------------------------------------------------------------------------*/
50edhoc_server_t servidor;
51
52static uint8_t msg_rx[EDHOC_MAX_PAYLOAD_LEN];
53static size_t msg_rx_len;
54
55static void res_edhoc_post_handler(coap_message_t *request,
56 coap_message_t *response,
57 uint8_t *buffer,
58 uint16_t preferred_size,
59 int32_t *offset);
60
61RESOURCE(res_edhoc, "title=\"EDHOC resource\"", NULL,
62 res_edhoc_post_handler, NULL, NULL);
63
64/*---------------------------------------------------------------------------*/
65/* Example allows only one request on time. There are no checks for multiple access !!! */
66static void
67res_edhoc_post_handler(coap_message_t *request,
68 coap_message_t *response,
69 uint8_t *buffer,
70 uint16_t preferred_size,
71 int32_t *offset)
72{
73 int block_size = 300; /* FIXME: Make configurable */
74
75 if(*offset == 0) {
76 if(coap_block1_handler(request, response, msg_rx, &msg_rx_len,
78 LOG_DBG("handler (%d)\n", (int)msg_rx_len);
79 LOG_DBG_BYTES(msg_rx, msg_rx_len);
80 LOG_DBG_("\n");
81 return;
82 } else {
83 LOG_DBG("RX msg (%d)\n", (int)msg_rx_len);
84 LOG_DBG_BYTES(msg_rx, msg_rx_len);
85 LOG_DBG_("\n");
86 edhoc_server_process(request, response, &servidor, msg_rx, msg_rx_len);
87 }
88 response->payload = (uint8_t *)edhoc_ctx->buffers.msg_tx;
89 response->payload_len = edhoc_ctx->buffers.tx_sz;
90 coap_set_header_block1(response, request->block1_num, 0,
91 request->block1_size);
92
93 if(response->payload_len > block_size) {
94 coap_set_option(response, COAP_OPTION_BLOCK2);
95 coap_set_header_block2(response, 0, 1, block_size);
96 }
97 } else {
98 coap_set_status_code(response, CHANGED_2_04);
99 memcpy(buffer, edhoc_ctx->buffers.msg_tx + *offset, block_size);
100 if(edhoc_ctx->buffers.tx_sz - *offset < preferred_size) {
101 preferred_size = edhoc_ctx->buffers.tx_sz - *offset;
102 *offset = -1;
103 } else {
104 *offset += preferred_size;
105 }
106 coap_set_payload(response, buffer, preferred_size);
107 }
108}
109/*---------------------------------------------------------------------------*/
CoAP engine implementation.
An implementation of the Constrained Application Protocol (RFC 7252).
EDHOC server API [RFC9528] with CoAP Block-Wise Transfer [RFC7959].
edhoc_context_t * edhoc_ctx
EDHOC context struct used in the EDHOC protocol.
Definition edhoc.c:62
int coap_block1_handler(coap_message_t *request, coap_message_t *response, uint8_t *target, size_t *len, size_t max_len)
Block 1 support within a CoAP resource.
Definition coap-block1.c:84
void edhoc_server_process(coap_message_t *req, coap_message_t *res, edhoc_server_t *ser, uint8_t *msg, size_t len)
Run the EDHOC Responder role process.
#define EDHOC_MAX_PAYLOAD_LEN
The max length of the EDHOC message.
struct edhoc_server edhoc_server_t
EDHOC Server Struct.
coap_resource_t res_edhoc
CoAP resource.
Header file for the logging system.