Skip to content

Commit 15ba020

Browse files
rlubosnashif
authored andcommitted
samples: net: coap_server: Remove observer on notification timeout
In case no reply is received for notification message, remove the corresponsding observer since it's no longer reachable, freeing slot for another one. Signed-off-by: Robert Lubos <[email protected]>
1 parent 33677ef commit 15ba020

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -949,12 +949,6 @@ static int large_create_post(struct coap_resource *resource,
949949
return r;
950950
}
951951

952-
static int send_notification_packet(const struct sockaddr *addr,
953-
socklen_t addr_len,
954-
uint16_t age, uint16_t id,
955-
const uint8_t *token, uint8_t tkl,
956-
bool is_response);
957-
958952
static void schedule_next_retransmission(void)
959953
{
960954
struct coap_pending *pending;
@@ -975,6 +969,8 @@ static void schedule_next_retransmission(void)
975969
k_work_reschedule(&retransmit_work, K_MSEC(remaining));
976970
}
977971

972+
static void remove_observer(struct sockaddr *addr);
973+
978974
static void retransmit_request(struct k_work *work)
979975
{
980976
struct coap_pending *pending;
@@ -986,6 +982,7 @@ static void retransmit_request(struct k_work *work)
986982
}
987983

988984
if (!coap_pending_cycle(pending)) {
985+
remove_observer(&pending->addr);
989986
k_free(pending->data);
990987
coap_pending_clear(pending);
991988
} else {
@@ -1142,6 +1139,7 @@ static int obs_get(struct coap_resource *resource,
11421139

11431140
observer = coap_observer_next_unused(observers, NUM_OBSERVERS);
11441141
if (!observer) {
1142+
LOG_ERR("Not enough observer slots.");
11451143
return -ENOMEM;
11461144
}
11471145

@@ -1318,7 +1316,7 @@ static struct coap_resource resources[] = {
13181316
{ },
13191317
};
13201318

1321-
static struct coap_resource *find_resouce_by_observer(
1319+
static struct coap_resource *find_resource_by_observer(
13221320
struct coap_resource *resources, struct coap_observer *o)
13231321
{
13241322
struct coap_resource *r;
@@ -1336,6 +1334,28 @@ static struct coap_resource *find_resouce_by_observer(
13361334
return NULL;
13371335
}
13381336

1337+
static void remove_observer(struct sockaddr *addr)
1338+
{
1339+
struct coap_resource *r;
1340+
struct coap_observer *o;
1341+
1342+
o = coap_find_observer_by_addr(observers, NUM_OBSERVERS, addr);
1343+
if (!o) {
1344+
return;
1345+
}
1346+
1347+
r = find_resource_by_observer(resources, o);
1348+
if (!r) {
1349+
LOG_ERR("Observer found but Resource not found\n");
1350+
return;
1351+
}
1352+
1353+
LOG_INF("Removing observer %p", o);
1354+
1355+
coap_remove_observer(r, o);
1356+
memset(o, 0, sizeof(struct coap_observer));
1357+
}
1358+
13391359
static void process_coap_request(uint8_t *data, uint16_t data_len,
13401360
struct sockaddr *client_addr,
13411361
socklen_t client_addr_len)
@@ -1371,28 +1391,10 @@ static void process_coap_request(uint8_t *data, uint16_t data_len,
13711391
not_found:
13721392

13731393
if (type == COAP_TYPE_RESET) {
1374-
struct coap_resource *r;
1375-
struct coap_observer *o;
1376-
1377-
o = coap_find_observer_by_addr(observers, NUM_OBSERVERS,
1378-
client_addr);
1379-
if (!o) {
1380-
LOG_ERR("Observer not found\n");
1381-
goto end;
1382-
}
1383-
1384-
r = find_resouce_by_observer(resources, o);
1385-
if (!r) {
1386-
LOG_ERR("Observer found but Resource not found\n");
1387-
goto end;
1388-
}
1389-
1390-
coap_remove_observer(r, o);
1391-
1394+
remove_observer(client_addr);
13921395
return;
13931396
}
13941397

1395-
end:
13961398
r = coap_handle_request(&request, resources, options, opt_num,
13971399
client_addr, client_addr_len);
13981400
if (r < 0) {

0 commit comments

Comments
 (0)