Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Improve network module
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Sep 18, 2023
1 parent 0d1f728 commit dc136e7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 219 deletions.
20 changes: 10 additions & 10 deletions lib/Xila/include/Network/IP_Address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,45 @@ namespace Xila_Namespace

/// @brief Constructor to choose between IPv4 or IPv6 address.
/// @param Is_IPv4 : true for IPv4, false for IPv6.
IP_Address_Class(bool Is_IPv4);
explicit IP_Address_Class(bool Is_IPv4);

/// @brief Constructor to create an IP v4 address.
/// @param Address : 32 bits integer representing the IP address.
IP_Address_Class(uint32_t Address);
explicit IP_Address_Class(uint32_t Address);

/// @brief Constructor to create an IP v4 or v6 address.
/// @param Address : Pointer to an array of 4 or 16 bytes representing the IP address.
IP_Address_Class(const uint8_t *Address, bool Is_IPv4 = true);
explicit IP_Address_Class(const uint8_t *Address, bool Is_IPv4 = true);

/// @brief Constructor to create an IP v4 or v6 address from a char array.
/// @param Address : Pointer to a char array representing the IP address.
IP_Address_Class(const char *Address);
explicit IP_Address_Class(const String_Type& Address);

/// @brief Constructor to create an IP v4 address.
/// @param Byte_1 First byte of the IP address.
/// @param Byte_2 Second byte of the IP address.
/// @param Byte_3 Third byte of the IP address.
/// @param Byte_4 Fourth byte of the IP address.
IP_Address_Class(uint8_t Byte_1, uint8_t Byte_2, uint8_t Byte_3, uint8_t Byte_4);
explicit IP_Address_Class(uint8_t Byte_1, uint8_t Byte_2, uint8_t Byte_3, uint8_t Byte_4);

/// @brief Constructor to create from an Arduino IPAddress object.
/// @param IP_Address : IPAddress object.
IP_Address_Class(const IPAddress &IP_Address);
explicit IP_Address_Class(const IPAddress &IP_Address);

/// @brief Constructor to create from an Arduino IPv6Address object.
/// @param IP_Address : IPv6Address object.
IP_Address_Class(const IPv6Address &IP_Address);
explicit IP_Address_Class(const IPv6Address &IP_Address);

// - - Operators

/// @brief Conversion operator to a 32 bits integer.
operator uint32_t() const;
explicit operator uint32_t() const;

/// @brief Conversion operator to an Arduino IPAddress object.
operator IPAddress() const;
explicit operator IPAddress() const;

/// @brief Conversion operator to an Arduino IPv6Address object.
operator IPv6Address() const;
explicit operator IPv6Address() const;

/// @brief Comparison operator.
/// @param IP_Address : IP address to compare to.
Expand Down
5 changes: 4 additions & 1 deletion lib/Xila/include/Network/Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ namespace Xila_Namespace
virtual IP_Address_Type Get_Gateway_IP_Address() = 0;
virtual IP_Address_Type Get_Subnet_Mask() = 0;
virtual IP_Address_Type Get_DNS_IP_Address(Natural_Type Index) = 0;
virtual IP_Address_Type Get_Broadcast_IP_Address() = 0;
virtual Byte_Type Get_Subnet_CIDR() = 0;
virtual bool Is_IPv6() = 0;
virtual bool Is_IP_v6() = 0;

virtual Client_Type& Create_Client() = 0;

Expand All @@ -84,6 +85,8 @@ namespace Xila_Namespace
/// @return `Result_Type::Success` if the network configuration has been set, `Result_Type::Error` otherwise.
virtual Result_Type Set_Configuration(IP_Address_Type IP_Address, IP_Address_Type Subnet_Mask, IP_Address_Type Gateway, IP_Address_Type DNS_1_IP_Address = static_cast<uint32_t>(0x00000000), IP_Address_Type DNS_2_IP_Address = static_cast<uint32_t>(0x00000000)) = 0;

virtual Result_Type Set_IP_v6(bool IP_v6) = 0;

/// @brief Set the host name of the device over the interface.
/// @param Host_Name
/// @return Result_Type `Result_Type::Success` if the host name has been setted successfully.
Expand Down
92 changes: 12 additions & 80 deletions lib/Xila/include/Network/WiFi/Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,24 @@ namespace Xila_Namespace
/// @return `Result_Type::Success` if the module has been stopped, `Result_Type::Error` otherwise.
Result_Type Stop() override;

