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

Commit dce35e33 authored by Henri Chataing's avatar Henri Chataing
Browse files

system/stack/rfcomm: Migrate to libbluetooth_log

Test: m com.android.btservices
Bug: 305066880
Flag: EXEMPT, mechanical refactor
Change-Id: Ic941a3b3bec0e19bc0c6b4c90d0638612ff5e838
parent 7a9d6675
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -800,6 +800,7 @@ cc_test {
        "libcutils",
    ],
    static_libs: [
        "libbase",
        "libbluetooth-types",
        "libbluetooth_crypto_toolbox",
        "libbluetooth_gd",
+77 −80
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include "stack/include/port_api.h"

#include <base/logging.h>
#include <bluetooth/log.h>

#include <cstdint>

@@ -41,8 +42,7 @@
#include "stack/rfcomm/rfc_int.h"
#include "types/raw_address.h"

#define error(fmt, ...) \
  LOG_ERROR("## ERROR : %s: " fmt "##", __func__, ##__VA_ARGS__)
using namespace bluetooth;

/* Mapping from PORT_* result codes to human readable strings. */
static const char* result_code_strings[] = {"Success",
@@ -113,11 +113,9 @@ int RFCOMM_CreateConnectionWithSecurity(uint16_t uuid, uint8_t scn,

  if ((scn == 0) || (scn > RFCOMM_MAX_SCN)) {
    // Server Channel Number (SCN) should be in range [1, 30]
    LOG(ERROR) << __func__ << ": Invalid SCN, bd_addr=" << bd_addr
               << ", scn=" << static_cast<int>(scn)
               << ", is_server=" << is_server
               << ", mtu=" << static_cast<int>(mtu)
               << ", uuid=" << loghex(uuid);
    log::error("Invalid SCN, bd_addr={}, scn={}, is_server={}, mtu={}, uuid={}",
               ADDRESS_TO_LOGGABLE_STR(bd_addr), static_cast<int>(scn),
               is_server, static_cast<int>(mtu), loghex(uuid));
    return (PORT_INVALID_SCN);
  }

@@ -139,16 +137,15 @@ int RFCOMM_CreateConnectionWithSecurity(uint16_t uuid, uint8_t scn,
    if (p_port != nullptr) {
      // if existing port is also a client port, error out
      if (!p_port->is_server) {
        LOG(ERROR) << __func__ << ": already at opened state "
                   << static_cast<int>(p_port->state)
                   << ", RFC_state=" << static_cast<int>(p_port->rfc.state)
                   << ", MCB_state="
                   << (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0)
                   << ", bd_addr=" << bd_addr << ", scn=" << std::to_string(scn)
                   << ", is_server=" << is_server << ", mtu=" << mtu
                   << ", uuid=" << loghex(uuid) << ", dlci=" << +dlci
                   << ", p_mcb=" << p_mcb
                   << ", port=" << std::to_string(p_port->handle);
        log::error(
            "already at opened state {}, RFC_state={}, MCB_state={}, "
            "bd_addr={}, scn={}, is_server={}, mtu={}, uuid={}, dlci={}, "
            "p_mcb={}, port={}",
            static_cast<int>(p_port->state),
            static_cast<int>(p_port->rfc.state),
            (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0),
            ADDRESS_TO_LOGGABLE_STR(bd_addr), scn, is_server, mtu, loghex(uuid),
            dlci, fmt::ptr(p_mcb), p_port->handle);
        *p_handle = p_port->handle;
        return (PORT_ALREADY_OPENED);
      }
@@ -158,10 +155,11 @@ int RFCOMM_CreateConnectionWithSecurity(uint16_t uuid, uint8_t scn,
  // On the server side, always allocate a new port.
  p_port = port_allocate_port(dlci, bd_addr);
  if (p_port == nullptr) {
    LOG(ERROR) << __func__ << ": no resources, bd_addr=" << bd_addr
               << ", scn=" << std::to_string(scn) << ", is_server=" << is_server
               << ", mtu=" << mtu << ", uuid=" << loghex(uuid)
               << ", dlci=" << +dlci;
    log::error(
        "no resources, bd_addr={}, scn={}, is_server={}, mtu={}, uuid={}, "
        "dlci={}",
        ADDRESS_TO_LOGGABLE_STR(bd_addr), scn, is_server, mtu, loghex(uuid),
        dlci);
    return PORT_NO_RESOURCES;
  }
  p_port->sec_mask = sec_mask;
@@ -219,12 +217,11 @@ int RFCOMM_CreateConnectionWithSecurity(uint16_t uuid, uint8_t scn,
  p_port->p_mgmt_callback = p_mgmt_cb;
  p_port->bd_addr = bd_addr;

  LOG(INFO) << __func__ << ": bd_addr=" << bd_addr
            << ", scn=" << std::to_string(scn) << ", is_server=" << is_server
            << ", mtu=" << mtu << ", uuid=" << loghex(uuid)
            << ", dlci=" << std::to_string(dlci)
            << ", signal_state=" << loghex(p_port->default_signal_state)
            << ", p_port=" << p_port;
  log::info(
      "bd_addr={}, scn={}, is_server={}, mtu={}, uuid={}, dlci={}, "
      "signal_state={}, p_port={}",
      ADDRESS_TO_LOGGABLE_STR(bd_addr), scn, is_server, mtu, loghex(uuid), dlci,
      loghex(p_port->default_signal_state), fmt::ptr(p_port));

  // If this is not initiator of the connection need to just wait
  if (p_port->is_server) {
@@ -288,17 +285,17 @@ int RFCOMM_ControlReqFromBTSOCK(uint8_t dlci, const RawAddress& bd_addr,
int RFCOMM_RemoveConnection(uint16_t handle) {
  tPORT* p_port;

  LOG_VERBOSE("RFCOMM_RemoveConnection() handle:%d", handle);
  log::verbose("RFCOMM_RemoveConnection() handle:{}", handle);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
    LOG_ERROR("RFCOMM_RemoveConnection() BAD handle:%d", handle);
    log::error("RFCOMM_RemoveConnection() BAD handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    LOG_VERBOSE("RFCOMM_RemoveConnection() Not opened:%d", handle);
    log::verbose("RFCOMM_RemoveConnection() Not opened:{}", handle);
    return (PORT_SUCCESS);
  }

@@ -321,7 +318,7 @@ int RFCOMM_RemoveConnection(uint16_t handle) {
int RFCOMM_RemoveServer(uint16_t handle) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
    LOG(ERROR) << __func__ << ": bad handle " << handle;
    log::error("bad handle {}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];
@@ -330,10 +327,10 @@ int RFCOMM_RemoveServer(uint16_t handle) {
  p_port->p_mgmt_callback = nullptr;

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    LOG_DEBUG("handle %u not opened", handle);
    log::debug("handle {} not opened", handle);
    return (PORT_SUCCESS);
  }
  LOG(INFO) << __func__ << ": handle=" << handle;
  log::info("handle={}", handle);

  /* this port will be deallocated after closing */
  p_port->keep_port_handle = false;
@@ -373,7 +370,7 @@ int PORT_SetEventCallback(uint16_t port_handle, tPORT_CALLBACK* p_port_cb) {
    return (PORT_NOT_OPENED);
  }

  LOG_VERBOSE("PORT_SetEventCallback() handle:%d", port_handle);
  log::verbose("PORT_SetEventCallback() handle:{}", port_handle);

  p_port->p_callback = p_port_cb;

@@ -419,8 +416,8 @@ int PORT_SetDataCOCallback(uint16_t port_handle,
                           tPORT_DATA_CO_CALLBACK* p_port_cb) {
  tPORT* p_port;

  LOG_VERBOSE("PORT_SetDataCOCallback() handle:%d cb 0x%p", port_handle,
              p_port_cb);
  log::verbose("PORT_SetDataCOCallback() handle:{} cb 0x{}", port_handle,
               fmt::ptr(p_port_cb));

  /* Check if handle is valid to avoid crashing */
  if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
@@ -451,7 +448,7 @@ int PORT_SetDataCOCallback(uint16_t port_handle,
int PORT_SetEventMask(uint16_t port_handle, uint32_t mask) {
  tPORT* p_port;

  LOG_VERBOSE("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, mask);
  log::verbose("PORT_SetEventMask() handle:{} mask:0x{:x}", port_handle, mask);

  /* Check if handle is valid to avoid crashing */
  if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
@@ -488,10 +485,10 @@ int PORT_CheckConnection(uint16_t handle, RawAddress* bd_addr,
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];
  LOG_VERBOSE(
      "%s: handle=%d, in_use=%d, port_state=%d, p_mcb=%p, peer_ready=%d, "
      "rfc_state=%d",
      __func__, handle, p_port->in_use, p_port->state, p_port->rfc.p_mcb,
  log::verbose(
      "handle={}, in_use={}, port_state={}, p_mcb={}, peer_ready={}, "
      "rfc_state={}",
      handle, p_port->in_use, p_port->state, fmt::ptr(p_port->rfc.p_mcb),
      (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->peer_ready : -1),
      p_port->rfc.state);

@@ -527,7 +524,7 @@ bool PORT_IsOpening(RawAddress* bd_addr) {
    if ((multiplexer_cb.state > RFC_MX_STATE_IDLE) &&
        (multiplexer_cb.state < RFC_MX_STATE_CONNECTED)) {
      *bd_addr = multiplexer_cb.bd_addr;
      LOG_INFO(
      log::info(
          "Found a rfc_mcb in the middle of opening a port, returning true");
      return true;
    }
@@ -544,20 +541,20 @@ bool PORT_IsOpening(RawAddress* bd_addr) {
        }
      }

      LOG_INFO("RFC_MX_STATE_CONNECTED, found_port=%d, tRFC_PORT_STATE=%d",
      log::info("RFC_MX_STATE_CONNECTED, found_port={}, tRFC_PORT_STATE={}",
                found_port, p_port != nullptr ? p_port->rfc.state : 0);
      if ((!found_port) ||
          (found_port && (p_port->rfc.state < RFC_STATE_OPENED))) {
        /* Port is not established yet. */
        *bd_addr = multiplexer_cb.bd_addr;
        LOG_INFO(
        log::info(
            "In RFC_MX_STATE_CONNECTED but port is not established yet, "
            "returning true");
        return true;
      }
    }
  }
  LOG_INFO("false");
  log::info("false");
  return false;
}

@@ -578,7 +575,7 @@ int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
  tPORT* p_port;
  uint8_t baud_rate;

  LOG_VERBOSE("PORT_SetState() handle:%d", handle);
  log::verbose("PORT_SetState() handle:{}", handle);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
@@ -595,7 +592,7 @@ int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
    return (PORT_LINE_ERR);
  }

  LOG_VERBOSE("PORT_SetState() handle:%d FC_TYPE:0x%x", handle,
  log::verbose("PORT_SetState() handle:{} FC_TYPE:0x{:x}", handle,
               p_settings->fc_type);

  baud_rate = p_port->user_port_pars.baud_rate;
@@ -623,7 +620,7 @@ int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings) {
  tPORT* p_port;

  LOG_VERBOSE("PORT_GetState() handle:%d", handle);
  log::verbose("PORT_GetState() handle:{}", handle);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
@@ -663,7 +660,7 @@ int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable) {
  bool old_fc;
  uint32_t events;

  LOG_VERBOSE("PORT_FlowControl() handle:%d enable: %d", handle, enable);
  log::verbose("PORT_FlowControl() handle:{} enable: {}", handle, enable);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
@@ -731,7 +728,7 @@ int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
  BT_HDR* p_buf;
  uint16_t count;

  LOG_VERBOSE("PORT_ReadData() handle:%d max_len:%d", handle, max_len);
  log::verbose("PORT_ReadData() handle:{} max_len:{}", handle, max_len);

  /* Initialize this in case of an error */
  *p_len = 0;
@@ -748,7 +745,7 @@ int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
  }

  if (p_port->state == PORT_CONNECTION_STATE_OPENING) {
    LOG_WARN("Trying to read a port in PORT_CONNECTION_STATE_OPENING state");
    log::warn("Trying to read a port in PORT_CONNECTION_STATE_OPENING state");
  }

  if (p_port->line_status) {
@@ -756,7 +753,7 @@ int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
  }

  if (fixed_queue_is_empty(p_port->rx.queue)) {
    LOG_WARN("Read on empty input queue");
    log::warn("Read on empty input queue");
    return (PORT_SUCCESS);
  }

@@ -803,10 +800,10 @@ int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
  }

  if (*p_len == 1) {
    LOG_VERBOSE("PORT_ReadData queue:%d returned:%d %x", p_port->rx.queue_size,
                *p_len, (p_data[0]));
    log::verbose("PORT_ReadData queue:{} returned:{} {:x}",
                 p_port->rx.queue_size, *p_len, (p_data[0]));
  } else {
    LOG_VERBOSE("PORT_ReadData queue:%d returned:%d", p_port->rx.queue_size,
    log::verbose("PORT_ReadData queue:{} returned:{}", p_port->rx.queue_size,
                 *p_len);
  }

@@ -846,7 +843,7 @@ static int port_write(tPORT* p_port, BT_HDR* p_buf) {
       (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED))) {
    if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM) ||
        (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_CRITICAL_WM)) {
      LOG_WARN("PORT_Write: Queue size: %d", p_port->tx.queue_size);
      log::warn("PORT_Write: Queue size: {}", p_port->tx.queue_size);

      osi_free(p_buf);

@@ -856,9 +853,9 @@ static int port_write(tPORT* p_port, BT_HDR* p_buf) {
      return (PORT_TX_FULL);
    }

    LOG_VERBOSE(
        "PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d "
        "ctrl_state %x",
    log::verbose(
        "PORT_Write : Data is enqued. flow disabled {} peer_ready {} state {} "
        "ctrl_state {:x}",
        p_port->tx.peer_fc,
        (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready), p_port->rfc.state,
        p_port->port_ctrl);
@@ -868,7 +865,7 @@ static int port_write(tPORT* p_port, BT_HDR* p_buf) {

    return (PORT_CMD_PENDING);
  } else {
    LOG_VERBOSE("PORT_Write : Data is being sent");
    log::verbose("PORT_Write : Data is being sent");

    RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf);
    return (PORT_SUCCESS);
@@ -894,7 +891,7 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
  int rc = 0;
  uint16_t length;

  LOG_VERBOSE("PORT_WriteDataCO() handle:%d", handle);
  log::verbose("PORT_WriteDataCO() handle:{}", handle);
  *p_len = 0;

  /* Check if handle is valid to avoid crashing */
@@ -904,12 +901,12 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
  p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    LOG_WARN("PORT_WriteDataByFd() no port state:%d", p_port->state);
    log::warn("PORT_WriteDataByFd() no port state:{}", p_port->state);
    return (PORT_NOT_OPENED);
  }

  if (!p_port->peer_mtu) {
    LOG_ERROR("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
    log::error("PORT_WriteDataByFd() peer_mtu:{}", p_port->peer_mtu);
    return (PORT_UNKNOWN_ERROR);
  }
  int available = 0;
@@ -917,9 +914,9 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
  if (!p_port->p_data_co_callback(handle, (uint8_t*)&available,
                                  sizeof(available),
                                  DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE)) {
    LOG_ERROR(
    log::error(
        "p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, "
        "available:%d",
        "available:{}",
        available);
    return (PORT_UNKNOWN_ERROR);
  }
@@ -944,9 +941,9 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
            available, DATA_CO_CALLBACK_TYPE_OUTGOING))

    {
      error(
      log::error(
          "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, "
          "available:%d",
          "available:{}",
          available);
      mutex_global_unlock();
      return (PORT_UNKNOWN_ERROR);
@@ -975,8 +972,8 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
        (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) {
      port_flow_control_user(p_port);
      event |= PORT_EV_FC;
      LOG_VERBOSE(
          "tx queue is full,tx.queue_size:%d,tx.queue.count:%zu,available:%d",
      log::verbose(
          "tx queue is full,tx.queue_size:{},tx.queue.count:{},available:{}",
          p_port->tx.queue_size, fixed_queue_length(p_port->tx.queue),
          available);
      break;
@@ -998,13 +995,13 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
    if (!p_port->p_data_co_callback(handle,
                                    (uint8_t*)(p_buf + 1) + p_buf->offset,
                                    length, DATA_CO_CALLBACK_TYPE_OUTGOING)) {
      error(
          "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d",
      log::error(
          "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:{}",
          length);
      return (PORT_UNKNOWN_ERROR);
    }

    LOG_VERBOSE("PORT_WriteData %d bytes", length);
    log::verbose("PORT_WriteData {} bytes", length);

    rc = port_write(p_port, p_buf);

@@ -1051,7 +1048,7 @@ int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,
  int rc = 0;
  uint16_t length;

  LOG_VERBOSE("PORT_WriteData() max_len:%d", max_len);
  log::verbose("PORT_WriteData() max_len:{}", max_len);

  *p_len = 0;

@@ -1062,16 +1059,16 @@ int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,
  p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    LOG_WARN("PORT_WriteData() no port state:%d", p_port->state);
    log::warn("PORT_WriteData() no port state:{}", p_port->state);
    return (PORT_NOT_OPENED);
  }

  if (p_port->state == PORT_CONNECTION_STATE_OPENING) {
    LOG_WARN("Write data received but port is in OPENING state");
    log::warn("Write data received but port is in OPENING state");
  }

  if (!max_len || !p_port->peer_mtu) {
    LOG_ERROR("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
    log::error("PORT_WriteData() peer_mtu:{}", p_port->peer_mtu);
    return (PORT_UNKNOWN_ERROR);
  }

@@ -1118,7 +1115,7 @@ int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,

    memcpy((uint8_t*)(p_buf + 1) + p_buf->offset, p_data, length);

    LOG_VERBOSE("PORT_WriteData %d bytes", length);
    log::verbose("PORT_WriteData {} bytes", length);

    rc = port_write(p_port, p_buf);

+57 −54

File changed.

Preview size limit exceeded, changes collapsed.

+31 −31
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#define LOG_TAG "rfcomm_port_utils"

#include <base/logging.h>
#include <bluetooth/log.h>

#include <cstdint>
#include <cstring>
@@ -40,6 +41,8 @@
#include "stack/rfcomm/rfc_int.h"
#include "types/raw_address.h"

using namespace bluetooth;

static const tPORT_STATE default_port_pars = {
    PORT_BAUD_RATE_9600,
    PORT_8_BITS,
@@ -85,16 +88,15 @@ tPORT* port_allocate_port(uint8_t dlci, const RawAddress& bd_addr) {
      p_port->dlci = dlci;
      p_port->bd_addr = bd_addr;
      rfc_cb.rfc.last_port_index = port_index;
      LOG_VERBOSE(
          "%s: rfc_cb.port.port[%d]:%p chosen, "
          "last_port_index:%d, bd_addr=%s",
          __func__, port_index, p_port, rfc_cb.rfc.last_port_index,
      log::verbose(
          "rfc_cb.port.port[{}]:{} chosen, last_port_index:{}, bd_addr={}",
          port_index, fmt::ptr(p_port), rfc_cb.rfc.last_port_index,
          ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
      return p_port;
    }
  }
  LOG(WARNING) << __func__ << ": running out of free ports for dlci "
               << std::to_string(dlci) << ", bd_addr " << bd_addr;
  log::warn("running out of free ports for dlci {}, bd_addr {}", dlci,
            ADDRESS_TO_LOGGABLE_STR(bd_addr));
  return nullptr;
}

@@ -151,7 +153,8 @@ void port_select_mtu(tPORT* p_port) {
        get_btm_client_interface().peer.BTM_GetMaxPacketSize(p_port->bd_addr);
    if (packet_size == 0) {
      /* something is very wrong */
      LOG(WARNING) << __func__ << ": bad packet size 0 for" << p_port->bd_addr;
      log::warn("bad packet size 0 for{}",
                ADDRESS_TO_LOGGABLE_STR(p_port->bd_addr));
      p_port->mtu = RFCOMM_DEFAULT_MTU;
    } else {
      /* We try to negotiate MTU that each packet can be split into whole
@@ -171,16 +174,14 @@ void port_select_mtu(tPORT* p_port) {
        p_port->mtu = ((L2CAP_MTU_SIZE + L2CAP_PKT_OVERHEAD) / packet_size *
                       packet_size) -
                      RFCOMM_DATA_OVERHEAD - L2CAP_PKT_OVERHEAD;
        LOG_VERBOSE("%s: selected %d based on connection speed", __func__,
                    p_port->mtu);
        log::verbose("selected {} based on connection speed", p_port->mtu);
      } else {
        p_port->mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
        LOG_VERBOSE("%s: selected %d based on l2cap PDU size", __func__,
                    p_port->mtu);
        log::verbose("selected {} based on l2cap PDU size", p_port->mtu);
      }
    }
  } else {
    LOG_VERBOSE("%s: application selected %d", __func__, p_port->mtu);
    log::verbose("application selected {}", p_port->mtu);
  }
  p_port->credit_rx_max = (PORT_RX_HIGH_WM / p_port->mtu);
  if (p_port->credit_rx_max > PORT_RX_BUF_HIGH_WM)
@@ -191,8 +192,8 @@ void port_select_mtu(tPORT* p_port) {
  p_port->rx_buf_critical = (PORT_RX_CRITICAL_WM / p_port->mtu);
  if (p_port->rx_buf_critical > PORT_RX_BUF_CRITICAL_WM)
    p_port->rx_buf_critical = PORT_RX_BUF_CRITICAL_WM;
  LOG_VERBOSE("%s: credit_rx_max %d, credit_rx_low %d, rx_buf_critical %d",
              __func__, p_port->credit_rx_max, p_port->credit_rx_low,
  log::verbose("credit_rx_max {}, credit_rx_low {}, rx_buf_critical {}",
               p_port->credit_rx_max, p_port->credit_rx_low,
               p_port->rx_buf_critical);
}

@@ -206,7 +207,7 @@ void port_select_mtu(tPORT* p_port) {
 *
 ******************************************************************************/
void port_release_port(tPORT* p_port) {
  LOG_VERBOSE("%s p_port: %p state: %d keep_handle: %d", __func__, p_port,
  log::verbose("p_port: {} state: {} keep_handle: {}", fmt::ptr(p_port),
               p_port->rfc.state, p_port->keep_port_handle);

  mutex_global_lock();
@@ -246,7 +247,7 @@ void port_release_port(tPORT* p_port) {
    mutex_global_unlock();

    if (p_port->keep_port_handle) {
      LOG_VERBOSE("%s Re-initialize handle: %d", __func__, p_port->handle);
      log::verbose("Re-initialize handle: {}", p_port->handle);

      /* save event mask and callback */
      uint32_t mask = p_port->ev_mask;
@@ -268,7 +269,7 @@ void port_release_port(tPORT* p_port) {
      p_port->local_ctrl.modem_signal = p_port->default_signal_state;
      p_port->bd_addr = RawAddress::kAny;
    } else {
      LOG_VERBOSE("%s Clean-up handle: %d", __func__, p_port->handle);
      log::verbose("Clean-up handle: {}", p_port->handle);
      alarm_free(p_port->rfc.port_timer);
      memset(p_port, 0, sizeof(tPORT));
    }
@@ -287,13 +288,13 @@ tRFC_MCB* port_find_mcb(const RawAddress& bd_addr) {
  for (tRFC_MCB& mcb : rfc_cb.port.rfc_mcb) {
    if ((mcb.state != RFC_MX_STATE_IDLE) && (mcb.bd_addr == bd_addr)) {
      /* Multiplexer channel found do not change anything */
      LOG_VERBOSE("found, bd_addr:%s, rfc_mcb:%p, lcid:%s",
                  ADDRESS_TO_LOGGABLE_CSTR(bd_addr), &mcb,
                  loghex(mcb.lcid).c_str());
      log::verbose("found, bd_addr:{}, rfc_mcb:{}, lcid:{}",
                   ADDRESS_TO_LOGGABLE_CSTR(bd_addr), fmt::ptr(&mcb),
                   loghex(mcb.lcid));
      return &mcb;
    }
  }
  LOG_WARN("not found, bd_addr:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
  log::warn("not found, bd_addr:{}", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
  return nullptr;
}

@@ -311,22 +312,21 @@ tRFC_MCB* port_find_mcb(const RawAddress& bd_addr) {
 ******************************************************************************/
tPORT* port_find_mcb_dlci_port(tRFC_MCB* p_mcb, uint8_t dlci) {
  if (!p_mcb) {
    LOG(ERROR) << __func__ << ": p_mcb is null, dlci=" << std::to_string(dlci);
    log::error("p_mcb is null, dlci={}", dlci);
    return nullptr;
  }

  if (dlci > RFCOMM_MAX_DLCI) {
    LOG(WARNING) << __func__ << ": DLCI " << std::to_string(dlci)
                 << " is too large, bd_addr=" << p_mcb->bd_addr
                 << ", p_mcb=" << p_mcb;
    log::warn("DLCI {} is too large, bd_addr={}, p_mcb={}", dlci,
              ADDRESS_TO_LOGGABLE_STR(p_mcb->bd_addr), fmt::ptr(p_mcb));
    return nullptr;
  }

  uint8_t handle = p_mcb->port_handles[dlci];
  if (handle == 0) {
    LOG(INFO) << __func__ << ": Cannot find allocated RFCOMM app port for DLCI "
              << std::to_string(dlci) << " on " << p_mcb->bd_addr
              << ", p_mcb=" << p_mcb;
    log::info(
        "Cannot find allocated RFCOMM app port for DLCI {} on {}, p_mcb={}",
        dlci, ADDRESS_TO_LOGGABLE_STR(p_mcb->bd_addr), fmt::ptr(p_mcb));
    return nullptr;
  }
  return &rfc_cb.port.port[handle - 1];
@@ -521,7 +521,7 @@ void port_flow_control_peer(tPORT* p_port, bool enable, uint16_t count) {
      else if (((p_port->rx.queue_size > PORT_RX_HIGH_WM) ||
                (fixed_queue_length(p_port->rx.queue) > PORT_RX_BUF_HIGH_WM)) &&
               !p_port->rx.peer_fc) {
        LOG_VERBOSE("PORT_DataInd Data reached HW. Sending FC set.");
        log::verbose("PORT_DataInd Data reached HW. Sending FC set.");

        p_port->rx.peer_fc = true;
        RFCOMM_FlowReq(p_port->rfc.p_mcb, p_port->dlci, false);
+11 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#pragma once

#include <bluetooth/log.h>

#include <cstdint>

#include "macros.h"
@@ -136,3 +138,12 @@ inline std::string rfcomm_port_event_text(const tRFC_PORT_EVENT& event) {
      return std::string("UNKNOWN[") + std::to_string(event) + std::string("]");
  }
}

namespace fmt {
template <>
struct formatter<tRFC_EVENT> : enum_formatter<tRFC_EVENT> {};
template <>
struct formatter<tRFC_MX_EVENT> : enum_formatter<tRFC_MX_EVENT> {};
template <>
struct formatter<tRFC_PORT_EVENT> : enum_formatter<tRFC_PORT_EVENT> {};
}  // namespace fmt
Loading