Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 BMW AG
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 <features/communication/dummy_serviceCommon.h>
14 : #include <features/communication/dummy_serviceProxy.h>
15 :
16 : #include <etl/delegate.h>
17 : #include <etl/expected.h>
18 : #include <etl/functional.h>
19 : #include <etl/optional.h>
20 :
21 : #include <cstdint>
22 :
23 : namespace application
24 : {
25 :
26 : namespace
27 : {
28 : constexpr uint8_t FOO_VALUE_A = 30U;
29 : constexpr uint8_t FOO_VALUE_B = 70U;
30 : constexpr uint8_t FNF_VALUE_A1 = 20U;
31 : constexpr uint8_t FNF_VALUE_A2 = 50U;
32 : constexpr uint8_t FNF_VALUE_B1 = 40U;
33 : constexpr uint8_t FNF_VALUE_B2 = 90U;
34 : constexpr uint32_t ATTRIBUTE_WRITE_VALUE = 0x0000002AU;
35 : } // namespace
36 :
37 : // [service-proxy-wrapper-start]
38 : class ProxyApp : public features::communication::DummyService::proxy::DummyServiceProxy
39 : {
40 : public:
41 : using Base = features::communication::DummyService::proxy::DummyServiceProxy;
42 : using Foo = features::communication::DummyService::Foo;
43 : using Baz = features::communication::DummyService::Baz;
44 : using MwInstanceId = features::communication::DummyService::internal::InstanceId;
45 : using FireAndForgetPayload = features::communication::DummyService::FireAndForgetPayload;
46 : using AttributeType
47 : = features::communication::DummyService::proxy::SimpleFieldAttribute::AttributeType;
48 : using AsyncMethodResult
49 : = features::communication::DummyService::proxy::DummyServiceProxy::AsyncMethodResult;
50 : using AsyncMethodCallback
51 : = features::communication::DummyService::proxy::DummyServiceProxy::AsyncMethodCallback;
52 : using AttributeGetterCallback
53 : = features::communication::DummyService::proxy::SimpleFieldAttribute::GetterCallback;
54 : using AttributeGetterResult
55 : = features::communication::DummyService::proxy::SimpleFieldAttribute::GetterResult;
56 : using AttributeReceiveCallback = features::communication::DummyService::proxy::
57 : SimpleFieldAttribute::OnFieldChangedCallback;
58 : using EventReceiveCallback = features::communication::DummyService::proxy::
59 : SimpleBroadcastEvent::OnFieldChangedCallback;
60 : using MwResult = etl::expected<uint16_t, ::middleware::core::HRESULT>;
61 :
62 6 : void startup()
63 : {
64 6 : if (init())
65 : {
66 6 : setEventReceiveHandler();
67 6 : setAttributeReceiveHandler();
68 : }
69 6 : }
70 :
71 : void shutdown() { Base::deInit(); }
72 :
73 : // [service-proxy-method-start]
74 1 : void runAsyncMethod(Foo const& input)
75 : {
76 : MwResult const result = Base::asyncMethod(
77 1 : input, etl::make_delegate<ProxyApp, &ProxyApp::asyncMethodResponse_>(*this));
78 : currentActiveRequestId_
79 1 : = result.has_value() ? etl::optional<uint16_t>{result.value()} : etl::nullopt;
80 1 : }
81 :
82 : // [service-proxy-method-end]
83 :
84 : // [service-proxy-fire-and-forget-start]
85 1 : MwResult runFireAndForgetMethod(FireAndForgetPayload const& payload)
86 : {
87 1 : return Base::fireAndForgetMethod(payload);
88 : }
89 :
90 : // [service-proxy-fire-and-forget-end]
91 :
92 : // [service-proxy-attribute-read-start]
93 1 : MwResult runAttributeGet()
94 : {
95 : return this->simpleField.get(
96 1 : etl::make_delegate<ProxyApp, &ProxyApp::attributeGetterResponse_>(*this));
97 : }
98 :
99 : // [service-proxy-attribute-read-end]
100 :
101 : // [service-proxy-attribute-write-start]
102 1 : MwResult runAttributeSet(uint32_t const value) { return this->simpleField.set(value); }
103 :
104 : // [service-proxy-attribute-write-end]
105 :
106 2 : [[nodiscard]] uint32_t receivedAttributeValue() const { return receivedAttributeValue_; }
107 :
108 1 : [[nodiscard]] uint32_t receivedEventValue() const { return receivedEventValue_; }
109 :
110 1 : [[nodiscard]] bool isRequestIdActive() const { return currentActiveRequestId_.has_value(); }
111 :
112 1 : [[nodiscard]] uint16_t getRequestIdActive() const { return currentActiveRequestId_.value(); }
113 :
114 1 : [[nodiscard]] bool isResponseValid() const { return latestResult_.has_value(); }
115 :
116 4 : [[nodiscard]] Baz getResponseValue() const { return latestResult_.value(); }
117 :
118 : private:
119 6 : bool init()
120 : {
121 6 : return Base::init(MwInstanceId::InstanceId_1, ::middleware::core::ClusterId::Core1)
122 : == ::middleware::core::HRESULT::Ok
123 6 : || Base::isInitialized();
124 : }
125 :
126 : // [service-attribute-subscription-start]
127 6 : void setAttributeReceiveHandler()
128 : {
129 6 : this->simpleField.setReceiveHandler(
130 : etl::make_delegate<ProxyApp, &ProxyApp::attributeChanged_>(*this));
131 6 : }
132 :
133 : // [service-attribute-subscription-end]
134 :
135 : // [service-proxy-method-subscription-start]
136 6 : void setEventReceiveHandler()
137 : {
138 6 : this->simpleBroadcast.setReceiveHandler(
139 : etl::make_delegate<ProxyApp, &ProxyApp::eventReceived_>(*this));
140 6 : }
141 :
142 : // [service-proxy-method-subscription-end]
143 :
144 1 : void asyncMethodResponse_(AsyncMethodResult const& result)
145 : {
146 : latestResult_
147 1 : = result.has_value() ? etl::optional<Baz>{result.value().get()} : etl::nullopt;
148 1 : currentActiveRequestId_ = etl::nullopt;
149 1 : }
150 :
151 1 : void attributeGetterResponse_(AttributeGetterResult const& result)
152 : {
153 1 : if (result.has_value())
154 : {
155 1 : receivedAttributeValue_ = result.value().get();
156 : }
157 1 : }
158 :
159 : // [service-proxy-attribute-subscription-callback-start]
160 1 : void attributeChanged_(uint32_t const& value) { receivedAttributeValue_ = value; }
161 :
162 : // [service-proxy-attribute-subscription-callback-end]
163 :
164 : // [service-proxy-broadcast-callback-start]
165 1 : void eventReceived_(uint32_t const& value) { receivedEventValue_ = value; }
166 :
167 : // [service-proxy-broadcast-callback-end]
168 :
169 : etl::optional<uint16_t> currentActiveRequestId_;
170 : etl::optional<Baz> latestResult_;
171 : uint32_t receivedAttributeValue_{};
172 : uint32_t receivedEventValue_{};
173 : };
174 :
175 : // [service-proxy-wrapper-end]
176 :
177 : } // namespace application
|