From d99dc5c7c1a25e60c23c39de3e9e11019c34f54b Mon Sep 17 00:00:00 2001 From: Chris Manton Date: Fri, 4 Dec 2020 17:01:50 -0800 Subject: [PATCH] Enum-ify bta/dm/bta_dm_int::tBTA_DM_CONN_STATE Towards readable code Bug: 163134718 Tag: #refactor Test: gd/cert/run --host Change-Id: I9f67a9c775a5183a705c822b71810cd880f1faee --- bta/dm/bta_dm_int.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/bta/dm/bta_dm_int.h b/bta/dm/bta_dm_int.h index 9dd8613e2..a50a07f6b 100644 --- a/bta/dm/bta_dm_int.h +++ b/bta/dm/bta_dm_int.h @@ -32,6 +32,12 @@ #include "bta/sys/bta_sys.h" #include "main/shim/dumpsys.h" +#ifndef CASE_RETURN_TEXT +#define CASE_RETURN_TEXT(code) \ + case code: \ + return #code +#endif + /***************************************************************************** * Constants and data types ****************************************************************************/ @@ -158,10 +164,21 @@ typedef union { #define BTA_DM_NUM_PEER_DEVICE 7 -#define BTA_DM_NOT_CONNECTED 0 -#define BTA_DM_CONNECTED 1 -#define BTA_DM_UNPAIRING 2 -typedef uint8_t tBTA_DM_CONN_STATE; +typedef enum : uint8_t { + BTA_DM_NOT_CONNECTED = 0, + BTA_DM_CONNECTED = 1, + BTA_DM_UNPAIRING = 2, +} tBTA_DM_CONN_STATE; + +inline std::string bta_conn_state_text(tBTA_DM_CONN_STATE state) { + switch (state) { + CASE_RETURN_TEXT(BTA_DM_NOT_CONNECTED); + CASE_RETURN_TEXT(BTA_DM_CONNECTED); + CASE_RETURN_TEXT(BTA_DM_UNPAIRING); + default: + return std::string("UNKNOWN"); + } +} typedef enum : uint8_t { BTA_DM_DI_NONE = 0x00, /* nothing special */ @@ -528,4 +545,6 @@ void bta_dm_search_set_state(uint8_t state); void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding); void bta_dm_eir_update_cust_uuid(const tBTA_CUSTOM_UUID &curr, bool adding); + +#undef CASE_RETURN_TEXT #endif /* BTA_DM_INT_H */ -- 2.11.0