1 /*
2  * Copyright (C) 2017 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 <stdint.h>
19 
20 #include <functional>
21 #include <string>
22 #include <vector>
23 
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/fs/shared_select.h"
26 
27 namespace monitor {
28 
29 enum BootEvent : int32_t {
30   BootStarted = 0,
31   BootCompleted = 1,
32   BootFailed = 2,
33   WifiNetworkConnected = 3,
34   MobileNetworkConnected = 4,
35   AdbdStarted = 5,
36 };
37 
38 enum class SubscriptionAction {
39   ContinueSubscription,
40   CancelSubscription,
41 };
42 
43 using BootEventCallback = std::function<SubscriptionAction(BootEvent)>;
44 
45 // KernelLogServer manages incoming kernel log connection from QEmu. Only accept
46 // one connection.
47 class KernelLogServer {
48  public:
49   KernelLogServer(cuttlefish::SharedFD pipe_fd,
50                   const std::string& log_name,
51                   bool deprecated_boot_completed);
52 
53   ~KernelLogServer() = default;
54 
55   // BeforeSelect is Called right before Select() to populate interesting
56   // SharedFDs.
57   void BeforeSelect(cuttlefish::SharedFDSet* fd_read) const;
58 
59   // AfterSelect is Called right after Select() to detect and respond to changes
60   // on affected SharedFDs.
61   void AfterSelect(const cuttlefish::SharedFDSet& fd_read);
62 
63   void SubscribeToBootEvents(BootEventCallback callback);
64  private:
65   // Respond to message from remote client.
66   // Returns false, if client disconnected.
67   bool HandleIncomingMessage();
68 
69   cuttlefish::SharedFD pipe_fd_;
70   cuttlefish::SharedFD log_fd_;
71   std::string line_;
72   bool deprecated_boot_completed_;
73   std::vector<BootEventCallback> subscribers_;
74 
75   KernelLogServer(const KernelLogServer&) = delete;
76   KernelLogServer& operator=(const KernelLogServer&) = delete;
77 };
78 
79 }  // namespace monitor
80