Skip to content

Commit 059abd8

Browse files
sidchacarlescufi
authored andcommitted
mgmt/osdp: Flush RX buffer before sending data
Partial packets in the RX buffers cause the subsequent packet to be treated as malformed. The RX buffer can have partial data if the sender is too slow in sending the packet of if there is an interruption in transmission mid-way. To avoid any issues due to such partials, flush the uart channel before sending the command/response. Signed-off-by: Siddharth Chandrasekaran <[email protected]>
1 parent a1f3c76 commit 059abd8

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

subsys/mgmt/osdp/src/osdp_cp.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ static int cp_send_command(struct osdp_pd *pd)
519519
return OSDP_CP_ERR_GENERIC;
520520
}
521521

522+
/* flush rx to remove any invalid data. */
523+
if (pd->channel.flush) {
524+
pd->channel.flush(pd->channel.data);
525+
}
526+
522527
ret = pd->channel.send(pd->channel.data, pd->rx_buf, len);
523528
if (ret != len) {
524529
LOG_ERR("Channel send for %d bytes failed! ret: %d", len, ret);
@@ -604,14 +609,9 @@ static inline void cp_set_state(struct osdp_pd *pd, enum osdp_cp_state_e state)
604609
CLEAR_FLAG(pd, PD_FLAG_AWAIT_RESP);
605610
}
606611

607-
static void cp_reset_channel(struct osdp_pd *pd)
608-
{
609-
pd->rx_buf_len = 0;
610-
if (pd->channel.flush) {
611-
pd->channel.flush(pd->channel.data);
612-
}
613-
}
614-
612+
/**
613+
* Note: This method must not dequeue cmd unless it reaches an invalid state.
614+
*/
615615
static int cp_phy_state_update(struct osdp_pd *pd)
616616
{
617617
int rc, ret = OSDP_CP_ERR_CAN_YIELD;
@@ -629,7 +629,6 @@ static int cp_phy_state_update(struct osdp_pd *pd)
629629
pd->cmd_id = cmd->id;
630630
memcpy(pd->cmd_data, cmd, sizeof(struct osdp_cmd));
631631
osdp_cmd_free(pd, cmd);
632-
cp_reset_channel(pd);
633632
/* fall-thru */
634633
case OSDP_CP_PHY_STATE_SEND_CMD:
635634
if ((cp_send_command(pd)) < 0) {
@@ -672,7 +671,6 @@ static int cp_phy_state_update(struct osdp_pd *pd)
672671
pd->phy_state = OSDP_CP_PHY_STATE_IDLE;
673672
break;
674673
case OSDP_CP_PHY_STATE_ERR:
675-
cp_reset_channel(pd);
676674
cp_flush_command_queue(pd);
677675
pd->phy_state = OSDP_CP_PHY_STATE_ERR_WAIT;
678676
ret = OSDP_CP_ERR_GENERIC;

subsys/mgmt/osdp/src/osdp_pd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ static int pd_send_reply(struct osdp_pd *pd)
630630
return OSDP_PD_ERR_GENERIC;
631631
}
632632

633+
/* flush rx to remove any invalid data. */
634+
if (pd->channel.flush) {
635+
pd->channel.flush(pd->channel.data);
636+
}
637+
633638
ret = pd->channel.send(pd->channel.data, pd->rx_buf, len);
634639
if (ret != len) {
635640
LOG_ERR("Channel send for %d bytes failed! ret: %d", len, ret);

0 commit comments

Comments
 (0)