Implement message kinds for flag printing

Bug: 162032964
Test: adb shell canhalctrl up test virtual vcan1
Change-Id: Ib71c928862b0d098cc698c09f2efdd72bf03d8b0
diff --git a/automotive/can/1.0/default/libnl++/Socket.cpp b/automotive/can/1.0/default/libnl++/Socket.cpp
index 1a34df8..9e96ea9 100644
--- a/automotive/can/1.0/default/libnl++/Socket.cpp
+++ b/automotive/can/1.0/default/libnl++/Socket.cpp
@@ -49,8 +49,8 @@
 
 bool Socket::send(const Buffer<nlmsghdr>& msg, const sockaddr_nl& sa) {
     if constexpr (kSuperVerbose) {
-        LOG(VERBOSE) << (mFailed ? "(not) " : "") << "sending Netlink message ("  //
-                     << msg->nlmsg_pid << " -> " << sa.nl_pid << "): " << toString(msg, mProtocol);
+        LOG(VERBOSE) << (mFailed ? "(not) " : "") << "sending to " << sa.nl_pid << ": "
+                     << toString(msg, mProtocol);
     }
     if (mFailed) return false;
 
@@ -97,8 +97,7 @@
 
     Buffer<nlmsghdr> msg(reinterpret_cast<nlmsghdr*>(mReceiveBuffer.data()), bytesReceived);
     if constexpr (kSuperVerbose) {
-        LOG(VERBOSE) << "received (" << sa.nl_pid << " -> " << msg->nlmsg_pid << "):"  //
-                     << toString(msg, mProtocol);
+        LOG(VERBOSE) << "received from " << sa.nl_pid << ": " << toString(msg, mProtocol);
     }
     return {msg, sa};
 }
diff --git a/automotive/can/1.0/default/libnl++/include/libnl++/printer.h b/automotive/can/1.0/default/libnl++/include/libnl++/printer.h
index 53a06d8..3570918 100644
--- a/automotive/can/1.0/default/libnl++/include/libnl++/printer.h
+++ b/automotive/can/1.0/default/libnl++/include/libnl++/printer.h
@@ -32,6 +32,6 @@
  * \param printPayload True will stringify message data, false will only stringify the header(s).
  * \return Stringified message.
  */
-std::string toString(const Buffer<nlmsghdr> hdr, int protocol, bool printPayload = false);
+std::string toString(const Buffer<nlmsghdr> hdr, int protocol, bool printPayload = true);
 
 }  // namespace android::nl
