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 "someip/IEventProvider.h"
14 : #include "someip/RequestContext.h"
15 : #include "someip/RpcClosure.h"
16 : #include "someip/ServiceInfo.h"
17 :
18 : #include <ip/IPEndpoint.h>
19 :
20 : #include <etl/ipool.h>
21 : #include <cstdint>
22 :
23 : namespace someip
24 : {
25 : /**
26 : * Base for generated service handler.
27 : */
28 : class ServiceHandler : public IEventProvider
29 : {
30 : public:
31 : using RpcCallback = BoundCallback<RequestContext, ServiceResultCode, ServiceResultCode>;
32 : using RpcCallbackPool = ::etl::ipool;
33 :
34 : static ::ip::IPEndpoint const& getEndpoint(CallDoneClosure const& done);
35 :
36 : static void setOperationResult(CallDoneClosure& done, uint8_t resultCode);
37 :
38 : void dispatchMethod(
39 : uint16_t methodId,
40 : ISomeIpSerializable const* pRequest,
41 : ISomeIpSerializable* pResponse,
42 : CallDoneClosure& done);
43 :
44 : virtual void
45 0 : onEventGroupSubscriptionStateChanged(uint16_t /* eventId */, bool /* isSubscribed */)
46 0 : {}
47 :
48 : virtual ISomeIpSerializable* getResponse(uint16_t methodId) = 0;
49 :
50 : virtual ISomeIpSerializable* getRequest(uint16_t methodId) = 0;
51 :
52 : virtual MethodDetail const* getMethodDetail(uint16_t methodId) const = 0;
53 :
54 : ISomeIpSerializable* createRequest(uint16_t methodId, SomeIpParser& parser);
55 :
56 : bool hasAvailableCallback() const;
57 :
58 : RpcCallback& getCallback();
59 :
60 : void releaseCallback(RpcCallback const& cb) noexcept;
61 :
62 : protected:
63 : explicit ServiceHandler(RpcCallbackPool& callbackPool);
64 :
65 : virtual void callMethod(
66 : uint16_t methodId,
67 : ISomeIpSerializable const* pRequest,
68 : ISomeIpSerializable* pResponse,
69 : CallDoneClosure& done)
70 : = 0;
71 :
72 : static bool isSerializableValid(ISomeIpSerializable const* obj, CallDoneClosure& done);
73 :
74 : static bool isRequestResponseValid(
75 : ISomeIpSerializable const* request,
76 : ISomeIpSerializable const* response,
77 : CallDoneClosure& done);
78 :
79 : private:
80 : RpcCallbackPool& _rpcCallbackPool;
81 : };
82 :
83 : /*
84 : * inline implementation
85 : */
86 54 : inline ServiceHandler::ServiceHandler(RpcCallbackPool& callbackPool)
87 54 : : _rpcCallbackPool(callbackPool)
88 54 : {}
89 :
90 8 : inline bool ServiceHandler::hasAvailableCallback() const { return !_rpcCallbackPool.full(); }
91 :
92 9 : inline ServiceHandler::RpcCallback& ServiceHandler::getCallback()
93 : {
94 9 : return *_rpcCallbackPool.create<RpcCallback>();
95 : }
96 :
97 3 : inline void ServiceHandler::releaseCallback(RpcCallback const& cb) noexcept
98 : {
99 3 : _rpcCallbackPool.release(&cb);
100 3 : }
101 :
102 : } // namespace someip
|