Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 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 | |
Jorge E. Moreira | 3d838dc | 2018-07-10 11:49:38 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
| 20 | #include <functional> |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 21 | #include <string> |
Jorge E. Moreira | 3d838dc | 2018-07-10 11:49:38 -0700 | [diff] [blame] | 22 | #include <vector> |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 23 | |
| 24 | #include "common/libs/fs/shared_fd.h" |
| 25 | #include "common/libs/fs/shared_select.h" |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 26 | |
| 27 | namespace monitor { |
Jorge E. Moreira | 3d838dc | 2018-07-10 11:49:38 -0700 | [diff] [blame] | 28 | |
| 29 | enum BootEvent : int32_t { |
| 30 | BootStarted = 0, |
| 31 | BootCompleted = 1, |
| 32 | BootFailed = 2, |
| 33 | WifiNetworkConnected = 3, |
| 34 | MobileNetworkConnected = 4, |
Jorge E. Moreira | 84f539b | 2019-05-02 17:52:58 -0700 | [diff] [blame] | 35 | AdbdStarted = 5, |
Jorge E. Moreira | 3d838dc | 2018-07-10 11:49:38 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | enum class SubscriptionAction { |
| 39 | ContinueSubscription, |
| 40 | CancelSubscription, |
| 41 | }; |
| 42 | |
| 43 | using BootEventCallback = std::function<SubscriptionAction(BootEvent)>; |
| 44 | |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 45 | // KernelLogServer manages incoming kernel log connection from QEmu. Only accept |
| 46 | // one connection. |
| 47 | class KernelLogServer { |
| 48 | public: |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 49 | KernelLogServer(cvd::SharedFD pipe_fd, |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 50 | const std::string& log_name, |
| 51 | bool deprecated_boot_completed); |
| 52 | |
| 53 | ~KernelLogServer() = default; |
| 54 | |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 55 | // BeforeSelect is Called right before Select() to populate interesting |
| 56 | // SharedFDs. |
| 57 | void BeforeSelect(cvd::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 cvd::SharedFDSet& fd_read); |
| 62 | |
Jorge E. Moreira | 3d838dc | 2018-07-10 11:49:38 -0700 | [diff] [blame] | 63 | void SubscribeToBootEvents(BootEventCallback callback); |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 64 | private: |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 65 | // Respond to message from remote client. |
| 66 | // Returns false, if client disconnected. |
| 67 | bool HandleIncomingMessage(); |
| 68 | |
Jorge E. Moreira | b1ec435 | 2019-06-04 11:43:08 -0700 | [diff] [blame] | 69 | cvd::SharedFD pipe_fd_; |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 70 | cvd::SharedFD log_fd_; |
| 71 | std::string line_; |
| 72 | bool deprecated_boot_completed_; |
Jorge E. Moreira | 3d838dc | 2018-07-10 11:49:38 -0700 | [diff] [blame] | 73 | std::vector<BootEventCallback> subscribers_; |
Ping-Hao Wu | 2571613 | 2017-11-16 11:22:22 -0800 | [diff] [blame] | 74 | |
| 75 | KernelLogServer(const KernelLogServer&) = delete; |
| 76 | KernelLogServer& operator=(const KernelLogServer&) = delete; |
| 77 | }; |
| 78 | |
| 79 | } // namespace monitor |