summaryrefslogtreecommitdiffstats
blob: 1c3dfefdf90da60971a4a6cd4c75eaf7dd1249ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <getopt.h>

#include <android-base/strings.h>
#include <vintf/VintfObject.h>
#include <vintf/parse_string.h>
#include <vintf/parse_xml.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace ::android::vintf;

static const std::string kColumnSeperator = "   ";

std::string existString(bool value) {
    return value ? "GOOD" : "DOES NOT EXIST";
}

std::string compatibleString(int32_t value) {
    switch (value) {
        case COMPATIBLE:
            return "GOOD";
        case INCOMPATIBLE:
            return "INCOMPATIBLE";
        default:
            return strerror(-value);
    }
}

std::string boolCompatString(bool value) {
    return compatibleString(value ? COMPATIBLE : INCOMPATIBLE);
}

std::string deprecateString(int32_t value) {
    switch (value) {
        case NO_DEPRECATED_HALS:
            return "GOOD";
        case DEPRECATED:
            return "DEPRECATED";
        default:
            return strerror(-value);
    }
}

enum Status : int {
    OK = 0,
    USAGE,
};

struct ParsedOptions {
    bool verbose = false;
};

struct Option {
    char shortOption = '\0';
    std::string longOption;
    std::string help;
    std::function<Status(ParsedOptions*)> op;
};

std::string getShortOptions(const std::vector<Option>& options) {
    std::stringstream ret;
    for (const auto& e : options)
        if (e.shortOption != '\0') ret << e.shortOption;
    return ret.str();
}

std::unique_ptr<struct option[]> getLongOptions(const std::vector<Option>& options,
                                                int* longOptFlag) {
    std::unique_ptr<struct option[]> ret{new struct option[options.size() + 1]};
    int i = 0;
    for (const auto& e : options) {
        ret[i].name = e.longOption.c_str();
        ret[i].has_arg = no_argument;
        ret[i].flag = longOptFlag;
        ret[i].val = i;

        i++;
    }
    // getopt_long last option has all zeros
    ret[i].name = NULL;
    ret[i].has_arg = 0;
    ret[i].flag = NULL;
    ret[i].val = 0;

    return ret;
}

Status parseOptions(int argc, char** argv, const std::vector<Option>& options, ParsedOptions* out) {
    int longOptFlag;
    std::unique_ptr<struct option[]> longOptions = getLongOptions(options, &longOptFlag);
    std::string shortOptions = getShortOptions(options);
    int optionIndex;
    for (;;) {
        int c = getopt_long(argc, argv, shortOptions.c_str(), longOptions.get(), &optionIndex);
        if (c == -1) {
            break;
        }
        const Option* found = nullptr;
        for (size_t i = 0; i < options.size(); ++i)
            if ((c == 0 && longOptFlag == static_cast<int>(i)) ||
                (c != 0 && c == options[i].shortOption))

                found = &options[i];

        if (found == nullptr) {
            // see unrecognized options
            std::cerr << "unrecognized option `" << argv[optind - 1] << "'" << std::endl;
            return USAGE;
        }

        Status status = found->op(out);
        if (status != OK) return status;
    }
    if (optind < argc) {
        // see non option
        std::cerr << "unrecognized option `" << argv[optind] << "'" << std::endl;
        return USAGE;
    }
    return OK;
}

void usage(char* me, const std::vector<Option>& options) {
    std::cerr << me << ": dump VINTF metadata via libvintf." << std::endl;
    for (const auto& e : options) {
        if (e.help.empty()) continue;
        std::cerr << "        ";
        if (e.shortOption != '\0') std::cerr << "-" << e.shortOption;
        if (e.shortOption != '\0' && !e.longOption.empty()) std::cerr << ", ";
        if (!e.longOption.empty()) std::cerr << "--" << e.longOption;
        std::cerr << ": "
                  << android::base::Join(android::base::Split(e.help, "\n"), "\n            ")
                  << std::endl;
    }
}