State_Type Get_State() override;
Client_Type& Create_Client() override;

State_Type Get_State() override;
IP_Address_Type Get_IP_Address(bool IPv6 = false) override;
IP_Address_Type Get_Gateway_IP_Address() override;
IP_Address_Type Get_Subnet_Mask() override;
IP_Address_Type Get_DNS_IP_Address(Natural_Type Index) override;
IP_Address_Type Get_Broadcast_IP_Address() override;
Byte_Type Get_Subnet_CIDR() override;
bool Is_IPv6() override;
bool Is_IP_v6() override;
String_Type &Get_Host_Name(String_Type &Host_Name) override;

Client_Type& Create_Client() override;

void Set_State(State_Type State) override;

Result_Type Set_Configuration(IP_Address_Type IP_Address, IP_Address_Type Subnet_Mask, IP_Address_Type Gateway, IP_Address_Type DNS_1_IP_Address = static_cast<uint32_t>(0x00000000), IP_Address_Type DNS_2_IP_Address = static_cast<uint32_t>(0x00000000)) override;
Result_Type Set_Host_Name(const char *Host_Name) override;
Result_Type Set_IP_v6(bool IP_v6) override;

String_Type &Get_Host_Name(String_Type &Host_Name) override;
Result_Type Set_Host_Name(const char *Host_Name);

// - - Managment

Expand Down Expand Up @@ -233,46 +234,11 @@ namespace Xila_Namespace
/// @param MAC_Address Pointer to a `uint8_t` array of 6 bytes where the MAC address will be stored.
uint8_t *Get_MAC_Address(uint8_t *MAC_Address);

/// @brief Get the IP address.
/// @param IPv6 `true` to get the IPv6 address, `false` to get the IPv4 address.
IP_Address_Type Get_IP_Address(bool IPv6 = false);

/// @brief Get the subnet mask.
/// @return Subnet mask.
IP_Address_Type Get_Subnet_Mask();

/// @brief Get the gateway IP address.
/// @return Gateway IP address.
IP_Address_Type Get_Gateway_IP_Address();

/// @brief Get the DNS IP address.
/// @param Index Index of the DNS IP address to get.
/// @return DNS IP address.
IP_Address_Type Get_DNS_IP_Address(uint8_t Index);

/// @brief Get the broadcast IP address.
/// @return Broadcast IP address.
IP_Address_Type Get_Broadcast_IP_Address();

IP_Address_Type Get_Network_ID();

/// @brief Get the subnet CIDR.
/// @return Subnet CIDR.
uint8_t Get_Subnet_CIDR();


// - - Setters

// - - - Network configuration

/// @brief Set the network configuration.
/// @param IP_Address IP address.
/// @param Subnet_Mask Subnet mask.
/// @param Gateway Gateway IP address.
/// @param DNS_1_IP_Address First DNS IP address.
/// @param DNS_2_IP_Address Second DNS IP address.
/// @return `Result_Type::Success` if the network configuration has been set, `Result_Type::Error` otherwise.
Result_Type Set_Configuration(IP_Address_Type IP_Address, IP_Address_Type Subnet_Mask, IP_Address_Type Gateway, IP_Address_Type DNS_1_IP_Address = static_cast<uint32_t>(0x00000000), IP_Address_Type DNS_2_IP_Address = static_cast<uint32_t>(0x00000000));

/// @brief Set the automatic reconnection mode.
/// @param Enable `true` to enable the automatic reconnection mode, `false` to disable it.
/// @return `Result_Type::Success` if the automatic reconnection mode has been set, `Result_Type::Error` otherwise.
Expand All @@ -289,11 +255,6 @@ namespace Xila_Namespace
private:
// - Attributes

/// @brief Network IP version.
bool IP_v6;

IP_Address_Type IP_Address, Subnet_Mask, Gateway_IP_Address, DNS_1_IP_Address, DNS_2_IP_Address;

friend class WiFi_Class;
} Station;

Expand Down Expand Up @@ -345,61 +306,30 @@ namespace Xila_Namespace
/// @return Current number of stations connected to the access point.
uint8_t Get_Station_Number();

/// @brief Get the IP address of the access point.
/// @return IP address of the access point.
IP_Address_Type Get_IP_Address();

/// @brief Get the subnet mask of the access point.
/// @return Subnet mask of the access point.
IP_Address_Type Get_Subnet_Mask();

/// @brief Get the gateway IP address of the access point.
/// @return Gateway IP address of the access point.
IP_Address_Type Get_Gateway_IP_Address();

