fix i-frame
This commit is contained in:
parent
659ce6c581
commit
38b0a7f8ad
@ -19,36 +19,29 @@ void init_hdlc_client(struct Client* client, int connecting_frame_timeout){
|
|||||||
client->connecting_frame_timeout = connecting_frame_timeout;
|
client->connecting_frame_timeout = connecting_frame_timeout;
|
||||||
connecting_frame_timeout_bf = connecting_frame_timeout;
|
connecting_frame_timeout_bf = connecting_frame_timeout;
|
||||||
|
|
||||||
client->current_state_hdlc.control_escape = 0;
|
// client->current_state_hdlc.control_escape = 0;
|
||||||
client->current_state_hdlc.fcs = FCS_INIT_VALUE;
|
// client->current_state_hdlc.fcs = FCS_INIT_VALUE;
|
||||||
client->current_state_hdlc.start_index = -1;
|
// client->current_state_hdlc.start_index = -1;
|
||||||
client->current_state_hdlc.end_index = -1;
|
// client->current_state_hdlc.end_index = -1;
|
||||||
client->current_state_hdlc.src_index = 0;
|
// client->current_state_hdlc.src_index = 0;
|
||||||
client->current_state_hdlc.dest_index = 0;
|
// client->current_state_hdlc.dest_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect(struct Client* client, hdlc_control_t* frame){
|
void hdlc_connect(struct Client* client, hdlc_control_t* frame){
|
||||||
client->state = CONNECTING;
|
client->state = CONNECTING;
|
||||||
|
|
||||||
|
if (client->frameS.seq_no != 0){
|
||||||
|
*frame = client->frameS;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
client->frameS.seq_no = 0;
|
client->frameS.seq_no = 0;
|
||||||
client->frameS.frame = S_FRAME;
|
client->frameS.frame = S_FRAME;
|
||||||
|
|
||||||
*frame = client->frameS;
|
*frame = client->frameS;
|
||||||
|
|
||||||
// if (client->frame1.seq_no == -4){
|
|
||||||
// client->frame1.seq_no = 0;
|
|
||||||
// client->frame1.frame = S_FRAME;
|
|
||||||
//
|
|
||||||
// *frame = client->frame1;
|
|
||||||
// } else if (client->frame1.seq_no == -4){
|
|
||||||
// client->frame1.seq_no = 0;
|
|
||||||
// client->frame1.frame = S_FRAME;
|
|
||||||
//
|
|
||||||
// *frame = client->frame1;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len){
|
int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len){
|
||||||
if (client->state != READY_STATE){
|
if (client->state != READY_STATE){
|
||||||
return ERR_INVALID_STATE;
|
return ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
@ -59,6 +52,11 @@ int send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_
|
|||||||
return ERR_INVALID_DATA_SIZE;
|
return ERR_INVALID_DATA_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (client->frameI.seq_no != 0){
|
||||||
|
*frame = client->frameI;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
client->frameI.seq_no = 0;
|
client->frameI.seq_no = 0;
|
||||||
client->frameI.frame = I_FRAME;
|
client->frameI.frame = I_FRAME;
|
||||||
client->data_i_frame = *data;
|
client->data_i_frame = *data;
|
||||||
@ -67,10 +65,11 @@ int send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_
|
|||||||
*frame = client->frameI;
|
*frame = client->frameI;
|
||||||
|
|
||||||
client->state = RECIVING;
|
client->state = RECIVING;
|
||||||
};
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t buffer[], size_t lenBuffer) {
|
int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t* buffer, size_t lenBuffer) {
|
||||||
if (frame->frame = S_FRAME){
|
if (frame->frame == S_FRAME || frame->frame == S_FRAME_NACK){
|
||||||
int ret = hdlc_frame_data(frame, NULL, 0, buffer, &lenBuffer);
|
int ret = hdlc_frame_data(frame, NULL, 0, buffer, &lenBuffer);
|
||||||
if (ret < 0){
|
if (ret < 0){
|
||||||
printf("err in get_frame: %d\n", ret);
|
printf("err in get_frame: %d\n", ret);
|
||||||
@ -86,11 +85,11 @@ int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t buf
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hdlc_decode_recived_raw_data(struct Client* client, uint8_t buffer[], size_t len_buffer){
|
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){
|
||||||
hdlc_control_t recv_control;
|
hdlc_control_t recv_control;
|
||||||
uint8_t recive[len_buffer];
|
uint8_t recive[len_buffer];
|
||||||
|
|
||||||
int ret = hdlc_get_data_with_state(&client->current_state_hdlc, &recv_control, buffer, len_buffer, recive,
|
int ret = hdlc_get_data(&recv_control, buffer, len_buffer, &recive,
|
||||||
&len_buffer);
|
&len_buffer);
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -102,12 +101,14 @@ int hdlc_decode_recived_raw_data(struct Client* client, uint8_t buffer[], size_t
|
|||||||
client->state = READY_STATE;
|
client->state = READY_STATE;
|
||||||
break;
|
break;
|
||||||
case I_FRAME:
|
case I_FRAME:
|
||||||
|
*recived_data = buffer[3];
|
||||||
|
*len_recived_data = sizeof(buffer[3]);
|
||||||
break;
|
break;
|
||||||
case S_FRAME_NACK:
|
case S_FRAME_NACK:
|
||||||
client->state = DISCONNECTING;
|
client->state = DISCONNECTING;
|
||||||
client->frame_rej.seq_no = 0;
|
client->frame_rej.seq_no = 0;
|
||||||
client->frame_rej.frame = S_FRAME_NACK;
|
client->frame_rej.frame = S_FRAME_NACK;
|
||||||
break;
|
return ERR_INVALID_SEQ_NUMBER_FRAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
client->connecting_frame_timeout = connecting_frame_timeout_bf;
|
client->connecting_frame_timeout = connecting_frame_timeout_bf;
|
||||||
|
@ -14,8 +14,6 @@ enum HDLCState {
|
|||||||
RECIVING // состояние приема и отправки
|
RECIVING // состояние приема и отправки
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
struct Client{
|
struct Client{
|
||||||
enum HDLCState state;
|
enum HDLCState state;
|
||||||
int connecting_frame_timeout; //-1
|
int connecting_frame_timeout; //-1
|
||||||
@ -31,11 +29,11 @@ struct Client{
|
|||||||
|
|
||||||
//название функций
|
//название функций
|
||||||
void init_hdlc_client(struct Client* client, int connecting_frame_timeout);
|
void init_hdlc_client(struct Client* client, int connecting_frame_timeout);
|
||||||
void connect(struct Client* client, hdlc_control_t* frame);
|
void hdlc_connect(struct Client* client, hdlc_control_t* frame);
|
||||||
int send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len);
|
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);
|
int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t* buffer, size_t lenBuffer);
|
||||||
//принимает буффер с уарта
|
//принимает буффер с уарта
|
||||||
int hdlc_decode_recived_raw_data(struct Client* client, uint8_t buffer[], size_t len_buffer);
|
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);
|
int hdlc_timeout_handler(struct Client* client, int delta_time);
|
||||||
|
|
||||||
#endif //CLIENT_H
|
#endif //CLIENT_H
|
||||||
|
18
hdlc/hdlc.h
18
hdlc/hdlc.h
@ -43,24 +43,6 @@ typedef struct {
|
|||||||
int dest_index;
|
int dest_index;
|
||||||
} hdlc_state_t;
|
} hdlc_state_t;
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the hdlc state
|
|
||||||
*
|
|
||||||
* @param[in] state The new hdlc state to be used
|
|
||||||
* @retval 0 Success
|
|
||||||
* @retval -EINVAL Invalid parameter
|
|
||||||
*/
|
|
||||||
int hdlc_set_state(hdlc_state_t *state);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get current hdlc state
|
|
||||||
*
|
|
||||||
* @param[out] state Current hdlc state
|
|
||||||
* @retval 0 Success
|
|
||||||
* @retval -EINVAL Invalid parameter
|
|
||||||
*/
|
|
||||||
int hdlc_get_state(hdlc_state_t *state);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves data from specified buffer containing the HDLC frame. Frames can be
|
* Retrieves data from specified buffer containing the HDLC frame. Frames can be
|
||||||
* parsed from multiple buffers e.g. when received via UART.
|
* parsed from multiple buffers e.g. when received via UART.
|
||||||
|
43
hdlc/main.c
43
hdlc/main.c
@ -1,39 +1,44 @@
|
|||||||
//#include "hdlc.h"
|
//#include "hdlc.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
#include <stdlib.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
struct Client hdlc;
|
struct Client hdlc;
|
||||||
init_hdlc_client(&hdlc, 200);
|
init_hdlc_client(&hdlc, 200);
|
||||||
hdlc_control_t frame;
|
hdlc_control_t frame;
|
||||||
connect(&hdlc, &frame);
|
hdlc_connect(&hdlc, &frame);
|
||||||
uint8_t buffer_for_ex[25];
|
uint8_t buffer_for_ex[20];
|
||||||
uint8_t fake_buffer[2];
|
uint8_t fake_buffer;
|
||||||
hdlc_get_raw_frame(&hdlc, &frame, buffer_for_ex, sizeof(buffer_for_ex));
|
hdlc_get_raw_frame(&hdlc, &frame, &buffer_for_ex, sizeof(buffer_for_ex));
|
||||||
|
|
||||||
for (int i = 0; i < 200; i++){
|
for (int i = 0; i < 200; i++){
|
||||||
int z = hdlc_timeout_handler(&hdlc, 1);
|
int z = hdlc_timeout_handler(&hdlc, 1);
|
||||||
if (z < 0){
|
hdlc_decode_recived_raw_data(&hdlc, &fake_buffer, sizeof(fake_buffer), 0, 0);
|
||||||
printf("%d\n", z);
|
|
||||||
}
|
|
||||||
hdlc_decode_recived_raw_data(&hdlc, fake_buffer, sizeof(fake_buffer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hdlc_get_raw_frame(&hdlc, &frame, buffer_for_ex, sizeof(buffer_for_ex));
|
hdlc_get_raw_frame(&hdlc, &frame, &buffer_for_ex, sizeof(buffer_for_ex));
|
||||||
int i = hdlc_decode_recived_raw_data(&hdlc, buffer_for_ex, sizeof(buffer_for_ex));
|
int i = hdlc_decode_recived_raw_data(&hdlc, &buffer_for_ex, sizeof(buffer_for_ex), 0, 0);
|
||||||
printf("%d\n", i);
|
if (i < 0){
|
||||||
|
printf("err connect: %d\n", i);
|
||||||
|
}
|
||||||
|
|
||||||
hdlc_control_t frame_data;
|
hdlc_control_t frame_data;
|
||||||
uint8_t data = 003;
|
uint8_t data = 33;
|
||||||
send_data(&hdlc, &frame_data, &data, sizeof(data));
|
printf("%llu\n", sizeof(data));
|
||||||
uint8_t buffer_for_ex_data[128];
|
hdlc_send_data(&hdlc, &frame_data, &data, sizeof(data));
|
||||||
|
uint8_t buffer_for_ex_data[7];
|
||||||
|
|
||||||
hdlc_get_raw_frame(&hdlc, &frame_data, buffer_for_ex_data, sizeof(buffer_for_ex_data));
|
hdlc_get_raw_frame(&hdlc, &frame_data, &buffer_for_ex_data, sizeof(buffer_for_ex_data));
|
||||||
|
|
||||||
|
//printf("first_ex:%d\n", buffer_for_ex_data[0]);
|
||||||
|
uint8_t recived_data;
|
||||||
|
size_t len_recived_data;
|
||||||
|
int x = hdlc_decode_recived_raw_data(&hdlc, &buffer_for_ex_data, sizeof(buffer_for_ex_data), &recived_data, &len_recived_data);
|
||||||
|
if (x < 0){
|
||||||
|
printf("err send: %d\n", x);
|
||||||
|
}
|
||||||
|
printf("recived data: %d\n", recived_data);
|
||||||
|
|
||||||
i = hdlc_decode_recived_raw_data(&hdlc, buffer_for_ex_data, sizeof(buffer_for_ex_data));
|
|
||||||
printf("%d\n", i);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user