• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 #pragma once
17 
18 #include <memory>
19 #include <thread>
20 
21 #include "common/libs/fs/shared_fd.h"
22 #include "host/commands/run_cvd/runner_defs.h"
23 
24 namespace cuttlefish {
25 
26 // Maintains the state of the boot process, once a final state is reached
27 // (success or failure) it sends the appropriate exit code to the foreground
28 // launcher process
29 class CvdBootStateMachine {
30  public:
31   CvdBootStateMachine(SharedFD fg_launcher_pipe, SharedFD reboot_notification,
32                       SharedFD boot_events_pipe);
33   ~CvdBootStateMachine();
34 
35  private:
36   // Returns true if the machine is left in a final state
37   bool OnBootEvtReceived(SharedFD boot_events_pipe);
38   bool BootCompleted() const;
39   bool BootFailed() const;
40 
41   void SendExitCode(RunnerExitCodes exit_code, SharedFD fd);
42   bool MaybeWriteNotification();
43 
44   std::thread boot_event_handler_;
45   SharedFD fg_launcher_pipe_;
46   SharedFD reboot_notification_;
47   int state_;
48   static const int kBootStarted = 0;
49   static const int kGuestBootCompleted = 1 << 0;
50   static const int kGuestBootFailed = 1 << 1;
51 };
52 
53 }  // namespace cuttlefish
54