meta-google: dhcp-done: Adding status report
Previously dhcp-done only sends status code, this one provides the
capability to send status code + status message for better
troubleshooting.
Provide a way to let other process upgrade the status.
Tested: Unit test passed.
Change-Id: I9c689f90502a32b586c41e3491ad47ebc78fcc38
Signed-off-by: Yuxiao Zhang <yuxiaozhang@google.com>
diff --git a/subprojects/dhcp-done/update-dhcp-status.cpp b/subprojects/dhcp-done/update-dhcp-status.cpp
new file mode 100644
index 0000000..b23706e
--- /dev/null
+++ b/subprojects/dhcp-done/update-dhcp-status.cpp
@@ -0,0 +1,63 @@
+#include "file-io.hpp"
+
+#include <stdplus/print.hpp>
+
+#include <cstring>
+#include <iostream>
+
+static void printUsage()
+{
+ stdplus::println(stderr, "Usage: update_dhcp_status <state> <message>");
+ stdplus::println(stderr,
+ "<state> is one of 'DONE', 'POWERCYCLE' or 'ONGOING'");
+}
+
+static int genStatusCode(char* state)
+{
+ if (std::strcmp(state, "DONE") == 0)
+ {
+ return 0;
+ }
+ else if (std::strcmp(state, "POWERCYCLE") == 0)
+ {
+ return 1;
+ }
+ else if (std::strcmp(state, "ONGOING") == 0)
+ {
+ return 2;
+ }
+
+ return -1;
+}
+
+int main(int argc, char* argv[])
+{
+ if (argc != 3)
+ {
+ printUsage();
+ return 1;
+ }
+
+ int statusCode = genStatusCode(argv[1]);
+
+ if (statusCode == -1)
+ {
+ printUsage();
+ return 1;
+ }
+
+ try
+ {
+ std::string status;
+ status.push_back(statusCode);
+ status.append(argv[2]);
+ fileWrite(statusFile, status);
+ }
+ catch (const std::exception& e)
+ {
+ stdplus::println(stderr, "Failed to update status file {}", e.what());
+ return 1;
+ }
+
+ return 0;
+}