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         /* sleep for 5 seconds and check 65 times, for a timeout of: 325
42          * seconds.
43          * TODO: make this command line configurable and provide smaller
44          * default value.
45          */
46         static constexpr int commandAttempts = 65;
47         int attempts = 0;
48         bool exitLoop = false;
49 
50         /* Reach back the current status from the verification service output.
51          */
52         while (attempts++ < commandAttempts)
53         {
54             ipmiblob::StatResponse resp = blob->getStat(session);
55 
56             if (resp.metadata.size() != sizeof(std::uint8_t))
57             {
58                 /* TODO: How do we want to handle the verification failures,
59                  * because closing the session to the verify blob has a special
60                  * as-of-yet not fully defined behavior.
61                  */
62                 std::fprintf(stderr, "Received invalid metadata response!!!\n");
63             }
64 
65             result = static_cast<ipmi_flash::ActionStatus>(resp.metadata[0]);
66 
67             switch (result)
68             {
69                 case ipmi_flash::ActionStatus::failed:
70                     std::fprintf(stderr, "failed\n");
71                     exitLoop = true;
72                     break;
73                 case ipmi_flash::ActionStatus::unknown:
74                     std::fprintf(stderr, "other\n");
75                     break;
76                 case ipmi_flash::ActionStatus::running:
77                     std::fprintf(stderr, "running\n");
78                     break;
79                 case ipmi_flash::ActionStatus::success:
80                     std::fprintf(stderr, "success\n");
81                     exitLoop = true;
82                     break;
83                 default:
84                     std::fprintf(stderr, "wat\n");
85             }
86 
87             if (exitLoop)
88             {
89                 break;
90             }
91             std::this_thread::sleep_for(verificationSleep);
92         }
93     }
94     catch (const ipmiblob::BlobException& b)
95     {
96         throw ToolException("blob exception received: " +
97                             std::string(b.what()));
98     }
99 
100     /* TODO: If this is reached and it's not success, it may be worth just
101      * throwing a ToolException with a timeout message specifying the final
102      * read's value.
103      *
104      * TODO: Given that excepting from certain points leaves the BMC update
105      * state machine in an inconsistent state, we need to carefully evaluate
106      * which exceptions from the lower layers allow one to try and delete the
107      * blobs to rollback the state and progress.
108      */
109     return (result == ipmi_flash::ActionStatus::success);
110 }
111 
112 } // namespace host_tool
113