diff --git a/automotive/can/1.0/default/libnl++/printer.cpp b/automotive/can/1.0/default/libnl++/printer.cpp
index 9735db1..320641c 100644
--- a/automotive/can/1.0/default/libnl++/printer.cpp
+++ b/automotive/can/1.0/default/libnl++/printer.cpp
@@ -28,10 +28,10 @@
 
 namespace android::nl {
 
-static void flagsToStream(std::stringstream& ss, __u16 nlmsg_flags) {
+static void flagsToStream(std::stringstream& ss, __u16 nlmsg_flags, protocols::MessageGenre genre) {
     bool first = true;
     auto printFlag = [&ss, &first, &nlmsg_flags](__u16 flag, const std::string& name) {
-        if (!(nlmsg_flags & flag)) return;
+        if ((nlmsg_flags & flag) != flag) return;
         nlmsg_flags &= ~flag;
 
         if (first) {
@@ -42,6 +42,7 @@
 
         ss << name;
     };
+
     printFlag(NLM_F_REQUEST, "REQUEST");
     printFlag(NLM_F_MULTI, "MULTI");
     printFlag(NLM_F_ACK, "ACK");
@@ -49,11 +50,29 @@
     printFlag(NLM_F_DUMP_INTR, "DUMP_INTR");
     printFlag(NLM_F_DUMP_FILTERED, "DUMP_FILTERED");
 
-    // TODO(twasilczyk): print flags depending on request type
-    printFlag(NLM_F_ROOT, "ROOT-REPLACE");
-    printFlag(NLM_F_MATCH, "MATCH-EXCL");
-    printFlag(NLM_F_ATOMIC, "ATOMIC-CREATE");
-    printFlag(NLM_F_APPEND, "APPEND");
+    switch (genre) {
+        case protocols::MessageGenre::UNKNOWN:
+            break;
+        case protocols::MessageGenre::GET:
+            printFlag(NLM_F_DUMP, "DUMP");  // ROOT | MATCH
+            printFlag(NLM_F_ROOT, "ROOT");
+            printFlag(NLM_F_MATCH, "MATCH");
+            printFlag(NLM_F_ATOMIC, "ATOMIC");
+            break;
+        case protocols::MessageGenre::NEW:
+            printFlag(NLM_F_REPLACE, "REPLACE");
+            printFlag(NLM_F_EXCL, "EXCL");
+            printFlag(NLM_F_CREATE, "CREATE");
+            printFlag(NLM_F_APPEND, "APPEND");
+            break;
+        case protocols::MessageGenre::DELETE:
+            printFlag(NLM_F_NONREC, "NONREC");
+            break;
+        case protocols::MessageGenre::ACK:
+            printFlag(NLM_F_CAPPED, "CAPPED");
+            printFlag(NLM_F_ACK_TLVS, "ACK_TLVS");
+            break;
+    }
 
     if (nlmsg_flags != 0) {
         if (!first) ss << '|';
@@ -128,27 +147,26 @@
     }
     protocols::NetlinkProtocol& protocolDescr = *protocolMaybe;
 
-    auto msgDescMaybe = protocolDescr.getMessageDescriptor(hdr->nlmsg_type);
-    const auto msgTypeName = msgDescMaybe.has_value()
-                                     ? msgDescMaybe->get().getMessageName(hdr->nlmsg_type)
-                                     : std::to_string(hdr->nlmsg_type);
+    const auto msgDescMaybe = protocolDescr.getMessageDescriptor(hdr->nlmsg_type);
+    const auto msgDetails =
+            protocols::MessageDescriptor::getMessageDetails(msgDescMaybe, hdr->nlmsg_type);
 
     ss << "nlmsg{" << protocolDescr.getName() << " ";
 
     ss << "hdr={";
-    ss << "type=" << msgTypeName;
+    ss << "type=" << msgDetails.name;
     if (hdr->nlmsg_flags != 0) {
         ss << ", flags=";
-        flagsToStream(ss, hdr->nlmsg_flags);
+        flagsToStream(ss, hdr->nlmsg_flags, msgDetails.genre);
     }
     if (hdr->nlmsg_seq != 0) ss << ", seq=" << hdr->nlmsg_seq;
     if (hdr->nlmsg_pid != 0) ss << ", pid=" << hdr->nlmsg_pid;
     ss << ", len=" << hdr->nlmsg_len;
-
     ss << ", crc=" << std::hex << std::setw(4) << crc16(hdr.data<uint8_t>()) << std::dec;
-    ss << "} ";
+    ss << '}';
 
     if (!printPayload) return ss.str();
+    ss << ' ';
 
     if (!msgDescMaybe.has_value()) {
         toStream(ss, hdr.data<uint8_t>());
diff --git a/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.cpp b/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.cpp
index dc56643..c93d865 100644
--- a/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.cpp
+++ b/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.cpp
@@ -32,11 +32,12 @@
     return find(nla_type)->second;
 }
 
-MessageDescriptor::MessageDescriptor(const std::string& name, const MessageTypeMap&& messageTypes,
+MessageDescriptor::MessageDescriptor(const std::string& name,
+                                     const MessageDetailsMap&& messageDetails,
                                      const AttributeMap&& attrTypes, size_t contentsSize)
     : mName(name),
       mContentsSize(contentsSize),
-      mMessageTypes(messageTypes),
+      mMessageDetails(messageDetails),
       mAttributeMap(attrTypes) {}
 
 MessageDescriptor::~MessageDescriptor() {}
@@ -45,18 +46,25 @@
     return mContentsSize;
 }
 
-const MessageDescriptor::MessageTypeMap& MessageDescriptor::getMessageTypeMap() const {
-    return mMessageTypes;
+const MessageDescriptor::MessageDetailsMap& MessageDescriptor::getMessageDetailsMap() const {
+    return mMessageDetails;
 }
 
 const AttributeMap& MessageDescriptor::getAttributeMap() const {
     return mAttributeMap;
 }
 
-const std::string MessageDescriptor::getMessageName(nlmsgtype_t msgtype) const {
-    const auto it = mMessageTypes.find(msgtype);
-    if (it == mMessageTypes.end()) return "?";
+MessageDescriptor::MessageDetails MessageDescriptor::getMessageDetails(nlmsgtype_t msgtype) const {
+    const auto it = mMessageDetails.find(msgtype);
+    if (it == mMessageDetails.end()) return {std::to_string(msgtype), MessageGenre::UNKNOWN};
     return it->second;
 }
 
+MessageDescriptor::MessageDetails MessageDescriptor::getMessageDetails(
+        const std::optional<std::reference_wrapper<const MessageDescriptor>>& msgDescMaybe,
+        nlmsgtype_t msgtype) {
+    if (msgDescMaybe.has_value()) return msgDescMaybe->get().getMessageDetails(msgtype);
+    return {std::to_string(msgtype), protocols::MessageGenre::UNKNOWN};
+}
+
 }  // namespace android::nl::protocols
diff --git a/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.h b/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.h
index ef73d09..bd0e60f 100644
--- a/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.h
+++ b/automotive/can/1.0/default/libnl++/protocols/MessageDefinition.h
@@ -68,30 +68,53 @@
 };
 
 /**
+ * General message type's kind.
+ *
+ * For example, RTM_NEWLINK is a NEW kind. For details, please see "Flags values"
+ * section in linux/netlink.h.
+ */
+enum class MessageGenre {
+    UNKNOWN,
+    GET,
+    NEW,
+    DELETE,
+    ACK,
+};
+
+/**
  * Message family descriptor.
  *
  * Describes the structure of all message types with the same header and attributes.
  */
 class MessageDescriptor {
-  protected:
-    typedef std::map<nlmsgtype_t, std::string> MessageTypeMap;
-
-    MessageDescriptor(const std::string& name, const MessageTypeMap&& messageTypes,
-                      const AttributeMap&& attrTypes, size_t contentsSize);
+  public:
+    struct MessageDetails {
+        std::string name;
+        MessageGenre genre;
+    };
+    typedef std::map<nlmsgtype_t, MessageDetails> MessageDetailsMap;
 
   public:
     virtual ~MessageDescriptor();
 
     size_t getContentsSize() const;
-    const MessageTypeMap& getMessageTypeMap() const;
+    const MessageDetailsMap& getMessageDetailsMap() const;
     const AttributeMap& getAttributeMap() const;
-    const std::string getMessageName(nlmsgtype_t msgtype) const;
+    MessageDetails getMessageDetails(nlmsgtype_t msgtype) const;
     virtual void dataToStream(std::stringstream& ss, const Buffer<nlmsghdr> hdr) const = 0;
 
+    static MessageDetails getMessageDetails(
+            const std::optional<std::reference_wrapper<const MessageDescriptor>>& msgDescMaybe,
+            nlmsgtype_t msgtype);
+
+  protected:
+    MessageDescriptor(const std::string& name, const MessageDetailsMap&& messageDetails,
+                      const AttributeMap&& attrTypes, size_t contentsSize);
+
   private:
     const std::string mName;
     const size_t mContentsSize;
-    const MessageTypeMap mMessageTypes;
+    const MessageDetailsMap mMessageDetails;
     const AttributeMap mAttributeMap;
 };
 
@@ -103,11 +126,11 @@
 template <typename T>
 class MessageDefinition : public MessageDescriptor {
   public:
-    MessageDefinition(
+    MessageDefinition(  //
             const std::string& name,
-            const std::initializer_list<MessageDescriptor::MessageTypeMap::value_type> messageTypes,
+            const std::initializer_list<MessageDescriptor::MessageDetailsMap::value_type> msgDet,
             const std::initializer_list<AttributeMap::value_type> attrTypes = {})
-        : MessageDescriptor(name, messageTypes, attrTypes, sizeof(T)) {}
+        : MessageDescriptor(name, msgDet, attrTypes, sizeof(T)) {}
 
     void dataToStream(std::stringstream& ss, const Buffer<nlmsghdr> hdr) const override {
         const auto& [ok, msg] = hdr.data<T>().getFirst();
diff --git a/automotive/can/1.0/default/libnl++/protocols/NetlinkProtocol.cpp b/automotive/can/1.0/default/libnl++/protocols/NetlinkProtocol.cpp
index 4b795d9..159b6d6 100644
--- a/automotive/can/1.0/default/libnl++/protocols/NetlinkProtocol.cpp
+++ b/automotive/can/1.0/default/libnl++/protocols/NetlinkProtocol.cpp
@@ -42,7 +42,7 @@
         const NetlinkProtocol::MessageDescriptorList& descrs, int protocol) {
     MessageDescriptorMap map;
     for (const auto& descr : descrs) {
-        for (const auto& [mtype, mname] : descr->getMessageTypeMap()) {
+        for (const auto& [mtype, mdet] : descr->getMessageDetailsMap()) {
             map.emplace(mtype, descr);
         }
     }
@@ -53,7 +53,7 @@
     };
 
     for (const auto& descr : baseDescriptors) {
-        for (const auto& [mtype, mname] : descr->getMessageTypeMap()) {
+        for (const auto& [mtype, mdet] : descr->getMessageDetailsMap()) {
             map.emplace(mtype, descr);
         }
     }
diff --git a/automotive/can/1.0/default/libnl++/protocols/common/Empty.cpp b/automotive/can/1.0/default/libnl++/protocols/common/Empty.cpp
index dbf10d4..8a672d3 100644
--- a/automotive/can/1.0/default/libnl++/protocols/common/Empty.cpp
+++ b/automotive/can/1.0/default/libnl++/protocols/common/Empty.cpp
@@ -20,9 +20,9 @@
 
 // clang-format off
 Empty::Empty() : MessageDefinition<char>("nlmsg", {
-    {NLMSG_NOOP, "NOOP"},
-    {NLMSG_DONE, "DONE"},
-    {NLMSG_OVERRUN, "OVERRUN"},
+    {NLMSG_NOOP, {"NOOP", MessageGenre::UNKNOWN}},
+    {NLMSG_DONE, {"DONE", MessageGenre::UNKNOWN}},
+    {NLMSG_OVERRUN, {"OVERRUN", MessageGenre::UNKNOWN}},
 }) {}
 // clang-format on
 
diff --git a/automotive/can/1.0/default/libnl++/protocols/common/Error.cpp b/automotive/can/1.0/default/libnl++/protocols/common/Error.cpp
index 1d6bd1c..44708a3 100644
--- a/automotive/can/1.0/default/libnl++/protocols/common/Error.cpp
+++ b/automotive/can/1.0/default/libnl++/protocols/common/Error.cpp
@@ -22,15 +22,21 @@
 
 namespace android::nl::protocols::base {
 
+using DataType = AttributeDefinition::DataType;
+
 // clang-format off
 Error::Error(int protocol) : MessageDefinition<nlmsgerr>("nlmsg", {
-    {NLMSG_ERROR, "ERROR"},
+    {NLMSG_ERROR, {"ERROR", MessageGenre::ACK}},
+}, {
+    {NLMSGERR_ATTR_MSG, {"MSG", DataType::String}},
+    {NLMSGERR_ATTR_OFFS, {"OFFS", DataType::Uint}},
+    {NLMSGERR_ATTR_COOKIE, {"COOKIE", DataType::Raw}},
 }), mProtocol(protocol) {}
 // clang-format on
 
 void Error::toStream(std::stringstream& ss, const nlmsgerr& data) const {
     ss << "nlmsgerr{error=" << data.error
-       << ", msg=" << toString({&data.msg, sizeof(data.msg)}, mProtocol) << "}";
+       << ", msg=" << toString({&data.msg, sizeof(data.msg)}, mProtocol, false) << "}";
 }
 
 }  // namespace android::nl::protocols::base
diff --git a/automotive/can/1.0/default/libnl++/protocols/generic/GenericMessageBase.cpp b/automotive/can/1.0/default/libnl++/protocols/generic/GenericMessageBase.cpp
index 877bb21..5b6bd97 100644
--- a/automotive/can/1.0/default/libnl++/protocols/generic/GenericMessageBase.cpp
+++ b/automotive/can/1.0/default/libnl++/protocols/generic/GenericMessageBase.cpp
@@ -22,7 +22,8 @@
         nlmsgtype_t msgtype, std::string msgname,
         const std::initializer_list<GenericCommandNameMap::value_type> commandNames,
         const std::initializer_list<AttributeMap::value_type> attrTypes)
-    : MessageDefinition<genlmsghdr>(msgname, {{msgtype, msgname}}, attrTypes),
+    : MessageDefinition<genlmsghdr>(msgname, {{msgtype, {msgname, MessageGenre::UNKNOWN}}},
+                                    attrTypes),
       mCommandNames(commandNames) {}
 
 void GenericMessageBase::toStream(std::stringstream& ss, const genlmsghdr& data) const {
diff --git a/automotive/can/1.0/default/libnl++/protocols/route/Link.cpp b/automotive/can/1.0/default/libnl++/protocols/route/Link.cpp
index bc44d8e..7db487a 100644
--- a/automotive/can/1.0/default/libnl++/protocols/route/Link.cpp
+++ b/automotive/can/1.0/default/libnl++/protocols/route/Link.cpp
@@ -26,9 +26,9 @@
 
 // clang-format off
 Link::Link() : MessageDefinition<ifinfomsg>("link", {
-    {RTM_NEWLINK, "NEWLINK"},
-    {RTM_DELLINK, "DELLINK"},
-    {RTM_GETLINK, "GETLINK"},
+    {RTM_NEWLINK, {"NEWLINK", MessageGenre::NEW}},
+    {RTM_DELLINK, {"DELLINK", MessageGenre::DELETE}},
+    {RTM_GETLINK, {"GETLINK", MessageGenre::GET}},
 }, {
     {IFLA_ADDRESS, {"ADDRESS"}},
     {IFLA_BROADCAST, {"BROADCAST"}},