1 /*
2 * Copyright (C) 2021 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
17 #include "structs.h"
18
19 #include <iomanip>
20
21 namespace android::nl::protocols {
22
flagsToStream(FlagsMap flags)23 AttributeDefinition::ToStream flagsToStream(FlagsMap flags) {
24 return [flags](std::stringstream& ss, const Buffer<nlattr> attr) {
25 auto value = attr.data<uint64_t>().copyFirst();
26 flagsToStream(ss, flags, value);
27 };
28 }
29
flagsToStream(std::stringstream & ss,const FlagsMap & flags,uint64_t val)30 void flagsToStream(std::stringstream& ss, const FlagsMap& flags, uint64_t val) {
31 bool first = true;
32 for (const auto& [flag, name] : flags) {
33 if ((val & flag) != flag) continue;
34 val &= ~flag;
35
36 if (!first) ss << '|';
37 first = false;
38
39 ss << name;
40 }
41
42 if (val == 0) return;
43
44 if (!first) ss << '|';
45 ss << std::hex << val << std::dec;
46 }
47
hwaddrToStream(std::stringstream & ss,const Buffer<nlattr> attr)48 void hwaddrToStream(std::stringstream& ss, const Buffer<nlattr> attr) {
49 ss << std::hex;
50 bool first = true;
51 for (const auto byte : attr.data<uint8_t>().getRaw()) {
52 if (!first) ss << ':';
53 first = false;
54
55 ss << std::setw(2) << unsigned(byte);
56 }
57 ss << std::dec;
58 }
59
60 } // namespace android::nl::protocols
61