| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 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 |  | 
| Steven Moreland | 0b8e387 | 2019-06-25 08:54:34 -0700 | [diff] [blame] | 17 | #pragma once | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 18 |  | 
 | 19 | #include <stdint.h> | 
 | 20 |  | 
 | 21 | #include <string> | 
 | 22 | #include <vector> | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 23 | #include <iostream> | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 24 |  | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 25 | #include <procpartition/procpartition.h> | 
| Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 26 | #include <vintf/Arch.h> | 
| Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 27 | #include <vintf/Transport.h> | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 28 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 29 | #include "TextTable.h" | 
 | 30 |  | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 31 | namespace android { | 
 | 32 | namespace lshal { | 
 | 33 |  | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 34 | using android::procpartition::Partition; | 
| Devin Moore | c03e3aa | 2020-12-11 15:11:17 -0800 | [diff] [blame] | 35 | using Pids = std::vector<pid_t>; | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 36 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 37 | enum class TableColumnType : unsigned int { | 
| Yifan Hong | 30528a2 | 2020-08-07 18:24:06 -0700 | [diff] [blame] | 38 |     INTERFACE_NAME = 0, | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 39 |     TRANSPORT, | 
 | 40 |     SERVER_PID, | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 41 |     SERVER_ADDR, | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 42 |     ARCH, | 
 | 43 |     THREADS, | 
| Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 44 |     RELEASED, | 
 | 45 |     HASH, | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 46 |     VINTF, | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 47 |     SERVICE_STATUS, | 
| Yifan Hong | 30528a2 | 2020-08-07 18:24:06 -0700 | [diff] [blame] | 48 |     CLIENT_PIDS, | 
 | 49 |  | 
 | 50 |     // Not a real TableColumnType. Used to determine all TableColumnTypes. | 
 | 51 |     LAST, | 
 | 52 |  | 
 | 53 |     // Not included in all TableColumnTypes because they replace *PID(S) when the | 
 | 54 |     // --cmdline option is set. | 
 | 55 |     SERVER_CMD, | 
 | 56 |     CLIENT_CMDS, | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 57 | }; | 
 | 58 |  | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 59 | enum : unsigned int { | 
 | 60 |     VINTF_INFO_EMPTY = 0, | 
 | 61 |     DEVICE_MANIFEST = 1 << 0, | 
 | 62 |     DEVICE_MATRIX = 1 << 1, | 
 | 63 |     FRAMEWORK_MANIFEST = 1 << 2, | 
 | 64 |     FRAMEWORK_MATRIX = 1 << 3, | 
 | 65 | }; | 
 | 66 | using VintfInfo = unsigned int; | 
 | 67 |  | 
| Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 68 | enum { | 
 | 69 |     NO_PID = -1, | 
 | 70 |     NO_PTR = 0 | 
 | 71 | }; | 
 | 72 |  | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 73 | enum class ServiceStatus { | 
 | 74 |     UNKNOWN, // For passthrough | 
 | 75 |     ALIVE, | 
 | 76 |     NON_RESPONSIVE, // registered but not respond to calls | 
 | 77 |     DECLARED, // in VINTF manifest | 
 | 78 | }; | 
 | 79 | std::string to_string(ServiceStatus s); | 
 | 80 |  | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 81 | struct TableEntry { | 
| Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 82 |     std::string interfaceName{}; | 
| Yifan Hong | 8304e41 | 2018-05-25 15:05:36 -0700 | [diff] [blame] | 83 |     vintf::Transport transport{vintf::Transport::EMPTY}; | 
| Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 84 |     int32_t serverPid{NO_PID}; | 
 | 85 |     uint32_t threadUsage{0}; | 
 | 86 |     uint32_t threadCount{0}; | 
 | 87 |     std::string serverCmdline{}; | 
 | 88 |     uint64_t serverObjectAddress{NO_PTR}; | 
 | 89 |     Pids clientPids{}; | 
 | 90 |     std::vector<std::string> clientCmdlines{}; | 
| Yifan Hong | 0ad64f5 | 2018-05-25 15:29:17 -0700 | [diff] [blame] | 91 |     vintf::Arch arch{vintf::Arch::ARCH_EMPTY}; | 
| Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 92 |     // empty: unknown, all zeros: unreleased, otherwise: released | 
 | 93 |     std::string hash{}; | 
| Yifan Hong | f31aa05 | 2018-02-02 15:17:51 -0800 | [diff] [blame] | 94 |     Partition partition{Partition::UNKNOWN}; | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 95 |     VintfInfo vintfInfo{VINTF_INFO_EMPTY}; | 
| Yifan Hong | 13ba0a9 | 2018-06-25 16:15:56 -0700 | [diff] [blame] | 96 |     // true iff hwbinder and service started | 
 | 97 |     ServiceStatus serviceStatus{ServiceStatus::UNKNOWN}; | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 98 |  | 
 | 99 |     static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) { | 
 | 100 |         return a.interfaceName < b.interfaceName; | 
 | 101 |     }; | 
 | 102 |     static bool sortByServerPid(const TableEntry &a, const TableEntry &b) { | 
 | 103 |         return a.serverPid < b.serverPid; | 
 | 104 |     }; | 
| Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 105 |  | 
 | 106 |     std::string getThreadUsage() const { | 
 | 107 |         if (threadCount == 0) { | 
 | 108 |             return "N/A"; | 
 | 109 |         } | 
 | 110 |  | 
 | 111 |         return std::to_string(threadUsage) + "/" + std::to_string(threadCount); | 
 | 112 |     } | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 113 |  | 
| Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 114 |     std::string isReleased() const; | 
 | 115 |  | 
| Yifan Hong | bdf44f8 | 2018-05-25 14:20:00 -0700 | [diff] [blame] | 116 |     std::string getVintfInfo() const; | 
 | 117 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 118 |     std::string getField(TableColumnType type) const; | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 119 |  | 
 | 120 |     bool operator==(const TableEntry& other) const; | 
 | 121 |     std::string to_string() const; | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 122 | }; | 
 | 123 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 124 | using SelectedColumns = std::vector<TableColumnType>; | 
| Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 125 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 126 | class Table { | 
 | 127 | public: | 
 | 128 |     using Entries = std::vector<TableEntry>; | 
 | 129 |  | 
 | 130 |     Entries::iterator begin() { return mEntries.begin(); } | 
 | 131 |     Entries::const_iterator begin() const { return mEntries.begin(); } | 
 | 132 |     Entries::iterator end() { return mEntries.end(); } | 
 | 133 |     Entries::const_iterator end() const { return mEntries.end(); } | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 134 |     size_t size() const { return mEntries.size(); } | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 135 |  | 
 | 136 |     void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); } | 
 | 137 |  | 
 | 138 |     void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; } | 
 | 139 |     const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; } | 
 | 140 |  | 
 | 141 |     void setDescription(std::string&& d) { mDescription = std::move(d); } | 
 | 142 |  | 
 | 143 |     // Write table content. | 
 | 144 |     TextTable createTextTable(bool neat = true, | 
 | 145 |         const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const; | 
 | 146 |  | 
 | 147 | private: | 
 | 148 |     std::string mDescription; | 
 | 149 |     Entries mEntries; | 
 | 150 |     SelectedColumns mSelectedColumns; | 
| Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 151 | }; | 
 | 152 |  | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 153 | using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>; | 
 | 154 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 155 | class MergedTable { | 
 | 156 | public: | 
| Chih-Hung Hsieh | 45e31c7 | 2018-12-20 15:47:01 -0800 | [diff] [blame] | 157 |     explicit MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {} | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 158 |     TextTable createTextTable(); | 
 | 159 | private: | 
 | 160 |     std::vector<const Table*> mTables; | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 161 | }; | 
 | 162 |  | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 163 | }  // namespace lshal | 
 | 164 | }  // namespace android |