| Yuxiao Zhang | 1e76060 | 2024-03-07 12:35:24 -0800 | [diff] [blame] | 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>"); |
| Yuxiao Zhang | b86429c | 2025-05-09 13:44:22 -0700 | [diff] [blame^] | 11 | stdplus::println( |
| 12 | stderr, |
| 13 | "<state> is one of 'DONE', 'POWERCYCLE', 'REBOOT' or 'ONGOING'"); |
| Yuxiao Zhang | 1e76060 | 2024-03-07 12:35:24 -0800 | [diff] [blame] | 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 | } |
| Yuxiao Zhang | b86429c | 2025-05-09 13:44:22 -0700 | [diff] [blame^] | 30 | else if (std::strcmp(state, "REBOOT") == 0) |
| 31 | { |
| 32 | return 3; |
| 33 | } |
| Yuxiao Zhang | 1e76060 | 2024-03-07 12:35:24 -0800 | [diff] [blame] | 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 | } |