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 #pragma once
18 
19 #include "MessageDefinition.h"
20 #include "common/Empty.h"
21 #include "common/Error.h"
22 
23 #include <libnl++/types.h>
24 
25 #include <string>
26 #include <vector>
27 
28 namespace android::nl::protocols {
29 
30 /**
31  * Netlink-based protocol definition.
32  *
33  * Usually it's just an id/name and a list of supported messages.
34  */
35 class NetlinkProtocol {
36   public:
37     virtual ~NetlinkProtocol();
38 
39     int getProtocol() const;
40 
41     const std::string& getName() const;
42 
43     virtual const std::optional<std::reference_wrapper<MessageDescriptor>> getMessageDescriptor(
44             nlmsgtype_t nlmsg_type);
45 
46   protected:
47     typedef std::vector<std::shared_ptr<MessageDescriptor>> MessageDescriptorList;
48 
49     NetlinkProtocol(int protocol, const std::string& name,
50                     const MessageDescriptorList&& messageDescrs);
51 
52   private:
53     typedef std::map<nlmsgtype_t, std::shared_ptr<MessageDescriptor>> MessageDescriptorMap;
54 
55     const int mProtocol;
56     const std::string mName;
57     const MessageDescriptorMap mMessageDescrs;
58 
59     static MessageDescriptorMap toMap(const MessageDescriptorList& descrs, int protocol);
60 };
61 
62 }  // namespace android::nl::protocols
63