/// @brief Get the DHCP lease start IP address.
/// @return DHCP lease start IP address.
IP_Address_Type Get_DHCP_Start_IP_Address();

/// @brief Get the broadcast IP address.
/// @return Broadcast IP address.
IP_Address_Type Get_Broadcast_IP_Address();

/// @brief Get the network ID.
/// @return Network ID.
IP_Address_Type Get_Network_ID();

/// @brief Get the subnet CIDR.
/// @return Subnet CIDR.
uint8_t Get_Subnet_CIDR();

// - - Setters

/// @brief Set the use of the IP v6 protocol.
/// @param Enable `true` to enable IP v6 protocol, `false` to disable it.
Result_Type Set_IP_v6(bool Enable = true);

/// @brief Get the MAC address of the access point.
/// @param MAC_Address A pointer to an array of 6 byte where the MAC address will be stored.
/// @return Pointer to the `uint8_t` array containing the MAC address.
uint8_t *Get_MAC_Address(uint8_t *MAC_Address);

/// @brief Set the network configuration of the access point.
/// @param Local_IP Local IP address.
/// @param Gateway Gateway IP address.
/// @param Subnet Subnet mask.
/// @param DHCP_Lease_Start_IP_Address DHCP lease start IP address.
/// @return `Result_Type::Success` if the network configuration has been set, `Result_Type::Error` otherwise.
Result_Type Set_Configuration(IP_Address_Type Local_IP, IP_Address_Type Gateway, IP_Address_Type Subnet, IP_Address_Type DHCP_Lease_Start_IP_Address = static_cast<uint32_t>(0));


private:
bool IP_v6;
Static_String_Type<64> Password;
int32_t Channel;
bool Hidden;
uint8_t Maximum_Stations;

IP_Address_Type IP_Address, Subnet_Mask, Gateway_IP_Address, DHCP_Lease_Start_IP_Address;
IP_Address_Type DHCP_Lease_Start_IP_Address;

friend class WiFi_Class;

Expand Down Expand Up @@ -483,6 +413,8 @@ namespace Xila_Namespace
Result_Type Save_Registry();

bool Long_Range;
bool Is_IP_v6;
IP_Address_Type IP_Address, Subnet_Mask, Gateway_IP_Address, DNS_IP_Address[2];

} WiFi_Interface_Type;

Expand Down
70 changes: 9 additions & 61 deletions lib/Xila/src/Network/IP_Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "Network/IP_Address.hpp"

#include "arpa/inet.h"

#include <WiFi.h>

using namespace Xila_Namespace;
Expand Down Expand Up @@ -43,70 +45,16 @@ IP_Address_Class::IP_Address_Class(const uint8_t *Address, bool Is_IPv4)
}
}

IP_Address_Class::IP_Address_Class(const char* Address)
IP_Address_Class::IP_Address_Class(const String_Type& Address) : IP_Address_Class()
{

uint8_t Index = 0;

bool Is_IPv4 = true;

{
uint8_t Separators = 0;

while (*Address != '\0')
{
if (*Address == ':' or *Address == '.')
{
Separators++;
}
Address++;
}

if (Separators == 3)
{
this->Is_IPv4 = true;
}
else if (Separators == 7)
{
this->Is_IPv4 = false;
}
else
{
return;
}
}


while (Address != '\0' and Index < sizeof(this->Address.Bytes))
{
char Character = tolower(*Address);
if ((Character >= '9' and Character <= '0') or (Character >= 'a' and Character <= 'f'))
{
switch (Character)
{
case '0':
Address[Index] = 0x0;
break;
case '1':
Address[Index] = 0x1;
break;
case '2':
Address[Index] = 2;
break;
case '3':
Address[Index] = 3;
break;

default:
break;
}
}
Address++;
}

if (inet_pton(AF_INET6, static_cast<const char*>(Address), &this->Address.Bytes) == 1)
this->Is_IPv4 = false;
else if (inet_pton(AF_INET, static_cast<const char*>(Address), &this->Address.Bytes) == 1)
this->Is_IPv4 = true;
else
memset(this->Address.Bytes, 0, sizeof(this->Address.Bytes));
}


IP_Address_Class::IP_Address_Class(uint8_t Byte_1, uint8_t Byte_2, uint8_t Byte_3, uint8_t Byte_4)
{
Address.Bytes[0] = Byte_1;
Expand Down
Loading

0 comments on commit dc136e7

Please sign in to comment.