Skip to content

Commit 33677ef

Browse files
rlubosnashif
authored andcommitted
samples: net: coap_server: Fix retransmission mechanism
The retransmission handler only increased the retransmission count, w/o sending the actual message again. Additionally, the next retransmission time calculation was broken - it did not take into consideration the time that has already passed since before retransmission, and re-applied the entire timeout value. Signed-off-by: Robert Lubos <[email protected]>
1 parent 2e4eb78 commit 33677ef

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

samples/net/sockets/coap_server/src/coap-server.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ LOG_MODULE_REGISTER(net_coap_server_sample, LOG_LEVEL_DBG);
3838
{ { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1 } } }
3939
#endif
4040

41+
#define ADDRLEN(sock) \
42+
(((struct sockaddr *)sock)->sa_family == AF_INET ? \
43+
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
44+
4145
#define NUM_OBSERVERS 3
4246

4347
#define NUM_PENDINGS 3
@@ -951,9 +955,30 @@ static int send_notification_packet(const struct sockaddr *addr,
951955
const uint8_t *token, uint8_t tkl,
952956
bool is_response);
953957

958+
static void schedule_next_retransmission(void)
959+
{
960+
struct coap_pending *pending;
961+
int32_t remaining;
962+
uint32_t now = k_uptime_get_32();
963+
964+
/* Get the first pending retansmission to expire after cycling. */
965+
pending = coap_pending_next_to_expire(pendings, NUM_PENDINGS);
966+
if (!pending) {
967+
return;
968+
}
969+
970+
remaining = pending->t0 + pending->timeout - now;
971+
if (remaining < 0) {
972+
remaining = 0;
973+
}
974+
975+
k_work_reschedule(&retransmit_work, K_MSEC(remaining));
976+
}
977+
954978
static void retransmit_request(struct k_work *work)
955979
{
956980
struct coap_pending *pending;
981+
int r;
957982

958983
pending = coap_pending_next_to_expire(pendings, NUM_PENDINGS);
959984
if (!pending) {
@@ -963,10 +988,17 @@ static void retransmit_request(struct k_work *work)
963988
if (!coap_pending_cycle(pending)) {
964989
k_free(pending->data);
965990
coap_pending_clear(pending);
966-
return;
991+
} else {
992+
net_hexdump("Retransmit", pending->data, pending->len);
993+
994+
r = sendto(sock, pending->data, pending->len, 0,
995+
&pending->addr, ADDRLEN(&pending->addr));
996+
if (r < 0) {
997+
LOG_ERR("Failed to send %d", errno);
998+
}
967999
}
9681000

969-
k_work_reschedule(&retransmit_work, K_MSEC(pending->timeout));
1001+
schedule_next_retransmission();
9701002
}
9711003

9721004
static void update_counter(struct k_work *work)
@@ -999,12 +1031,7 @@ static int create_pending_request(struct coap_packet *response,
9991031

10001032
coap_pending_cycle(pending);
10011033

1002-
pending = coap_pending_next_to_expire(pendings, NUM_PENDINGS);
1003-
if (!pending) {
1004-
return 0;
1005-
}
1006-
1007-
k_work_reschedule(&retransmit_work, K_MSEC(pending->timeout));
1034+
schedule_next_retransmission();
10081035

10091036
return 0;
10101037
}

0 commit comments

Comments
 (0)