From ca36c67ba29797c3f9e153611c91bf4e3e0e7dfa Mon Sep 17 00:00:00 2001 From: Qukich Date: Wed, 7 Feb 2024 19:39:27 +0300 Subject: [PATCH] fix index --- hdlc/client.c | 22 ++++++++++++---------- hdlc/client.h | 3 ++- hdlc/main.c | 1 - 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/hdlc/client.c b/hdlc/client.c index 947a67b..5408d36 100644 --- a/hdlc/client.c +++ b/hdlc/client.c @@ -18,6 +18,7 @@ void init_hdlc_client(struct Client* client, int connecting_frame_timeout){ client->state = IDLE_STATE; client->connecting_frame_timeout = connecting_frame_timeout; connecting_frame_timeout_bf = connecting_frame_timeout; + client->current_index_frame = 20; // client->current_state_hdlc.control_escape = 0; // client->current_state_hdlc.fcs = FCS_INIT_VALUE; @@ -30,13 +31,9 @@ void init_hdlc_client(struct Client* client, int connecting_frame_timeout){ void hdlc_connect(struct Client* client, hdlc_control_t* frame){ client->state = CONNECTING; - if (client->frameS.seq_no != 0){ - *frame = client->frameS; - return; - } - client->frameS.seq_no = 0; client->frameS.frame = S_FRAME; + client->current_index_frame = client->frameS.seq_no; *frame = client->frameS; } @@ -52,15 +49,11 @@ int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, return ERR_INVALID_DATA_SIZE; } - if (client->frameI.seq_no != 0){ - *frame = client->frameI; - return 0; - } - client->frameI.seq_no = 0; client->frameI.frame = I_FRAME; client->data_i_frame = *data; client->len_data_i_frame = data_len; + client->current_index_frame = client->frameI.seq_no; *frame = client->frameI; @@ -71,6 +64,7 @@ int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t* buffer, size_t lenBuffer) { if (frame->frame == S_FRAME || frame->frame == S_FRAME_NACK){ int ret = hdlc_frame_data(frame, NULL, 0, buffer, &lenBuffer); + if (ret < 0){ printf("err in get_frame: %d\n", ret); } @@ -96,6 +90,13 @@ int hdlc_decode_recived_raw_data(struct Client* client, uint8_t* buffer, size_t return ret; } + if (recv_control.seq_no != client->current_index_frame){ + client->state = DISCONNECTING; + client->frame_rej.seq_no = 0; + client->frame_rej.frame = S_FRAME_NACK; + return ERR_INVALID_SEQ_NUMBER_FRAME; + } + switch (recv_control.frame) { case S_FRAME: client->state = READY_STATE; @@ -121,4 +122,5 @@ int hdlc_timeout_handler(struct Client* client, int delta_time){ if (client->connecting_frame_timeout <= 0){ return ERR_FRAME_TIME_OUT; } + return 0; } \ No newline at end of file diff --git a/hdlc/client.h b/hdlc/client.h index bd5c751..64ec4bb 100644 --- a/hdlc/client.h +++ b/hdlc/client.h @@ -17,7 +17,8 @@ enum HDLCState { struct Client{ enum HDLCState state; int connecting_frame_timeout; //-1 - hdlc_state_t current_state_hdlc; + uint8_t current_index_frame; + //hdlc_state_t current_state_hdlc; hdlc_control_t frameS; hdlc_control_t frameI; uint8_t data_i_frame; diff --git a/hdlc/main.c b/hdlc/main.c index 0624f37..db27ab5 100644 --- a/hdlc/main.c +++ b/hdlc/main.c @@ -24,7 +24,6 @@ int main(){ hdlc_control_t frame_data; uint8_t data = 33; - printf("%llu\n", sizeof(data)); hdlc_send_data(&hdlc, &frame_data, &data, sizeof(data)); uint8_t buffer_for_ex_data[7];