Implement Netlink message printer
Bug: 158756457
Test: manual
Change-Id: I93e40bbf1eff1c4c0e502ca38e07865909ba04f3
diff --git a/automotive/can/1.0/default/libnetdevice/NetlinkSocket.cpp b/automotive/can/1.0/default/libnetdevice/NetlinkSocket.cpp
index 7817169..15c0f9b 100644
--- a/automotive/can/1.0/default/libnetdevice/NetlinkSocket.cpp
+++ b/automotive/can/1.0/default/libnetdevice/NetlinkSocket.cpp
@@ -16,11 +16,18 @@
#include "NetlinkSocket.h"
+#include <libnetdevice/printer.h>
+
#include <android-base/logging.h>
namespace android::netdevice {
-NetlinkSocket::NetlinkSocket(int protocol) {
+/**
+ * Print all outbound/inbound Netlink messages.
+ */
+static constexpr bool kSuperVerbose = false;
+
+NetlinkSocket::NetlinkSocket(int protocol) : mProtocol(protocol) {
mFd.reset(socket(AF_NETLINK, SOCK_RAW, protocol));
if (!mFd.ok()) {
PLOG(ERROR) << "Can't open Netlink socket";
@@ -38,7 +45,13 @@
}
}
-bool NetlinkSocket::send(struct nlmsghdr* nlmsg) {
+bool NetlinkSocket::send(struct nlmsghdr* nlmsg, size_t totalLen) {
+ if constexpr (kSuperVerbose) {
+ nlmsg->nlmsg_seq = mSeq;
+ LOG(VERBOSE) << (mFailed ? "(not)" : "")
+ << "sending Netlink message: " << toString(nlmsg, totalLen, mProtocol);
+ }
+
if (mFailed) return false;
nlmsg->nlmsg_pid = 0; // kernel
@@ -91,6 +104,11 @@
for (auto nlmsg = reinterpret_cast<struct nlmsghdr*>(buf); NLMSG_OK(nlmsg, remainingLen);
nlmsg = NLMSG_NEXT(nlmsg, remainingLen)) {
+ if constexpr (kSuperVerbose) {
+ LOG(VERBOSE) << "received Netlink response: "
+ << toString(nlmsg, sizeof(buf), mProtocol);
+ }
+
// We're looking for error/ack message only, ignoring others.
if (nlmsg->nlmsg_type != NLMSG_ERROR) {
LOG(WARNING) << "Received unexpected Netlink message (ignored): " << nlmsg->nlmsg_type;