• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 val = attr.data<uint64_t>().copyFirst();
26 
27         bool first = true;
28         for (const auto& [flag, name] : flags) {
29             if ((val & flag) != flag) continue;
30             val &= ~flag;
31 
32             if (!first) ss << '|';
33             first = false;
34 
35             ss << name;
36         }
37 
38         if (val == 0) return;
39 
40         if (!first) ss << '|';
41         ss << std::hex << val << std::dec;
42     };
43 }
44 
hwaddrToStream(std::stringstream & ss,const Buffer<nlattr> attr)45 void hwaddrToStream(std::stringstream& ss, const Buffer<nlattr> attr) {
46     ss << std::hex;
47     bool first = true;
48     for (const auto byte : attr.data<uint8_t>().getRaw()) {
49         if (!first) ss << ':';
50         first = false;
51 
52         ss << std::setw(2) << unsigned(byte);
53     }
54     ss << std::dec;
55 }
56 
57 }  // namespace android::nl::protocols
58