1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

gcoap: lock CoAP state mutex in observe API

This commit is contained in:
Fabian Hüßler 2024-06-07 10:39:12 +02:00
parent f45ab68d04
commit 08f6ec49c4
2 changed files with 21 additions and 17 deletions

View File

@ -1077,6 +1077,9 @@ static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
*
* First verifies that an observer has been registered for the resource.
*
* @post If this function returns @see GCOAP_OBS_INIT_OK you have to call
* @ref gcoap_obs_send() afterwards to release a mutex.
*
* @param[out] pdu Notification metadata
* @param[out] buf Buffer containing the PDU
* @param[in] len Length of the buffer

View File

@ -1898,9 +1898,11 @@ int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
{
gcoap_observe_memo_t *memo = NULL;
mutex_lock(&_coap_state.lock);
_find_obs_memo_resource(&memo, resource);
if (memo == NULL) {
/* Unique return value to specify there is not an observer */
mutex_unlock(&_coap_state.lock);
return GCOAP_OBS_INIT_UNUSED;
}
@ -1909,26 +1911,27 @@ int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
ssize_t hdrlen = coap_build_hdr(pdu->hdr, COAP_TYPE_NON, &memo->token[0],
memo->token_len, COAP_CODE_CONTENT, msgid);
if (hdrlen > 0) {
coap_pkt_init(pdu, buf, len, hdrlen);
_add_generated_observe_option(pdu);
/* Store message ID of the last notification sent. This is needed
* to match a potential RST returned by a client in order to signal
* it does not recognize this notification. */
memo->last_msgid = msgid;
return GCOAP_OBS_INIT_OK;
}
else {
if (hdrlen <= 0) {
/* reason for negative hdrlen is not defined, so we also are vague */
mutex_unlock(&_coap_state.lock);
return GCOAP_OBS_INIT_ERR;
}
coap_pkt_init(pdu, buf, len, hdrlen);
_add_generated_observe_option(pdu);
/* Store message ID of the last notification sent. This is needed
* to match a potential RST returned by a client in order to signal
* it does not recognize this notification. */
memo->last_msgid = msgid;
return GCOAP_OBS_INIT_OK;
}
size_t gcoap_obs_send(const uint8_t *buf, size_t len,
const coap_resource_t *resource)
{
ssize_t ret = 0;
gcoap_observe_memo_t *memo = NULL;
_find_obs_memo_resource(&memo, resource);
@ -1938,12 +1941,10 @@ size_t gcoap_obs_send(const uint8_t *buf, size_t len,
memcpy(&aux.local, memo->notifier, sizeof(*memo->notifier));
aux.flags = SOCK_AUX_SET_LOCAL;
}
ssize_t bytes = _tl_send(&memo->socket, buf, len, memo->observer, &aux);
return (size_t)((bytes > 0) ? bytes : 0);
}
else {
return 0;
ret = _tl_send(&memo->socket, buf, len, memo->observer, &aux);
}
mutex_unlock(&_coap_state.lock);
return ret <= 0 ? 0 : (size_t)ret;
}
uint8_t gcoap_op_state(void)