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

Commit f376373c authored by Henri Chataing's avatar Henri Chataing Committed by Automerger Merge Worker
Browse files

Merge "RootCanal: Move Address and AddressWithType formatters to the header...

Merge "RootCanal: Move Address and AddressWithType formatters to the header source" into main am: d8c448b7

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2730944



Change-Id: I60104d136c5203e2f466bd2c69b3351f34daee0b
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 717847dc d8c448b7
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#pragma once

#include <fmt/core.h>
#include <packet_runtime.h>

#include <array>
@@ -104,3 +105,43 @@ struct hash<bluetooth::hci::Address> {
  }
};
}  // namespace std

template <>
struct fmt::formatter<bluetooth::hci::Address> {
  // Presentation format: 'x' - lowercase, 'X' - uppercase.
  char presentation = 'x';

  // Parses format specifications of the form ['x' | 'X'].
  constexpr auto parse(format_parse_context& ctx)
      -> format_parse_context::iterator {
    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin();
    auto end = ctx.end();
    if (it != end && (*it == 'x' || *it == 'X')) {
      presentation = *it++;
    }

    // Check if reached the end of the range:
    if (it != end && *it != '}') {
      ctx.on_error("invalid format");
    }

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the address a using the parsed format specification (presentation)
  // stored in this formatter.
  auto format(const bluetooth::hci::Address& a, format_context& ctx) const
      -> format_context::iterator {
    return presentation == 'x'
               ? fmt::format_to(ctx.out(),
                                "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
                                a.address[5], a.address[4], a.address[3],
                                a.address[2], a.address[1], a.address[0])
               : fmt::format_to(ctx.out(),
                                "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
                                a.address[5], a.address[4], a.address[3],
                                a.address[2], a.address[1], a.address[0]);
  }
};
+37 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#pragma once

#include <fmt/core.h>

#include <sstream>
#include <string>
#include <utility>
@@ -134,3 +136,38 @@ struct hash<bluetooth::hci::AddressWithType> {
  }
};
}  // namespace std

template <>
struct fmt::formatter<bluetooth::hci::AddressWithType> {
  // Presentation format: 'x' - lowercase, 'X' - uppercase.
  char presentation = 'x';

  // Parses format specifications of the form ['x' | 'X'].
  constexpr auto parse(format_parse_context& ctx)
      -> format_parse_context::iterator {
    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin();
    auto end = ctx.end();
    if (it != end && (*it == 'x' || *it == 'X')) {
      presentation = *it++;
    }

    // Check if reached the end of the range:
    if (it != end && *it != '}') {
      ctx.on_error("invalid format");
    }

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the address a using the parsed format specification (presentation)
  // stored in this formatter.
  auto format(const bluetooth::hci::AddressWithType& a,
              format_context& ctx) const -> format_context::iterator {
    auto out = presentation == 'x'
                   ? fmt::format_to(ctx.out(), "{:x}", a.GetAddress())
                   : fmt::format_to(ctx.out(), "{:X}", a.GetAddress());
    return fmt::format_to(out, "[{}]", AddressTypeText(a.GetAddressType()));
  }
};
+4 −83
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

#include "hci/address.h"

#include <fmt/core.h>
#include <packet_runtime.h>

#include <algorithm>
#include <cstdint>
@@ -24,10 +24,7 @@
#include <iomanip>
#include <sstream>

#include "hci/address_with_type.h"

namespace bluetooth {
namespace hci {
namespace bluetooth::hci {

const Address Address::kAny{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const Address Address::kEmpty{0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
@@ -123,86 +120,10 @@ bool Address::FromString(const std::string& from, Address& to) {
size_t Address::FromOctets(const uint8_t* from) {
  std::copy(from, from + kLength, data());
  return kLength;
};
}

bool Address::IsValidAddress(const std::string& address) {
  return Address::FromString(address).has_value();
}

}  // namespace hci
}  // namespace bluetooth

template <>
struct fmt::formatter<bluetooth::hci::Address> {
  // Presentation format: 'x' - lowercase, 'X' - uppercase.
  char presentation = 'x';

  // Parses format specifications of the form ['x' | 'X'].
  constexpr auto parse(format_parse_context& ctx)
      -> format_parse_context::iterator {
    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin();
    auto end = ctx.end();
    if (it != end && (*it == 'x' || *it == 'X')) {
      presentation = *it++;
    }

    // Check if reached the end of the range:
    if (it != end && *it != '}') {
      ctx.on_error("invalid format");
    }

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the address a using the parsed format specification (presentation)
  // stored in this formatter.
  auto format(const bluetooth::hci::Address& a, format_context& ctx) const
      -> format_context::iterator {
    return presentation == 'x'
               ? fmt::format_to(ctx.out(),
                                "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
                                a.address[5], a.address[4], a.address[3],
                                a.address[2], a.address[1], a.address[0])
               : fmt::format_to(ctx.out(),
                                "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
                                a.address[5], a.address[4], a.address[3],
                                a.address[2], a.address[1], a.address[0]);
  }
};

template <>
struct fmt::formatter<bluetooth::hci::AddressWithType> {
  // Presentation format: 'x' - lowercase, 'X' - uppercase.
  char presentation = 'x';

  // Parses format specifications of the form ['x' | 'X'].
  constexpr auto parse(format_parse_context& ctx)
      -> format_parse_context::iterator {
    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin();
    auto end = ctx.end();
    if (it != end && (*it == 'x' || *it == 'X')) {
      presentation = *it++;
    }

    // Check if reached the end of the range:
    if (it != end && *it != '}') {
      ctx.on_error("invalid format");
    }

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the address a using the parsed format specification (presentation)
  // stored in this formatter.
  auto format(const bluetooth::hci::AddressWithType& a,
              format_context& ctx) const -> format_context::iterator {
    auto out = presentation == 'x'
                   ? fmt::format_to(ctx.out(), "{:x}", a.GetAddress())
                   : fmt::format_to(ctx.out(), "{:X}", a.GetAddress());
    return fmt::format_to(out, "[{}]", AddressTypeText(a.GetAddressType()));
  }
};
}  // namespace bluetooth::hci