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 <json/json.h> 25 26 #include "common/libs/fs/shared_fd.h" 27 #include "common/libs/fs/shared_select.h" 28 29 namespace cuttlefish::monitor { 30 31 enum Event : int32_t { 32 BootStarted = 0, 33 BootCompleted = 1, 34 BootFailed = 2, 35 WifiNetworkConnected = 3, 36 MobileNetworkConnected = 4, 37 AdbdStarted = 5, 38 ScreenChanged = 6, 39 EthernetNetworkConnected = 7, 40 KernelLoaded = 8, // BootStarted actually comes quite late in the boot. 41 BootloaderLoaded = 9, /* BootloaderLoaded is the earliest possible indicator 42 * that we're booting a device. 43 */ 44 DisplayPowerModeChanged = 10, 45 FastbootStarted = 11, 46 BootPending = 12, 47 HibernationExited = 13, 48 }; 49 50 enum class SubscriptionAction { 51 ContinueSubscription, 52 CancelSubscription, 53 }; 54 55 using EventCallback = std::function<SubscriptionAction(Json::Value)>; 56 57 // KernelLogServer manages an incoming kernel log connection from the VMM. 58 // Only accept one connection. 59 class KernelLogServer { 60 public: 61 KernelLogServer(SharedFD pipe_fd, const std::string& log_name); 62 63 ~KernelLogServer() = default; 64 65 // BeforeSelect is Called right before Select() to populate interesting 66 // SharedFDs. 67 void BeforeSelect(SharedFDSet* fd_read) const; 68 69 // AfterSelect is Called right after Select() to detect and respond to changes 70 // on affected SharedFDs. 71 void AfterSelect(const SharedFDSet& fd_read); 72 73 void SubscribeToEvents(EventCallback callback); 74 75 private: 76 // Respond to message from remote client. 77 // Returns false, if client disconnected. 78 bool HandleIncomingMessage(); 79 80 SharedFD pipe_fd_; 81 SharedFD log_fd_; 82 std::string line_; 83 std::vector<EventCallback> subscribers_; 84 85 KernelLogServer(const KernelLogServer&) = delete; 86 KernelLogServer& operator=(const KernelLogServer&) = delete; 87 }; 88 89 } // namespace cuttlefish::monitor 90