diff --git a/client.c b/client.c deleted file mode 100644 index a456c45..0000000 --- a/client.c +++ /dev/null @@ -1,125 +0,0 @@ -#include "client.h" - -#include -#include - -#define START_FLAG 0x7E -#define END_FLAG 0x7E - -void init_Client(Client* client, bool test_is_valid, uint8_t address) { - client->TEST_IS_VALID = test_is_valid; - client->address = address; - client->_send_sequence_number = 0; - client->poll_final = 1; - client->_receive_sequence_number = 0; -} - - -void connect(Client* client) { - UFrame u_frame; - init_UFrame(&u_frame, client->address, client->poll_final, "BP", NULL, 0); - - uint8_t result[256]; - create_frame(&u_frame.base, result); - - - // Wait for acknowledgment frame - bool flag = true; - uint8_t data; - while(flag){ - data = uart_read(); - if (data > 0){ - flag = false; - } - } - - HDLCFrame frame; - init_HDLCFrame(&frame, 0, 0, &data + 3, 256 - 6); - if (validate(&data, 256)) { - return; - } else { - // Connection failed - } -} - -void send(Client* client, uint8_t* data, size_t data_length) { - connect(client); - - IFrame i_frame; - init_IFrame(&i_frame, client->address, client->_receive_sequence_number, client->poll_final, client->_send_sequence_number, data, data_length); - - uint8_t result[256]; - create_frame(&i_frame.base, result); - - uart_send_byte(*i_frame.base.data); - - client->_send_sequence_number++; -} - -void receive_data(uint8_t* recivedData) { - bool flag = true; - while(flag){ - *recivedData = uart_read(); - if (recivedData > 0){ - flag = false; - } - } -} - -#include - -bool validate(const uint8_t* frame, size_t length) { - if (length < 4 || frame[0] != START_FLAG || frame[length - 1] != END_FLAG) { - // Invalid frame length or missing start/end flag - return false; - } - - uint16_t received_fcs = (frame[length - 3] << 8) | frame[length - 2]; - uint16_t calculated_fcs = 0xFFFF; - - for (size_t i = 1; i < length - 3; i++) { - uint8_t byte = frame[i]; - calculated_fcs = (calculated_fcs >> 8) ^ (calculated_fcs << 8) ^ byte; - calculated_fcs &= 0xFFFF; - } - - return received_fcs == calculated_fcs; -} - -int sendSerialData(const char* port, uint8_t* data, size_t length) { - int serial_port = open(port, O_RDWR); - - if (serial_port < 0) { - perror("Error opening the serial port"); - return -1; - } - - ssize_t bytes_written = write(serial_port, data, length); - - if (bytes_written < 0) { - perror("Error writing to the serial port"); - return -1; - } - - close(serial_port); - return bytes_written; -} - -int receiveSerialData(const char* port, uint8_t* data, int length) { - int serial_port = open(port, O_RDWR); // Replace "port" with your serial port device - - if (serial_port < 0) { - perror("Error opening the serial port"); - return -1; - } - - ssize_t bytes_read = read(serial_port, data, length); - - if (bytes_read < 0) { - perror("Error reading from the serial port"); - return -1; - } - - close(serial_port); - return bytes_read; -} \ No newline at end of file diff --git a/client.h b/client.h deleted file mode 100644 index 0a19d58..0000000 --- a/client.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef CLIENT_H -#define CLIENT_H - -#include "hdlc_frame.h" -#include "uart_module.h" -#include -#include // Для использования size_t -#include - -typedef struct { - bool TEST_IS_VALID; - uint8_t address; - uint8_t _send_sequence_number; - uint8_t poll_final; - uint8_t _receive_sequence_number; -} Client; - -void init_Client(Client* client, bool test_is_valid, uint8_t address); -void connect(Client* client); -void send(Client* client, uint8_t* data, size_t data_length); -void receive_data(uint8_t* recivedData); -bool validate(const uint8_t* frame, size_t length); - -#endif diff --git a/frame.c b/frame.c new file mode 100644 index 0000000..cef78fd --- /dev/null +++ b/frame.c @@ -0,0 +1,380 @@ +#include "frame.h" + +#include + +extern int window_length; +extern int raw_data_length; +extern int frame_number_range; + + +/* 33 crc*/ +short crc[] = {1, 0, 0, 0, 0, 0, + 1, 0, 0, 1, 1, 0, + 0, 0, 0, 0, 1, + 0, 0, 0, 1, 1, 1, + 0, 1, 1, 0, 1, 1, + 0, 1, 1, 1}; + + + +short send_receive_num[][3] = {{0, 0, 0}, + {0, 0, 1}, + {0, 1, 0}, + {0, 1, 1}, + {1, 0, 0}, + {1, 0, 1}, + {1, 1, 0}, + {1, 1, 1} +}; + +static Frame *fill_flag_and_fcs(Frame *p_frame) { + + short s[] = {0, 1, 1, 1, 1, 1, 1, 0}; + int i; + for (i = 0; i < 8; ++i) { + p_frame ->head_flag[i] = s[i]; + p_frame ->rail_flag[i] = s[i]; + } + for (i = 0; i < (sizeof(crc) / sizeof(short) - 1); ++i) { + p_frame ->fcs[i] = 0; + } + + return p_frame; +} + +static Frame *create_generic_unnumbered_frame(Frame *p_frame) { + + fill_flag_and_fcs(p_frame); + p_frame ->control[0] = 1; + p_frame ->control[1] = 1; + p_frame ->s.size = 0; + return p_frame; +} + +static Frame *create_generic_info_frame(Frame *p_frame) { + + fill_flag_and_fcs(p_frame); + p_frame ->control[0] = 0; + p_frame ->s.size = 0; + return p_frame; +} + +static Frame *create_generic_sup_frame(Frame *p_frame) { + + fill_flag_and_fcs(p_frame); + p_frame ->control[0] = 1; + p_frame ->control[1] = 0; + p_frame ->s.size = 0; + return p_frame; +} + + +static void convert_frame_to_seq(Frame *p_frame, short seq[], int *size) { + + int i; + (*size) = 0; + for (i = 0; i < 8; ++i) { + seq[*size] = p_frame ->address[i]; + ++(*size); + } + + for (i = 0; i < 8; ++i) { + seq[*size] = p_frame ->control[i]; + ++(*size); + } + + for (i = 0; i < p_frame ->s.size; ++i) { + if (1 == (p_frame ->s.infor_type[i])) { + seq[*size] = p_frame ->s.information[i]; + ++(*size); + } + } + for (i = 0; i < sizeof(crc) / sizeof(short) - 1; ++i) { + seq[*size] = p_frame ->fcs[i]; + ++(*size); + } +} +/* 0 has been filled before calling this method.*/ + +static int seq_fcs(short seq[], int size) { + + int i; + int non_zero = 0; + int temp_non_zero; + bool new_non_zero = 0; + + for (i = 0; i < size; ++i) { + if (1 == seq[i]) { + non_zero = i; + new_non_zero = 1; + break; + } + } + + for (;non_zero <= size - sizeof(crc)/sizeof(short);) { + new_non_zero = 0; + temp_non_zero = -1; + for (i = non_zero; i < non_zero + sizeof(crc)/sizeof(short); ++i) { + seq[i] ^= crc[i - non_zero]; + if ((1 == seq[i]) && !new_non_zero) { + temp_non_zero = i; + new_non_zero = 1; + } + } + + if (-1 == temp_non_zero) { + for (i = non_zero + sizeof(crc) / sizeof(short); i < size; ++i) { + if (1 == seq[i]) { + non_zero = i; + break; + } + } + if (i == size) return -1; + } else { + non_zero = temp_non_zero; + } + } + + return non_zero; +} + +/*Here add fcs to one frame*/ +static void add_fcs_to_frame(Frame *p_frame) { + + short seq[1024]; + int size = 0; + int i; + int non_zero = 0; + + convert_frame_to_seq(p_frame, seq, &size); + + seq_fcs(seq, size); + + for (i = 0; i < sizeof(crc)/sizeof(short) - 1; ++i) { + p_frame ->fcs[i] = seq[size - (sizeof(crc)/sizeof(short) - 1) + i]; + } +} + + +static void convert_decimal_2_binary(int decimal, short binary[]) { + + binary[2] = decimal % 2; + binary[0] = decimal / 4; + binary[1] = (decimal - binary[0] * 4) / 2; +} + +/*Here create SABME, UA and DISC.*/ +Frame create_unnumbered_frame(short addr[], enum FRAME_TYPE ft, bool p_f) { + + Frame f; + short s1[] = {1, 1, 0, 1, 1, 0};/* For SABME*/ + short s2[] = {0, 0, 0, 1, 1, 0};/* For UA*/ + short s3[] = {0, 0, 0, 0, 1, 0};/* For DISC*/ + int i; + + create_generic_unnumbered_frame(&f); + + for (i = 0; i < 8; ++i) { + f.address[i] = addr[i]; + } + + switch(ft) { + case SABME : + for (i = 2; i < 8; ++i) { + f.control[i] = s1[i - 2]; + } + break; + + case UA : + for (i = 2; i < 8; ++i) { + f.control[i] = s2[i - 2]; + } + break; + + case DISC : + for (i = 2; i < 8; ++i) { + f.control[i] = s3[i - 2]; + } + break; + default : fprintf(stderr, "Call create unnumbered frame error!\n"); + + break; + } + + f.control[4] = p_f; + + add_fcs_to_frame(&f); + return f; +} + +Frame create_sup_frame(short addr[], enum FRAME_TYPE ft, bool p_f, short want_receive) { + Frame f; + int i; + short next_receive[3]; + create_generic_sup_frame(&f); + + for (i = 0; i < 8; ++i) { + f.address[i] = addr[i]; + } + + f.control[2] = 0; + switch(ft) { + case RR : + f.control[3] = 0; + break; + case REJ : + f.control[3] = 1; + break; + default : + fprintf(stderr, "create sup frame error!\n"); + break; + } + f.control[4] = p_f; + + + convert_decimal_2_binary(want_receive, next_receive); + for (i = 5; i < 8; ++i) { + f.control[i] = next_receive[i - 5]; + } + + add_fcs_to_frame(&f); + return f; +} + + + +/* prequestic : ($end - $start) % $raw_data_length = 0*/ +Frame* create_infor_frames(short addr[], short send_n, bool p_f, short receive_n, + short data[], int start, int end, Frame *frames) { /*Normal data transferring.*/ + + Frame f; + int i; + int j; + int k; + int ite = 0; + short receive_number[3]; + short send_number[3]; + + if ((end -start + 1) > (850 * window_length)) { + fprintf(stderr, "One frame takes too long data!\n"); + return &f; + } + + convert_decimal_2_binary(receive_n, receive_number); + + for (ite = 0; ite < ((end - start) / raw_data_length); ++ite) { + + create_generic_info_frame(&f); + convert_decimal_2_binary(send_n, send_number); + for (i = 1; i < 4; ++i) { + + f.control[i] = send_number[i - 1]; + f.control[i + 4] = receive_number[i - 1]; + } + + send_n = (send_n + 1) % frame_number_range; + f.control[4] = p_f; + + j = 0; + k = 0; + + + /*bit stuffing!*/ + for (i = start + ite * raw_data_length; i < start + (ite + 1) * raw_data_length; ++i) { + + f.s.information[k] = data[i]; f.s.infor_type[k] = 1; ++k; ++f.s.size; + if (1 == data[i]) { + if (4 == (i - j)) { + f.s.information[k] = 0; f.s.infor_type[k] = 0; ++k; ++f.s.size; + j = i + 1; + } + } else { + j = i + 1; + } + } + + add_fcs_to_frame(&f); + frames[ite] = f; + } + return frames; +} + +short unnumbered[][8] = { + {1, 1, 1, 1, 0, 1, 1, 0}, /*SABME*/ + {1, 1, 0, 0, 0, 1, 1, 0}, /*UA*/ + {1, 1, 0, 0, 0, 0, 1, 0} /*DISC*/ }; + +enum FRAME_TYPE get_frame_type(Frame *p_frame) { + + if (0 == p_frame ->control[0]) { + return INFOR; + } + + if (0 == p_frame ->control[1]) { + if (0 == p_frame ->control[3]) return RR; + else return REJ; + } else { + if (1 == p_frame ->control[2]) return SABME; + else { + if (1 == p_frame ->control[5]) return UA; + else return DISC; + } + } + + fprintf(stdout, "Inteprete Frame type error!\n"); + return INFOR; +} + +void get_address(Frame *p_frame, short *storage) { + + int i; + for (i = 0; i < 8; ++i) { + storage[i] = p_frame ->address[i]; + } +} + +/* Only for INFOR, */ +short get_send_number(Frame *p_frame) { + + int num; + num = p_frame ->control[1] * 4 + p_frame ->control[2] * 2 + p_frame ->control[3]; + return num; +} +/* For INFOR, RR, REJ.*/ +short get_expect_number(Frame *p_frame) { + + int num; + num = p_frame ->control[5] * 4 + p_frame ->control[6] * 2 + p_frame ->control[7]; + return num; +} + +/* Only for INFOR.*/ +void get_infor(Frame *p_frame, short *storage, int *size) { + int i; + *size = 0; + + for (i = 0; i < p_frame ->s.size; ++i) { + if (1 == p_frame ->s.infor_type[i]) { + storage[*size] = p_frame ->s.information[i]; + ++(*size); + } + } +} + +/*Here using fcs to judge whether the data is right.*/ +bool is_fcs_right(Frame *p_frame) { + + short seq[1024]; + int size = 0; + int non_zero; + int i; + + convert_frame_to_seq(p_frame, seq, &size); + + non_zero = seq_fcs(seq, size); + if (-1 == non_zero) { + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/frame.h b/frame.h new file mode 100644 index 0000000..2fa3391 --- /dev/null +++ b/frame.h @@ -0,0 +1,57 @@ +#ifndef _FRAME_H +#define _FRAME_H + +typedef int bool; + +/*This is the frame structure in HDLC.*/ +typedef struct Frame { + + short head_flag[8]; + short address[8]; + short control[8]; + struct{ + + /*Actuall size should also be arry. It is also sequence of '01', here I simplify it.*/ + short size; /*Record the real length of the data.*/ + short information[1024];/*Capacity is 1024.*/ + short infor_type[1024]; /*0 : bit stuffing; 1 : real data.*/ + }s; + + short fcs[32]; + short rail_flag[8]; +}Frame; + +enum FRAME_TYPE {SABME, DISC, UA, REJ, RR, INFOR}; + +/* SABME, UA and DISC*/ +Frame create_unnumbered_frame(short addr[], enum FRAME_TYPE ft, bool p_f); + +/* REJ, RR*/ +Frame create_sup_frame(short addr[], enum FRAME_TYPE ft, bool p_f, short next_receive); + +/* Frame for normal data transferring.*/ +Frame* create_infor_frames(short addr[], short send_number, bool p_f, + short receive_number, short data[], int start, int end, Frame *frames);/* Using $start,$end, better than $size*/ + +/*Extract frame type from the given frame. */ +enum FRAME_TYPE get_frame_type(Frame *p_frame); + + +/* Extract address field from the given frame.*/ +void get_address(Frame *p_frame, short *storage); + +/* The # of the frame.*/ +short get_send_number(Frame *p_frame); + +/* The # of the frame sender wants.*/ +short get_expect_number(Frame *p_frame); + +/* Extract the real data from the given frame.*/ +void get_infor(Frame *p_frame, short *storage, int *size); + + +/*Here using fcs to judge whether the data is right.*/ +bool is_fcs_right(Frame *p_frame); + + +#endif \ No newline at end of file diff --git a/hdlc_frame.c b/hdlc_frame.c deleted file mode 100644 index e7965c6..0000000 --- a/hdlc_frame.c +++ /dev/null @@ -1,108 +0,0 @@ -#include "hdlc_frame.h" - -#include - -#define START_FLAG 0x7E -#define END_FLAG 0x7E -#define ESCAPE_FLAG 0x7D -#define ESCAPE_XOR 0x20 - -void init_HDLCFrame(HDLCFrame* frame, uint8_t address, uint8_t control, uint8_t* data, size_t data_length) { - frame->address = address; - frame->control = control; - frame->data = data; - frame->data_length = data_length; -} - -uint16_t calculate_fcs(const HDLCFrame *frame, size_t i) { - uint16_t fcs = 0xFFFF; - - uint8_t data_bytes[2 + frame->data_length]; - data_bytes[0] = frame->address; - data_bytes[1] = frame->control; - memcpy(data_bytes + 2, frame->data, frame->data_length); - - for (size_t i = 0; i < frame->data_length + 2; i++) { - uint8_t byte = data_bytes[i]; - fcs = (fcs >> 8) ^ (fcs << 8) ^ byte; - fcs &= 0xFFFF; - } - - return fcs; -} - -void create_frame(const HDLCFrame* frame, uint8_t* result) { - size_t index = 0; - - result[index++] = START_FLAG; - result[index++] = frame->address; - result[index++] = frame->control; - - if (frame->data != NULL) { - for (size_t i = 0; i < frame->data_length; i++) { - uint8_t byte = frame->data[i]; - if (byte == START_FLAG || byte == END_FLAG || byte == ESCAPE_FLAG) { - result[index++] = ESCAPE_FLAG; - result[index++] = byte ^ ESCAPE_XOR; - } else { - result[index++] = byte; - } - } - } - - uint16_t fcs = calculate_fcs(frame, 0); - result[index++] = fcs & 0xFF; - result[index++] = (fcs >> 8) & 0xFF; - result[index++] = END_FLAG; -} - -void init_IFrame(IFrame* frame, uint8_t address, uint8_t receive_sequence_number, uint8_t poll_final, - uint8_t send_sequence_number, uint8_t* data, size_t data_length) { - init_HDLCFrame(&frame->base, address, - ((receive_sequence_number & 0b111) << 6) | ((poll_final & 0b1) << 4) | - ((send_sequence_number & 0b111) << 1) | 0, - data, data_length); - frame->receive_sequence_number = receive_sequence_number; - frame->poll_final = poll_final; - frame->send_sequence_number = send_sequence_number; -} - -void init_SFrame(SFrame* frame, uint8_t address, uint8_t receive_sequence_number, uint8_t poll_final, - const char* frame_type) { - uint8_t frame_type_value; - if (strcmp(frame_type, "RR") == 0) { - frame_type_value = 0; - } else if (strcmp(frame_type, "RNR") == 0) { - frame_type_value = 1; - } else if (strcmp(frame_type, "REJ") == 0) { - frame_type_value = 2; - } else if (strcmp(frame_type, "SREJ") == 0) { - frame_type_value = 3; - } else { - // Handle error - return; - } - - init_HDLCFrame(&frame->base, address, - ((receive_sequence_number & 0b111) << 6) | ((poll_final & 0b1) << 5) | - ((frame_type_value & 0b111) << 2) | 1, - NULL, 0); - frame->receive_sequence_number = receive_sequence_number; - frame->poll_final = poll_final; - frame->frame_type = frame_type; -} - -void init_UFrame(UFrame* frame, uint8_t address, uint8_t poll_final, const char* frame_type, - uint8_t* data, size_t data_length) { - uint8_t frame_type_value; - if (strcmp(frame_type, "BP") == 0) { - frame_type_value = 63; - } else { - // Handle error - return; - } - - init_HDLCFrame(&frame->base, address, (frame_type_value << 2) | 3, data, data_length); - frame->poll_final = poll_final; - frame->frame_type = frame_type; -} diff --git a/hdlc_frame.h b/hdlc_frame.h deleted file mode 100644 index c12eaf5..0000000 --- a/hdlc_frame.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef HDLC_FRAME_H -#define HDLC_FRAME_H - -#include - -typedef struct { - uint8_t address; - uint8_t control; - uint8_t* data; - size_t data_length; -} HDLCFrame; - -void init_HDLCFrame(HDLCFrame* frame, uint8_t address, uint8_t control, uint8_t* data, size_t data_length); -uint16_t calculate_fcs(const HDLCFrame *frame, size_t i); -void create_frame(const HDLCFrame* frame, uint8_t* result); - -typedef struct { - HDLCFrame base; - uint8_t receive_sequence_number; - uint8_t poll_final; - uint8_t send_sequence_number; -} IFrame; - -void init_IFrame(IFrame* frame, uint8_t address, uint8_t receive_sequence_number, uint8_t poll_final, - uint8_t send_sequence_number, uint8_t* data, size_t data_length); - -typedef struct { - HDLCFrame base; - uint8_t receive_sequence_number; - uint8_t poll_final; - const char* frame_type; -} SFrame; - -void init_SFrame(SFrame* frame, uint8_t address, uint8_t receive_sequence_number, uint8_t poll_final, - const char* frame_type); - -typedef struct { - HDLCFrame base; - uint8_t poll_final; - const char* frame_type; -} UFrame; - -void init_UFrame(UFrame* frame, uint8_t address, uint8_t poll_final, const char* frame_type, uint8_t* data, size_t data_length); - -#endif diff --git a/main.c b/main.c deleted file mode 100644 index 93e2b83..0000000 --- a/main.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include "config.h" -#include -#include -#include -#include "uart_hal.h" - - -int main(void) -{ - //example - uint8_t data = 'A'; - - uart_init(9600,0); - uart_send_byte(data); - sei(); - while (1) - { - if(uart_read_count() > 0){ - data = uart_read(); - uart_send_byte(data); - } - - } -} \ No newline at end of file diff --git a/uart_module.c b/uart_module.c deleted file mode 100644 index c4cbc2a..0000000 --- a/uart_module.c +++ /dev/null @@ -1,82 +0,0 @@ -#include "uart_hal.h" - -volatile static uint8_t rx_buffer[RX_BUFFER_SIZE] = {0}; -volatile static uint16_t rx_count = 0; -volatile static uint8_t uart_tx_busy = 1; -// кольцевой буффер -ISR(USART_RX_vect){ - - volatile static uint16_t rx_write_pos = 0; - - rx_buffer[rx_write_pos] = UDR0; - rx_count++; - rx_write_pos++; - if(rx_write_pos >= RX_BUFFER_SIZE){ - rx_write_pos = 0; - } - -} - - -ISR(USART_TX_vect){ - uart_tx_busy = 1; -} - - -void uart_init(uint32_t baud,uint8_t high_speed){ - - uint8_t speed = 16; - - if(high_speed != 0){ - speed = 8; - UCSR0A |= 1 << U2X0; - } - - baud = (F_CPU/(speed*baud)) - 1; - - UBRR0H = (baud & 0x0F00) >> 8; - UBRR0L = (baud & 0x00FF); - - UCSR0B |= (1 << TXEN0) | (1 << RXEN0) | (1 << TXCIE0) | (1 << RXCIE0); - -} - - -void uart_send_byte(uint8_t c){ - while(uart_tx_busy == 0); - uart_tx_busy = 0; - UDR0 = c; -} - -void uart_send_array(uint8_t *c,uint16_t len){ - for(uint16_t i = 0; i < len;i++){ - uart_send_byte(c[i]); - } -} - -void uart_send_string(uint8_t *c){ - uint16_t i = 0; - do{ - uart_send_byte(c[i]); - i++; - - }while(c[i] != '\0'); - uart_send_byte(c[i]); -} - -uint16_t uart_read_count(void){ - return rx_count; -} - -uint8_t uart_read(void){ - static uint16_t rx_read_pos = 0; - uint8_t data = 0; - - data = rx_buffer[rx_read_pos]; - rx_read_pos++; - rx_count--; - if(rx_read_pos >= RX_BUFFER_SIZE){ - rx_read_pos = 0; - } - return data; -} \ No newline at end of file diff --git a/uart_module.h b/uart_module.h deleted file mode 100644 index 284a941..0000000 --- a/uart_module.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef UART_HAL_H_ -#define UART_HAL_H_ - - -#include -#include "config.h" -#include -#include -#include - -#define RX_BUFFER_SIZE 128 - -void uart_init(uint32_t baud,uint8_t high_speed); -void uart_send_byte(uint8_t c); -void uart_send_array(uint8_t *c,uint16_t len); -void uart_send_string(uint8_t *c); -uint16_t uart_read_count(void); -uint8_t uart_read(void); - -#endif /* UART_HAL_H_ */ \ No newline at end of file