It remains to complete the validation of frames

This commit is contained in:
Qukich 2023-06-22 13:11:57 +03:00
parent eceecf15d4
commit 8e4329c9d3
2 changed files with 44 additions and 27 deletions

View File

@ -2,7 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port) { void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port) {
client->TEST_IS_VALID = test_is_valid; client->TEST_IS_VALID = test_is_valid;
@ -22,24 +21,21 @@ void connect(Client* client) {
create_frame(&u_frame.base, result); create_frame(&u_frame.base, result);
void* serial_port = client->serial_port; void* serial_port = client->serial_port;
// Write result to serial port to establish connection sendSerialData(serial_port, u_frame.base.data, u_frame.base.data_length);
// ...
// Wait for acknowledgment frame // Wait for acknowledgment frame
// Read bytes from serial port 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;
}
}
uint8_t received[256]; // Example buffer size, adjust as needed
size_t received_length = 0;
// Process received bytes
// ...
// Validate acknowledgment frame
HDLCFrame frame; HDLCFrame frame;
init_HDLCFrame(&frame, 0, 0, received + 3, received_length - 6); init_HDLCFrame(&frame, 0, 0, data + 3, 256 - 6);
if (validate(client, &frame, received, received_length)) { if (validate(client, &frame, data, 256)) {
// Connection established return;
} else { } else {
// Connection failed // Connection failed
} }
@ -54,19 +50,20 @@ void send(Client* client, uint8_t* data, size_t data_length) {
uint8_t result[256]; uint8_t result[256];
create_frame(&i_frame.base, result); create_frame(&i_frame.base, result);
void* serial_port = client->serial_port; sendSerialData(client->serial_port, i_frame.base.data, i_frame.base.data_length);
sendSerialData(client->serial_port, &i_frame.base.data, i_frame.base.data_length);
client->_send_sequence_number++; client->_send_sequence_number++;
} }
void receive_data(Client* client) { void receive_data(Client* client, uint8_t* recivedData, int lenRecived) {
void* serial_port = client->serial_port; void* serial_port = client->serial_port;
// Read data from serial port bool flag = true;
// ... while(flag){
int bytes_received = receiveSerialData(serial_port, recivedData, lenRecived);
// Process received data if (bytes_received > 0){
// ... flag = false;
}
}
} }
bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t received_length) { bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t received_length) {
@ -93,7 +90,7 @@ bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t r
return false; return false;
} }
int sendSerialData(const char* port, const char* data, size_t length) { 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 int serial_port = open(port, O_RDWR); // Replace "port" with your serial port device
if (serial_port < 0) { if (serial_port < 0) {
@ -111,3 +108,22 @@ int sendSerialData(const char* port, const char* data, size_t length) {
close(serial_port); close(serial_port);
return bytes_written; 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;
}

View File

@ -22,8 +22,9 @@ typedef struct {
void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port); void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port);
void connect(Client* client); void connect(Client* client);
void send(Client* client, uint8_t* data, size_t data_length); void send(Client* client, uint8_t* data, size_t data_length);
void receive_data(Client* client); void receive_data(Client* client, uint8_t* recivedData, int lenRecived);
bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t received_length); bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t received_length);
int sendSerialData(const char* port, const char* data, size_t length); int sendSerialData(const char* port, uint8_t* data, size_t length);
int receiveSerialData(const char* port, uint8_t* data, int length);
#endif #endif