diff options
author | Ningyuan Wang | 2016-08-25 18:45:05 -0500 |
---|---|---|
committer | Ningyuan Wang | 2016-08-26 16:17:23 -0500 |
commit | 84b17dff63534af6957003f89fd4110c06d100e6 (patch) | |
tree | c6e95c8864ae0abf372d40201e13a2cc82df8be1 /net | |
parent | 537c1ba391765b3aa7750450c208c650a02c04f0 (diff) | |
download | system-connectivity-wificond-84b17dff63534af6957003f89fd4110c06d100e6.tar.gz system-connectivity-wificond-84b17dff63534af6957003f89fd4110c06d100e6.tar.xz system-connectivity-wificond-84b17dff63534af6957003f89fd4110c06d100e6.zip |
Add wrapper functions of SendPacketAndGetResponses
This can help simplify the existing code of handling netlink
responses.
BUG=31111084
TEST=compile
Change-Id: I9a48ddb9af805c59c2733b7c1e2413b3f049edda
Diffstat (limited to 'net')
-rw-r--r-- | net/netlink_manager.cpp | 44 | ||||
-rw-r--r-- | net/netlink_manager.h | 20 |
2 files changed, 63 insertions, 1 deletions
diff --git a/net/netlink_manager.cpp b/net/netlink_manager.cpp index 9d816dd..73b97df 100644 --- a/net/netlink_manager.cpp +++ b/net/netlink_manager.cpp | |||
@@ -288,6 +288,50 @@ bool NetlinkManager::SendMessageAndGetResponses( | |||
288 | return true; | 288 | return true; |
289 | } | 289 | } |
290 | 290 | ||
291 | bool NetlinkManager::SendMessageAndGetSingleResponse( | ||
292 | const NL80211Packet& packet, | ||
293 | unique_ptr<const NL80211Packet>* response) { | ||
294 | vector<unique_ptr<const NL80211Packet>> response_vec; | ||
295 | if (!SendMessageAndGetResponses(packet, &response_vec)) { | ||
296 | return false; | ||
297 | } | ||
298 | if (response_vec.size() != 1) { | ||
299 | LOG(ERROR) << "Unexpected repsonse size: " << response_vec.size(); | ||
300 | return false; | ||
301 | } | ||
302 | |||
303 | *response = std::move(response_vec[0]); | ||
304 | return true; | ||
305 | } | ||
306 | |||
307 | bool NetlinkManager::SendMessageAndGetAckOrError(const NL80211Packet& packet, | ||
308 | int* error_code) { | ||
309 | unique_ptr<const NL80211Packet> response; | ||
310 | if (!SendMessageAndGetSingleResponse(packet, &response)) { | ||
311 | return false; | ||
312 | } | ||
313 | uint16_t type = response->GetMessageType(); | ||
314 | if (type != NLMSG_ERROR) { | ||
315 | LOG(ERROR) << "Receive unexpected message type :" << type; | ||
316 | return false; | ||
317 | } | ||
318 | |||
319 | *error_code = response->GetErrorCode(); | ||
320 | return true; | ||
321 | } | ||
322 | |||
323 | bool NetlinkManager::SendMessageAndGetAck(const NL80211Packet& packet) { | ||
324 | int error_code; | ||
325 | if (!SendMessageAndGetAckOrError(packet, &error_code)) { | ||
326 | return false; | ||
327 | } | ||
328 | if (error_code != 0) { | ||
329 | LOG(ERROR) << "Received error messsage: " << strerror(error_code); | ||
330 | return false; | ||
331 | } | ||
332 | |||
333 | return true; | ||
334 | } | ||
291 | 335 | ||
292 | bool NetlinkManager::SendMessageInternal(const NL80211Packet& packet, int fd) { | 336 | bool NetlinkManager::SendMessageInternal(const NL80211Packet& packet, int fd) { |
293 | const vector<uint8_t>& data = packet.GetConstData(); | 337 | const vector<uint8_t>& data = packet.GetConstData(); |
diff --git a/net/netlink_manager.h b/net/netlink_manager.h index 47cb98c..36f2772 100644 --- a/net/netlink_manager.h +++ b/net/netlink_manager.h | |||
@@ -71,6 +71,7 @@ class NetlinkManager { | |||
71 | virtual uint32_t GetSequenceNumber(); | 71 | virtual uint32_t GetSequenceNumber(); |
72 | // Get NL80211 netlink family id, | 72 | // Get NL80211 netlink family id, |
73 | virtual uint16_t GetFamilyId(); | 73 | virtual uint16_t GetFamilyId(); |
74 | |||
74 | // Send |packet| to kernel. | 75 | // Send |packet| to kernel. |
75 | // This works in an asynchronous way. | 76 | // This works in an asynchronous way. |
76 | // |handler| will be run when we receive a valid reply from kernel. | 77 | // |handler| will be run when we receive a valid reply from kernel. |
@@ -80,10 +81,27 @@ class NetlinkManager { | |||
80 | std::function<void(std::unique_ptr<const NL80211Packet>)> handler); | 81 | std::function<void(std::unique_ptr<const NL80211Packet>)> handler); |
81 | // Synchronous version of |RegisterHandlerAndSendMessage|. | 82 | // Synchronous version of |RegisterHandlerAndSendMessage|. |
82 | // Returns true on successfully receiving an valid reply. | 83 | // Returns true on successfully receiving an valid reply. |
83 | // Reply packets will be stored in |response|. | 84 | // Reply packets will be stored in |*response|. |
84 | virtual bool SendMessageAndGetResponses( | 85 | virtual bool SendMessageAndGetResponses( |
85 | const NL80211Packet& packet, | 86 | const NL80211Packet& packet, |
86 | std::vector<std::unique_ptr<const NL80211Packet>>* response); | 87 | std::vector<std::unique_ptr<const NL80211Packet>>* response); |
88 | // Wrapper of |SendMessageAndGetResponses| for messages with a single | ||
89 | // response. | ||
90 | // Returns true on successfully receiving an valid reply. | ||
91 | // Reply packet will be stored in |*response|. | ||
92 | virtual bool SendMessageAndGetSingleResponse( | ||
93 | const NL80211Packet& packet, | ||
94 | std::unique_ptr<const NL80211Packet>* response); | ||
95 | // Wrapper of |SendMessageAndGetResponses| for messages that trigger | ||
96 | // only a NLMSG_ERROR response | ||
97 | // Returns true if the message is successfully sent and a NLMSG_ERROR response | ||
98 | // comes back, regardless of the error code. | ||
99 | // Error code will be stored in |*error_code| | ||
100 | virtual bool SendMessageAndGetAckOrError(const NL80211Packet& packet, | ||
101 | int* error_code); | ||
102 | // Wrapper of |SendMessageAndGetResponses| that returns true iff the response | ||
103 | // is an ACK. | ||
104 | virtual bool SendMessageAndGetAck(const NL80211Packet& packet); | ||
87 | 105 | ||
88 | // Sign up to receive and log multicast events of a specific type. | 106 | // Sign up to receive and log multicast events of a specific type. |
89 | // |group| is one of the string NL80211_MULTICAST_GROUP_* in nl80211.h. | 107 | // |group| is one of the string NL80211_MULTICAST_GROUP_* in nl80211.h. |