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 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 FastbootdStarted = 11 46 }; 47 48 enum class SubscriptionAction { 49 ContinueSubscription, 50 CancelSubscription, 51 }; 52 53 using EventCallback = std::function<SubscriptionAction(Json::Value)>; 54 55 // KernelLogServer manages an incoming kernel log connection from the VMM. 56 // Only accept one connection. 57 class KernelLogServer { 58 public: 59 KernelLogServer(cuttlefish::SharedFD pipe_fd, const std::string& log_name); 60 61 ~KernelLogServer() = default; 62 63 // BeforeSelect is Called right before Select() to populate interesting 64 // SharedFDs. 65 void BeforeSelect(cuttlefish::SharedFDSet* fd_read) const; 66 67 // AfterSelect is Called right after Select() to detect and respond to changes 68 // on affected SharedFDs. 69 void AfterSelect(const cuttlefish::SharedFDSet& fd_read); 70 71 void SubscribeToEvents(EventCallback callback); 72 73 private: 74 // Respond to message from remote client. 75 // Returns false, if client disconnected. 76 bool HandleIncomingMessage(); 77 78 cuttlefish::SharedFD pipe_fd_; 79 cuttlefish::SharedFD log_fd_; 80 std::string line_; 81 std::vector<EventCallback> subscribers_; 82 83 KernelLogServer(const KernelLogServer&) = delete; 84 KernelLogServer& operator=(const KernelLogServer&) = delete; 85 }; 86 87 } // namespace monitor 88