Display_Avr_3/hdlc/client.c

129 lines
3.7 KiB
C

#include "client.h"
#include <stdio.h>
#include <fcntl.h>
void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port) {
client->TEST_IS_VALID = test_is_valid;
client->address = address;
client->_send_sequence_number = 0;
client->poll_final = 1;
client->_receive_sequence_number = 0;
client->serial_port = serial_port;
}
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);
void* serial_port = client->serial_port;
sendSerialData(serial_port, u_frame.base.data, u_frame.base.data_length);
// Wait for acknowledgment frame
bool flag = true;
uint8_t data[u_frame.base.data_length];
while(flag){
int bytes_received = receiveSerialData(serial_port, data, 256);
if (bytes_received > 0){
flag = false;
}
}
HDLCFrame frame;
init_HDLCFrame(&frame, 0, 0, data + 3, 256 - 6);
if (validate(client, &frame, 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);
sendSerialData(client->serial_port, i_frame.base.data, i_frame.base.data_length);
client->_send_sequence_number++;
}
void receive_data(Client* client, uint8_t* recivedData, int lenRecived) {
void* serial_port = client->serial_port;
bool flag = true;
while(flag){
int bytes_received = receiveSerialData(serial_port, recivedData, lenRecived);
if (bytes_received > 0){
flag = false;
}
}
}
bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t received_length) {
uint8_t control = received_data[2];
if ((control & 0x01) == 0) {
// I-frame
printf("i frame\n");
uint8_t send_sequence_number = (control >> 1) & 0x07;
uint16_t crc = (received_data[received_length - 2] << 8) | received_data[received_length - 3];
uint16_t calculated_crc = calculate_fcs(received_data + 1, received_length - 4);
if (crc == calculated_crc && send_sequence_number == client->_receive_sequence_number && client->TEST_IS_VALID) {
client->_receive_sequence_number++;
return true;
}
} else if ((control & 0x03) == 1) {
// S-frame
printf("s frame\n");
// Handle S-frame
} else {
// U-frame
printf("u frame\n");
// Handle U-frame
}
return false;
}
int sendSerialData(const char* port, uint8_t* data, size_t 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_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;
}