coap_message.h
Go to the documentation of this file.
1 /**
2  * @file coap_message.h
3  * @brief CoAP message formatting and parsing
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _COAP_MESSAGE_H
32 #define _COAP_MESSAGE_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "coap/coap_common.h"
37 
38 //Maximum size of CoAP messages
39 #ifndef COAP_MAX_MSG_SIZE
40  #define COAP_MAX_MSG_SIZE 1152
41 #elif (COAP_MAX_MSG_SIZE < 16)
42  #error COAP_MAX_MSG_SIZE parameter is not valid
43 #endif
44 
45 //C++ guard
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 
51 /**
52  * @brief CoAP message
53  **/
54 
55 typedef struct
56 {
57  uint8_t buffer[COAP_MAX_MSG_SIZE];
58  size_t length;
59  size_t pos;
60 } CoapMessage;
61 
62 
63 //CoAP related functions
65 
66 error_t coapParseMessageHeader(const uint8_t *p, size_t length,
67  size_t *consumed);
68 
71 
74 
76  size_t payloadLen);
77 
78 error_t coapGetPayload(const CoapMessage *message, const uint8_t **payload,
79  size_t *payloadLen);
80 
82  size_t length);
83 
84 error_t coapReadPayload(CoapMessage *message, void *data, size_t size,
85  size_t *length);
86 
88  const CoapMessageHeader *header2);
89 
90 //C++ guard
91 #ifdef __cplusplus
92 }
93 #endif
94 
95 #endif
#define COAP_MAX_MSG_SIZE
Definition: coap_message.h:40
uint8_t code
Definition: coap_common.h:179
int bool_t
Definition: compiler_port.h:53
error_t coapParseMessageHeader(const uint8_t *p, size_t length, size_t *consumed)
Parse CoAP message header.
Definition: coap_message.c:112
error_t coapSetCode(CoapMessage *message, CoapCode code)
Set method or response code.
Definition: coap_message.c:201
uint8_t p
Definition: ndp.h:300
uint8_t message[]
Definition: chap.h:154
uint8_t data[]
Definition: ethernet.h:222
uint8_t type
Definition: coap_common.h:176
error_t coapGetPayload(const CoapMessage *message, const uint8_t **payload, size_t *payloadLen)
Get CoAP message payload.
Definition: coap_message.c:324
error_t
Error codes.
Definition: error.h:43
size_t pos
Definition: coap_message.h:59
error_t coapGetType(const CoapMessage *message, CoapMessageType *type)
Get message type.
Definition: coap_message.c:176
CoAP message.
Definition: coap_message.h:56
uint8_t length
Definition: tcp.h:368
error_t coapWritePayload(CoapMessage *message, const void *data, size_t length)
Write payload data.
Definition: coap_message.c:386
CoapMessageType
CoAP message types.
Definition: coap_common.h:88
bool_t coapCompareToken(const CoapMessageHeader *header1, const CoapMessageHeader *header2)
Token comparison.
Definition: coap_message.c:547
error_t coapSetType(CoapMessage *message, CoapMessageType type)
Set message type.
Definition: coap_message.c:151
uint8_t payload[]
Definition: ipv6.h:286
size_t length
Definition: coap_message.h:58
error_t coapGetCode(const CoapMessage *message, CoapCode *code)
Get method or response code.
Definition: coap_message.c:226
error_t coapReadPayload(CoapMessage *message, void *data, size_t size, size_t *length)
Read payload data.
Definition: coap_message.c:468
error_t coapSetPayload(CoapMessage *message, const void *payload, size_t payloadLen)
Set CoAP message payload.
Definition: coap_message.c:252
uint16_t payloadLen
Definition: ipv6.h:281
TCP/IP stack core.
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
error_t coapParseMessage(const CoapMessage *message)
Parse CoAP message.
Definition: coap_message.c:51
Definitions common to CoAP client and server.
CoapMessageHeader
Definition: coap_common.h:182