blob: f5709eec60ec66062be6764a474864b923b59b2b [file] [log] [blame]
Ping-Hao Wu25716132017-11-16 11:22:22 -08001/*
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. Moreira3d838dc2018-07-10 11:49:38 -070018#include <stdint.h>
19
20#include <functional>
Ping-Hao Wu25716132017-11-16 11:22:22 -080021#include <string>
Jorge E. Moreira3d838dc2018-07-10 11:49:38 -070022#include <vector>
Ping-Hao Wu25716132017-11-16 11:22:22 -080023
24#include "common/libs/fs/shared_fd.h"
25#include "common/libs/fs/shared_select.h"
Ping-Hao Wu25716132017-11-16 11:22:22 -080026
27namespace monitor {
Jorge E. Moreira3d838dc2018-07-10 11:49:38 -070028
29enum BootEvent : int32_t {
30 BootStarted = 0,
31 BootCompleted = 1,
32 BootFailed = 2,
33 WifiNetworkConnected = 3,
34 MobileNetworkConnected = 4,
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070035 AdbdStarted = 5,
Jorge E. Moreira3d838dc2018-07-10 11:49:38 -070036};
37
38enum class SubscriptionAction {
39 ContinueSubscription,
40 CancelSubscription,
41};
42
43using BootEventCallback = std::function<SubscriptionAction(BootEvent)>;
44
Ping-Hao Wu25716132017-11-16 11:22:22 -080045// KernelLogServer manages incoming kernel log connection from QEmu. Only accept
46// one connection.
47class KernelLogServer {
48 public:
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070049 KernelLogServer(cvd::SharedFD pipe_fd,
Ping-Hao Wu25716132017-11-16 11:22:22 -080050 const std::string& log_name,
51 bool deprecated_boot_completed);
52
53 ~KernelLogServer() = default;
54
Ping-Hao Wu25716132017-11-16 11:22:22 -080055 // 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. Moreira3d838dc2018-07-10 11:49:38 -070063 void SubscribeToBootEvents(BootEventCallback callback);
Ping-Hao Wu25716132017-11-16 11:22:22 -080064 private:
Ping-Hao Wu25716132017-11-16 11:22:22 -080065 // Respond to message from remote client.
66 // Returns false, if client disconnected.
67 bool HandleIncomingMessage();
68
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -070069 cvd::SharedFD pipe_fd_;
Ping-Hao Wu25716132017-11-16 11:22:22 -080070 cvd::SharedFD log_fd_;
71 std::string line_;
72 bool deprecated_boot_completed_;
Jorge E. Moreira3d838dc2018-07-10 11:49:38 -070073 std::vector<BootEventCallback> subscribers_;
Ping-Hao Wu25716132017-11-16 11:22:22 -080074
75 KernelLogServer(const KernelLogServer&) = delete;
76 KernelLogServer& operator=(const KernelLogServer&) = delete;
77};
78
79} // namespace monitor