1 /*
2 * Copyright (C) 2020 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 "GenericMessageBase.h"
18
19 namespace android::nl::protocols::generic {
20
GenericMessageBase(nlmsgtype_t msgtype,const std::string && msgname,const std::initializer_list<GenericCommandNameMap::value_type> commandNames,const std::initializer_list<AttributeMap::value_type> attrTypes)21 GenericMessageBase::GenericMessageBase(
22 nlmsgtype_t msgtype, const std::string&& msgname,
23 const std::initializer_list<GenericCommandNameMap::value_type> commandNames,
24 const std::initializer_list<AttributeMap::value_type> attrTypes)
25 : MessageDefinition<genlmsghdr>(msgname, {{msgtype, {msgname, MessageGenre::Unknown}}},
26 attrTypes),
27 mCommandNames(commandNames) {}
28
toStream(std::stringstream & ss,const genlmsghdr & data) const29 void GenericMessageBase::toStream(std::stringstream& ss, const genlmsghdr& data) const {
30 const auto commandNameIt = mCommandNames.find(data.cmd);
31 const auto commandName = (commandNameIt == mCommandNames.end())
32 ? std::nullopt
33 : std::optional<std::string>(commandNameIt->second);
34
35 if (commandName.has_value() && data.version == 1 && data.reserved == 0) {
36 // short version
37 ss << *commandName;
38 return;
39 }
40
41 ss << "genlmsghdr{";
42 if (commandName.has_value()) {
43 ss << "cmd=" << unsigned(data.cmd);
44 } else {
45 ss << "cmd=" << *commandName;
46 }
47 ss << ", version=" << unsigned(data.version);
48 if (data.reserved != 0) ss << ", reserved=" << data.reserved;
49 ss << "}";
50 }
51
52 } // namespace android::nl::protocols::generic
53