diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 5d179777..f1b49aa7 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -18,6 +18,16 @@ struct coap_endpoint_t; struct coap_contex_t; struct coap_queue_t; +/** +* Abstraction of a fixed point number that can be used where necessary instead +* of a float. 1,000 fractional bits equals one integer +*/ +typedef struct coap_fixed_point_t { + uint16_t integer_part; /**< Integer part of fixed point variable */ + uint16_t fractional_part; /**< Fractional part of fixed point variable + 1/1000 (3 points) precision */ +} coap_fixed_point_t; + #define COAP_DEFAULT_SESSION_TIMEOUT 300 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS) @@ -69,6 +79,9 @@ typedef struct coap_session_t { uint8_t *psk_key; size_t psk_key_len; void *app; /**< application-specific data */ + unsigned int max_retransmit; /**< maximum re-transmit count (default 4) */ + coap_fixed_point_t ack_timeout; /**< timeout waiting for ack (default 2 secs) */ + coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default 1.5) */ } coap_session_t; /** @@ -342,5 +355,114 @@ coap_session_t *coap_session_get_by_peer(struct coap_context_t *ctx, void coap_session_free(coap_session_t *session); + /** + * @defgroup cc Rate Control + * The transmission parameters for CoAP rate control ("Congestion + * Control" in stream-oriented protocols) are defined in + * https://tools.ietf.org/html/rfc7252#section-4.8 + * @{ + */ + + /** + * Number of seconds when to expect an ACK or a response to an + * outstanding CON message. + * RFC 7252, Section 4.8 Default value of ACK_TIMEOUT is 2 + */ +#define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0}) + + /** + * A factor that is used to randomize the wait time before a message + * is retransmitted to prevent synchronization effects. + * RFC 7252, Section 4.8 Default value of ACK_RANDOM_FACTOR is 1.5 + */ +#define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500}) + + /** + * Number of message retransmissions before message sending is stopped + * RFC 7252, Section 4.8 Default value of MAX_RETRANSMIT is 4 + */ +#define COAP_DEFAULT_MAX_RETRANSMIT 4 + + /** + * The number of simultaneous outstanding interactions that a client + * maintains to a given server. + * RFC 7252, Section 4.8 Default value of NSTART is 1 + */ +#define COAP_DEFAULT_NSTART 1 + + /** @} */ + +/** +* Set the CoAP maximum retransmit count before failure +* +* Number of message retransmissions before message sending is stopped +* +* @param session The CoAP session. +* @param value The value to set to. The default is 4 and should not normally +* get changed. +*/ +void coap_session_set_max_retransmit(coap_session_t *session, + unsigned int value); + +/** +* Set the CoAP initial ack response timeout before the next re-transmit +* +* Number of seconds when to expect an ACK or a response to an +* outstanding CON message. +* +* @param session The CoAP session. +* @param value The value to set to. The default is 2 and should not normally +* get changed. +*/ +void coap_session_set_ack_timeout(coap_session_t *session, + coap_fixed_point_t value); + +/** +* Set the CoAP ack randomize factor +* +* A factor that is used to randomize the wait time before a message +* is retransmitted to prevent synchronization effects. +* +* @param session The CoAP session. +* @param value The value to set to. The default is 1.5 and should not normally +* get changed. +*/ +void coap_session_set_ack_random_factor(coap_session_t *session, + coap_fixed_point_t value); + +/** +* Get the CoAP maximum retransmit before failure +* +* Number of message retransmissions before message sending is stopped +* +* @param session The CoAP session. +* +* @return Current maximum retransmit value +*/ +unsigned int coap_session_get_max_transmit(coap_session_t *session); + +/** +* Get the CoAP initial ack response timeout before the next re-transmit +* +* Number of seconds when to expect an ACK or a response to an +* outstanding CON message. +* +* @param session The CoAP session. +* +* @return Current ack response timeout value +*/ +coap_fixed_point_t coap_session_get_ack_timeout(coap_session_t *session); + +/** +* Get the CoAP ack randomize factor +* +* A factor that is used to randomize the wait time before a message +* is retransmitted to prevent synchronization effects. +* +* @param session The CoAP session. +* +* @return Current ack randomize value +*/ +coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session); #endif /* _SESSION_H */ diff --git a/include/coap/net.h b/include/coap/net.h index f7303e5a..24d0dc2a 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -619,6 +619,19 @@ coap_pdu_t *coap_wellknown_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *request); -unsigned int calc_timeout(unsigned char r); +/** + * Calculates the initial timeout based on the session CoAP transmission + * parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND. + * The calculation requires 'ack_timeout' and 'ack_random_factor' to be in + * Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r + * is interpreted as the fractional part of a Q0.MAX_BITS random value. + * + * @param session session timeout is associated with + * @param r random value as fractional part of a Q0.MAX_BITS fixed point + * value + * @return COAP_TICKS_PER_SECOND * 'ack_timeout' * + * (1 + ('ack_random_factor' - 1) * r) + */ +unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r); #endif /* _COAP_NET_H_ */ diff --git a/libcoap-1.map b/libcoap-1.map index 4256f6bf..831ebc08 100644 --- a/libcoap-1.map +++ b/libcoap-1.map @@ -154,8 +154,11 @@ global: coap_session_delay_pdu; coap_session_disconnected; coap_session_free; + coap_session_get_ack_random_factor; + coap_session_get_ack_timeout; coap_session_get_app_data; coap_session_get_by_peer; + coap_session_get_max_transmit; coap_session_max_pdu_size; coap_session_reference; coap_session_release; @@ -163,7 +166,10 @@ global: coap_session_send; coap_session_send_csm; coap_session_send_pdu; + coap_session_set_ack_random_factor; + coap_session_set_ack_timeout; coap_session_set_app_data; + coap_session_set_max_retransmit; coap_session_set_mtu; coap_session_str; coap_session_write; diff --git a/libcoap-1.sym b/libcoap-1.sym index a6b363c1..06e062b0 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -152,8 +152,11 @@ coap_session_connected coap_session_delay_pdu coap_session_disconnected coap_session_free +coap_session_get_ack_random_factor +coap_session_get_ack_timeout coap_session_get_app_data coap_session_get_by_peer +coap_session_get_max_transmit coap_session_max_pdu_size coap_session_reference coap_session_release @@ -161,7 +164,10 @@ coap_session_reset coap_session_send coap_session_send_csm coap_session_send_pdu +coap_session_set_ack_random_factor +coap_session_set_ack_timeout coap_session_set_app_data +coap_session_set_max_retransmit coap_session_set_mtu coap_session_str coap_session_write diff --git a/src/coap_session.c b/src/coap_session.c index 258649db..ea064eba 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -21,6 +21,51 @@ #include "encode.h" #include +void +coap_session_set_max_retransmit (coap_session_t *session, unsigned int value) { + if (value > 0) + session->max_retransmit = value; + coap_log(LOG_DEBUG, "*** %s: session max_retransmit set to %d\n", + coap_session_str(session), session->max_retransmit); + return; +} + +void +coap_session_set_ack_timeout (coap_session_t *session, coap_fixed_point_t value) { + if (value.integer_part > 0 && value.fractional_part < 1000) + session->ack_timeout = value; + coap_log(LOG_DEBUG, "*** %s: session ack_timeout set to %d.%03d\n", + coap_session_str(session), session->ack_timeout.integer_part, + session->ack_timeout.fractional_part); + return; +} + +void +coap_session_set_ack_random_factor (coap_session_t *session, + coap_fixed_point_t value) { + if (value.integer_part > 0 && value.fractional_part < 1000) + session->ack_random_factor = value; + coap_log(LOG_DEBUG, "*** %s: session ack_random_factor set to %d.%03d\n", + coap_session_str(session), session->ack_random_factor.integer_part, + session->ack_random_factor.fractional_part); + return; +} + +unsigned int +coap_session_get_max_transmit (coap_session_t *session) { + return session->max_retransmit; +} + +coap_fixed_point_t +coap_session_get_ack_timeout (coap_session_t *session) { + return session->ack_timeout; +} + +coap_fixed_point_t +coap_session_get_ack_random_factor (coap_session_t *session) { + return session->ack_random_factor; +} + coap_session_t * coap_session_reference(coap_session_t *session) { ++session->ref; @@ -87,6 +132,9 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n"); } } + session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT; + session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT; + session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR; /* initialize message id */ prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid)); @@ -213,7 +261,7 @@ coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, uint8_t r; prng(&r, sizeof(r)); /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ - node->timeout = calc_timeout(r); + node->timeout = coap_calc_timeout(session, r); } } LL_APPEND(session->sendqueue, node); diff --git a/src/net.c b/src/net.c index bd2fc11f..f6cad511 100644 --- a/src/net.c +++ b/src/net.c @@ -58,47 +58,6 @@ #define min(a,b) ((a) < (b) ? (a) : (b)) #endif - /** - * @defgroup cc Rate Control - * The transmission parameters for CoAP rate control ("Congestion - * Control" in stream-oriented protocols) are defined in - * https://tools.ietf.org/html/rfc7252#section-4.8 - * @{ - */ - -#ifndef COAP_DEFAULT_ACK_TIMEOUT - /** - * Number of seconds when to expect an ACK or a response to an - * outstanding CON message. - */ -#define COAP_DEFAULT_ACK_TIMEOUT 2 /* see RFC 7252, Section 4.8 */ -#endif - -#ifndef COAP_DEFAULT_ACK_RANDOM_FACTOR - /** - * A factor that is used to randomize the wait time before a message - * is retransmitted to prevent synchronization effects. - */ -#define COAP_DEFAULT_ACK_RANDOM_FACTOR 1.5 /* see RFC 7252, Section 4.8 */ -#endif - -#ifndef COAP_DEFAULT_MAX_RETRANSMIT - /** - * Number of message retransmissions before message sending is stopped - */ -#define COAP_DEFAULT_MAX_RETRANSMIT 4 /* see RFC 7252, Section 4.8 */ -#endif - -#ifndef COAP_DEFAULT_NSTART - /** - * The number of simultaneous outstanding interactions that a client - * maintains to a given server. - */ -#define COAP_DEFAULT_NSTART 1 /* see RFC 7252, Section 4.8 */ -#endif - - /** @} */ - /** * The number of bits for the fractional part of ACK_TIMEOUT and * ACK_RANDOM_FACTOR. Must be less or equal 8. @@ -115,15 +74,16 @@ #error FRAC_BITS must be less or equal 8 #endif - /** creates a Qx.frac from fval */ -#define Q(frac,fval) ((uint16_t)(((1 << (frac)) * (fval)))) + /** creates a Qx.frac from fval in coap_fixed_point_t */ +#define Q(frac,fval) ((uint16_t)(((1 << (frac)) * fval.integer_part) + \ + ((1 << (frac)) * fval.fractional_part + 500)/1000)) -/** creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_RANDOM_FACTOR */ +/** creates a Qx.FRAC_BITS from session's 'ack_random_factor' */ #define ACK_RANDOM_FACTOR \ - Q(FRAC_BITS, COAP_DEFAULT_ACK_RANDOM_FACTOR) + Q(FRAC_BITS, session->ack_random_factor) -/** creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_TIMEOUT */ -#define ACK_TIMEOUT Q(FRAC_BITS, COAP_DEFAULT_ACK_TIMEOUT) +/** creates a Qx.FRAC_BITS from session's 'ack_timeout' */ +#define ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout) #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) @@ -782,22 +742,24 @@ coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned ch } /** - * Calculates the initial timeout based on the global CoAP transmission - * parameters ACK_TIMEOUT, ACK_RANDOM_FACTOR, and COAP_TICKS_PER_SECOND. - * The calculation requires ACK_TIMEOUT and ACK_RANDOM_FACTOR to be in + * Calculates the initial timeout based on the session CoAP transmission + * parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND. + * The calculation requires 'ack_timeout' and 'ack_random_factor' to be in * Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r * is interpreted as the fractional part of a Q0.MAX_BITS random value. * + * @param session session timeout is associated with * @param r random value as fractional part of a Q0.MAX_BITS fixed point * value - * @return COAP_TICKS_PER_SECOND * ACK_TIMEOUT * (1 + (ACK_RANDOM_FACTOR - 1) * r) + * @return COAP_TICKS_PER_SECOND * 'ack_timeout' * + * (1 + ('ack_random_factor' - 1) * r) */ unsigned int -calc_timeout(unsigned char r) { +coap_calc_timeout(coap_session_t *session, unsigned char r) { unsigned int result; /* The integer 1.0 as a Qx.FRAC_BITS */ -#define FP1 Q(FRAC_BITS, 1) +#define FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0})) /* rounds val up and right shifts by frac positions */ #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac)) @@ -918,7 +880,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { node->pdu = pdu; prng(&r, sizeof(r)); /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ - node->timeout = calc_timeout(r); + node->timeout = coap_calc_timeout(session, r); return coap_wait_ack(session->context, session, node); error: coap_delete_pdu(pdu); @@ -931,7 +893,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { return COAP_INVALID_TID; /* re-initialize timeout when maximum number of retransmissions are not reached yet */ - if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT) { + if (node->retransmit_cnt < node->session->max_retransmit) { ssize_t bytes_written; coap_tick_t now;