libnetdevice: define default request flags
Bug: 372814636
Test: verified with b/372814636 test service
Change-Id: I81e8ac625e6d075d9780b6e2e0a172aec0a04468
diff --git a/automotive/can/1.0/default/libnetdevice/can.cpp b/automotive/can/1.0/default/libnetdevice/can.cpp
index 8d82c36..9cf0253 100644
--- a/automotive/can/1.0/default/libnetdevice/can.cpp
+++ b/automotive/can/1.0/default/libnetdevice/can.cpp
@@ -70,7 +70,7 @@
can_bittiming bt = {};
bt.bitrate = bitrate;
- nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK);
+ nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK);
req->ifi_index = nametoindex(ifname);
if (req->ifi_index == 0) {
diff --git a/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp b/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
index 4c4b77b..1830633 100644
--- a/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
+++ b/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
@@ -104,8 +104,7 @@
}
bool addAddr4(std::string_view ifname, std::string_view addr, uint8_t prefixlen) {
- android::nl::MessageFactory<ifaddrmsg> req(
- RTM_NEWADDR, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK);
+ nl::MessageFactory<ifaddrmsg> req(RTM_NEWADDR, nl::kCreateFlags);
req->ifa_family = AF_INET;
req->ifa_prefixlen = prefixlen;
req->ifa_flags = IFA_F_SECONDARY;
@@ -120,8 +119,7 @@
}
bool add(std::string_view dev, std::string_view type) {
- nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK,
- NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK);
+ nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK, nl::kCreateFlags);
req.add(IFLA_IFNAME, dev);
{
@@ -134,7 +132,7 @@
}
bool del(std::string_view dev) {
- nl::MessageFactory<ifinfomsg> req(RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK);
+ nl::MessageFactory<ifinfomsg> req(RTM_DELLINK);
req.add(IFLA_IFNAME, dev);
nl::Socket sock(NETLINK_ROUTE);
diff --git a/automotive/can/1.0/default/libnetdevice/vlan.cpp b/automotive/can/1.0/default/libnetdevice/vlan.cpp
index a437c32..e5b5a61 100644
--- a/automotive/can/1.0/default/libnetdevice/vlan.cpp
+++ b/automotive/can/1.0/default/libnetdevice/vlan.cpp
@@ -33,8 +33,7 @@
return false;
}
- nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK,
- NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK);
+ nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK, nl::kCreateFlags);
req.add(IFLA_IFNAME, vlan);
req.add<uint32_t>(IFLA_LINK, ethidx);
diff --git a/automotive/can/1.0/default/libnl++/include/libnl++/MessageFactory.h b/automotive/can/1.0/default/libnl++/include/libnl++/MessageFactory.h
index a5a425e..f65f055 100644
--- a/automotive/can/1.0/default/libnl++/include/libnl++/MessageFactory.h
+++ b/automotive/can/1.0/default/libnl++/include/libnl++/MessageFactory.h
@@ -26,6 +26,9 @@
namespace android::nl {
+static constexpr uint16_t kDefaultFlags = NLM_F_REQUEST | NLM_F_ACK;
+static constexpr uint16_t kCreateFlags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
+
class MessageFactoryBase {
protected:
static nlattr* add(nlmsghdr* msg, size_t maxLen, nlattrtype_t type, const void* data,
@@ -54,7 +57,7 @@
* \param type Message type (such as RTM_NEWLINK).
* \param flags Message flags (such as NLM_F_REQUEST).
*/
- MessageFactory(nlmsgtype_t type, uint16_t flags)
+ MessageFactory(nlmsgtype_t type, uint16_t flags = kDefaultFlags)
: header(mMessage.header), data(mMessage.data) {
mMessage.header.nlmsg_len = offsetof(Message, attributesBuffer);
mMessage.header.nlmsg_type = type;