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

Commit

Permalink
Improving network abstraction layer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Sep 9, 2023
1 parent e1a032b commit 206428b
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 120 deletions.
8 changes: 1 addition & 7 deletions lib/Xila/include/Network/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Xila_Namespace

// - - Constructors / destructors

Client_Class();
Client_Class() = default;

virtual Result_Type Connect(const char* Host, Word_Type Port, int32_t Timeout = 30000) = 0;

Expand Down Expand Up @@ -52,12 +52,6 @@ namespace Xila_Namespace

virtual operator bool() = 0;

virtual bool operator==(Client_Class& Client) = 0;
virtual bool operator!=(Client_Class& Client) = 0;

protected:

void* Data;
} Client_Type;
}

Expand Down
12 changes: 8 additions & 4 deletions lib/Xila/include/Network/HTTP_Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

#ifdef Xila_WiFi_Hardware_ESP32
#include "HTTPClient.h"
#include "WiFi_Client.hpp"
#endif

#include "Client.hpp"

namespace Xila_Namespace
{
namespace Communication_Types
namespace Network_Types
{
enum class HTTP_Code_Type
{
Expand Down Expand Up @@ -91,7 +92,7 @@ namespace Xila_Namespace

// - - Operations

Result_Type Begin(WiFi_Client_Type &Client, const char *Host, uint16_t Port, const char *URI = NULL, bool HTTPS = false);
Result_Type Begin(Client_Type &Client, const char *Host, uint16_t Port, const char *URI = NULL, bool HTTPS = false);

void End();

Expand Down Expand Up @@ -139,7 +140,10 @@ namespace Xila_Namespace

protected:

void* Data;
#ifdef Xila_WiFi_Hardware_ESP32
HTTPClient Client;
#endif

} HTTPS_Client_Type;
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Xila/include/Network/Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace Xila_Namespace
// - - Constructor / Destructor

Interface_Class();
~Interface_Class();
virtual ~Interface_Class();

// - -

Expand All @@ -62,10 +62,11 @@ namespace Xila_Namespace

virtual IP_Address_Type Get_IP_Address(bool IPv6 = false) = 0;
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 Byte_Type Get_Subnet_CIDR() = 0;

virtual Client_Class Create_Client() = 0;
virtual Client_Type& Create_Client() = 0;



Expand All @@ -80,7 +81,7 @@ namespace Xila_Namespace
/// @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));
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;


friend class Xila_Namespace::Network_Class;
Expand Down
13 changes: 11 additions & 2 deletions lib/Xila/include/Network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#define Xila_Network_Hpp_Included

#include "IP_Address.hpp"
#include "WiFi/WiFi.hpp"
#include "./WiFi/Client.hpp"
#include "./WiFi/Interface.hpp"
#include "HTTP_Client.hpp"

#include "Interface.hpp"
Expand Down Expand Up @@ -42,11 +43,19 @@ namespace Xila_Namespace
/// @return
Network_Types::Interface_Type* Get_Interface(Natural_Type Index = 0);

/// @brief
/// @brief
///
Natural_Type Get_Interface_Count();

/// @brief Get interface from the connected one's.
/// @param Index
/// @return
Network_Types::Interface_Type* Get_Connected_Interface(Natural_Type Index = 0);

/// @brief Get the count of the connected interfaces.
/// @return Count of the connected interface.
Natural_Type Get_Connected_Interface_Count();


} Network_Type;

Expand Down
15 changes: 13 additions & 2 deletions lib/Xila/include/Network/WiFi/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include "../Client.hpp"

#ifdef Xila_WiFi_Hardware_ESP32
#include "WiFiClientSecure.h"
#endif

namespace Xila_Namespace
{
namespace Network_Types
Expand All @@ -23,6 +27,7 @@ namespace Xila_Namespace
// - - Constructors / Destructors

WiFi_Client_Class();
~WiFi_Client_Class();

// - - Operations

Expand Down Expand Up @@ -59,8 +64,14 @@ namespace Xila_Namespace

operator bool() override;

bool operator==(WiFi_Client_Class &Client) override;
bool operator!=(WiFi_Client_Class &Client) override;
private:

#ifdef Xila_WiFi_Hardware_ESP32
WiFiClientSecure Client;
#endif

friend class HTTPS_Client_Class;

} WiFi_Client_Type;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
/// @copyright Copyright (c) 2022
///

#ifndef WiFi_Hpp_Included
#define WiFi_Hpp_Included
#ifndef Xila_Network_WiFi_Interface_Hpp_Included
#define Xila_Network_WiFi_Interface_Hpp_Included

#include "../Module/Module.hpp"

#include "IP_Address.hpp"
#include "WiFi_Client.hpp"
#include "../Interface.hpp"
#include "./Client.hpp"
#include "../IP_Address.hpp"

namespace Xila_Namespace
{
Expand Down Expand Up @@ -68,22 +67,38 @@ namespace Xila_Namespace
};

/// @brief WiFi class
typedef class WiFi_Class : public Module_Class
typedef class WiFi_Interface_Class : Interface_Type
{
public:
// - Methods
// - - Constructors / destructors

/// @brief Default constructor.
WiFi_Class();
WiFi_Interface_Class();

// - - Override base class methods

/// @brief Start the WiFi module.
/// @return `Result_Type::Success` if the module has been started, `Result_Type::Error` otherwise.
Result_Type Start();
Result_Type Start() override;

/// @brief Stop (and turn off) the WiFi module.
/// @return `Result_Type::Success` if the module has been stopped, `Result_Type::Error` otherwise.
Result_Type Stop();
Result_Type Stop() 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;
Byte_Type Get_Subnet_CIDR() 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;

// - - Managment

Expand Down Expand Up @@ -473,7 +488,7 @@ namespace Xila_Namespace

bool Long_Range;

} WiFi_Type;
} WiFi_Interface_Type;

}
}
Expand Down
60 changes: 47 additions & 13 deletions lib/Xila/src/Network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,87 @@ Result_Type Network_Class::Start()
{
Log_Information("WiFi", "Start Communication module...");

Result_Type Result = Result_Type::Success;

Interface_Type* Interface_Pointer = Interface_Type::First_Instance_Pointer;

while (Interface_Pointer)
{
Interface_Pointer->Start();
if (Interface_Pointer->Start() != Result_Type::Success)
Result = Result_Type::Error;

Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
}


if (WiFi.Start() == Result_Type::Success)
return Result_Type::Success;

return Result_Type::Error;
return Result;
}

