Skip to content

Commit

Permalink
examples/gcoap: send deregistration request with -d
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelRottleuthner committed Feb 8, 2024
1 parent 26f99ba commit 0eb81c9
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions examples/gcoap/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,33 +356,31 @@ int gcoap_cli_cmd(int argc, char **argv)
uri = proxy_uri;
}

gcoap_req_init(&pdu, buf, CONFIG_GCOAP_PDU_BUF_SIZE, code_pos,
(_proxied || observe) ? NULL : uri);
gcoap_req_init(&pdu, buf, CONFIG_GCOAP_PDU_BUF_SIZE, code_pos, NULL);

if (observe) {
uint8_t *token = coap_get_token(&pdu);
if (obs_value == COAP_OBS_REGISTER) {
obs_req_tkl = coap_get_token_len(&pdu);
/* backup the token of the initial observe registration */
memcpy(obs_req_token, token, obs_req_tkl);
observing = true;
coap_opt_add_uint(&pdu, COAP_OPT_OBSERVE, obs_value);
if (!_proxied) {
/* add uri path option separately
* (options must be added in order) */
coap_opt_add_uri_path(&pdu, uri);
}
} else {
/* use the token of the registration for deregistration */
/* use the token of the registration for deregistration
* (manually replace the token set by gcoap_req_init) */
memcpy(token, obs_req_token, obs_req_tkl);
if (gcoap_obs_req_forget(&remote, obs_req_token, obs_req_tkl)) {
printf("could not remove observe request\n");
return 1;
}
observing = false;
/* deregistration does not require further actions */
return 0;
}

coap_opt_add_uint(&pdu, COAP_OPT_OBSERVE, obs_value);
}

if (!_proxied) {
/* add uri path option separately
* (options must be added in order) */
coap_opt_add_uri_path(&pdu, uri);
}

coap_hdr_set_type(pdu.hdr, msg_type);
Expand Down Expand Up @@ -422,6 +420,11 @@ int gcoap_cli_cmd(int argc, char **argv)
puts("gcoap_cli: msg send failed");
}
else {
if (observe) {
/* on successful observe request, store that this node is
* observing / not observing anymore */
observing = obs_value == COAP_OBS_REGISTER;
}
/* send Observe notification for /cli/stats */
notify_observers();
}
Expand Down

0 comments on commit 0eb81c9

Please sign in to comment.