struct TableRow {
    // Whether the HAL version is in device manifest, framework manifest, device compatibility
    // matrix, framework compatibility matrix, respectively.
    bool dm = false;
    bool fm = false;
    bool dcm = false;
    bool fcm = false;
    // If the HAL version is in device / framework compatibility matrix, whether it is required
    // or not.
    bool required = false;

    // Return true if:
    // - not a required HAL version; OR
    // - required in device matrix and framework manifest;
    // - required in framework matrix and device manifest.
    bool meetsReqeuirement() const {
        if (!required) return true;
        if (dcm && !fm) return false;
        if (fcm && !dm) return false;
        return true;
    }
};

std::ostream& operator<<(std::ostream& out, const TableRow& row) {
    return out << (row.required ? "R" : " ") << (row.meetsReqeuirement() ? " " : "!")
               << kColumnSeperator << (row.dm ? "DM" : "  ") << kColumnSeperator
               << (row.fm ? "FM" : "  ") << kColumnSeperator << (row.fcm ? "FCM" : "   ")
               << kColumnSeperator << (row.dcm ? "DCM" : "   ");
}

using RowMutator = std::function<void(TableRow*)>;
using Table = std::map<std::string, TableRow>;

// Insert each fqInstanceName foo@x.y::IFoo/instance to the table by inserting the key
// if it does not exist and setting the corresponding indicator (as specified by "mutate").
void insert(const HalManifest* manifest, Table* table, const RowMutator& mutate) {
    if (manifest == nullptr) return;
    manifest->forEachInstance([&](const auto& manifestInstance) {
        std::string key = toFQNameString(manifestInstance.package(), manifestInstance.version(),
                                         manifestInstance.interface(), manifestInstance.instance());
        mutate(&(*table)[key]);
        return true;
    });
}

void insert(const CompatibilityMatrix* matrix, Table* table, const RowMutator& mutate) {
    if (matrix == nullptr) return;
    matrix->forEachInstance([&](const auto& matrixInstance) {
        for (auto minorVer = matrixInstance.versionRange().minMinor;
             minorVer <= matrixInstance.versionRange().maxMinor; ++minorVer) {
            std::string key = toFQNameString(
                matrixInstance.package(), Version{matrixInstance.versionRange().majorVer, minorVer},
                matrixInstance.interface(), matrixInstance.instance());
            auto it = table->find(key);
            if (it == table->end()) {
                mutate(&(*table)[key]);
            } else {
                mutate(&it->second);
                if (minorVer == matrixInstance.versionRange().minMinor) {
                    it->second.required = !matrixInstance.optional();
                }
            }
        }
        return true;
    });
}

Table generateHalSummary(const HalManifest* vm, const HalManifest* fm,
                         const CompatibilityMatrix* vcm, const CompatibilityMatrix* fcm) {
    Table table;
    insert(vm, &table, [](auto* row) { row->dm = true; });
    insert(fm, &table, [](auto* row) { row->fm = true; });
    insert(vcm, &table, [](auto* row) { row->dcm = true; });
    insert(fcm, &table, [](auto* row) { row->fcm = true; });

    return table;
}

static const std::vector<Option> gAvailableOptions{
    {'h', "help", "Print help message.", [](auto) { return USAGE; }},
    {'v', "verbose", "Dump detailed and raw content, including kernel configurations", [](auto o) {
         o->verbose = true;
         return OK;
     }}};
