14 #ifndef COAP_SESSION_H_
15 #define COAP_SESSION_H_
34 #define COAP_DEFAULT_SESSION_TIMEOUT 300
35 #define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
36 #define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
38 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
39 #define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
45 #define COAP_SESSION_TYPE_CLIENT 1
46 #define COAP_SESSION_TYPE_SERVER 2
47 #define COAP_SESSION_TYPE_HELLO 3
53 #define COAP_SESSION_STATE_NONE 0
54 #define COAP_SESSION_STATE_CONNECTING 1
55 #define COAP_SESSION_STATE_HANDSHAKE 2
56 #define COAP_SESSION_STATE_CSM 3
57 #define COAP_SESSION_STATE_ESTABLISHED 4
201 const char *identity,
254 const uint8_t *data,
size_t datalen);
270 const uint8_t *data,
size_t datalen);
375 #define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
382 #define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
388 #define COAP_DEFAULT_MAX_RETRANSMIT 4
395 #define COAP_DEFAULT_NSTART 1
480 #define SESSIONS_ADD(e, obj) \
481 HASH_ADD(hh, (e), addr_info, sizeof((obj)->addr_info), (obj))
483 #define SESSIONS_DELETE(e, obj) \
484 HASH_DELETE(hh, (e), (obj))
486 #define SESSIONS_ITER(e, el, rtmp) \
487 HASH_ITER(hh, (e), el, rtmp)
489 #define SESSIONS_ITER_SAFE(e, el, rtmp) \
490 for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
492 #define SESSIONS_FIND(e, k, res) { \
493 HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
COAP forward definitions.
void coap_session_set_max_retransmit(coap_session_t *session, unsigned int value)
Set the CoAP maximum retransmit count before failure.
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, struct coap_queue_t *node)
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...
void coap_free_endpoint(coap_endpoint_t *ep)
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
coap_session_t * coap_session_get_by_peer(struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex)
uint8_t coap_session_state_t
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const struct coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
coap_session_t * coap_new_client_session_psk(struct 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.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
unsigned int coap_session_get_max_transmit(coap_session_t *session)
Get the CoAP maximum retransmit before failure.
uint8_t coap_session_type_t
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
coap_endpoint_t * coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
coap_session_t * coap_new_client_session_pki(struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, struct coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
void coap_session_set_app_data(coap_session_t *session, void *data)
Stores data with the given session.
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
coap_fixed_point_t coap_session_get_ack_timeout(coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
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.
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
coap_session_t * coap_new_server_session(struct coap_context_t *ctx, struct coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
void coap_session_free(coap_session_t *session)
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
void coap_session_mfree(coap_session_t *session)
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
coap_session_t * coap_new_client_session(struct 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.
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
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.
struct coap_session_t coap_session_t
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session)
Get the CoAP ack randomize factor.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
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.
Pre-defined constants that reflect defaults for CoAP.
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
multi-purpose address abstraction
The CoAP stack's global state is stored in a coap_context_t object.
The structure used for defining the PKI setup data to be used.
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.
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
uint16_t integer_part
Integer part of fixed point variable.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int dtls_timeout_count
dtls setup retry counter
coap_socket_t sock
socket object for the session, if any
unsigned int max_retransmit
maximum re-transmit count (default 4)
coap_pdu_t * partial_pdu
incomplete incoming pdu
struct coap_endpoint_t * endpoint
session's endpoint
unsigned tls_overhead
overhead of TLS layer
coap_session_state_t state
current state of relationaship with peer
uint8_t read_header[8]
storage space for header of incoming message header
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
unsigned ref
reference count from queues
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
struct coap_context_t * context
session's context
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this sesison.
void * tls
security parameters
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t con_active
Active CON request sent.
void * app
application-specific data
coap_address_t local_if
optional local interface address
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
unsigned mtu
path or CSM mtu
coap_session_type_t type
client or server side socket
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
int ifindex
interface index