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

Commit 5a8a36c4 authored by Henri Chataing's avatar Henri Chataing
Browse files

RootCanal: Implement fmt::formatter for Address, AddressWithType

Allows use of "{}" for formatting addresses

Bug: 284355200
Test: m root-canal
Change-Id: Ie370ffbfa08642b7072bbf08def3553993106a75
parent 498899d5
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line
@@ -18,12 +18,16 @@

#include "hci/address.h"

#include <fmt/core.h>

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <sstream>

#include "hci/address_with_type.h"

namespace bluetooth {
namespace hci {

@@ -115,3 +119,78 @@ bool Address::IsValidAddress(const std::string& address) {

}  // 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()));
  }
};
+5 −5
Original line number Diff line number Diff line
@@ -1359,7 +1359,7 @@ void DualModeController::WriteScanEnable(CommandView command) {
      scan_enable == bluetooth::hci::ScanEnable::INQUIRY_AND_PAGE_SCAN ||
      scan_enable == bluetooth::hci::ScanEnable::PAGE_SCAN_ONLY;

  INFO(id_, "{} | WriteScanEnable {}", GetAddress().ToString(),
  INFO(id_, "{} | WriteScanEnable {}", GetAddress(),
       bluetooth::hci::ScanEnableText(scan_enable));

  link_layer_controller_.SetInquiryScanEnable(inquiry_scan);
@@ -1552,8 +1552,8 @@ void DualModeController::LeReadLocalSupportedFeatures(CommandView command) {
      bluetooth::hci::LeReadLocalSupportedFeaturesView::Create(command);
  ASSERT(command_view.IsValid());
  uint64_t le_features = link_layer_controller_.GetLeSupportedFeatures();
  INFO(id_, "{} | LeReadLocalSupportedFeatures {:016x}",
       GetAddress().ToString(), le_features);
  INFO(id_, "{} | LeReadLocalSupportedFeatures {:016x}", GetAddress(),
       le_features);

  send_event_(
      bluetooth::hci::LeReadLocalSupportedFeaturesCompleteBuilder::Create(
@@ -1620,7 +1620,7 @@ void DualModeController::LeSetAdvertisingEnable(CommandView command) {
      bluetooth::hci::LeSetAdvertisingEnableView::Create(command);
  ASSERT(command_view.IsValid());

  INFO(id_, "{} | LeSetAdvertisingEnable ({})", GetAddress().ToString(),
  INFO(id_, "{} | LeSetAdvertisingEnable ({})", GetAddress(),
       command_view.GetAdvertisingEnable() == bluetooth::hci::Enable::ENABLED);

  ErrorCode status = link_layer_controller_.LeSetAdvertisingEnable(
@@ -1645,7 +1645,7 @@ void DualModeController::LeSetScanEnable(CommandView command) {
  auto command_view = bluetooth::hci::LeSetScanEnableView::Create(command);
  ASSERT(command_view.IsValid());

  INFO(id_, "{} | LeSetScanEnable ({})", GetAddress().ToString(),
  INFO(id_, "{} | LeSetScanEnable ({})", GetAddress(),
       command_view.GetLeScanEnable() == bluetooth::hci::Enable::ENABLED);

  ErrorCode status = link_layer_controller_.LeSetScanEnable(
+122 −154

File changed.

Preview size limit exceeded, changes collapsed.