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

Commit d3bd6bc4 authored by cmanton@google.com's avatar cmanton@google.com Committed by Chris Manton
Browse files

Text logging for classic stack::l2cap result code

Bug: 269757189
Test: net_test_stack_l2cap
Tag: #refactor

Change-Id: If73a86177c8042822c7436b886d9fddf7b586ed0
parent 7f6c079e
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -156,6 +156,44 @@ typedef enum : uint16_t {
      L2CAP_CONN_LE_MASK + L2CAP_LE_RESULT_INVALID_PARAMETERS,
} tL2CAP_CONN;

#ifndef CASE_RETURN_TEXT
#define CASE_RETURN_TEXT(code) \
  case code:                   \
    return #code
#endif

inline std::string l2cap_result_code_text(const tL2CAP_CONN& result) {
  switch (result) {
    CASE_RETURN_TEXT(L2CAP_CONN_OK);
    CASE_RETURN_TEXT(L2CAP_CONN_PENDING);
    CASE_RETURN_TEXT(L2CAP_CONN_NO_PSM);
    CASE_RETURN_TEXT(L2CAP_CONN_SECURITY_BLOCK);
    CASE_RETURN_TEXT(L2CAP_CONN_NO_RESOURCES);
    CASE_RETURN_TEXT(L2CAP_CONN_TIMEOUT);
    CASE_RETURN_TEXT(L2CAP_CONN_OTHER_ERROR);
    CASE_RETURN_TEXT(L2CAP_CONN_ACL_CONNECTION_FAILED);
    CASE_RETURN_TEXT(L2CAP_CONN_CLIENT_SECURITY_CLEARANCE_FAILED);
    CASE_RETURN_TEXT(L2CAP_CONN_NO_LINK);
    CASE_RETURN_TEXT(L2CAP_CONN_CANCEL);
    CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_AUTHENTICATION);
    CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_AUTHORIZATION);
    CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_ENCRYP_KEY_SIZE);
    CASE_RETURN_TEXT(L2CAP_CONN_INSUFFICIENT_ENCRYP);
    CASE_RETURN_TEXT(L2CAP_CONN_INVALID_SOURCE_CID);
    CASE_RETURN_TEXT(L2CAP_CONN_SOURCE_CID_ALREADY_ALLOCATED);
    CASE_RETURN_TEXT(L2CAP_CONN_UNACCEPTABLE_PARAMETERS);
    CASE_RETURN_TEXT(L2CAP_CONN_INVALID_PARAMETERS);
    default:
      return std::string("UNKNOWN[") + std::to_string(result) +
             std::string("]");
  }
}
#undef CASE_RETURN_TEXT

inline tL2CAP_CONN to_l2cap_result_code(uint16_t result) {
  return static_cast<tL2CAP_CONN>(result);
}

inline std::string l2cap_le_result_code_text(
    const tL2CAP_LE_RESULT_CODE& code) {
  switch (code) {
+46 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "internal_include/bt_trace.h"
#include "stack/btm/btm_int_types.h"
#include "stack/include/l2cap_hci_link_interface.h"
#include "stack/include/l2cdefs.h"
#include "stack/l2cap/l2c_int.h"
#include "types/raw_address.h"

@@ -203,3 +204,48 @@ TEST_F(StackL2capChannelTest, l2c_link_init) {
  ASSERT_EQ(controller_.get_acl_buffer_count_classic(),
            l2cb.controller_xmit_window);
}

TEST_F(StackL2capTest, l2cap_result_code_text) {
  std::vector<std::pair<tL2CAP_CONN, std::string>> results = {
      std::make_pair(L2CAP_CONN_OK, "L2CAP_CONN_OK"),
      std::make_pair(L2CAP_CONN_PENDING, "L2CAP_CONN_PENDING"),
      std::make_pair(L2CAP_CONN_NO_PSM, "L2CAP_CONN_NO_PSM"),
      std::make_pair(L2CAP_CONN_SECURITY_BLOCK, "L2CAP_CONN_SECURITY_BLOCK"),
      std::make_pair(L2CAP_CONN_NO_RESOURCES, "L2CAP_CONN_NO_RESOURCES"),
      std::make_pair(L2CAP_CONN_TIMEOUT, "L2CAP_CONN_TIMEOUT"),
      std::make_pair(L2CAP_CONN_OTHER_ERROR, "L2CAP_CONN_OTHER_ERROR"),
      std::make_pair(L2CAP_CONN_ACL_CONNECTION_FAILED,
                     "L2CAP_CONN_ACL_CONNECTION_FAILED"),
      std::make_pair(L2CAP_CONN_CLIENT_SECURITY_CLEARANCE_FAILED,
                     "L2CAP_CONN_CLIENT_SECURITY_CLEARANCE_FAILED"),
      std::make_pair(L2CAP_CONN_NO_LINK, "L2CAP_CONN_NO_LINK"),
      std::make_pair(L2CAP_CONN_CANCEL, "L2CAP_CONN_CANCEL"),
      std::make_pair(L2CAP_CONN_INSUFFICIENT_AUTHENTICATION,
                     "L2CAP_CONN_INSUFFICIENT_AUTHENTICATION"),
      std::make_pair(L2CAP_CONN_INSUFFICIENT_AUTHORIZATION,
                     "L2CAP_CONN_INSUFFICIENT_AUTHORIZATION"),
      std::make_pair(L2CAP_CONN_INSUFFICIENT_ENCRYP_KEY_SIZE,
                     "L2CAP_CONN_INSUFFICIENT_ENCRYP_KEY_SIZE"),
      std::make_pair(L2CAP_CONN_INSUFFICIENT_ENCRYP,
                     "L2CAP_CONN_INSUFFICIENT_ENCRYP"),
      std::make_pair(L2CAP_CONN_INVALID_SOURCE_CID,
                     "L2CAP_CONN_INVALID_SOURCE_CID"),
      std::make_pair(L2CAP_CONN_SOURCE_CID_ALREADY_ALLOCATED,
                     "L2CAP_CONN_SOURCE_CID_ALREADY_ALLOCATED"),
      std::make_pair(L2CAP_CONN_UNACCEPTABLE_PARAMETERS,
                     "L2CAP_CONN_UNACCEPTABLE_PARAMETERS"),
      std::make_pair(L2CAP_CONN_INVALID_PARAMETERS,
                     "L2CAP_CONN_INVALID_PARAMETERS"),
  };
  for (const auto& result : results) {
    ASSERT_STREQ(result.second.c_str(),
                 l2cap_result_code_text(result.first).c_str());
  }
  std::ostringstream oss;
  oss << "UNKNOWN[" << std::numeric_limits<std::uint16_t>::max() << "]";
  ASSERT_STREQ(
      oss.str().c_str(),
      l2cap_result_code_text(
          static_cast<tL2CAP_CONN>(std::numeric_limits<std::uint16_t>::max()))
          .c_str());
}