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-2019 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 1.9.6
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 1024
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 
75 error_t coapSetPayload(CoapMessage *message, const void *payload,
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
uint8_t length
Definition: dtls_misc.h:149
#define COAP_MAX_MSG_SIZE
Definition: coap_message.h:40
uint8_t code
Definition: coap_common.h:181
int bool_t
Definition: compiler_port.h:49
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:298
error_t coapGetPayload(const CoapMessage *message, const uint8_t **payload, size_t *payloadLen)
Get CoAP message payload.
Definition: coap_message.c:324
char_t type
error_t
Error codes.
Definition: error.h:42
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:55
__start_packed struct @106 CoapMessageHeader
CoAP message format.
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:92
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
size_t length
Definition: coap_message.h:58
uint8_t message[]
Definition: chap.h:152
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
uint8_t payloadLen
Definition: ipv6.h:343
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
CoapCode
CoAP method and response codes.
Definition: coap_common.h:117
error_t coapParseMessage(const CoapMessage *message)
Parse CoAP message.
Definition: coap_message.c:51
Definitions common to CoAP client and server.