• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 <atomic>
20 #include <functional>
21 #include <mutex>
22 #include <stdint.h>
23 #include <unordered_map>
24 
25 class NetlinkMessage;
26 
27 class Netlink {
28 public:
29     using ReplyHandler = std::function<void (const NetlinkMessage&)>;
30     using StopHandler = std::function<void ()>;
31     Netlink();
32     ~Netlink();
33 
34     bool init();
35     void stop(StopHandler stopHandler);
36 
37     bool eventLoop();
38 
39     uint32_t getSequenceNumber();
40 
41     bool sendMessage(const NetlinkMessage& message, ReplyHandler handler);
42 private:
43     Netlink(const Netlink&) = delete;
44     Netlink& operator=(const Netlink&) = delete;
45 
46     bool readNetlinkMessage(int fd);
47     bool readControlMessage();
48 
49     void notifyHandler(const char* data, size_t size);
50 
51     std::atomic<uint32_t> mNextSequenceNumber;
52     int mSocket;
53     int mControlPipe[2];
54     // Map sequence number to reply handler
55     std::unordered_map<uint32_t, ReplyHandler> mHandlers;
56     std::mutex mHandlersMutex;
57     std::mutex mStopHandlerMutex;
58     StopHandler mStopHandler;
59 };
60 
61