Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "helper.hpp" |
| 18 | |
| 19 | #include "status.hpp" |
| 20 | #include "tool_errors.hpp" |
| 21 | |
| 22 | #include <chrono> |
| 23 | #include <ipmiblob/blob_errors.hpp> |
| 24 | #include <thread> |
| 25 | |
| 26 | namespace host_tool |
| 27 | { |
| 28 | |
| 29 | /* Poll an open verification session. Handling closing the session is not yet |
| 30 | * owned by this method. |
| 31 | */ |
| 32 | bool pollStatus(std::uint16_t session, ipmiblob::BlobInterface* blob) |
| 33 | { |
| 34 | using namespace std::chrono_literals; |
| 35 | |
| 36 | static constexpr auto verificationSleep = 5s; |
| 37 | ipmi_flash::ActionStatus result = ipmi_flash::ActionStatus::unknown; |
| 38 | |
| 39 | try |
| 40 | { |
| 41 | static constexpr int commandAttempts = 20; |
| 42 | int attempts = 0; |
| 43 | bool exitLoop = false; |
| 44 | |
| 45 | /* Reach back the current status from the verification service output. |
| 46 | */ |
| 47 | while (attempts++ < commandAttempts) |
| 48 | { |
| 49 | ipmiblob::StatResponse resp = blob->getStat(session); |
| 50 | |
| 51 | if (resp.metadata.size() != sizeof(std::uint8_t)) |
| 52 | { |
| 53 | /* TODO: How do we want to handle the verification failures, |
| 54 | * because closing the session to the verify blob has a special |
| 55 | * as-of-yet not fully defined behavior. |
| 56 | */ |
| 57 | std::fprintf(stderr, "Received invalid metadata response!!!\n"); |
| 58 | } |
| 59 | |
| 60 | result = static_cast<ipmi_flash::ActionStatus>(resp.metadata[0]); |
| 61 | |
| 62 | switch (result) |
| 63 | { |
| 64 | case ipmi_flash::ActionStatus::failed: |
| 65 | std::fprintf(stderr, "failed\n"); |
| 66 | exitLoop = true; |
| 67 | break; |
| 68 | case ipmi_flash::ActionStatus::unknown: |
| 69 | std::fprintf(stderr, "other\n"); |
| 70 | break; |
| 71 | case ipmi_flash::ActionStatus::running: |
| 72 | std::fprintf(stderr, "running\n"); |
| 73 | break; |
| 74 | case ipmi_flash::ActionStatus::success: |
| 75 | std::fprintf(stderr, "success\n"); |
| 76 | exitLoop = true; |
| 77 | break; |
| 78 | default: |
| 79 | std::fprintf(stderr, "wat\n"); |
| 80 | } |
| 81 | |
| 82 | if (exitLoop) |
| 83 | { |
| 84 | break; |
| 85 | } |
| 86 | std::this_thread::sleep_for(verificationSleep); |
| 87 | } |
| 88 | } |
| 89 | catch (const ipmiblob::BlobException& b) |
| 90 | { |
| 91 | throw ToolException("blob exception received: " + |
| 92 | std::string(b.what())); |
| 93 | } |
| 94 | |
| 95 | /* TODO: If this is reached and it's not success, it may be worth just |
| 96 | * throwing a ToolException with a timeout message specifying the final |
| 97 | * read's value. |
| 98 | * |
| 99 | * TODO: Given that excepting from certain points leaves the BMC update |
| 100 | * state machine in an inconsistent state, we need to carefully evaluate |
| 101 | * which exceptions from the lower layers allow one to try and delete the |
| 102 | * blobs to rollback the state and progress. |
| 103 | */ |
| 104 | return (result == ipmi_flash::ActionStatus::success); |
| 105 | } |
| 106 | |
| 107 | } // namespace host_tool |