diff --git a/hdlc/client.c b/hdlc/client.c index 62a53b9..a4307af 100644 --- a/hdlc/client.c +++ b/hdlc/client.c @@ -2,7 +2,6 @@ #include #include -#include void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port) { client->TEST_IS_VALID = test_is_valid; @@ -22,24 +21,21 @@ void connect(Client* client) { create_frame(&u_frame.base, result); 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 - // 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; - init_HDLCFrame(&frame, 0, 0, received + 3, received_length - 6); - if (validate(client, &frame, received, received_length)) { - // Connection established + init_HDLCFrame(&frame, 0, 0, data + 3, 256 - 6); + if (validate(client, &frame, data, 256)) { + return; } else { // Connection failed } @@ -54,19 +50,20 @@ void send(Client* client, uint8_t* data, size_t data_length) { uint8_t result[256]; 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++; } -void receive_data(Client* client) { +void receive_data(Client* client, uint8_t* recivedData, int lenRecived) { void* serial_port = client->serial_port; - // Read data from serial port - // ... - - // Process received data - // ... + 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) { @@ -93,7 +90,7 @@ bool validate(Client* client, HDLCFrame* frame, uint8_t* received_data, size_t r 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 if (serial_port < 0) { @@ -110,4 +107,23 @@ int sendSerialData(const char* port, const char* data, size_t length) { 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/hdlc/client.h b/hdlc/client.h index 6b48c0a..ca306cb 100644 --- a/hdlc/client.h +++ b/hdlc/client.h @@ -22,8 +22,9 @@ typedef struct { void init_Client(Client* client, bool test_is_valid, uint8_t address, void* serial_port); void connect(Client* client); 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); -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