Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asymcute: Compare request message type when matching acknowledgement #18434

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions sys/net/application_layer/asymcute/asymcute.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,25 @@ static uint16_t _msg_id_next(asymcute_con_t *con)
return con->last_id;
}

static uint8_t _req_type(asymcute_req_t *req)
{
size_t len;
size_t pos = _len_get(req->data, &len);
return req->data[pos];
}

/* @pre con is locked */
static asymcute_req_t *_req_preprocess(asymcute_con_t *con,
size_t msg_len, size_t min_len,
const uint8_t *buf, unsigned id_pos)
const uint8_t *buf, unsigned id_pos,
uint8_t rtype)
{
/* verify message length */
if (msg_len < min_len) {
return NULL;
}

uint16_t msg_id = (buf == NULL) ? 0 : byteorder_bebuftohs(&buf[id_pos]);
uint16_t msg_id = (buf == NULL) ? 0 : byteorder_bebuftohs(&buf[id_pos]);

asymcute_req_t *res = NULL;
asymcute_req_t *iter = con->pending;
Expand All @@ -126,8 +134,9 @@ static asymcute_req_t *_req_preprocess(asymcute_con_t *con,
con->pending = iter->next;
}
while (iter && !res) {
if (iter->next && (iter->next->msg_id == msg_id)) {
res = iter->next;
asymcute_req_t *r = iter->next;
if (r && (r->msg_id == msg_id) && (_req_type(r) == rtype)) {
res = r;
iter->next = iter->next->next;
}
iter = iter->next;
Expand Down Expand Up @@ -340,7 +349,7 @@ static void _on_keepalive_evt(void *arg)
static void _on_connack(asymcute_con_t *con, const uint8_t *data, size_t len)
{
mutex_lock(&con->lock);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_CONNACK, NULL, 0);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_CONNACK, NULL, 0, MQTTSN_CONNECT);
if (req == NULL) {
mutex_unlock(&con->lock);
return;
Expand All @@ -367,7 +376,7 @@ static void _on_disconnect(asymcute_con_t *con, size_t len)

/* we might have triggered the DISCONNECT process ourselves, so make sure
* the pending request is being handled */
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_DISCONNECT, NULL, 0);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_DISCONNECT, NULL, 0, MQTTSN_DISCONNECT);

/* put the connection back to NOTCON in any case and let the user know */
_disconnect(con, NOTCON);
Expand Down Expand Up @@ -403,7 +412,8 @@ static void _on_regack(asymcute_con_t *con, const uint8_t *data, size_t len)
{
mutex_lock(&con->lock);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_REGACK,
data, IDPOS_REGACK);
data, IDPOS_REGACK,
MQTTSN_REGISTER);
if (req == NULL) {
mutex_unlock(&con->lock);
return;
Expand Down Expand Up @@ -471,7 +481,8 @@ static void _on_puback(asymcute_con_t *con, const uint8_t *data, size_t len)
{
mutex_lock(&con->lock);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_PUBACK,
data, IDPOS_PUBACK);
data, IDPOS_PUBACK,
MQTTSN_PUBLISH);
if (req == NULL) {
mutex_unlock(&con->lock);
return;
Expand All @@ -488,7 +499,8 @@ static void _on_suback(asymcute_con_t *con, const uint8_t *data, size_t len)
{
mutex_lock(&con->lock);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_SUBACK,
data, IDPOS_SUBACK);
data, IDPOS_SUBACK,
MQTTSN_SUBSCRIBE);
if (req == NULL) {
mutex_unlock(&con->lock);
return;
Expand Down Expand Up @@ -527,7 +539,8 @@ static void _on_unsuback(asymcute_con_t *con, const uint8_t *data, size_t len)
{
mutex_lock(&con->lock);
asymcute_req_t *req = _req_preprocess(con, len, MINLEN_UNSUBACK,
data, IDPOS_UNSUBACK);
data, IDPOS_UNSUBACK,
MQTTSN_UNSUBSCRIBE);
if (req == NULL) {
mutex_unlock(&con->lock);
return;
Expand Down