xref: /openbmc/google-misc/libcr51sign/subprojects/dhcp-done/update-dhcp-status.cpp (revision b86429c404528da10dbdfad8b8e38e1f550431e9)
1 #include "file-io.hpp"
2 
3 #include <stdplus/print.hpp>
4 
5 #include <cstring>
6 #include <iostream>
7 
8 static void printUsage()
9 {
10     stdplus::println(stderr, "Usage: update_dhcp_status <state> <message>");
11     stdplus::println(
12         stderr,
13         "<state> is one of 'DONE', 'POWERCYCLE', 'REBOOT' or 'ONGOING'");
14 }
15 
16 static int genStatusCode(char* state)
17 {
18     if (std::strcmp(state, "DONE") == 0)
19     {
20         return 0;
21     }
22     else if (std::strcmp(state, "POWERCYCLE") == 0)
23     {
24         return 1;
25     }
26     else if (std::strcmp(state, "ONGOING") == 0)
27     {
28         return 2;
29     }
30     else if (std::strcmp(state, "REBOOT") == 0)
31     {
32         return 3;
33     }
34 
35     return -1;
36 }
37 
38 int main(int argc, char* argv[])
39 {
40     if (argc != 3)
41     {
42         printUsage();
43         return 1;
44     }
45 
46     int statusCode = genStatusCode(argv[1]);
47 
48     if (statusCode == -1)
49     {
50         printUsage();
51         return 1;
52     }
53 
54     try
55     {
56         std::string status;
57         status.push_back(statusCode);
58         status.append(argv[2]);
59         fileWrite(statusFile, status);
60     }
61     catch (const std::exception& e)
62     {
63         stdplus::println(stderr, "Failed to update status file {}", e.what());
64         return 1;
65     }
66 
67     return 0;
68 }
69