mikroSDK Reference Manual
coap_client.h
Go to the documentation of this file.
1
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
143struct _CoapClientContext;
144#define CoapClientContext struct _CoapClientContext
145
146//Forward declaration of CoapClientRequest structure
147struct _CoapClientRequest;
148#define CoapClientRequest struct _CoapClientRequest
149
150//Dependencies
151#include "coap_client_request.h"
152
153//C++ guard
154#ifdef __cplusplus
155extern "C" {
156#endif
157
158
163typedef enum
164{
165 COAP_CLIENT_STATE_DISCONNECTED = 0,
166 COAP_CLIENT_STATE_CONNECTING = 1,
167 COAP_CLIENT_STATE_CONNECTED = 2
169
170
171//DTLS supported?
172#if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
173
178typedef error_t (*CoapClientDtlsInitCallback)(CoapClientContext *context,
179 TlsContext *dtlsContext);
180
181#endif
182
183
189{
194 NetInterface *interface;
195 Socket *socket;
196#if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
197 TlsContext *dtlsContext;
198 TlsSessionState dtlsSession;
200#endif
203 uint16_t mid;
204 size_t tokenLen;
205 CoapClientRequest request[COAP_CLIENT_NSTART];
207 COAP_CLIENT_PRIVATE_CONTEXT
208};
209
210
211//CoAP client related functions
212error_t coapClientInit(CoapClientContext *context);
213
214error_t coapClientSetTransportProtocol(CoapClientContext *context,
215 CoapTransportProtocol transportProtocol);
216
217#if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
218
219error_t coapClientRegisterDtlsInitCallback(CoapClientContext *context,
221
222#endif
223
224error_t coapClientSetTimeout(CoapClientContext *context, systime_t timeout);
225error_t coapClientSetTokenLength(CoapClientContext *context, size_t length);
226
227error_t coapClientBindToInterface(CoapClientContext *context,
228 NetInterface *interface);
229
230error_t coapClientConnect(CoapClientContext *context,
231 const IpAddr *serverIpAddr, uint16_t serverPort);
232
233error_t coapClientTask(CoapClientContext *context, systime_t timeout);
234
235error_t coapClientDisconnect(CoapClientContext *context);
236
237void 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
error_t(* CoapClientDtlsInitCallback)(CoapClientContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
Definition coap_client.h:178
CoAP request handling.
Definitions common to CoAP client and server.
CoapTransportProtocol
CoAP transport protocols.
Definition coap_common.h:77
CoAP message formatting and parsing.
CoAP option formatting and parsing.
error_t
Error codes.
Definition error.h:43
TCP/IP stack core.
uint_t OsMutex
Mutex object.
Definition os_port_none.h:118
uint_t OsEvent
Event object.
Definition os_port_none.h:104
uint32_t systime_t
System time.
Definition os_port_none.h:90
CoAP message.
Definition coap_message.h:56
IP network address.
Definition ip.h:72
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
OsEvent event
Event object used to receive notifications.
Definition coap_client.h:191
TlsContext * dtlsContext
DTLS context.
Definition coap_client.h:197
uint16_t mid
Message identifier.
Definition coap_client.h:203
Socket * socket
Underlying UDP socket.
Definition coap_client.h:195
size_t tokenLen
Token length.
Definition coap_client.h:204
TlsSessionState dtlsSession
DTLS session state.
Definition coap_client.h:198
OsMutex mutex
Mutex preventing simultaneous access to the context.
Definition coap_client.h:190
systime_t timeout
Timeout value.
Definition coap_client.h:202
CoapTransportProtocol transportProtocol
Transport protocol (UDP or DTLS)
Definition coap_client.h:193
systime_t startTime
Start time.
Definition coap_client.h:201
CoapClientDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition coap_client.h:199
NetInterface * interface
Underlying network interface.
Definition coap_client.h:194
CoapClientRequest request[COAP_CLIENT_NSTART]
Outstanding CoAP requests.
Definition coap_client.h:205
CoAP request.
Definition coap_client_request.h:91
CoapClientContext * context
CoAP client context.
Definition coap_client_request.h:93