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