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 "MessageDefinition.h"
18
19 namespace android::nl::protocols {
20
AttributeMap(const std::initializer_list<value_type> attrTypes)21 AttributeMap::AttributeMap(const std::initializer_list<value_type> attrTypes)
22 : std::map<std::optional<nlattrtype_t>, AttributeDefinition>(attrTypes) {}
23
operator [](nlattrtype_t nla_type) const24 const AttributeDefinition AttributeMap::operator[](nlattrtype_t nla_type) const {
25 if (count(nla_type) == 0) {
26 if (count(std::nullopt) == 0) return {std::to_string(nla_type)};
27
28 auto definition = find(std::nullopt)->second;
29 definition.name += std::to_string(nla_type);
30 return definition;
31 }
32 return find(nla_type)->second;
33 }
34
MessageDescriptor(const std::string & name,const MessageDetailsMap && messageDetails,const AttributeMap && attrTypes,size_t contentsSize)35 MessageDescriptor::MessageDescriptor(const std::string& name,
36 const MessageDetailsMap&& messageDetails,
37 const AttributeMap&& attrTypes, size_t contentsSize)
38 : mName(name),
39 mContentsSize(contentsSize),
40 mMessageDetails(messageDetails),
41 mAttributeMap(attrTypes) {}
42
~MessageDescriptor()43 MessageDescriptor::~MessageDescriptor() {}
44
getContentsSize() const45 size_t MessageDescriptor::getContentsSize() const {
46 return mContentsSize;
47 }
48
getMessageDetailsMap() const49 const MessageDescriptor::MessageDetailsMap& MessageDescriptor::getMessageDetailsMap() const {
50 return mMessageDetails;
51 }
52
getAttributeMap() const53 const AttributeMap& MessageDescriptor::getAttributeMap() const {
54 return mAttributeMap;
55 }
56
getMessageDetails(nlmsgtype_t msgtype) const57 MessageDescriptor::MessageDetails MessageDescriptor::getMessageDetails(nlmsgtype_t msgtype) const {
58 const auto it = mMessageDetails.find(msgtype);
59 if (it == mMessageDetails.end()) return {std::to_string(msgtype), MessageGenre::Unknown};
60 return it->second;
61 }
62
getMessageDetails(const std::optional<std::reference_wrapper<MessageDescriptor>> & msgDescMaybe,nlmsgtype_t msgtype)63 MessageDescriptor::MessageDetails MessageDescriptor::getMessageDetails(
64 const std::optional<std::reference_wrapper<MessageDescriptor>>& msgDescMaybe,
65 nlmsgtype_t msgtype) {
66 if (msgDescMaybe.has_value()) return msgDescMaybe->get().getMessageDetails(msgtype);
67 return {std::to_string(msgtype), protocols::MessageGenre::Unknown};
68 }
69
track(const Buffer<nlmsghdr>)70 void MessageDescriptor::track(const Buffer<nlmsghdr> /* hdr */) {}
71
72 } // namespace android::nl::protocols
73