Loading automotive/can/1.0/default/libnetdevice/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ cc_library_static { "protocols/generic/Unknown.cpp", "protocols/route/Link.cpp", "protocols/route/Route.cpp", "protocols/route/structs.cpp", "protocols/MessageDefinition.cpp", "protocols/NetlinkProtocol.cpp", "protocols/all.cpp", Loading automotive/can/1.0/default/libnetdevice/include/libnetdevice/nlbuf.h +13 −6 Original line number Diff line number Diff line Loading @@ -66,9 +66,12 @@ class nlbuf { return mData; } std::optional<std::reference_wrapper<const T>> getFirst() const { if (!ok()) return std::nullopt; return *mData; std::pair<bool, const T&> getFirst() const { if (!ok()) { static const T dummy = {}; return {false, dummy}; } return {true, *mData}; } /** Loading Loading @@ -141,7 +144,7 @@ class nlbuf { size_t len() const { return mBuffer.remainingLength(); } private: const nlbuf<T>& mBuffer; const nlbuf<T> mBuffer; }; raw_view getRaw() const { return {*this}; } Loading @@ -160,8 +163,12 @@ class nlbuf { size_t declaredLength() const { // We can't even fit a header, so let's return some absurd high value to trip off // buffer overflow checks. if (sizeof(T) > remainingLength()) return std::numeric_limits<size_t>::max() / 2; return declaredLengthImpl(); static constexpr size_t badHeaderLength = std::numeric_limits<size_t>::max() / 2; if (sizeof(T) > remainingLength()) return badHeaderLength; const auto len = declaredLengthImpl(); if (sizeof(T) > len) return badHeaderLength; return len; } size_t remainingLength() const { Loading automotive/can/1.0/default/libnetdevice/printer.cpp +9 −5 Original line number Diff line number Diff line Loading @@ -85,17 +85,16 @@ static void toStream(std::stringstream& ss, const nlbuf<nlattr> attr, ss << attrtype.name << ": "; switch (attrtype.dataType) { case DataType::Raw: { case DataType::Raw: toStream(ss, attr.data<uint8_t>()); break; } case DataType::Nested: { ss << '{'; bool first = true; for (auto childattr : attr.data<nlattr>()) { for (const auto childattr : attr.data<nlattr>()) { if (!first) ss << ", "; first = false; toStream(ss, childattr, attrtype.subTypes); toStream(ss, childattr, std::get<protocols::AttributeMap>(attrtype.ops)); } ss << '}'; break; Loading @@ -105,9 +104,14 @@ static void toStream(std::stringstream& ss, const nlbuf<nlattr> attr, ss << '"' << sanitize({str.ptr(), str.len()}) << '"'; break; } case DataType::Uint: { case DataType::Uint: ss << attr.data<uint32_t>().copyFirst(); break; case DataType::Struct: { const auto structToStream = std::get<protocols::AttributeDefinition::ToStream>(attrtype.ops); structToStream(ss, attr); break; } } } Loading automotive/can/1.0/default/libnetdevice/protocols/MessageDefinition.h +7 −4 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <map> #include <sstream> #include <variant> namespace android::netdevice::protocols { Loading Loading @@ -57,11 +58,13 @@ struct AttributeDefinition { Nested, String, Uint, Struct, }; using ToStream = std::function<void(std::stringstream& ss, const nlbuf<nlattr> attr)>; std::string name; DataType dataType = DataType::Raw; AttributeMap subTypes = {}; std::variant<AttributeMap, ToStream> ops = AttributeMap{}; }; /** Loading Loading @@ -107,13 +110,13 @@ class MessageDefinition : public MessageDescriptor { : MessageDescriptor(name, messageTypes, attrTypes, sizeof(T)) {} void dataToStream(std::stringstream& ss, const nlbuf<nlmsghdr> hdr) const override { const auto msg = hdr.data<T>().getFirst(); if (!msg.has_value()) { const auto& [ok, msg] = hdr.data<T>().getFirst(); if (!ok) { ss << "{incomplete payload}"; return; } toStream(ss, *msg); toStream(ss, msg); } protected: Loading automotive/can/1.0/default/libnetdevice/protocols/generic/Ctrl.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -37,14 +37,14 @@ Ctrl::Ctrl() : GenericMessageBase(GENL_ID_CTRL, "ID_CTRL", { {CTRL_ATTR_VERSION, {"VERSION", DataType::Uint}}, {CTRL_ATTR_HDRSIZE, {"HDRSIZE", DataType::Uint}}, {CTRL_ATTR_MAXATTR, {"MAXATTR", DataType::Uint}}, {CTRL_ATTR_OPS, {"OPS", DataType::Nested, { {std::nullopt, {"OP", DataType::Nested, { {CTRL_ATTR_OPS, {"OPS", DataType::Nested, AttributeMap{ {std::nullopt, {"OP", DataType::Nested, AttributeMap{ {CTRL_ATTR_OP_ID, {"ID", DataType::Uint}}, {CTRL_ATTR_OP_FLAGS, {"FLAGS", DataType::Uint}}, }}}, }}}, {CTRL_ATTR_MCAST_GROUPS, {"MCAST_GROUPS", DataType::Nested, { {std::nullopt, {"GRP", DataType::Nested, { {CTRL_ATTR_MCAST_GROUPS, {"MCAST_GROUPS", DataType::Nested, AttributeMap{ {std::nullopt, {"GRP", DataType::Nested, AttributeMap{ {CTRL_ATTR_MCAST_GRP_NAME, {"NAME", DataType::String}}, {CTRL_ATTR_MCAST_GRP_ID, {"ID", DataType::Uint}}, }}}, Loading Loading
automotive/can/1.0/default/libnetdevice/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ cc_library_static { "protocols/generic/Unknown.cpp", "protocols/route/Link.cpp", "protocols/route/Route.cpp", "protocols/route/structs.cpp", "protocols/MessageDefinition.cpp", "protocols/NetlinkProtocol.cpp", "protocols/all.cpp", Loading
automotive/can/1.0/default/libnetdevice/include/libnetdevice/nlbuf.h +13 −6 Original line number Diff line number Diff line Loading @@ -66,9 +66,12 @@ class nlbuf { return mData; } std::optional<std::reference_wrapper<const T>> getFirst() const { if (!ok()) return std::nullopt; return *mData; std::pair<bool, const T&> getFirst() const { if (!ok()) { static const T dummy = {}; return {false, dummy}; } return {true, *mData}; } /** Loading Loading @@ -141,7 +144,7 @@ class nlbuf { size_t len() const { return mBuffer.remainingLength(); } private: const nlbuf<T>& mBuffer; const nlbuf<T> mBuffer; }; raw_view getRaw() const { return {*this}; } Loading @@ -160,8 +163,12 @@ class nlbuf { size_t declaredLength() const { // We can't even fit a header, so let's return some absurd high value to trip off // buffer overflow checks. if (sizeof(T) > remainingLength()) return std::numeric_limits<size_t>::max() / 2; return declaredLengthImpl(); static constexpr size_t badHeaderLength = std::numeric_limits<size_t>::max() / 2; if (sizeof(T) > remainingLength()) return badHeaderLength; const auto len = declaredLengthImpl(); if (sizeof(T) > len) return badHeaderLength; return len; } size_t remainingLength() const { Loading
automotive/can/1.0/default/libnetdevice/printer.cpp +9 −5 Original line number Diff line number Diff line Loading @@ -85,17 +85,16 @@ static void toStream(std::stringstream& ss, const nlbuf<nlattr> attr, ss << attrtype.name << ": "; switch (attrtype.dataType) { case DataType::Raw: { case DataType::Raw: toStream(ss, attr.data<uint8_t>()); break; } case DataType::Nested: { ss << '{'; bool first = true; for (auto childattr : attr.data<nlattr>()) { for (const auto childattr : attr.data<nlattr>()) { if (!first) ss << ", "; first = false; toStream(ss, childattr, attrtype.subTypes); toStream(ss, childattr, std::get<protocols::AttributeMap>(attrtype.ops)); } ss << '}'; break; Loading @@ -105,9 +104,14 @@ static void toStream(std::stringstream& ss, const nlbuf<nlattr> attr, ss << '"' << sanitize({str.ptr(), str.len()}) << '"'; break; } case DataType::Uint: { case DataType::Uint: ss << attr.data<uint32_t>().copyFirst(); break; case DataType::Struct: { const auto structToStream = std::get<protocols::AttributeDefinition::ToStream>(attrtype.ops); structToStream(ss, attr); break; } } } Loading
automotive/can/1.0/default/libnetdevice/protocols/MessageDefinition.h +7 −4 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <map> #include <sstream> #include <variant> namespace android::netdevice::protocols { Loading Loading @@ -57,11 +58,13 @@ struct AttributeDefinition { Nested, String, Uint, Struct, }; using ToStream = std::function<void(std::stringstream& ss, const nlbuf<nlattr> attr)>; std::string name; DataType dataType = DataType::Raw; AttributeMap subTypes = {}; std::variant<AttributeMap, ToStream> ops = AttributeMap{}; }; /** Loading Loading @@ -107,13 +110,13 @@ class MessageDefinition : public MessageDescriptor { : MessageDescriptor(name, messageTypes, attrTypes, sizeof(T)) {} void dataToStream(std::stringstream& ss, const nlbuf<nlmsghdr> hdr) const override { const auto msg = hdr.data<T>().getFirst(); if (!msg.has_value()) { const auto& [ok, msg] = hdr.data<T>().getFirst(); if (!ok) { ss << "{incomplete payload}"; return; } toStream(ss, *msg); toStream(ss, msg); } protected: Loading
automotive/can/1.0/default/libnetdevice/protocols/generic/Ctrl.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -37,14 +37,14 @@ Ctrl::Ctrl() : GenericMessageBase(GENL_ID_CTRL, "ID_CTRL", { {CTRL_ATTR_VERSION, {"VERSION", DataType::Uint}}, {CTRL_ATTR_HDRSIZE, {"HDRSIZE", DataType::Uint}}, {CTRL_ATTR_MAXATTR, {"MAXATTR", DataType::Uint}}, {CTRL_ATTR_OPS, {"OPS", DataType::Nested, { {std::nullopt, {"OP", DataType::Nested, { {CTRL_ATTR_OPS, {"OPS", DataType::Nested, AttributeMap{ {std::nullopt, {"OP", DataType::Nested, AttributeMap{ {CTRL_ATTR_OP_ID, {"ID", DataType::Uint}}, {CTRL_ATTR_OP_FLAGS, {"FLAGS", DataType::Uint}}, }}}, }}}, {CTRL_ATTR_MCAST_GROUPS, {"MCAST_GROUPS", DataType::Nested, { {std::nullopt, {"GRP", DataType::Nested, { {CTRL_ATTR_MCAST_GROUPS, {"MCAST_GROUPS", DataType::Nested, AttributeMap{ {std::nullopt, {"GRP", DataType::Nested, AttributeMap{ {CTRL_ATTR_MCAST_GRP_NAME, {"NAME", DataType::String}}, {CTRL_ATTR_MCAST_GRP_ID, {"ID", DataType::Uint}}, }}}, Loading