LCOV - code coverage report
Current view: top level - cpp2ethernet/include/udp/socket - AbstractDatagramSocket.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4 4 100.0 %
Date: 2025-08-28 06:39:11 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2025 Accenture.
       2             : 
       3             : #pragma once
       4             : 
       5             : #include "ip/IPAddress.h"
       6             : #include "udp/DatagramPacket.h"
       7             : #include "udp/IDataListener.h"
       8             : #include "udp/IDataSentListener.h"
       9             : 
      10             : #include <etl/span.h>
      11             : 
      12             : #include <platform/estdint.h>
      13             : 
      14             : namespace udp
      15             : {
      16             : /**
      17             :  * Interface for all udp sockets.
      18             :  */
      19             : class AbstractDatagramSocket
      20             : {
      21             : public:
      22             :     // [AbstractDatagramSocket]
      23             :     enum
      24             :     {
      25             :         INVALID_PORT = 0xFFFF
      26             :     };
      27             : 
      28             :     /**
      29             :      * all ErrorCodes used by socket
      30             :      * \enum  ErrorCode
      31             :      */
      32             :     enum class ErrorCode : uint8_t
      33             :     {
      34             :         /** everything ok */
      35             :         UDP_SOCKET_OK,
      36             :         /** an error occurred */
      37             :         UDP_SOCKET_NOT_OK,
      38             :         /** no IDataListener registerd */
      39             :         UDP_SOCKET_NO_DATA_LISTENER
      40             :     };
      41             : 
      42             :     AbstractDatagramSocket();
      43             :     AbstractDatagramSocket(AbstractDatagramSocket const&)            = delete;
      44             :     AbstractDatagramSocket& operator=(AbstractDatagramSocket const&) = delete;
      45             : 
      46             :     /**
      47             :      * Binds this UDPSocket to a specific port & local address
      48             :      * \param port  local port
      49             :      * \return  status of method
      50             :      *          - UDP_SOCKET_OK when everything was fine
      51             :      *          - UDP_SOCKET_NOT_OK when something went wrong
      52             :      */
      53             :     virtual ErrorCode bind(ip::IPAddress const* pIpAddress, uint16_t port) = 0;
      54             : 
      55             :     virtual ErrorCode join(ip::IPAddress const& groupAddr) = 0;
      56             : 
      57             :     /**
      58             :      * Returns the binding state of the socket.
      59             :      * \return  binding state of the socket
      60             :      *          - false: not bound
      61             :      *          - true: bound
      62             :      */
      63             :     virtual bool isBound() const = 0;
      64             : 
      65             :     virtual void close() = 0;
      66             : 
      67             :     /**
      68             :      * Returns whether the socket is closed or not.
      69             :      * \return  state of the socket
      70             :      *          - false: not closed
      71             :      *          - true: closed
      72             :      */
      73             :     virtual bool isClosed() const = 0;
      74             : 
      75             :     /**
      76             :      * Connects the socket to a remote address for this socket.
      77             :      * \param   address  remote IPAddress
      78             :      * \param   port     remote port
      79             :      * \return  status of method
      80             :      *          - UDP_SOCKET_OK when everything was fine
      81             :      *          - UDP_SOCKET_NOT_OK when something went wrong
      82             :      */
      83             :     virtual ErrorCode
      84             :     connect(ip::IPAddress const& address, uint16_t port, ip::IPAddress* pLocalAddress)
      85             :         = 0;
      86             : 
      87             :     /**
      88             :      * Disconnects the socket
      89             :      */
      90             :     virtual void disconnect() = 0;
      91             : 
      92             :     /**
      93             :      * Returns the connection state of the socket.
      94             :      * \return  connection state of the socket
      95             :      *          - false: not connected
      96             :      *          - true: connected
      97             :      */
      98             :     virtual bool isConnected() const = 0;
      99             : 
     100             :     /**
     101             :      * reads a given number of bytes from the socket
     102             :      * \param  buffer   buffer to receive data to. If 0L is passed the n
     103             :      * bytes should be skipped, i.e. the input stream has to advance by
     104             :      * n bytes.
     105             :      * \param   n   max number of bytes to receive
     106             :      * \return  number of bytes really read
     107             :      *          - 0: an error occurred reading from the socket
     108             :      *          - else: bytes read
     109             :      */
     110             :     virtual size_t read(uint8_t* buffer, size_t n) = 0;
     111             : 
     112             :     /**
     113             :      * sends an amount of data
     114             :      * \param  data   data to send
     115             :      * \return  status of transmission
     116             :      *          - UDP_SOCKET_OK when data was sent to UDP stack
     117             :      *          - UDP_SOCKET_NOT_OK when socket has not been opened
     118             :      * \note
     119             :      * sending the data may be asynchronous. for this reason the
     120             :      * IDataSendNotificationListener has a appropriate callback.
     121             :      */
     122             :     virtual ErrorCode send(::etl::span<uint8_t const> const& data) = 0;
     123             : 
     124             :     /**
     125             :      * Sends a DatagramPacket.
     126             :      * \return  status of transmission
     127             :      *          - UDP_SOCKET_OK when data was sent to UDP stack
     128             :      *          - UDP_SOCKET_NOT_OK when socket has not been opened
     129             :      */
     130             :     virtual ErrorCode send(DatagramPacket const& packet) = 0;
     131             : 
     132             :     /**
     133             :      * sets the listener to this socket instance
     134             :      * \param  pListener  IDataListener to attach
     135             :      */
     136             :     void setDataListener(IDataListener* pListener);
     137             : 
     138             :     void setDataSentListener(IDataSentListener* pListener);
     139             : 
     140             :     /**
     141             :      * \return  Local IPAddress, 0L if this operation is not allowed
     142             :      */
     143             :     virtual ip::IPAddress const* getIPAddress() const = 0;
     144             : 
     145             :     virtual ip::IPAddress const* getLocalIPAddress() const = 0;
     146             : 
     147             :     /**
     148             :      * Returns remote port if connected, INVALID_PORT otherwise
     149             :      */
     150             :     virtual uint16_t getPort() const = 0;
     151             : 
     152             :     virtual uint16_t getLocalPort() const = 0;
     153             : 
     154             :     // [AbstractDatagramSocket]
     155             : protected:
     156             :     IDataListener* _dataListener;
     157             :     IDataSentListener* _dataSentListener;
     158             : };
     159             : 
     160             : /*
     161             :  * inline methods
     162             :  */
     163           2 : inline void AbstractDatagramSocket::setDataListener(IDataListener* const pListener)
     164             : {
     165           2 :     _dataListener = pListener;
     166             : }
     167             : 
     168           2 : inline void AbstractDatagramSocket::setDataSentListener(IDataSentListener* const pListener)
     169             : {
     170           2 :     _dataSentListener = pListener;
     171             : }
     172             : 
     173             : } // namespace udp

Generated by: LCOV version 1.14