xaizek / tos (License: GPLv3 only) (since 2018-12-07)
This is an alternative version of sources presented as part of Write Your Own OS video tutorial by Viktor Engelmann.
<root> / src / drv / drivers.hpp (4b1ce7e3b1ae8c2a8dd2bbbcdf5f2663811ed9cd) (1,521B) (mode 100644) [raw]
#ifndef TOS__DRV__DRIVERS_HPP__
#define TOS__DRV__DRIVERS_HPP__

#include <cstdint>

#include "utils/span.hpp"
#include "utils/struct.hpp"

namespace drv {

class Driver
{
public:
    virtual ~Driver() = default;

public:
    virtual void activate();
    virtual int reset();
    virtual void deactivate();
    virtual const char * getName() const = 0;
};

class RawDataHandler;

class EthernetDriver : public Driver
{
public:
    // `handler` can be `nullptr`.
    void setHandler(RawDataHandler *handler);

    void setIPAddress(NetOrder<std::uint32_t> ip);
    NetOrder<std::uint32_t> getIPAddress() const;

public:
    virtual void send(span<const std::uint8_t> msg) = 0;
    virtual void receive() = 0;
    virtual NetOrder<std::uint64_t> getMAC() const = 0;

protected:
    RawDataHandler *const &handler = handlerValue;

private:
    RawDataHandler *handlerValue = nullptr;
    NetOrder<std::uint32_t> ipAddress { 0U };
};

class RawDataHandler
{
public:
    RawDataHandler(EthernetDriver &backend);
    ~RawDataHandler();

public:
    void send(span<const std::uint8_t> msg);

public:
    virtual bool onRawDataReceived(span<std::uint8_t> msg) = 0;

protected:
    EthernetDriver &backend;
};

class DriverManager
{
public:
    explicit DriverManager();

public:
    void addDriver(Driver &drv);
    void activateAll();

    Driver ** begin() { return drivers; }
    Driver ** end() { return drivers + numDrivers; }

private:
    Driver *drivers[256];
    int numDrivers;
};

}

#endif // TOS__DRV__DRIVERS_HPP__
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/tos

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/tos

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master