Result_Type Network_Class::Stop()
{
if (WiFi.Stop() == Result_Type::Success)
return Result_Type::Success;
Result_Type Result = Result_Type::Success;

Interface_Type* Interface_Pointer = Interface_Type::First_Instance_Pointer;

while (Interface_Pointer)
{
if (Interface_Pointer->Start() != Result_Type::Success)
Result = Result_Type::Error;

Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
}

return Result_Type::Error;
return Result;
}

Interface_Type* Network_Class::Get_Interface(Natural_Type Index)
{
Interface_Type* Interface_Pointer = Interface_Class::First_Instance_Pointer;
while (Index--)
while (Index-- && Interface_Pointer)
{
Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
}
return Interface_Pointer;
}

Natural_Type Network_Class::Get_Interface_Count()
{
Natural_Type Count = 0;
Interface_Type* Interface_Pointer = Interface_Class::First_Instance_Pointer;
while (Interface_Pointer)
{
Count++;
Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
}
return Count;
}

Interface_Type* Network_Class::Get_Connected_Interface(Natural_Type Index)
{
Interface_Type* Interface_Pointer = Interface_Class::First_Instance_Pointer;

for (Natural_Type i = 0; i < Index; i++)
while (Index-- && Interface_Pointer)
{
Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
// Skip if not connected
while (Interface_Pointer->Get_Status() != Interface_Status::Connected)

while (Interface_Pointer->Get_State() != State_Type::Connected && Interface_Pointer)
{
Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
}
}

return Interface_Pointer;
}

Natural_Type Network_Class::Get_Connected_Interface_Count()
{
Natural_Type Count = 0;
Interface_Type* Interface_Pointer = Interface_Class::First_Instance_Pointer;
while (Interface_Pointer)
{
if (Interface_Pointer->Get_State() == State_Type::Connected)
Count++;

Interface_Pointer = Interface_Pointer->Next_Instance_Pointer;
}
return Count;
}
18 changes: 5 additions & 13 deletions lib/Xila/src/Network/WiFi/Drivers/ESP32/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
using namespace Xila_Namespace;
using namespace Network_Types;

WiFi_Client_Class::WiFi_Client_Class()
WiFi_Client_Class::WiFi_Client_Class() : Client()
{
Data = new
}

Result_Type WiFi_Client_Class::Connect(const char *Host, uint16_t Port, int32_t Timeout)
WiFi_Client_Class::~WiFi_Client_Class()
{
}

Result_Type WiFi_Client_Class::Connect(const char *Host, uint16_t Port, int32_t Timeout)
{
return (Result_Type)Client.connect(Host, Port, Timeout);
}

Expand Down Expand Up @@ -116,14 +118,4 @@ WiFi_Client_Class::operator bool()
return Client;
}

bool WiFi_Client_Class::operator==(WiFi_Client_Class &Client)
{
return Client == Client;
}

bool WiFi_Client_Class::operator!=(WiFi_Client_Class &Client)
{
return Client != Client;
}

#endif
Loading

0 comments on commit 206428b

Please sign in to comment.