Skip to content

Commit 3529e7f

Browse files
Add handler for rauc ::Complete callback
1 parent e57fad5 commit 3529e7f

16 files changed

+178
-39
lines changed

Diff for: src/FSM/State.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ namespace sua {
3838
ctx.mqttProcessor->send(topic, ctx.messagingProtocol->createMessage(ctx, messageName), retained);
3939
}
4040

41+
void State::send(Context& ctx, const std::string& topic, const std::string& messageName, const std::string& message, bool retained)
42+
{
43+
ctx.mqttProcessor->send(topic, ctx.messagingProtocol->createMessage(ctx, messageName, message), retained);
44+
}
45+
4146
} // namespace sua

Diff for: src/FSM/State.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace sua {
3737
virtual FotaEvent body(Context& ctx);
3838

3939
void send(Context& ctx, const std::string& topic, const std::string& messageName, bool retained = false);
40+
void send(Context& ctx, const std::string& topic, const std::string& messageName, const std::string& message, bool retained = false);
4041

4142
private:
4243
std::string _name;

Diff for: src/FSM/States/Installing.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ namespace sua {
5252
}
5353

5454
if(result == TechCode::InstallationFailed) {
55-
Logger::error("RAUC install failed");
56-
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, "installFailed");
55+
const auto lastError = ctx.installerAgent->getLastError();
56+
Logger::error("RAUC install failed: {}", lastError);
57+
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, "installFailed", lastError);
5758
return FotaEvent::InstallFailed;
5859
}
5960

Diff for: src/Install/DBusRaucInstaller.cpp

