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