fix final version(hdlc)

This commit is contained in:
Qukich 2024-02-11 17:16:36 +03:00
parent 80cd73c3d6
commit 800f48bc11
4 changed files with 79 additions and 70 deletions

4
.gitignore vendored
View File

@ -7,3 +7,7 @@ uart.c
uart.h
!./hdlc
/.idea/Display_Avr_3.iml
/.idea/.gitignore
/.idea/vcs.xml
/.idea/modules.xml

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}
}
}