// A convenience binary to dump information available through libvintf.
int main(int argc, char** argv) {
    ParsedOptions options;
    Status status = parseOptions(argc, argv, gAvailableOptions, &options);
    if (status == USAGE) usage(argv[0], gAvailableOptions);
    if (status != OK) return status;

    auto vm = VintfObject::GetDeviceHalManifest();
    auto fm = VintfObject::GetFrameworkHalManifest();
    auto vcm = VintfObject::GetDeviceCompatibilityMatrix();
    auto fcm = VintfObject::GetFrameworkCompatibilityMatrix();
    auto ki = VintfObject::GetRuntimeInfo();

    if (!options.verbose) {
        std::cout << "======== HALs =========" << std::endl
                  << "R: required. (empty): optional or missing from matrices. "
                  << "!: required and not in manifest." << std::endl
                  << "DM: device manifest. FM: framework manifest." << std::endl
                  << "FCM: framework compatibility matrix. DCM: device compatibility matrix."
                  << std::endl
                  << std::endl;
        auto table = generateHalSummary(vm.get(), fm.get(), vcm.get(), fcm.get());

        for (const auto& pair : table)
            std::cout << pair.second << kColumnSeperator << pair.first << std::endl;

        std::cout << std::endl;
    }

    SerializeFlags flags = SerializeFlag::EVERYTHING;
    if (!options.verbose) {
        flags |= SerializeFlag::NO_HALS;
        flags |= SerializeFlag::NO_KERNEL;
    }
    std::cout << "======== Device HAL Manifest =========" << std::endl;
    if (vm != nullptr) std::cout << gHalManifestConverter(*vm, flags);
    std::cout << "======== Framework HAL Manifest =========" << std::endl;
    if (fm != nullptr) std::cout << gHalManifestConverter(*fm, flags);
    std::cout << "======== Device Compatibility Matrix =========" << std::endl;
    if (vcm != nullptr) std::cout << gCompatibilityMatrixConverter(*vcm, flags);
    std::cout << "======== Framework Compatibility Matrix =========" << std::endl;
    if (fcm != nullptr) std::cout << gCompatibilityMatrixConverter(*fcm, flags);

    std::cout << "======== Runtime Info =========" << std::endl;
    if (ki != nullptr) std::cout << dump(*ki, options.verbose);

    std::cout << std::endl;

    std::cout << "======== Summary =========" << std::endl;
    std::cout << "Device Manifest?    " << existString(vm != nullptr) << std::endl
              << "Device Matrix?      " << existString(vcm != nullptr) << std::endl
              << "Framework Manifest? " << existString(fm != nullptr) << std::endl
              << "Framework Matrix?   " << existString(fcm != nullptr) << std::endl;
    std::string error;
    if (vm && fcm) {
        bool compatible = vm->checkCompatibility(*fcm, &error);
        std::cout << "Device HAL Manifest <==> Framework Compatibility Matrix? "
                  << boolCompatString(compatible);
        if (!compatible)
            std::cout << ", " << error;
        std::cout << std::endl;
    }
    if (fm && vcm) {
        bool compatible = fm->checkCompatibility(*vcm, &error);
        std::cout << "Framework HAL Manifest <==> Device Compatibility Matrix? "
                  << boolCompatString(compatible);
        if (!compatible)
            std::cout << ", " << error;
        std::cout << std::endl;
    }
    if (ki && fcm) {
        bool compatible = ki->checkCompatibility(*fcm, &error);
        std::cout << "Runtime info <==> Framework Compatibility Matrix?        "
                  << boolCompatString(compatible);
        if (!compatible) std::cout << ", " << error;
        std::cout << std::endl;
    }

    {
        auto compatible = VintfObject::CheckCompatibility({}, &error);
        std::cout << "VintfObject::CheckCompatibility?                         "
                  << compatibleString(compatible);
        if (compatible != COMPATIBLE) std::cout << ", " << error;
        std::cout << std::endl;
    }

    if (vm && fcm) {
        auto deprecate = VintfObject::CheckDeprecation(&error);
        std::cout << "VintfObject::CheckDeprecation (against device manifest)? "
                  << deprecateString(deprecate);
        if (deprecate != NO_DEPRECATED_HALS) std::cout << ", " << error;
        std::cout << std::endl;
    }
}