Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include <ip/IPAddress.h>
14 : #include <ip/IPEndpoint.h>
15 : #include <someip/SomeIpConstants.h>
16 :
17 : #include <etl/expected.h>
18 : #include <etl/span.h>
19 : #include <cstdint>
20 :
21 : namespace someip
22 : {
23 : class INetworkListener;
24 :
25 : /**
26 : * Provides access to a protocol-specific network-resource.
27 : */
28 : class NetworkResource
29 : {
30 : public:
31 : static ::ip::IPAddress const INVALID_IP;
32 : static ::ip::IPEndpoint const INVALID_ADDRESS;
33 :
34 292 : NetworkResource() = default;
35 :
36 292 : virtual ~NetworkResource() = default;
37 :
38 : virtual bool isInitialized() const;
39 :
40 292 : void setInputBuffer(::etl::span<uint8_t> const& buffer) { _pInputBuffer = buffer; }
41 :
42 : ::etl::span<uint8_t> getInputBuffer() const;
43 :
44 322 : void setOutputBuffer(::etl::span<uint8_t> const& buffer) { _pOutputBuffer = buffer; }
45 :
46 : ::etl::span<uint8_t> getOutputBuffer() const;
47 :
48 132 : void setListener(INetworkListener& listener) { _pListener = &listener; }
49 :
50 3 : INetworkListener* getListener() const { return _pListener; }
51 :
52 : virtual ::etl::expected<uint16_t, PortError> getLocalPort() const = 0;
53 : virtual uint8_t getProto() const = 0;
54 :
55 : virtual bool isOpen() const = 0;
56 : virtual bool isConnected() const = 0;
57 : virtual void close() = 0;
58 :
59 181 : inline void tryClose()
60 : {
61 181 : if (_refCounter == 0)
62 : {
63 16 : close();
64 : }
65 181 : }
66 :
67 : virtual bool send(::ip::IPEndpoint const& remoteEndpoint, uint32_t length);
68 : virtual bool send(uint32_t length);
69 :
70 277 : void incRefCounter() { ++_refCounter; }
71 :
72 189 : void decRefCounter() { --_refCounter; }
73 :
74 : protected:
75 : ::etl::span<uint8_t> _pInputBuffer;
76 : ::etl::span<uint8_t> _pOutputBuffer;
77 :
78 : INetworkListener* _pListener{nullptr};
79 :
80 : private:
81 : size_t _refCounter{0U};
82 : };
83 :
84 : } // namespace someip
|