Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f2dcb649 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

libnetdevice: migrate API to std::string_view

Bug: 372814636
Test: verified with b/372814636 test service
Change-Id: I9a6226a914195e895193b8a4c2d4d833fcc5dade
parent 426708b9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ namespace android::netdevice::can {

static constexpr can_err_mask_t kErrMask = CAN_ERR_MASK;

base::unique_fd socket(const std::string& ifname) {
base::unique_fd socket(std::string_view ifname) {
    sockaddr_can addr = {};
    addr.can_family = AF_CAN;
    addr.can_ifindex = nametoindex(ifname);
@@ -66,7 +66,7 @@ base::unique_fd socket(const std::string& ifname) {
    return sock;
}

bool setBitrate(std::string ifname, uint32_t bitrate) {
bool setBitrate(std::string_view ifname, uint32_t bitrate) {
    can_bittiming bt = {};
    bt.bitrate = bitrate;

+2 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@

namespace android::netdevice {

unsigned int nametoindex(const std::string& ifname) {
    const auto ifidx = if_nametoindex(ifname.c_str());
unsigned int nametoindex(std::string_view ifname) {
    const auto ifidx = if_nametoindex(std::string(ifname).c_str());
    if (ifidx != 0) return ifidx;

    if (errno != ENODEV) {
+1 −1
Original line number Diff line number Diff line
@@ -32,6 +32,6 @@ namespace android::netdevice {
 * \param ifname Interface to check
 * \return Interface index, or 0 if the interface doesn't exist
 */
unsigned int nametoindex(const std::string& ifname);
unsigned int nametoindex(std::string_view ifname);

}  // namespace android::netdevice
+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@

namespace android::netdevice::ethtool {

std::optional<uint32_t> getValue(const std::string& ifname, uint32_t command) {
std::optional<uint32_t> getValue(std::string_view ifname, uint32_t command) {
    struct ethtool_value valueop = {};
    valueop.cmd = command;

@@ -34,7 +34,7 @@ std::optional<uint32_t> getValue(const std::string& ifname, uint32_t command) {
    return valueop.data;
}

bool setValue(const std::string& ifname, uint32_t command, uint32_t value) {
bool setValue(std::string_view ifname, uint32_t command, uint32_t value) {
    struct ethtool_value valueop = {};
    valueop.cmd = command;
    valueop.data = value;
+4 −3
Original line number Diff line number Diff line
@@ -70,10 +70,11 @@ bool send(unsigned long request, struct ifreq& ifr) {
    return true;
}

struct ifreq fromName(const std::string& ifname) {
struct ifreq fromName(std::string_view ifname) {
    struct ifreq ifr = {};
    // strncpy: last \0 initialized with ifreq above
    strncpy(ifr.ifr_name, ifname.c_str(), IF_NAMESIZE - 1);
    // memcpy: last \0 initialized with ifreq above
    memcpy(ifr.ifr_name, ifname.data(),
           std::min(ifname.size(), static_cast<size_t>(IF_NAMESIZE - 1)));
    return ifr;
}

Loading