Compare commits
	
		
			No commits in common. "0f99988c3e75360ab5fb19775c8ffd6508c1ca5d" and "9d42cbee7f6a64e26e78339cdb82fe9fbe0c2d22" have entirely different histories.
		
	
	
		
			0f99988c3e
			...
			9d42cbee7f
		
	
		
| @ -19,29 +19,36 @@ void init_hdlc_client(struct Client* client, int connecting_frame_timeout){ | ||||
|     client->connecting_frame_timeout = connecting_frame_timeout; | ||||
|     connecting_frame_timeout_bf = connecting_frame_timeout; | ||||
| 
 | ||||
| //    client->current_state_hdlc.control_escape = 0;
 | ||||
| //    client->current_state_hdlc.fcs = FCS_INIT_VALUE;
 | ||||
| //    client->current_state_hdlc.start_index = -1;
 | ||||
| //    client->current_state_hdlc.end_index = -1;
 | ||||
| //    client->current_state_hdlc.src_index = 0;
 | ||||
| //    client->current_state_hdlc.dest_index = 0;
 | ||||
|     client->current_state_hdlc.control_escape = 0; | ||||
|     client->current_state_hdlc.fcs = FCS_INIT_VALUE; | ||||
|     client->current_state_hdlc.start_index = -1; | ||||
|     client->current_state_hdlc.end_index = -1; | ||||
|     client->current_state_hdlc.src_index = 0; | ||||
|     client->current_state_hdlc.dest_index = 0; | ||||
| } | ||||
| 
 | ||||
| void hdlc_connect(struct Client* client, hdlc_control_t* frame){ | ||||
| void 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; | ||||
| 
 | ||||
|     *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 hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len){ | ||||
| int send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, size_t data_len){ | ||||
|     if (client->state != READY_STATE){ | ||||
|         return ERR_INVALID_STATE; | ||||
|     } | ||||
| @ -52,11 +59,6 @@ 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; | ||||
| @ -65,11 +67,10 @@ int hdlc_send_data(struct Client* client, hdlc_control_t* frame, uint8_t* data, | ||||
|     *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 hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t buffer[], size_t lenBuffer) { | ||||
|     if (frame->frame = S_FRAME){ | ||||
|         int ret = hdlc_frame_data(frame, NULL, 0, buffer, &lenBuffer); | ||||
|         if (ret < 0){ | ||||
|             printf("err in get_frame: %d\n", ret); | ||||
| @ -85,11 +86,11 @@ int hdlc_get_raw_frame(struct Client *client, hdlc_control_t* frame, uint8_t* bu | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| 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_decode_recived_raw_data(struct Client* client, uint8_t buffer[], size_t len_buffer){ | ||||
|     hdlc_control_t recv_control; | ||||
|     uint8_t recive[len_buffer]; | ||||
| 
 | ||||
|     int ret = hdlc_get_data(&recv_control, buffer, len_buffer, &recive, | ||||
|     int ret = hdlc_get_data_with_state(&client->current_state_hdlc, &recv_control, buffer, len_buffer, recive, | ||||
|                             &len_buffer); | ||||
| 
 | ||||
|     if (ret < 0) { | ||||
| @ -101,14 +102,12 @@ int hdlc_decode_recived_raw_data(struct Client* client, uint8_t* buffer, size_t | ||||
|             client->state = READY_STATE; | ||||
|             break; | ||||
|         case I_FRAME: | ||||
|             *recived_data = buffer[3]; | ||||
|             *len_recived_data = sizeof(buffer[3]); | ||||
|             break; | ||||
|         case S_FRAME_NACK: | ||||
|             client->state = DISCONNECTING; | ||||
|             client->frame_rej.seq_no = 0; | ||||
|             client->frame_rej.frame = S_FRAME_NACK; | ||||
|             return ERR_INVALID_SEQ_NUMBER_FRAME; | ||||
|             break; | ||||
|     } | ||||
| 
 | ||||
|     client->connecting_frame_timeout = connecting_frame_timeout_bf; | ||||
|  | ||||
| @ -14,6 +14,8 @@ enum HDLCState { | ||||
|     RECIVING // состояние приема и отправки
 | ||||
| }; | ||||
| 
 | ||||
| //
 | ||||
| 
 | ||||
| struct Client{ | ||||
|     enum HDLCState state; | ||||
|     int connecting_frame_timeout; //-1
 | ||||
| @ -29,11 +31,11 @@ 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 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_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, uint8_t* recived_data, size_t* len_recived_data); | ||||
| int hdlc_decode_recived_raw_data(struct Client* client, uint8_t buffer[], size_t len_buffer); | ||||
| int hdlc_timeout_handler(struct Client* client, int delta_time); | ||||
| 
 | ||||
| #endif //CLIENT_H
 | ||||
|  | ||||
							
								
								
									
										18
									
								
								hdlc/hdlc.h
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								hdlc/hdlc.h
									
									
									
									
									
								
							| @ -43,6 +43,24 @@ typedef struct { | ||||
|     int dest_index; | ||||
| } 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 | ||||
|  * parsed from multiple buffers e.g. when received via UART. | ||||
|  | ||||
							
								
								
									
										43
									
								
								hdlc/main.c
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								hdlc/main.c
									
									
									
									
									
								
							| @ -1,44 +1,39 @@ | ||||
| //#include "hdlc.h"
 | ||||
| #include "stdio.h" | ||||
| #include "client.h" | ||||
| #include <stdlib.h> | ||||
| #include <inttypes.h> | ||||
| 
 | ||||
| int main(){ | ||||
|     struct Client hdlc; | ||||
|     init_hdlc_client(&hdlc, 200); | ||||
|     hdlc_control_t frame; | ||||
|     hdlc_connect(&hdlc, &frame); | ||||
|     uint8_t buffer_for_ex[20]; | ||||
|     uint8_t fake_buffer; | ||||
|     hdlc_get_raw_frame(&hdlc, &frame, &buffer_for_ex, sizeof(buffer_for_ex)); | ||||
|     connect(&hdlc, &frame); | ||||
|     uint8_t buffer_for_ex[25]; | ||||
|     uint8_t fake_buffer[2]; | ||||
|     hdlc_get_raw_frame(&hdlc, &frame, buffer_for_ex, sizeof(buffer_for_ex)); | ||||
| 
 | ||||
|     for (int i = 0; i < 200; i++){ | ||||
|         int z = hdlc_timeout_handler(&hdlc, 1); | ||||
|         hdlc_decode_recived_raw_data(&hdlc, &fake_buffer, sizeof(fake_buffer), 0, 0); | ||||
|         if (z < 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)); | ||||
|     int i = hdlc_decode_recived_raw_data(&hdlc, &buffer_for_ex, sizeof(buffer_for_ex), 0, 0); | ||||
|     if (i < 0){ | ||||
|         printf("err connect: %d\n", i); | ||||
|     } | ||||
|     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)); | ||||
|     printf("%d\n", i); | ||||
| 
 | ||||
|     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]; | ||||
|     uint8_t data = 003; | ||||
|     send_data(&hdlc, &frame_data, &data, sizeof(data)); | ||||
|     uint8_t buffer_for_ex_data[128]; | ||||
| 
 | ||||
|     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); | ||||
|     hdlc_get_raw_frame(&hdlc, &frame_data, buffer_for_ex_data, sizeof(buffer_for_ex_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