+114-14
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,49 @@ namespace {
2929

3030
namespace sua {
3131

32+
void on_completed(
33+
GDBusConnection * connection,
34+
const gchar * sender_name,
35+
const gchar * object_path,
36+
const gchar * interface_name,
37+
const gchar * signal_name,
38+
GVariant * parameters,
39+
gpointer user_data)
40+
{
41+
Logger::trace("de.pengutronix.rauc.Installer::Completed callback");
42+
43+
int32_t status = 0;
44+
g_variant_get(parameters, "(i)", &status);
45+
Logger::trace("RAUC install status = {}", status);
46+
47+
DBusRaucInstaller * installer = (DBusRaucInstaller *)user_data;
48+
if(status == 0) {
49+
installer->setSuccess(true);
50+
} else {
51+
installer->setSuccess(false);
52+
}
53+
installer->setFinished();
54+
}
55+
3256
DBusRaucInstaller::DBusRaucInstaller()
3357
{
34-
Logger::trace("DBusRaucInstaller::DBusRaucInstaller()");
3558
setupDBusRaucConnection();
3659
}
3760

3861
DBusRaucInstaller::~DBusRaucInstaller()
3962
{
40-
Logger::trace("DBusRaucInstaller::~DBusRaucInstaller()");
4163
if(nullptr != connection) {
4264
g_object_unref(connection);
4365
}
66+
67+
if(nullptr != loop) {
68+
g_main_loop_unref(loop);
69+
}
4470
}
4571

4672
TechCode DBusRaucInstaller::installBundle(const std::string& input)
4773
{
48-
Logger::info("Installing rauc bundle {}", input);
74+
Logger::trace("Installing rauc bundle {}", input);
4975
return installDBusRaucBundle(input);
5076
}
5177

@@ -69,8 +95,22 @@ namespace sua {
6995
GError* connectionError = nullptr;
7096
connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &connectionError);
7197

98+
loop = g_main_loop_new(NULL, FALSE);
99+
100+
auto id = g_dbus_connection_signal_subscribe(
101+
connection,
102+
"de.pengutronix.rauc",
103+
"de.pengutronix.rauc.Installer",
104+
"Completed",
105+
"/",
106+
NULL,
107+
G_DBUS_SIGNAL_FLAGS_NONE,
108+
on_completed,
109+
this,
110+
NULL
111+
);
72112
if(nullptr != connection) {
73-
Logger::info("Valid connection to dbus");
113+
Logger::trace("Valid connection to dbus");
74114
} else {
75115
Logger::error("Unable to connect to dbus, code = {}, message = {}",
76116
connectionError->code,
@@ -80,6 +120,9 @@ namespace sua {
80120

81121
TechCode DBusRaucInstaller::installDBusRaucBundle(const std::string& bundleName)
82122
{
123+
is_installing = true;
124+
is_succeeded = false;
125+
83126
GError* connectionError = nullptr;
84127
GVariant* result = g_dbus_connection_call_sync(connection,
85128
"de.pengutronix.rauc",
@@ -99,6 +142,10 @@ namespace sua {
99142
Logger::error("Install call to Rauc via DBus failed, code = {}, message = {}",
100143
connectionError->code,
101144
connectionError->message);
145+
146+
is_installing = false;
147+
is_succeeded = false;
148+
102149
return TechCode::InstallationFailed;
103150
}
104151

@@ -107,7 +154,7 @@ namespace sua {
107154

108155
int32_t DBusRaucInstaller::getDBusRaucInstallProgress() const
109156
{
110-
Logger::info("Install progress");
157+
Logger::trace("Install progress");
111158
GError* connectionError = nullptr;
112159
GVariant* progressInfo = g_dbus_connection_call_sync(
113160
connection,
@@ -129,10 +176,10 @@ namespace sua {
129176
gchar* message;
130177
g_variant_get(progressInfo, "(v)", &internalVal);
131178
g_variant_get(internalVal, "(isi)", &progressPercentage, &message, &nesting);
132-
Logger::info("Installing");
133-
Logger::info(" message = {}", message);
134-
Logger::info(" progress percentage = {}", progressPercentage);
135-
Logger::info("nesting = {}", nesting);
179+
Logger::trace("Installing");
180+
Logger::trace(" message = {}", message);
181+
Logger::trace(" progress percentage = {}", progressPercentage);
182+
Logger::trace(" nesting = {}", nesting);
136183
g_variant_unref(progressInfo);
137184
} else {
138185
Logger::error("Connection to DBus lost? code = {}, message = {}",
@@ -217,7 +264,7 @@ namespace sua {
217264

218265
std::string DBusRaucInstaller::getDBusRaucBundleVersion(const std::string& input) const
219266
{
220-
Logger::info("getDBusRaucBundleVersion, input={}", input);
267+
Logger::trace("getDBusRaucBundleVersion, input={}", input);
221268
std::string bundleVersion = "bundle_version_not_available";
222269
GError* connectionError = nullptr;
223270
GVariant* result = g_dbus_connection_call_sync(connection,
@@ -233,21 +280,74 @@ namespace sua {
233280
&connectionError);
234281

235282
if(nullptr != result) {
236-
Logger::info("Retrieved the version data, processing...");
283+
Logger::trace("Retrieved the version data, processing...");
237284
gchar* compatible;
238285
gchar* version;
239286
g_variant_get(result, "(ss)", &compatible, &version);
240287
bundleVersion = std::string(version);
241-
Logger::info("Version of downloaded bundle: {}", bundleVersion);
288+
Logger::trace("Version of downloaded bundle: {}", bundleVersion);
242289
g_free(compatible);
243290
g_free(version);
244291
} else {
245-
Logger::info("Retrieval of bundle version was not succesfull, error {}",
292+
Logger::trace("Retrieval of bundle version was not succesfull, error {}",
246293
connectionError->message);
247294
}
248295

249-
Logger::info("Retrieved version of the incoming bundle is: " + bundleVersion);
296+
Logger::trace("Retrieved version of the incoming bundle is: " + bundleVersion);
250297
return bundleVersion;
251298
}
252299

300+
std::string DBusRaucInstaller::getLastError()
301+
{
302+
GError* connectionError = nullptr;
303+
GVariant* lastErrorInfo = g_dbus_connection_call_sync(
304+
connection,
305+
"de.pengutronix.rauc",
306+
"/",
307+
"org.freedesktop.DBus.Properties",
308+
"Get",
309+
g_variant_new("(ss)", "de.pengutronix.rauc.Installer", "LastError"),
310+
NULL,
311+
G_DBUS_CALL_FLAGS_NONE,
312+
-1,
313+
NULL,
314+
&connectionError);
315+
316+
std::string lastError;
317+
if(nullptr != lastErrorInfo) {
318+
GVariant* internalVal;
319+
gchar* message;
320+
g_variant_get(lastErrorInfo, "(v)", &internalVal);
321+
g_variant_get(internalVal, "s", &message);
322+
g_variant_unref(lastErrorInfo);
323+
lastError = message;
324+
} else {
325+
Logger::error("Connection to DBus lost? code = {}, message = {}",
326+
connectionError->code,
327+
connectionError->message);
328+
}
329+
return lastError;
330+
}
331+
332+
bool DBusRaucInstaller::installing()
333+
{
334+
g_main_context_iteration(g_main_loop_get_context(loop), FALSE);
335+
return is_installing;
336+
}
337+
338+
bool DBusRaucInstaller::succeeded()
339+
{
340+
return is_succeeded;
341+
}
342+
343+
void DBusRaucInstaller::setSuccess(const bool value)
344+
{
345+
is_succeeded = value;
346+
}
347+
348+
void DBusRaucInstaller::setFinished()
349+
{
350+
is_installing = false;
351+
}
352+
253353
} // namespace sua

Diff for: src/Install/DBusRaucInstaller.h

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "Install/IRaucInstaller.h"
2121

22+
#include <atomic>
2223
#include <gio/gio.h>
2324

2425
namespace sua {
@@ -32,9 +33,20 @@ namespace sua {
3233
int32_t getInstallProgress() override;
3334
std::string getBundleVersion() override;
3435
std::string getBundleVersion(const std::string& input) override;
36+
std::string getLastError() override;
37+
38+
bool installing() override;
39+
bool succeeded() override;
40+
41+
void setFinished();
42+
void setSuccess(bool value);
3543

3644
private:
3745
GDBusConnection* connection{nullptr};
46+
GMainLoop* loop{nullptr};
47+
48+
std::atomic_bool is_installing;
49+
std::atomic_bool is_succeeded;
3850

3951
void setupDBusRaucConnection();
4052
TechCode installDBusRaucBundle(const std::string& bundleName);

Diff for: src/Install/DummyRaucInstaller.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ namespace sua {
4747
return "dummy_version";
4848
}
4949

50+
std::string DummyRaucInstaller::getLastError()
51+
{
52+
return "";
53+
}
54+
5055
} // namespace sua

Diff for: src/Install/DummyRaucInstaller.h

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ namespace sua {
3131
int32_t getInstallProgress() override;
3232
std::string getBundleVersion() override;
3333
std::string getBundleVersion(const std::string& input) override;
34+
std::string getLastError() override;
35+
36+
bool installing() override { return false; }
37+
bool succeeded() override { return false; }
3438
};
3539

3640
} // namespace sua

Diff for: src/Install/IRaucInstaller.h

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ namespace sua {
3131
virtual int32_t getInstallProgress() = 0;
3232
virtual std::string getBundleVersion() = 0;
3333
virtual std::string getBundleVersion(const std::string & input) = 0;
34+
virtual std::string getLastError() = 0;
35+
36+
virtual bool installing() = 0;
37+
virtual bool succeeded() = 0;
3438
};
3539

3640
} // namespace sua

Diff for: src/Install/Installer.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ namespace sua {
3737
{
3838
Logger::trace("Installer::start({})", input);
3939

40-
if(_installerAgent->installBundle(input) != TechCode::OK) {
41-
return TechCode::InstallationFailed;
42-
}
40+
_installerAgent->installBundle(input);
4341

4442
bool installing = true;
4543
uint32_t count = 0;
@@ -49,18 +47,18 @@ namespace sua {
4947
while(installing) {
5048
progressPercentage = _installerAgent->getInstallProgress();
5149
std::this_thread::sleep_for(2000ms);
52-
count++;
53-
if(progressPercentage >= 100 || count >= 120) {
54-
installing = false;
50+
tries++;
51+
if(tries >= 120) {
52+
return TechCode::InstallationFailed;
5553
}
5654

5755
if(progressPercentage >= progressNotificationLimiter) {
5856
if(progressPercentage != 100) {
5957
std::map<std::string, std::string> payload;
6058
payload["percentage"] = std::to_string(progressPercentage);
61-
sua::Dispatcher::instance().dispatch(EVENT_INSTALLING, payload);
59+
sua::Dispatcher::instance().dispatch(EVENT_INSTALLING, payload);\
6260
}
63-
progressNotificationLimiter += 10;
61+
progressNotificationLimiter += 10;
6462
}
6563
}
6664

Diff for: src/Mqtt/IMqttMessagingProtocol.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace sua {
2929

3030
virtual class DesiredState readCurrentStateRequest(const std::string & input) = 0;
3131

32-
virtual std::string createMessage(const class Context& ctx, const std::string& name) = 0;
32+
virtual std::string createMessage(const class Context& ctx, const std::string& name, const std::string& message = "") = 0;
3333
};
3434

3535
} // namespace sua

0 commit comments

Comments
 (0)