-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathExampleSomeipInterfaceWrapper.h
375 lines (354 loc) · 18.1 KB
/
ExampleSomeipInterfaceWrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "CollectionInspectionAPITypes.h"
#include "ISomeipInterfaceWrapper.h"
#include "LoggingModule.h"
#include "RawDataManager.h"
#include "v1/commonapi/ExampleSomeipInterfaceProxy.hpp"
#include <CommonAPI/CommonAPI.hpp>
#include <boost/optional/optional.hpp>
#include <string>
#include <unordered_map>
namespace Aws
{
namespace IoTFleetWise
{
/**
* This class is the wrapper class for the example SOME/IP interface. It's responsible for building
* SOME/IP interface proxy and expose the proxy as CommonAPI::Proxy base class shared pointer. The
* SOME/IP method is encapsulated in a method wrapper with unified input signature.
*
* To support custom SOME/IP interface:
* 1. Create a new wrapper class by copying this class
* 2. Update the class name with the new interface name
* 3. Update the Proxy class name with the new interface proxy name
* 4. Create a new method wrapper to encapsulate the method call. Take referenceMethodWrapper as example
* 5. Update mSupportedActuatorInfo with a new entry containing the actuator name, signal type and
* method wrapper function name
*/
class ExampleSomeipInterfaceWrapper : public ISomeipInterfaceWrapper
{
public:
ExampleSomeipInterfaceWrapper( std::string domain,
std::string instance,
std::string connection,
std::function<std::shared_ptr<v1::commonapi::ExampleSomeipInterfaceProxy<>>(
std::string, std::string, std::string )> buildProxy,
std::shared_ptr<RawData::BufferManager> rawBufferManager,
bool subscribeToLongRunningCommandStatus )
: mDomain( std::move( domain ) )
, mInstance( std::move( instance ) )
, mConnection( std::move( connection ) )
, mBuildProxy( std::move( buildProxy ) )
, mRawBufferManager( std::move( rawBufferManager ) )
, mSubscribeToLongRunningCommandStatus( subscribeToLongRunningCommandStatus )
, mSupportedActuatorInfo( {
{ "Vehicle.actuator1",
{ SignalType::INT32,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper1(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
{ "Vehicle.actuator2",
{ SignalType::INT64,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper2(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
{ "Vehicle.actuator3",
{ SignalType::BOOLEAN,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper3(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
{ "Vehicle.actuator4",
{ SignalType::FLOAT,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper4(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
{ "Vehicle.actuator5",
{ SignalType::DOUBLE,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper5(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
{ "Vehicle.actuator9",
{ SignalType::STRING,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper9(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
{ "Vehicle.actuator20",
{ SignalType::INT32,
[this]( auto signalValue,
auto commandId,
auto issuedTimestampMs,
auto executionTimeoutMs,
auto notifyStatusCallback ) {
referenceMethodWrapper20(
signalValue, commandId, issuedTimestampMs, executionTimeoutMs, notifyStatusCallback );
} } },
} )
{
}
~ExampleSomeipInterfaceWrapper() override
{
if ( mLongRunningCommandStatusSubscription.has_value() )
{
mProxy->getNotifyLRCStatusEvent().unsubscribe( *mLongRunningCommandStatusSubscription );
}
};
ExampleSomeipInterfaceWrapper( const ExampleSomeipInterfaceWrapper & ) = delete;
ExampleSomeipInterfaceWrapper &operator=( const ExampleSomeipInterfaceWrapper & ) = delete;
ExampleSomeipInterfaceWrapper( ExampleSomeipInterfaceWrapper && ) = delete;
ExampleSomeipInterfaceWrapper &operator=( ExampleSomeipInterfaceWrapper && ) = delete;
bool
init() override
{
mProxy = mBuildProxy( mDomain, mInstance, mConnection );
if ( mProxy == nullptr )
{
FWE_LOG_ERROR( "Failed to build proxy " );
return false;
}
if ( mSubscribeToLongRunningCommandStatus )
{
/* Subscribe to the long running command broadcast messages with
the given lambda as the handler.*/
mLongRunningCommandStatusSubscription = mProxy->getNotifyLRCStatusEvent().subscribe(
// coverity[autosar_cpp14_a5_1_9_violation] This lambda isn't duplicated anywhere
[this]( const std::string &commandID,
const int32_t &commandStatus,
const int32_t &commandReasonCode,
const std::string &commandReasonDescription ) {
std::lock_guard<std::mutex> lock( mCommandIDToNotifyCallbackMapMutex );
auto commandCallback = mCommandIDToNotifyCallbackMap.find( commandID );
if ( commandCallback == mCommandIDToNotifyCallbackMap.end() )
{
FWE_LOG_ERROR( "Unknown command ID: " + commandID );
return;
}
// coverity[autosar_cpp14_a7_2_1_violation] Basic checking is performed above
CommandStatus cmdStatus = static_cast<CommandStatus>( commandStatus );
CommandReasonCode cmdReasonCode = static_cast<CommandReasonCode>( commandReasonCode );
commandCallback->second( cmdStatus, cmdReasonCode, commandReasonDescription );
} );
}
return true;
}
std::shared_ptr<CommonAPI::Proxy>
getProxy() const override
{
return std::static_pointer_cast<CommonAPI::Proxy>( mProxy );
}
// Create one method wrapper for each SOME/IP method
void
referenceMethodWrapper1( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
FWE_LOG_TRACE( "set actuator value to " + std::to_string( signalValue.value.int32Val ) + " for command ID " +
commandId );
mProxy->setInt32Async(
signalValue.value.int32Val,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
},
&info );
}
void
referenceMethodWrapper2( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
FWE_LOG_TRACE( "set actuator value to " + std::to_string( signalValue.value.int64Val ) + " for command ID " +
commandId );
mProxy->setInt64Async(
signalValue.value.int64Val,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
},
&info );
}
void
referenceMethodWrapper3( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
FWE_LOG_TRACE( "set actuator value to " + std::to_string( signalValue.value.boolVal ) + " for command ID " +
commandId );
mProxy->setBooleanAsync(
signalValue.value.boolVal,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
},
&info );
}
void
referenceMethodWrapper4( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
FWE_LOG_TRACE( "set actuator value to " + std::to_string( signalValue.value.floatVal ) + " for command ID " +
commandId );
mProxy->setFloatAsync(
signalValue.value.floatVal,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
},
&info );
}
void
referenceMethodWrapper5( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
FWE_LOG_TRACE( "set actuator value to " + std::to_string( signalValue.value.doubleVal ) + " for command ID " +
commandId );
mProxy->setDoubleAsync(
signalValue.value.doubleVal,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
},
&info );
}
void
referenceMethodWrapper9( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
auto loanedFrame = mRawBufferManager->borrowFrame( signalValue.value.rawDataVal.signalId,
signalValue.value.rawDataVal.handle );
if ( loanedFrame.isNull() )
{
notifyStatusCallback( CommandStatus::EXECUTION_FAILED, REASON_CODE_REJECTED, "" );
return;
}
std::string stringVal;
stringVal.assign( reinterpret_cast<const char *>( loanedFrame.getData() ), loanedFrame.getSize() );
FWE_LOG_TRACE( "set actuator value to " + stringVal + " for command ID " + commandId );
mProxy->setStringAsync(
stringVal,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
},
&info );
}
void
referenceMethodWrapper20( SignalValueWrapper signalValue,
const CommandID &commandId,
Timestamp issuedTimestampMs,
Timestamp executionTimeoutMs,
NotifyCommandStatusCallback notifyStatusCallback )
{
CommonAPI::CallInfo info( commonapiGetRemainingTimeout( issuedTimestampMs, executionTimeoutMs ) );
{
std::lock_guard<std::mutex> lock( mCommandIDToNotifyCallbackMapMutex );
auto result = mCommandIDToNotifyCallbackMap.emplace( commandId, notifyStatusCallback );
if ( !result.second )
{
FWE_LOG_ERROR( "Duplicate command ID: " + commandId );
return;
}
}
FWE_LOG_TRACE( "set actuator value to " + std::to_string( signalValue.value.int32Val ) + " for command ID " +
commandId );
mProxy->setInt32LongRunningAsync(
commandId,
signalValue.value.int32Val,
// coverity[autosar_cpp14_a5_1_9_violation] Local variables need to be captured, so cannot be made common
[this, commandId, notifyStatusCallback]( const CommonAPI::CallStatus &callStatus ) {
notifyStatusCallback( commonapiCallStatusToCommandStatus( callStatus ),
commonapiCallStatusToReasonCode( callStatus ),
commonapiCallStatusToString( callStatus ) );
std::lock_guard<std::mutex> lock( mCommandIDToNotifyCallbackMapMutex );
mCommandIDToNotifyCallbackMap.erase( commandId );
},
&info );
}
const std::unordered_map<std::string, SomeipMethodInfo> &
getSupportedActuatorInfo() const override
{
return mSupportedActuatorInfo;
}
private:
std::shared_ptr<v1::commonapi::ExampleSomeipInterfaceProxy<>> mProxy;
std::string mDomain;
std::string mInstance;
std::string mConnection;
std::function<std::shared_ptr<v1::commonapi::ExampleSomeipInterfaceProxy<>>(
std::string, std::string, std::string )>
mBuildProxy;
std::shared_ptr<RawData::BufferManager> mRawBufferManager;
bool mSubscribeToLongRunningCommandStatus;
boost::optional<v1::commonapi::ExampleSomeipInterfaceProxyBase::NotifyLRCStatusEvent::Subscription>
mLongRunningCommandStatusSubscription;
std::unordered_map<std::string, SomeipMethodInfo> mSupportedActuatorInfo;
// Mutex to ensure atomic insertion to the command ID to notification callback map
std::mutex mCommandIDToNotifyCallbackMapMutex;
std::unordered_map<std::string, NotifyCommandStatusCallback> mCommandIDToNotifyCallbackMap;
};
} // namespace IoTFleetWise
} // namespace Aws