libcoap  4.3.0rc3
coap_session.h
Go to the documentation of this file.
1 /* coap_session.h -- Session management for libcoap
2 *
3 * Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
4 *
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
7 */
8 
14 #ifndef COAP_SESSION_H_
15 #define COAP_SESSION_H_
16 
27 typedef struct coap_fixed_point_t {
28  uint16_t integer_part;
29  uint16_t fractional_part;
32 
33 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
34 #define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
35 
39 typedef enum coap_session_type_t {
46 
50 typedef enum coap_session_state_t {
57 
65 
74 
83  coap_nack_reason_t reason);
84 
92 void coap_session_set_app_data(coap_session_t *session, void *data);
93 
103 void *coap_session_get_app_data(const coap_session_t *session);
104 
113  const coap_session_t *session);
114 
123  const coap_session_t *session);
124 
133 
142 
151 
159 int coap_session_get_ifindex(const coap_session_t *session);
160 
174 void *coap_session_get_tls(const coap_session_t *session,
175  coap_tls_library_t *tls_lib);
176 
185 
198 
206 void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
207 
214 size_t coap_session_max_pdu_size(const coap_session_t *session);
215 
226  coap_context_t *ctx,
227  const coap_address_t *local_if,
228  const coap_address_t *server,
229  coap_proto_t proto
230 );
231 
245  coap_context_t *ctx,
246  const coap_address_t *local_if,
247  const coap_address_t *server,
248  coap_proto_t proto,
249  const char *identity,
250  const uint8_t *key,
251  unsigned key_len
252 );
253 
270  coap_context_t *ctx,
271  const coap_address_t *local_if,
272  const coap_address_t *server,
273  coap_proto_t proto,
274  coap_dtls_cpsk_t *setup_data
275 );
276 
285  const coap_session_t *session);
286 
295  const coap_session_t *session);
296 
313  coap_context_t *ctx,
314  const coap_address_t *local_if,
315  const coap_address_t *server,
316  coap_proto_t proto,
317  coap_dtls_pki_t *setup_data
318 );
319 
328 void coap_session_init_token(coap_session_t *session, size_t length,
329  const uint8_t *token);
330 
339 void coap_session_new_token(coap_session_t *session, size_t *length,
340  uint8_t *token);
341 
349 const char *coap_session_str(const coap_session_t *session);
350 
359 coap_endpoint_t *coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
360 
368 void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu);
369 
371 
381 const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
382 
384  const coap_address_t *remote_addr, int ifindex);
385 
401 #define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
402 
410 #define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
411 
418 #define COAP_DEFAULT_MAX_RETRANSMIT 4
419 
425 #define COAP_DEFAULT_NSTART 1
426 
432 #ifndef COAP_DEFAULT_LEISURE
433 #define COAP_DEFAULT_LEISURE (5U)
434 #endif /* COAP_DEFAULT_LEISURE */
435 
442 #define COAP_MAX_TRANSMIT_SPAN(s) \
443  ((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
444  ((1 << (s->max_retransmit)) -1) * \
445  (s->ack_random_factor.integer_part * 1000 + \
446  s->ack_random_factor.fractional_part) \
447  / 1000000)
448 
455 #define COAP_MAX_TRANSMIT_WAIT(s) \
456  ((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
457  ((1 << (s->max_retransmit + 1)) -1) * \
458  (s->ack_random_factor.integer_part * 1000 + \
459  s->ack_random_factor.fractional_part) \
460  / 1000000)
461 
466 #define COAP_MAX_LATENCY 100
467 
474 #define COAP_PROCESSING_DELAY(s) \
475  ((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part + 500) \
476  / 1000)
477 
484 #define COAP_MAX_RTT(s) \
485  ((2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
486 
493 #define COAP_EXCHANGE_LIFETIME(s) \
494  (COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
495 
502 #define COAP_NON_LIFETIME(s) \
503  (COAP_MAX_TRANSMIT_SPAN(s) + COAP_MAX_LATENCY)
504 
517  unsigned int value);
518 
530  coap_fixed_point_t value);
531 
543  coap_fixed_point_t value);
544 
554 unsigned int coap_session_get_max_retransmit(const coap_session_t *session);
555 
567 
579  const coap_session_t *session);
580 
588 
589 #endif /* COAP_SESSION_H */
coap_nack_reason_t
Definition: coap_io.h:62
void coap_session_set_max_retransmit(coap_session_t *session, unsigned int value)
Set the CoAP maximum retransmit count before failure.
Definition: coap_session.c:23
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:379
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
Definition: coap_session.c:42
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:63
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
void coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:32
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:58
unsigned int coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:53
coap_tls_library_t
Definition: coap_dtls.h:50
const char * coap_session_str(const coap_session_t *session)
Get session description.
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:229
coap_proto_t
CoAP protocol types.
Definition: pdu.h:278
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent)
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:39
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
Definition: coap_session.c:102
void coap_free_endpoint(coap_endpoint_t *ep)
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address from the session.
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:263
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:242
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
coap_session_state_t
coap_session_state_t values
Definition: coap_session.h:50
coap_session_t * coap_new_client_session_psk(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
Definition: coap_session.c:911
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
coap_session_t * coap_new_client_session(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
Definition: coap_session.c:896
void coap_session_init_token(coap_session_t *session, size_t length, const uint8_t *token)
Initializes the token value to use as a starting point.
void coap_session_new_token(coap_session_t *session, size_t *length, uint8_t *token)
Creates a new token for use.
void coap_session_set_app_data(coap_session_t *session, void *data)
Stores data with the given session.
Definition: coap_session.c:96
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address from the session.
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
coap_session_t * coap_new_client_session_psk2(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
Definition: coap_session.c:938
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:454
struct coap_fixed_point_t coap_fixed_point_t
Abstraction of a fixed point number that can be used where necessary instead of a float.
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
@ COAP_SESSION_TYPE_NONE
Not defined.
Definition: coap_session.h:40
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
Definition: coap_session.h:43
@ COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:42
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:41
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:53
@ COAP_SESSION_STATE_CSM
Definition: coap_session.h:54
@ COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:55
@ COAP_SESSION_STATE_NONE
Definition: coap_session.h:51
@ COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:52
multi-purpose address abstraction
Definition: address.h:94
CoAP binary data definition with const data.
Definition: str.h:56
The CoAP stack's global state is stored in a coap_context_t object.
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:336
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:242
Abstraction of virtual endpoint that can be attached to coap_context_t.
Abstraction of a fixed point number that can be used where necessary instead of a float.
Definition: coap_session.h:27
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:29
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:28
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...