diff --git a/.gitignore b/.gitignore index 2d8ff23..b68e17f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,8 @@ main1.c uart.c uart.h -!./hdlc \ No newline at end of file +!./hdlc +/.idea/Display_Avr_3.iml +/.idea/.gitignore +/.idea/vcs.xml +/.idea/modules.xml diff --git a/hdlc/client.c b/hdlc/client.c index 5408d36..1a8a851 100644 --- a/hdlc/client.c +++ b/hdlc/client.c @@ -28,17 +28,15 @@ void init_hdlc_client(struct Client* client, int connecting_frame_timeout){ // client->current_state_hdlc.dest_index = 0; } -void hdlc_connect(struct Client* client, hdlc_control_t* frame){ +void hdlc_connect(struct Client* client){ client->state = CONNECTING; client->frameS.seq_no = 0; client->frameS.frame = S_FRAME; client->current_index_frame = client->frameS.seq_no; - - *frame = client->frameS; } -int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len){ +int hdlc_send_data(struct Client* client, uint8_t* data, size_t data_len){ if (client->state != READY_STATE){ return ERR_INVALID_STATE; } @@ -55,22 +53,30 @@ int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, client->len_data_i_frame = data_len; client->current_index_frame = client->frameI.seq_no; - *frame = client->frameI; - client->state = RECIVING; return 0; } -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); +int hdlc_get_raw_frame(struct Client *client, uint8_t* buffer, size_t lenBuffer) { + if(client->state == RECIVING){ + int ret = hdlc_frame_data(client->frameI, &client->data_i_frame, + client->len_data_i_frame, buffer, &lenBuffer); + if (ret < 0){ + printf("err in get_frame: %d\n", ret); + } + } + + if (client->state == CONNECTING){ + int ret = hdlc_frame_data(client->frameS, NULL, 0, buffer, &lenBuffer); if (ret < 0){ printf("err in get_frame: %d\n", ret); } - } else { - int ret = hdlc_frame_data(frame, &client->data_i_frame, - client->len_data_i_frame, buffer, &lenBuffer); + } + + if (client->state == DISCONNECTING){ + int ret = hdlc_frame_data(client->frame_rej, NULL, 0, buffer, &lenBuffer); + if (ret < 0){ printf("err in get_frame: %d\n", ret); } diff --git a/hdlc/client.h b/hdlc/client.h index 64ec4bb..759e6a7 100644 --- a/hdlc/client.h +++ b/hdlc/client.h @@ -30,9 +30,9 @@ struct Client{ //название функций void init_hdlc_client(struct Client* client, int connecting_frame_timeout); -void hdlc_connect(struct Client* client, hdlc_control_t* frame); -int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len); -int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t* buffer, size_t lenBuffer); +void hdlc_connect(struct Client* client); +int hdlc_send_data(struct Client* client, uint8_t* data, size_t data_len); +int hdlc_get_raw_frame(struct Client *client, uint8_t* buffer, size_t lenBuffer); //принимает буффер с уарта int hdlc_decode_recived_raw_data(struct Client* client, uint8_t* buffer, size_t len_buffer, uint8_t* recived_data, size_t* len_recived_data); int hdlc_timeout_handler(struct Client* client, int delta_time); diff --git a/main_sketch/main_sketch.ino b/main_sketch/main_sketch.ino index e8eac90..9cdffa7 100644 --- a/main_sketch/main_sketch.ino +++ b/main_sketch/main_sketch.ino @@ -8,66 +8,65 @@ struct Client hdlc; bool flag_connection = false; void setup() { - Lsd_inciliation(); - UART_init(); - init_hdlc_client(&hdlc, 200); + Lsd_inciliation(); + UART_init(); + init_hdlc_client(&hdlc, 200); } void loop() { - if (!flag_connection){ - hdlc_control_t frame; - hdlc_connect(&hdlc, &frame); - uint8_t buffer; - hdlc_get_raw_frame(&hdlc, &frame, &buffer, sizeof(buffer)); - - UART_send(&buffer, sizeof(buffer)); - - bool flag_recive = true; - while(flag_recive){ - uint8_t buffer; - UART_receive(&buffer, sizeof(buffer)); - - int err = hdlc_timeout_handler(&hdlc, 1); - if (err != 0){ - return err; - } - - int ret = hdlc_decode_recived_raw_data(&hdlc, &fake_buffer, sizeof(fake_buffer), 0, 0); - - if (ret == -6){ + if (!flag_connection){ + hdlc_connect(&hdlc); uint8_t buffer; - hdlc_get_raw_frame(&hdlc, &hdlc->frame_rej, &buffer, sizeof(buffer)); + hdlc_get_raw_frame(&hdlc, &buffer, sizeof(buffer)); + UART_send(&buffer, sizeof(buffer)); - } - if (ret == 0 && client->state == READY_STATE){ - flag_connection = true; - } + bool flag_recive = true; + while(flag_recive){ + uint8_t buffer; + UART_receive(&buffer, sizeof(buffer)); + + int err = hdlc_timeout_handler(&hdlc, 1); + if (err != 0){ + return err; + } + + int ret = hdlc_decode_recived_raw_data(&hdlc, &fake_buffer, sizeof(fake_buffer), 0, 0); + + if (ret == -6){ + uint8_t buffer; + hdlc_get_raw_frame(&hdlc, &hdlc->frame_rej, &buffer, sizeof(buffer)); + UART_send(&buffer, sizeof(buffer)); + } + + if (ret == 0 && client->state == READY_STATE){ + flag_connection = true; + } + } + } else { + bool flag_recive = true; + while(flag_recive){ + uint8_t buffer; + UART_receive(&buffer, sizeof(buffer)); + + int err = hdlc_timeout_handler(&hdlc, 1); + if (err != 0){ + return err; + } + + uint8_t recived_data; + size_t len_recived_data; + + int ret = hdlc_decode_recived_raw_data(&hdlc, &fake_buffer, sizeof(fake_buffer), &recived_data, &len_recived_data); + + if (ret == -6){ + uint8_t buffer; + hdlc_get_raw_frame(&hdlc, &hdlc->frame_rej, &buffer, sizeof(buffer)); + UART_send(&buffer, sizeof(buffer)); + } + + printLsd(recived_data); + } } - } else { - bool flag_recive = true; - while(flag_recive){ - uint8_t buffer; - UART_receive(&buffer, sizeof(buffer)); - - int err = hdlc_timeout_handler(&hdlc, 1); - if (err != 0){ - return err; - } - - uint8_t recived_data; - size_t len_recived_data; - - int ret = hdlc_decode_recived_raw_data(&hdlc, &fake_buffer, sizeof(fake_buffer), &recived_data, &len_recived_data); - - if (ret == -6){ - uint8_t buffer; - hdlc_get_raw_frame(&hdlc, &hdlc->frame_rej, &buffer, sizeof(buffer)); - UART_send(&buffer, sizeof(buffer)); - } - - printLsd(recived_data); - } - } } \ No newline at end of file