-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAwsGreengrassV2Sender.cpp
186 lines (164 loc) · 5.96 KB
/
AwsGreengrassV2Sender.cpp
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "AwsGreengrassV2Sender.h"
#include "AwsSDKMemoryManager.h"
#include "IConnectionTypes.h"
#include "IConnectivityModule.h"
#include "LoggingModule.h"
#include <aws/crt/Optional.h>
#include <aws/crt/Types.h>
#include <chrono>
#include <functional>
#include <future>
namespace Aws
{
namespace IoTFleetWise
{
AwsGreengrassV2Sender::AwsGreengrassV2Sender(
IConnectivityModule *connectivityModule,
std::shared_ptr<Aws::Greengrass::GreengrassCoreIpcClient> &greengrassClient,
const TopicConfig &topicConfig )
: mConnectivityModule( connectivityModule )
, mGreengrassClient( greengrassClient )
, mTopicConfig( topicConfig )
{
}
bool
AwsGreengrassV2Sender::isAlive()
{
std::lock_guard<std::mutex> connectivityLock( mConnectivityMutex );
return isAliveNotThreadSafe();
}
bool
AwsGreengrassV2Sender::isAliveNotThreadSafe()
{
if ( mConnectivityModule == nullptr )
{
return false;
}
return mConnectivityModule->isAlive();
}
size_t
AwsGreengrassV2Sender::getMaxSendSize() const
{
return AWS_IOT_MAX_MESSAGE_SIZE;
}
void
AwsGreengrassV2Sender::sendBuffer(
const std::string &topic, const uint8_t *buf, size_t size, OnDataSentCallback callback, QoS qos )
{
std::lock_guard<std::mutex> connectivityLock( mConnectivityMutex );
if ( topic.empty() )
{
FWE_LOG_WARN( "Invalid topic provided" );
callback( ConnectivityError::NotConfigured );
return;
}
if ( ( buf == nullptr ) || ( size == 0 ) )
{
FWE_LOG_WARN( "No valid data provided" );
callback( ConnectivityError::WrongInputData );
return;
}
if ( size > getMaxSendSize() )
{
FWE_LOG_WARN( "Payload provided is too long" );
callback( ConnectivityError::WrongInputData );
return;
}
if ( !isAliveNotThreadSafe() )
{
FWE_LOG_WARN( "No alive IPC Connection." );
callback( ConnectivityError::NoConnection );
return;
}
if ( !AwsSDKMemoryManager::getInstance().reserveMemory( size ) )
{
FWE_LOG_ERROR( "Not sending out the message with size " + std::to_string( size ) +
" because IoT device SDK allocated the maximum defined memory." );
callback( ConnectivityError::QuotaReached );
return;
}
if ( mGreengrassClient == nullptr )
{
FWE_LOG_ERROR( "mGreengrassClient is null, not initialised" )
callback( ConnectivityError::NoConnection );
return;
}
auto greengrassPublishQoS = Aws::Greengrass::QOS_AT_MOST_ONCE;
switch ( qos )
{
case QoS::AT_MOST_ONCE:
greengrassPublishQoS = Aws::Greengrass::QOS_AT_MOST_ONCE;
break;
case QoS::AT_LEAST_ONCE:
greengrassPublishQoS = Aws::Greengrass::QOS_AT_LEAST_ONCE;
break;
}
auto publishOperation = mGreengrassClient->NewPublishToIoTCore();
Aws::Greengrass::PublishToIoTCoreRequest publishRequest;
publishRequest.SetTopicName( topic.c_str() != nullptr ? topic.c_str() : "" );
Aws::Crt::Vector<uint8_t> payload( buf, buf + size );
publishRequest.SetPayload( payload );
publishRequest.SetQos( greengrassPublishQoS );
FWE_LOG_TRACE( "Attempting to publish to " + topic + " topic" );
auto onMessageFlushCallback = [callback, topicName = topic]( int errorCode ) {
if ( errorCode != 0 )
{
FWE_LOG_ERROR( "Failed to publish to " + topicName + " topic with error code " +
std::to_string( errorCode ) );
callback( ConnectivityError::TransmissionError );
return;
}
callback( ConnectivityError::Success );
};
auto requestStatus = publishOperation->Activate( publishRequest, onMessageFlushCallback ).get();
if ( !requestStatus )
{
auto errString = requestStatus.StatusToString();
FWE_LOG_ERROR( "Failed to publish to " + topic + " topic with error " +
std::string( errString.c_str() != nullptr ? errString.c_str() : "Unknown error" ) );
callback( ConnectivityError::NoConnection );
return;
}
auto publishResultFuture = publishOperation->GetResult();
// To avoid throwing exceptions, wait on the result for a specified timeout:
if ( publishResultFuture.wait_for( std::chrono::seconds( 10 ) ) == std::future_status::timeout )
{
FWE_LOG_ERROR( "Timed out while waiting for response from Greengrass Core" );
callback( ConnectivityError::NoConnection );
return;
}
auto publishResult = publishResultFuture.get();
if ( !publishResult )
{
FWE_LOG_ERROR( "Failed to publish to " + topic + " topic" );
auto errorType = publishResult.GetResultType();
if ( errorType == OPERATION_ERROR )
{
OperationError *error = publishResult.GetOperationError();
/*
* This pointer can be casted to any error type like so:
* if(error->GetModelName() == UnauthorizedError::MODEL_NAME)
* UnauthorizedError *unauthorizedError = static_cast<UnauthorizedError*>(error);
*/
if ( error->GetMessage().has_value() )
{
auto errString = error->GetMessage().value().c_str();
FWE_LOG_ERROR( "Greengrass Core responded with an error: " +
( errString != nullptr ? std::string( errString ) : std::string( "Unknown error" ) ) );
}
}
else
{
auto errString = publishResult.GetRpcError().StatusToString();
FWE_LOG_ERROR( "Attempting to receive the response from the server failed with error code " +
std::string( errString.c_str() != nullptr ? errString.c_str() : "Unknown error" ) );
}
callback( ConnectivityError::NoConnection );
return;
}
callback( ConnectivityError::Success );
}
} // namespace IoTFleetWise
} // namespace Aws