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 "Mac80211hwsim.h"
18
19 #include "../../structs.h"
20 #include "common.h"
21
22 #include <libnl++/generic/families/mac80211_hwsim.h>
23
24 namespace android::nl::protocols::generic::families {
25
26 using DataType = AttributeDefinition::DataType;
27 using Flags = AttributeDefinition::Flags;
28
29 static void hwsim_tx_rateToStream(std::stringstream& ss, const Buffer<nlattr> attr);
30
31 static const FlagsMap txControlFlags{
32 {HWSIM_TX_CTL_REQ_TX_STATUS, "REQ_TX"},
33 {HWSIM_TX_CTL_NO_ACK, "NO_ACK"},
34 {HWSIM_TX_STAT_ACK, "ACK"},
35 };
36
37 // clang-format off
Mac80211hwsim(nlmsgtype_t familyId)38 Mac80211hwsim::Mac80211hwsim(nlmsgtype_t familyId) : GenericMessageBase(familyId, "hwsim", {
39 {HWSIM_CMD_UNSPEC, "UNSPEC"},
40 {HWSIM_CMD_REGISTER, "REGISTER"},
41 {HWSIM_CMD_FRAME, "FRAME"},
42 {HWSIM_CMD_TX_INFO_FRAME, "TX_INFO_FRAME"},
43 {HWSIM_CMD_NEW_RADIO, "NEW_RADIO"},
44 {HWSIM_CMD_DEL_RADIO, "DEL_RADIO"},
45 {HWSIM_CMD_GET_RADIO, "GET_RADIO"},
46 {HWSIM_CMD_ADD_MAC_ADDR, "ADD_MAC_ADDR"},
47 {HWSIM_CMD_DEL_MAC_ADDR, "DEL_MAC_ADDR"},
48 }, {
49 {HWSIM_ATTR_UNSPEC, {"UNSPEC"}},
50 {HWSIM_ATTR_ADDR_RECEIVER, {"ADDR_RECEIVER", DataType::Struct, hwaddrToStream}},
51 {HWSIM_ATTR_ADDR_TRANSMITTER, {"ADDR_TRANSMITTER", DataType::Struct, hwaddrToStream}},
52 {HWSIM_ATTR_FRAME, {"FRAME", DataType::Raw, AttributeMap{}, Flags::Verbose}},
53 {HWSIM_ATTR_FLAGS, {"FLAGS", DataType::Struct, flagsToStream(txControlFlags)}},
54 {HWSIM_ATTR_RX_RATE, {"RX_RATE", DataType::Uint}},
55 {HWSIM_ATTR_SIGNAL, {"SIGNAL", DataType::Uint}},
56 {HWSIM_ATTR_TX_INFO, {"TX_INFO", DataType::Struct, hwsim_tx_rateToStream}},
57 {HWSIM_ATTR_COOKIE, {"COOKIE", DataType::Uint}},
58 {HWSIM_ATTR_CHANNELS, {"CHANNELS", DataType::Uint}},
59 {HWSIM_ATTR_RADIO_ID, {"RADIO_ID", DataType::Uint}},
60 {HWSIM_ATTR_REG_HINT_ALPHA2, {"REG_HINT_ALPHA2", DataType::String}},
61 {HWSIM_ATTR_REG_CUSTOM_REG, {"REG_CUSTOM_REG", DataType::Uint}},
62 {HWSIM_ATTR_REG_STRICT_REG, {"REG_STRICT_REG", DataType::Flag}},
63 {HWSIM_ATTR_SUPPORT_P2P_DEVICE, {"SUPPORT_P2P_DEVICE", DataType::Flag}},
64 {HWSIM_ATTR_USE_CHANCTX, {"USE_CHANCTX", DataType::Flag}},
65 {HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE, {"DESTROY_RADIO_ON_CLOSE", DataType::Flag}},
66 {HWSIM_ATTR_RADIO_NAME, {"RADIO_NAME", DataType::String}},
67 {HWSIM_ATTR_NO_VIF, {"NO_VIF", DataType::Flag}},
68 {HWSIM_ATTR_FREQ, {"FREQ", DataType::Uint}},
69 {HWSIM_ATTR_PAD, {"PAD", DataType::Uint}},
70 {HWSIM_ATTR_TX_INFO_FLAGS, {"TX_INFO_FLAGS"}}, // hwsim_tx_rate_flag
71 {HWSIM_ATTR_PERM_ADDR, {"PERM_ADDR"}},
72 {HWSIM_ATTR_IFTYPE_SUPPORT, {"IFTYPE_SUPPORT", DataType::Uint}}, // NL80211_IFTYPE_STATION etc
73 {HWSIM_ATTR_CIPHER_SUPPORT, {"CIPHER_SUPPORT", DataType::Struct, arrayToStream<int32_t>}},
74 }) {}
75 // clang-format on
76
hwsim_tx_rateToStream(std::stringstream & ss,const Buffer<nlattr> attr)77 static void hwsim_tx_rateToStream(std::stringstream& ss, const Buffer<nlattr> attr) {
78 ss << '{';
79 bool first = true;
80 for (const auto rate : attr.data<hwsim_tx_rate>().getRaw()) {
81 if (rate.idx == -1) continue;
82
83 ss << (int)rate.idx << ": " << (unsigned)rate.count;
84
85 if (!first) ss << ", ";
86 first = false;
87 }
88 ss << '}';
89 }
90
91 } // namespace android::nl::protocols::generic::families
92