coap_client.h
Go to the documentation of this file.
1 /**
2  * @file coap_client.h
3  * @brief CoAP client
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.0
29  **/
30 
31 #ifndef _COAP_CLIENT_H
32 #define _COAP_CLIENT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "coap/coap_common.h"
37 #include "coap/coap_message.h"
38 #include "coap/coap_option.h"
39 
40 //CoAP client support
41 #ifndef COAP_CLIENT_SUPPORT
42  #define COAP_CLIENT_SUPPORT ENABLED
43 #elif (COAP_CLIENT_SUPPORT != ENABLED && COAP_CLIENT_SUPPORT != DISABLED)
44  #error COAP_CLIENT_SUPPORT parameter is not valid
45 #endif
46 
47 //DTLS-secured CoAP support
48 #ifndef COAP_CLIENT_DTLS_SUPPORT
49  #define COAP_CLIENT_DTLS_SUPPORT DISABLED
50 #elif (COAP_CLIENT_DTLS_SUPPORT != ENABLED && COAP_CLIENT_DTLS_SUPPORT != DISABLED)
51  #error COAP_CLIENT_DTLS_SUPPORT parameter is not valid
52 #endif
53 
54 //CoAP observe support
55 #ifndef COAP_CLIENT_OBSERVE_SUPPORT
56  #define COAP_CLIENT_OBSERVE_SUPPORT ENABLED
57 #elif (COAP_CLIENT_OBSERVE_SUPPORT != ENABLED && COAP_CLIENT_OBSERVE_SUPPORT != DISABLED)
58  #error COAP_CLIENT_OBSERVE_SUPPORT parameter is not valid
59 #endif
60 
61 //CoAP block-wise transfer support
62 #ifndef COAP_CLIENT_BLOCK_SUPPORT
63  #define COAP_CLIENT_BLOCK_SUPPORT ENABLED
64 #elif (COAP_CLIENT_BLOCK_SUPPORT != ENABLED && COAP_CLIENT_BLOCK_SUPPORT != DISABLED)
65  #error COAP_CLIENT_BLOCK_SUPPORT parameter is not valid
66 #endif
67 
68 //CoAP client tick interval
69 #ifndef COAP_CLIENT_TICK_INTERVAL
70  #define COAP_CLIENT_TICK_INTERVAL 100
71 #elif (COAP_CLIENT_TICK_INTERVAL < 10)
72  #error COAP_CLIENT_TICK_INTERVAL parameter is not valid
73 #endif
74 
75 //Default timeout
76 #ifndef COAP_CLIENT_DEFAULT_TIMEOUT
77  #define COAP_CLIENT_DEFAULT_TIMEOUT 20000
78 #elif (COAP_CLIENT_DEFAULT_TIMEOUT < 0)
79  #error COAP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
80 #endif
81 
82 //Maximum number of simultaneous outstanding requests
83 #ifndef COAP_CLIENT_NSTART
84  #define COAP_CLIENT_NSTART 1
85 #elif (COAP_CLIENT_NSTART < 1)
86  #error COAP_CLIENT_NSTART parameter is not valid
87 #endif
88 
89 //Maximum number of retransmissions
90 #ifndef COAP_CLIENT_MAX_RETRANSMIT
91  #define COAP_CLIENT_MAX_RETRANSMIT 4
92 #elif (COAP_CLIENT_MAX_RETRANSMIT < 1)
93  #error COAP_CLIENT_MAX_RETRANSMIT parameter is not valid
94 #endif
95 
96 //Initial retransmission timeout (minimum)
97 #ifndef COAP_CLIENT_ACK_TIMEOUT_MIN
98  #define COAP_CLIENT_ACK_TIMEOUT_MIN 2000
99 #elif (COAP_CLIENT_ACK_TIMEOUT_MIN < 1000)
100  #error COAP_CLIENT_ACK_TIMEOUT_MIN parameter is not valid
101 #endif
102 
103 //Initial retransmission timeout (maximum)
104 #ifndef COAP_CLIENT_ACK_TIMEOUT_MAX
105  #define COAP_CLIENT_ACK_TIMEOUT_MAX 3000
106 #elif (COAP_CLIENT_ACK_TIMEOUT_MAX < COAP_CLIENT_ACK_TIMEOUT_MIN)
107  #error COAP_CLIENT_ACK_TIMEOUT_MAX parameter is not valid
108 #endif
109 
110 //Random delay after Max-Age has expired (minimum)
111 #ifndef COAP_CLIENT_RAND_DELAY_MIN
112  #define COAP_CLIENT_RAND_DELAY_MIN 5000
113 #elif (COAP_CLIENT_RAND_DELAY_MIN < 1000)
114  #error COAP_CLIENT_RAND_DELAY_MIN parameter is not valid
115 #endif
116 
117 //Random delay after Max-Age has expired (maximum)
118 #ifndef COAP_CLIENT_RAND_DELAY_MAX
119  #define COAP_CLIENT_RAND_DELAY_MAX 15000
120 #elif (COAP_CLIENT_RAND_DELAY_MAX < COAP_CLIENT_RAND_DELAY_MIN)
121  #error COAP_CLIENT_RAND_DELAY_MAX parameter is not valid
122 #endif
123 
124 //Default token length
125 #ifndef COAP_CLIENT_DEFAULT_TOKEN_LEN
126  #define COAP_CLIENT_DEFAULT_TOKEN_LEN 4
127 #elif (COAP_CLIENT_DEFAULT_TOKEN_LEN < 0 || COAP_CLIENT_DEFAULT_TOKEN_LEN > 8)
128  #error COAP_CLIENT_DEFAULT_TOKEN_LEN parameter is not valid
129 #endif
130 
131 //Application specific context
132 #ifndef COAP_CLIENT_PRIVATE_CONTEXT
133  #define COAP_CLIENT_PRIVATE_CONTEXT
134 #endif
135 
136 //DTLS supported?
137 #if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
138  #include "core/crypto.h"
139  #include "tls.h"
140 #endif
141 
142 //Forward declaration of CoapClientContext structure
143 struct _CoapClientContext;
144 #define CoapClientContext struct _CoapClientContext
145 
146 //Forward declaration of CoapClientRequest structure
147 struct _CoapClientRequest;
148 #define CoapClientRequest struct _CoapClientRequest
149 
150 //Dependencies
151 #include "coap_client_request.h"
152 
153 //C++ guard
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 
159 /**
160  * @brief CoAP client states
161  */
162 
163 typedef enum
164 {
169 
170 
171 //DTLS supported?
172 #if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
173 
174 /**
175  * @brief DTLS initialization callback
176  **/
177 
179  TlsContext *dtlsContext);
180 
181 #endif
182 
183 
184 /**
185  * @brief CoAP client context
186  **/
187 
189 {
190  OsMutex mutex; ///<Mutex preventing simultaneous access to the context
191  OsEvent event; ///<Event object used to receive notifications
192  CoapClientState state; ///<CoAP client state
193  CoapTransportProtocol transportProtocol; ///<Transport protocol (UDP or DTLS)
194  NetInterface *interface; ///<Underlying network interface
195  Socket *socket; ///<Underlying UDP socket
196 #if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
197  TlsContext *dtlsContext; ///<DTLS context
198  TlsSessionState dtlsSession; ///<DTLS session state
199  CoapClientDtlsInitCallback dtlsInitCallback; ///<DTLS initialization callback
200 #endif
201  systime_t startTime; ///<Start time
202  systime_t timeout; ///<Timeout value
203  uint16_t mid; ///<Message identifier
204  size_t tokenLen; ///<Token length
205  CoapClientRequest request[COAP_CLIENT_NSTART]; ///<Outstanding CoAP requests
206  CoapMessage response; ///<CoAP response message
207  COAP_CLIENT_PRIVATE_CONTEXT ///<Application specific context
208 };
209 
210 
211 //CoAP client related functions
213 
215  CoapTransportProtocol transportProtocol);
216 
217 #if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
218 
220  CoapClientDtlsInitCallback callback);
221 
222 #endif
223 
226 
228  NetInterface *interface);
229 
231  const IpAddr *serverIpAddr, uint16_t serverPort);
232 
234 
236 
237 void coapClientDeinit(CoapClientContext *context);
238 
239 //C++ guard
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif
CoapClientState
CoAP client states.
Definition: coap_client.h:164
@ COAP_CLIENT_STATE_DISCONNECTED
Definition: coap_client.h:165
@ COAP_CLIENT_STATE_CONNECTING
Definition: coap_client.h:166
@ COAP_CLIENT_STATE_CONNECTED
Definition: coap_client.h:167
error_t coapClientTask(CoapClientContext *context, systime_t timeout)
Process CoAP client events.
Definition: coap_client.c:373
#define COAP_CLIENT_NSTART
Definition: coap_client.h:84
error_t coapClientBindToInterface(CoapClientContext *context, NetInterface *interface)
Bind the CoAP client to a particular network interface.
Definition: coap_client.c:240
#define COAP_CLIENT_PRIVATE_CONTEXT
Definition: coap_client.h:133
#define CoapClientContext
Definition: coap_client.h:144
#define CoapClientRequest
Definition: coap_client.h:148
error_t coapClientSetTimeout(CoapClientContext *context, systime_t timeout)
Set default request timeout.
Definition: coap_client.c:186
error_t coapClientDisconnect(CoapClientContext *context)
Disconnect from the CoAP server.
Definition: coap_client.c:399
error_t coapClientSetTokenLength(CoapClientContext *context, size_t length)
Set the length of the token.
Definition: coap_client.c:211
void coapClientDeinit(CoapClientContext *context)
Release CoAP client context.
Definition: coap_client.c:438
error_t(* CoapClientDtlsInitCallback)(CoapClientContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
Definition: coap_client.h:178
error_t coapClientInit(CoapClientContext *context)
Initialize CoAP client context.
Definition: coap_client.c:53
error_t coapClientSetTransportProtocol(CoapClientContext *context, CoapTransportProtocol transportProtocol)
Set the transport protocol to be used.
Definition: coap_client.c:130
error_t coapClientRegisterDtlsInitCallback(CoapClientContext *context, CoapClientDtlsInitCallback callback)
Register DTLS initialization callback function.
Definition: coap_client.c:158
error_t coapClientConnect(CoapClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish connection with the CoAP server.
Definition: coap_client.c:267
CoAP request handling.
Definitions common to CoAP client and server.
CoapTransportProtocol
CoAP transport protocols.
Definition: coap_common.h:77
CoAP message formatting and parsing.
Formatting and parsing of CoAP options.
General definitions for cryptographic algorithms.
error_t
Error codes.
Definition: error.h:43
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
#define Socket
Definition: socket.h:36
CoAP client context.
Definition: coap_client.h:189
CoapClientState state
CoAP client state.
Definition: coap_client.h:192
CoapMessage response
CoAP response message.
Definition: coap_client.h:206
CoapClientDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition: coap_client.h:199
TlsSessionState dtlsSession
DTLS session state.
Definition: coap_client.h:198
uint16_t mid
Message identifier.
Definition: coap_client.h:203
TlsContext * dtlsContext
DTLS context.
Definition: coap_client.h:197
CoapTransportProtocol transportProtocol
Transport protocol (UDP or DTLS)
Definition: coap_client.h:193
systime_t timeout
Timeout value.
Definition: coap_client.h:202
Socket * socket
Underlying UDP socket.
Definition: coap_client.h:195
OsEvent event
Event object used to receive notifications.
Definition: coap_client.h:191
size_t tokenLen
Token length.
Definition: coap_client.h:204
systime_t startTime
Start time.
Definition: coap_client.h:201
CoapClientRequest request[COAP_CLIENT_NSTART]
Outstanding CoAP requests.
Definition: coap_client.h:205
NetInterface * interface
Underlying network interface.
Definition: coap_client.h:194
OsMutex mutex
Mutex preventing simultaneous access to the context.
Definition: coap_client.h:190
CoapClientContext * context
CoAP client context.
CoAP message.
Definition: coap_message.h:56
IP network address.
Definition: ip.h:79
Event object.
Mutex object.
TLS session state.
Definition: tls.h:2021
uint8_t length
Definition: tcp.h:368
TLS (Transport Layer Security)
#define TlsContext
Definition: tls.h:36