Cleanup: Config: use std::optional for NxpConfig_GetXXX()

NxpConfig_GetXXX() returns std::optional<type>
and add NxpConfig_GetBool().

Use std::span<const uint8_t> for bytearray type.

Bug: 321604848
Test: Check logcat from W24 and W25
Change-Id: Id743d63cba7f07cda4df2cbbc4a9f4747fcbbf59
diff --git a/halimpl/hal/hbci/NxpUwbChipHbciModule.cc b/halimpl/hal/hbci/NxpUwbChipHbciModule.cc
index 124bf29..532ba1d 100644
--- a/halimpl/hal/hbci/NxpUwbChipHbciModule.cc
+++ b/halimpl/hal/hbci/NxpUwbChipHbciModule.cc
@@ -1,3 +1,5 @@
+#include <optional>
+#include <string_view>
 #include <vector>
 
 #include "NxpUwbChip.h"
@@ -271,20 +273,17 @@
 static int16_t sr1xx_extra_group_delay(const uint8_t ch)
 {
   int16_t required_compensation = 0;
-  char calibrated_with_fw[15] = {0};
 
   /* Calibrated with D4X and we are on D5X or later */
   bool is_calibrated_with_d4x = false;
 
-  int has_calibrated_with_fw_config = NxpConfig_GetStr(
-      "cal.fw_version", calibrated_with_fw, sizeof(calibrated_with_fw) - 1);
+  std::optional<std::string_view> res = NxpConfig_GetStr("cal.fw_version");
 
-  if (has_calibrated_with_fw_config) {
+  if (res.has_value()) {
+    std::string_view fw_version = *res;
     // Conf file has entry of `cal.fw_version`
-    if ( ( 0 == memcmp("48.", calibrated_with_fw, 3)) ||
-         ( 0 == memcmp("49.", calibrated_with_fw, 3))) {
-      is_calibrated_with_d4x = true;
-    }
+    is_calibrated_with_d4x =
+      fw_version.starts_with("48.") || fw_version.starts_with("49.");
   }
   else
   {
@@ -303,15 +302,10 @@
 
     // Calibrated with D49
     // Required extra negative offset, Channel specific, but antenna agnostic.
-    unsigned short cal_chx_extra_d49_offset_n = 0;
     char key[32];
     std::snprintf(key, sizeof(key), "cal.ch%u.extra_d49_offset_n", ch);
-    int has_extra_d49_offset_n = NxpConfig_GetNum(
-      key, &cal_chx_extra_d49_offset_n, sizeof(cal_chx_extra_d49_offset_n));
-
-    if (has_extra_d49_offset_n) { /*< Extra correction from conf file ... */
-      required_compensation -= cal_chx_extra_d49_offset_n;
-    }
+    uint16_t cal_chx_extra_d49_offset_n = NxpConfig_GetNum<uint16_t>(key).value_or(0);
+    required_compensation -= cal_chx_extra_d49_offset_n;
   }
   else
   {
@@ -408,9 +402,8 @@
       return UWBSTATUS_SUCCESS;
   }
 
-  unsigned long val = 0;
-  NxpConfig_GetNum(NAME_UWB_BINDING_LOCKING_ALLOWED, &val, sizeof(val));
-  bool isBindingLockingAllowed = (val != 0);
+  bool isBindingLockingAllowed =
+    NxpConfig_GetBool(NAME_UWB_BINDING_LOCKING_ALLOWED).value_or(false);
   if (!isBindingLockingAllowed) {
     return UWBSTATUS_SUCCESS;
   }
diff --git a/halimpl/hal/hbci/phNxpUciHal_hbci_fwd.cc b/halimpl/hal/hbci/phNxpUciHal_hbci_fwd.cc
index ee56a7f..08fbb4f 100644
--- a/halimpl/hal/hbci/phNxpUciHal_hbci_fwd.cc
+++ b/halimpl/hal/hbci/phNxpUciHal_hbci_fwd.cc
@@ -23,7 +23,9 @@
 #include <stdlib.h>
 #include <sys/ioctl.h>
 
+#include <optional>
 #include <string>
+#include <string_view>
 
 #include "phNxpConfig.h"
 #include "phNxpLog.h"
@@ -34,13 +36,17 @@
 using namespace std;
 #define FILEPATH_MAXLEN 500
 
-static uint8_t chip_id = 0x00;
-static uint8_t deviceLcInfo = 0x00;
-static uint8_t is_fw_download_log_enabled = 0x00;
-static const char* default_prod_fw = "libsr100t_prod_fw.bin";
-static const char* default_dev_fw = "libsr100t_dev_fw.bin";
-static const char* default_fw_dir = "/vendor/firmware/uwb/";
-static string default_fw_path;
+namespace {
+
+uint8_t chip_id = 0x00;
+uint8_t deviceLcInfo = 0x00;
+uint8_t is_fw_download_log_enabled = 0x00;
+constexpr std::string_view default_prod_fw = "libsr100t_prod_fw.bin";
+constexpr std::string_view default_dev_fw = "libsr100t_dev_fw.bin";
+constexpr std::string_view default_fw_dir = "/vendor/firmware/uwb/";
+string default_fw_path;
+
+}   // namespace
 
 /*************************************************************************************/
 /*   LOCAL FUNCTIONS                                                                 */
@@ -58,37 +64,22 @@
     gOpts.fMiso         = NULL;
 }
 
-
+// TODO: change function name & return type
 static int init(void)
 {
-  const char *pDefaultFwFileName = NULL;
-  char configured_fw_name[FILEPATH_MAXLEN];
   default_fw_path = default_fw_dir;
 
   if((deviceLcInfo == PHHBCI_HELIOS_PROD_KEY_1) || (deviceLcInfo == PHHBCI_HELIOS_PROD_KEY_2)) {
-    pDefaultFwFileName = default_prod_fw;
-    if (!NxpConfig_GetStr(NAME_NXP_UWB_PROD_FW_FILENAME, configured_fw_name, sizeof(configured_fw_name))) {
-      ALOGD("Invalid Prod Fw  name keeping the default name: %s", pDefaultFwFileName);
-      default_fw_path += pDefaultFwFileName;
-    } else{
-      ALOGD("configured_fw_name : %s", configured_fw_name);
-      default_fw_path += configured_fw_name;
-    }
+    default_fw_path += NxpConfig_GetStr(NAME_NXP_UWB_PROD_FW_FILENAME).value_or(default_prod_fw);
   } else if (deviceLcInfo == PHHBCI_HELIOS_DEV_KEY) {
-    pDefaultFwFileName = default_dev_fw;
-    if (!NxpConfig_GetStr(NAME_NXP_UWB_DEV_FW_FILENAME, configured_fw_name, sizeof(configured_fw_name))) {
-      ALOGD("Invalid Dev Fw  name keeping the default name: %s", pDefaultFwFileName);
-      default_fw_path += pDefaultFwFileName;
-    } else{
-      ALOGD("configured_fw_name : %s", configured_fw_name);
-      default_fw_path += configured_fw_name;
-    }
+    default_fw_path += NxpConfig_GetStr(NAME_NXP_UWB_DEV_FW_FILENAME).value_or(default_dev_fw);
   } else {
     ALOGD("Invalid DeviceLCInfo : 0x%x\n", deviceLcInfo);
     return 1;
   }
-
   ALOGD("Referring FW path..........: %s", default_fw_path.c_str());
+
+  // TODO: remove these out.
   // gOpts.capture = Capture_Apdu_With_Dummy_Miso;
 
   if (Capture_Off != gOpts.capture) {
@@ -941,14 +932,9 @@
         ALOGD("phHbci_GetChipIdInfo Failure!\n");
         return 1;
     }
-    is_fw_download_log_enabled = false;
+    is_fw_download_log_enabled = NxpConfig_GetBool(NAME_UWB_FW_DOWNLOAD_LOG).value_or(false);
+    ALOGD("NAME_UWB_FW_DOWNLOAD_LOG: 0x%02x\n",is_fw_download_log_enabled);
 
-    if(NxpConfig_GetNum(NAME_UWB_FW_DOWNLOAD_LOG, &num, sizeof(num))){
-        is_fw_download_log_enabled = (uint8_t)num;
-        ALOGD("NAME_UWB_FW_DOWNLOAD_LOG: 0x%02x\n",is_fw_download_log_enabled);
-    } else {
-        ALOGD("NAME_UWB_FW_DOWNLOAD_LOG: failed 0x%02x\n",is_fw_download_log_enabled);
-    }
     if (init())
     {
         ALOGD("INIT Failed.....\n");
diff --git a/halimpl/hal/phNxpUciHal.cc b/halimpl/hal/phNxpUciHal.cc
index 76a70d7..b170c25 100644
--- a/halimpl/hal/phNxpUciHal.cc
+++ b/halimpl/hal/phNxpUciHal.cc
@@ -17,10 +17,14 @@
 
 #include <array>
 #include <functional>
-#include <string.h>
 #include <list>
 #include <map>
+#include <memory>
 #include <mutex>
+#include <optional>
+#include <span>
+#include <string>
+#include <thread>
 #include <unordered_set>
 #include <vector>
 
@@ -645,31 +649,26 @@
  ******************************************************************************/
 static void parseAntennaConfig(const char *configName)
 {
-  std::array<uint8_t, NXP_MAX_CONFIG_STRING_LEN> buffer;
-  size_t retlen = 0;
-  int gotConfig = NxpConfig_GetByteArray(configName, buffer.data(), buffer.size(), &retlen);
-  if (gotConfig) {
-    if (retlen <= UCI_MSG_HDR_SIZE) {
-      NXPLOG_UCIHAL_E("parseAntennaConfig: %s is too short. Aborting.", configName);
-      return;
-    }
+  auto res = NxpConfig_GetByteArray(configName);
+  if (!res.has_value()) {
+    NXPLOG_UCIHAL_D("No antenna pair info found, %s is missing.", configName);
+    return;
   }
-  else
-  {
-    NXPLOG_UCIHAL_E("parseAntennaConfig: Failed to get '%s'. Aborting.", configName);
+  std::span<const uint8_t> data = *res;
+  if (data.size() <= UCI_MSG_HDR_SIZE) {
+    NXPLOG_UCIHAL_D("No antenna pair info found, %s is too short.", configName);
     return;
   }
 
-  const uint16_t dataLength = retlen;
-  const uint8_t *data = buffer.data();
+  int index = 1;  // Excluding number of params
+  while (index < data.size()) {
+    if ((index + 3) > data.size()) {
+      break;
+    }
+    uint8_t tagId = data[index++];
+    uint8_t subTagId = data[index++];
+    uint8_t length = data[index++];
 
-  uint8_t index = 1; // Excluding number of params
-  uint8_t tagId, subTagId;
-  int length;
-  while (index < dataLength) {
-    tagId = data[index++];
-    subTagId = data[index++];
-    length = data[index++];
     if ((ANTENNA_RX_PAIR_DEFINE_TAG_ID == tagId) &&
         (ANTENNA_RX_PAIR_DEFINE_SUB_TAG_ID == subTagId)) {
       nxpucihal_ctrl.numberOfAntennaPairs = data[index];
@@ -679,6 +678,7 @@
       index = index + length;
     }
   }
+  NXPLOG_UCIHAL_D("No antenna pair info found in from %s.", configName)
 }
 
 /******************************************************************************
@@ -692,9 +692,6 @@
 tHAL_UWB_STATUS phNxpUciHal_applyVendorConfig()
 {
   std::vector<const char *> vendorParamNames;
-  std::array<uint8_t, NXP_MAX_CONFIG_STRING_LEN> buffer;
-  size_t retlen = 0;
-  tHAL_UWB_STATUS status = UWBSTATUS_FAILED;
 
   // Base parameter names
   if (nxpucihal_ctrl.fw_boot_mode == USER_FW_BOOT_MODE) {
@@ -710,15 +707,15 @@
     per_chip_param = NAME_UWB_CORE_EXT_DEVICE_SR1XX_S_CONFIG;
   }
 
-  if (NxpConfig_GetByteArray(per_chip_param, buffer.data(), buffer.size(),
-                             &retlen)) {
-    if (retlen > 0 && retlen < UCI_MAX_DATA_LEN) {
-      NXPLOG_UCIHAL_D("VendorConfig: apply %s", per_chip_param);
-      status = phNxpUciHal_sendCoreConfig(buffer.data(), retlen);
-      if (status != UWBSTATUS_SUCCESS) {
-        NXPLOG_UCIHAL_E("VendorConfig: failed to apply %s", per_chip_param);
-        return status;
-      }
+  // TODO: split this into a function.
+  auto chip_pkt = NxpConfig_GetByteArray(per_chip_param);
+  if (chip_pkt.has_value()) {
+    NXPLOG_UCIHAL_D("VendorConfig: apply %s", per_chip_param);
+    tHAL_UWB_STATUS status =
+      phNxpUciHal_sendCoreConfig((*chip_pkt).data(), (*chip_pkt).size());
+    if (status != UWBSTATUS_SUCCESS) {
+      NXPLOG_UCIHAL_E("VendorConfig: failed to apply %s", per_chip_param);
+      return status;
     }
   }
 
@@ -740,29 +737,28 @@
 
   // Execute
   for (const auto paramName : vendorParamNames) {
-    if (NxpConfig_GetByteArray(paramName, buffer.data(), buffer.size(), &retlen)) {
-      if (retlen > 0 && retlen < UCI_MAX_DATA_LEN) {
-        NXPLOG_UCIHAL_D("VendorConfig: apply %s", paramName);
-        status = phNxpUciHal_send_ext_cmd(retlen, buffer.data());
-        if (status != UWBSTATUS_SUCCESS) {
-          NXPLOG_UCIHAL_E("VendorConfig: failed to apply %s", paramName);
-          return status;
-        }
+    auto extra_pkt = NxpConfig_GetByteArray(paramName);
+    if (extra_pkt.has_value()) {
+      NXPLOG_UCIHAL_D("VendorConfig: apply %s", paramName);
+      tHAL_UWB_STATUS status =
+        phNxpUciHal_send_ext_cmd((*extra_pkt).size(), (*extra_pkt).data());
+      if (status != UWBSTATUS_SUCCESS) {
+        NXPLOG_UCIHAL_E("VendorConfig: failed to apply %s", paramName);
+        return status;
       }
     }
   }
 
   // Low Power Mode
   // TODO: remove this out, this can be move to Chip parameter names
-  uint8_t lowPowerMode = 0;
-  if (NxpConfig_GetNum(NAME_NXP_UWB_LOW_POWER_MODE, &lowPowerMode, sizeof(lowPowerMode))) {
-    NXPLOG_UCIHAL_D("VendorConfig: apply %s", NAME_NXP_UWB_LOW_POWER_MODE);
+  bool lowPowerMode =  NxpConfig_GetBool(NAME_NXP_UWB_LOW_POWER_MODE).value_or(false);
+  NXPLOG_UCIHAL_D("VendorConfig: apply %s", NAME_NXP_UWB_LOW_POWER_MODE);
 
+  if (lowPowerMode) {
     // Core set config packet: GID=0x00 OID=0x04
     const std::vector<uint8_t> packet(
         {((UCI_MT_CMD << UCI_MT_SHIFT) | UCI_GID_CORE), UCI_MSG_CORE_SET_CONFIG,
-         0x00, 0x04, 0x01, LOW_POWER_MODE_TAG_ID, LOW_POWER_MODE_LENGTH,
-         lowPowerMode});
+         0x00, 0x04, 0x01, LOW_POWER_MODE_TAG_ID, LOW_POWER_MODE_LENGTH, 0x01 });
 
     if (phNxpUciHal_send_ext_cmd(packet.size(), packet.data()) != UWBSTATUS_SUCCESS) {
       NXPLOG_UCIHAL_E("VendorConfig: failed to apply NAME_NXP_UWB_LOW_POWER_MODE");
diff --git a/halimpl/hal/phNxpUciHal_ext.cc b/halimpl/hal/phNxpUciHal_ext.cc
index bcb4d8e..4564aca 100644
--- a/halimpl/hal/phNxpUciHal_ext.cc
+++ b/halimpl/hal/phNxpUciHal_ext.cc
@@ -13,13 +13,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <string.h>
 #include <sys/stat.h>
 
 #include <atomic>
 #include <bitset>
 #include <limits>
 #include <map>
+#include <optional>
+#include <span>
 #include <vector>
 
 #include <cutils/properties.h>
@@ -164,32 +165,18 @@
  *                  update the acutual state of operation in arg pointer
  *
  ******************************************************************************/
-tHAL_UWB_STATUS phNxpUciHal_set_board_config(){
-  tHAL_UWB_STATUS status;
-  uint8_t buffer[] = {0x2E,0x00,0x00,0x02,0x01,0x01};
-  /* Set the board variant configurations */
-  unsigned long num = 0;
-  NXPLOG_UCIHAL_D("%s: enter; ", __func__);
-  uint8_t boardConfig = 0, boardVersion = 0;
+tHAL_UWB_STATUS phNxpUciHal_set_board_config() {
+  uint8_t buffer[6] = {0x2E,0x00,0x00,0x02,0x01,0x01};
 
-  if(NxpConfig_GetNum(NAME_UWB_BOARD_VARIANT_CONFIG, &num, sizeof(num))){
-    boardConfig = (uint8_t)num;
-    NXPLOG_UCIHAL_D("%s: NAME_UWB_BOARD_VARIANT_CONFIG: %x", __func__,boardConfig);
-  } else {
-    NXPLOG_UCIHAL_D("%s: NAME_UWB_BOARD_VARIANT_CONFIG: failed %x", __func__,boardConfig);
-  }
-  if(NxpConfig_GetNum(NAME_UWB_BOARD_VARIANT_VERSION, &num, sizeof(num))){
-    boardVersion = (uint8_t)num;
-    NXPLOG_UCIHAL_D("%s: NAME_UWB_BOARD_VARIANT_VERSION: %x", __func__,boardVersion);
-  } else{
-    NXPLOG_UCIHAL_D("%s: NAME_UWB_BOARD_VARIANT_VERSION: failed %lx", __func__,num);
-  }
+  uint8_t boardConfig = NxpConfig_GetNum<uint8_t>(NAME_UWB_BOARD_VARIANT_CONFIG).value_or(0);
+  uint8_t boardVersion = NxpConfig_GetNum<uint8_t>(NAME_UWB_BOARD_VARIANT_VERSION).value_or(0);
+
+  NXPLOG_UCIHAL_D("Board variant config: config=0x%x, version=0x%x", boardConfig, boardVersion);
+
   buffer[4] = boardConfig;
   buffer[5] = boardVersion;
 
-  status = phNxpUciHal_send_ext_cmd(sizeof(buffer), buffer);
-
-  return status;
+  return phNxpUciHal_send_ext_cmd(sizeof(buffer), buffer);
 }
 
 /*******************************************************************************
@@ -485,52 +472,65 @@
 
 static void extcal_do_xtal(void)
 {
-  int ret;
-
   // RF_CLK_ACCURACY_CALIB (otp supported)
   // parameters: cal.otp.xtal=0|1, cal.xtal=X
-  uint8_t otp_xtal_flag = 0;
+  constexpr std::string_view kConfKeyOtp = "cal.otp.xtal";  // as-if, safe to use data() for c-string.
+  constexpr std::string_view kConfKeyXtal = "cal.xtal";
+
   uint8_t xtal_data[32];
   size_t xtal_data_len = 0;
+  bool xtal_otp_provided = false;
 
-  if (NxpConfig_GetNum("cal.otp.xtal", &otp_xtal_flag, 1) && otp_xtal_flag) {
-    nxpucihal_ctrl.uwb_chip->read_otp(EXTCAL_PARAM_CLK_ACCURACY, xtal_data, sizeof(xtal_data), &xtal_data_len);
+  // Reads xtal calibration from OTP.
+  bool use_otp_xtal = NxpConfig_GetBool(kConfKeyOtp).value_or(false);
+  if (use_otp_xtal) {
+    tHAL_UWB_STATUS status = nxpucihal_ctrl.uwb_chip->read_otp(
+      EXTCAL_PARAM_CLK_ACCURACY, xtal_data, sizeof(xtal_data), &xtal_data_len);
+    xtal_otp_provided = status == UWBSTATUS_SUCCESS && xtal_data_len > 0;
   }
-  if (!xtal_data_len) {
+
+  // Reads xtal calibration from configuration file.
+  if (!xtal_otp_provided) {
+    if (use_otp_xtal) {
+      NXPLOG_UCIHAL_W("%s is set but cannot read it from otp, fallback to config.", kConfKeyOtp.data());
+    }
     size_t retlen = 0;
-    if (NxpConfig_GetByteArray("cal.xtal", xtal_data, sizeof(xtal_data), &retlen)) {
-      xtal_data_len = retlen;
+    auto res = NxpConfig_GetByteArray(kConfKeyXtal);
+    if (!res.has_value()) {
+      NXPLOG_UCIHAL_E("Failed to get clock calibration data: %s is not provided.", kConfKeyXtal.data());
+      return;
     }
+    std::span<const uint8_t> data = *res;
+    if (data.empty() || data.size() > sizeof(xtal_data)) {
+      NXPLOG_UCIHAL_E("Failed to get clock calibration data: cannot parse %s", kConfKeyXtal.data());
+      return;
+    }
+    memcpy(xtal_data, data.data(), data.size());
+    xtal_data_len = data.size();
   }
 
-  if (xtal_data_len) {
-    NXPLOG_UCIHAL_D("Apply CLK_ACCURARY (len=%zu, from-otp=%c)", xtal_data_len, otp_xtal_flag ? 'y' : 'n');
+  NXPLOG_UCIHAL_D("Apply CLK_ACCURARY (len=%zu, from-otp=%c)", xtal_data_len, xtal_otp_provided ? 'y' : 'n');
 
-    ret = nxpucihal_ctrl.uwb_chip->apply_calibration(EXTCAL_PARAM_CLK_ACCURACY, 0, xtal_data, xtal_data_len);
-
-    if (ret != UWBSTATUS_SUCCESS) {
-      NXPLOG_UCIHAL_E("Failed to apply CLK_ACCURACY (len=%zu, from-otp=%c)",
-          xtal_data_len, otp_xtal_flag ? 'y' : 'n');
-    }
+  tHAL_UWB_STATUS ret =
+    nxpucihal_ctrl.uwb_chip->apply_calibration(
+      EXTCAL_PARAM_CLK_ACCURACY, 0, xtal_data, xtal_data_len);
+  if (ret != UWBSTATUS_SUCCESS) {
+    NXPLOG_UCIHAL_E("Failed to apply CLK_ACCURACY.");
   }
 }
 
 // Returns a pair of limit values <lower limit, upper limit>
 static std::pair<uint16_t, uint16_t> extcal_get_ant_delay_limits(uint8_t ant_id, uint8_t ch)
 {
-  uint16_t lower_limit = std::numeric_limits<uint16_t>::min();
-  uint16_t upper_limit = std::numeric_limits<uint16_t>::max();
+  constexpr uint16_t def_lower_limit = std::numeric_limits<uint16_t>::min();
+  constexpr uint16_t def_upper_limit = std::numeric_limits<uint16_t>::max();
 
   const std::string key_lower_limit = std::format("cal.ant{}.ch{}.ant_delay.lower_limit", ant_id, ch);
   const std::string key_upper_limit = std::format("cal.ant{}.ch{}.ant_delay.upper_limit", ant_id, ch);
 
-  uint16_t val = 0;
-  if (NxpConfig_GetNum(key_lower_limit.c_str(), &val, sizeof(val))) {
-    lower_limit = val;
-  }
-  if (NxpConfig_GetNum(key_upper_limit.c_str(), &val, sizeof(val))) {
-    upper_limit = val;
-  }
+  uint16_t lower_limit = NxpConfig_GetNum<uint16_t>(key_lower_limit).value_or(def_lower_limit);
+  uint16_t upper_limit = NxpConfig_GetNum<uint16_t>(key_upper_limit).value_or(def_upper_limit);
+
   return std::make_pair(lower_limit, upper_limit);
 }
 
@@ -544,36 +544,36 @@
 
     const uint8_t ant_id = i + 1;
 
-    uint16_t delay_value, version_value;
-    bool value_provided = false;
-
     const std::string key_ant_delay = std::format("cal.ant{}.ch{}.ant_delay", ant_id, ch);
-    const std::string key_force_version = key_ant_delay + std::format(".force_version", ant_id, ch);
+    const std::string key_force_version = key_ant_delay + ".force_version";
+
+    std::optional<uint16_t> delay_value;
 
     // 1) try cal.ant{N}.ch{N}.ant_delay.force_value.{N}
-    if (NxpConfig_GetNum(key_force_version.c_str(), &version_value, 2)) {
-      const std::string key_force_value = key_ant_delay + std::format(".force_value.{}", ant_id, ch, version_value);
-      if (NxpConfig_GetNum(key_force_value.c_str(), &delay_value, 2)) {
-        value_provided = true;
-        NXPLOG_UCIHAL_D("Apply RX_ANT_DELAY_CALIB %s = %u", key_force_value.c_str(), delay_value);
+    std::optional<uint16_t> force_version = NxpConfig_GetNum<uint16_t>(key_force_version);
+    if (force_version.has_value()) {
+      const std::string key_force_value = key_ant_delay + std::format(".force_value.{}", *force_version);
+      delay_value = NxpConfig_GetNum<uint16_t>(key_force_value);
+      if (delay_value.has_value()) {
+        NXPLOG_UCIHAL_D("Apply RX_ANT_DELAY_CALIB %s = %u", key_force_value.c_str(), *delay_value);
       }
     }
 
     // 2) try cal.ant{N}.ch{N}.ant_delay
-    if (!value_provided) {
-      if (NxpConfig_GetNum(key_ant_delay.c_str(), &delay_value, 2)) {
-        value_provided = true;
-        NXPLOG_UCIHAL_D("Apply RX_ANT_DELAY_CALIB: %s = %u", key_ant_delay.c_str(), delay_value);
+    if (!delay_value.has_value()) {
+      delay_value = NxpConfig_GetNum<uint16_t>(key_ant_delay);
+      if (delay_value.has_value()) {
+        NXPLOG_UCIHAL_D("Apply RX_ANT_DELAY_CALIB: %s = %u", key_ant_delay.c_str(), *delay_value);
       }
     }
 
-    if (!value_provided) {
+    if (!delay_value.has_value()) {
       NXPLOG_UCIHAL_V("%s was not provided from configuration files.", key_ant_delay.c_str());
       return;
     }
 
     // clamping
-    uint16_t clamped_delay = delay_value;
+    uint16_t clamped_delay = *delay_value;
     std::pair<uint16_t, uint16_t> limits = extcal_get_ant_delay_limits(ant_id, ch);
     if (clamped_delay < limits.first) { clamped_delay = limits.first; }
     if (clamped_delay > limits.second) { clamped_delay = limits.second; }
@@ -640,19 +640,17 @@
         if (!tx_antenna_mask[i])
           continue;
 
-        char key[32];
         const uint8_t ant_id = i + 1;
-        std::snprintf(key, sizeof(key), "cal.ant%u.ch%u.tx_power", ant_id, ch);
 
-        uint8_t power_value[32];
-        size_t retlen = 0;
-        if (!NxpConfig_GetByteArray(key, power_value, sizeof(power_value), &retlen)) {
+        std::string key = std::format("cal.ant{}.ch{}.tx_power", ant_id, ch);
+        std::optional<std::span<const uint8_t>> res = NxpConfig_GetByteArray(key);
+        if (!res.has_value()) {
           continue;
         }
-
-        NXPLOG_UCIHAL_D("Apply TX_POWER: %s = { %zu bytes }", key, retlen);
+        std::span<const uint8_t> pkt = *res;
+        NXPLOG_UCIHAL_D("Apply TX_POWER: %s = { %zu bytes }", key.c_str(), (*res).size());
         entries.push_back(ant_id);
-        entries.insert(entries.end(), power_value, power_value + retlen);
+        entries.insert(entries.end(), pkt.begin(), pkt.end());
         n_entries++;
       }
 
@@ -670,14 +668,12 @@
 
 static void extcal_do_tx_pulse_shape(void)
 {
-  // parameters: cal.tx_pulse_shape={...}
-  size_t retlen = 0;
-  uint8_t data[64];
+  std::optional<std::span<const uint8_t>> res = NxpConfig_GetByteArray("cal.tx_pulse_shape");
+  if (res.has_value()) {
+      NXPLOG_UCIHAL_D("Apply TX_PULSE_SHAPE: data = { %zu bytes }", (*res).size());
 
-  if (NxpConfig_GetByteArray("cal.tx_pulse_shape", data, sizeof(data), &retlen) && retlen) {
-      NXPLOG_UCIHAL_D("Apply TX_PULSE_SHAPE: data = { %zu bytes }", retlen);
-
-      tHAL_UWB_STATUS ret = nxpucihal_ctrl.uwb_chip->apply_calibration(EXTCAL_PARAM_TX_PULSE_SHAPE, 0, data, (size_t)retlen);
+      tHAL_UWB_STATUS ret = nxpucihal_ctrl.uwb_chip->apply_calibration(
+        EXTCAL_PARAM_TX_PULSE_SHAPE, /*ch=*/0, (*res).data(), (*res).size());
       if (ret != UWBSTATUS_SUCCESS) {
         NXPLOG_UCIHAL_E("Failed to apply TX_PULSE_SHAPE.");
       }
@@ -688,27 +684,20 @@
 {
   // TX_BASE_BAND_CONTROL, DDFS_TONE_CONFIG
   // parameters: cal.ddfs_enable=1|0, cal.dc_suppress=1|0, ddfs_tone_config={...}
-  uint8_t ddfs_enable = 0, dc_suppress = 0;
-  uint8_t ddfs_tone[256];
-  size_t retlen = 0;
-  tHAL_UWB_STATUS ret;
-
-  if (NxpConfig_GetNum("cal.ddfs_enable", &ddfs_enable, 1)) {
-    NXPLOG_UCIHAL_D("Apply TX_BASE_BAND_CONTROL: ddfs_enable=%u", ddfs_enable);
-  }
-  if (NxpConfig_GetNum("cal.dc_suppress", &dc_suppress, 1)) {
-    NXPLOG_UCIHAL_D("Apply TX_BASE_BAND_CONTROL: dc_suppress=%u", dc_suppress);
-  }
+  bool ddfs_enable = NxpConfig_GetBool("cal.ddfs_enable").value_or(false);
+  bool dc_suppress = NxpConfig_GetBool("cal.dc_suppress").value_or(false);
 
   // DDFS_TONE_CONFIG
   if (ddfs_enable) {
-    if (!NxpConfig_GetByteArray("cal.ddfs_tone_config", ddfs_tone, sizeof(ddfs_tone), &retlen) || !retlen) {
+    std::optional<std::span<const uint8_t>> ddfs_tone = NxpConfig_GetByteArray("cal.ddfs_tone_config");
+    if (!ddfs_tone.has_value()) {
       NXPLOG_UCIHAL_E("cal.ddfs_tone_config is not supplied while cal.ddfs_enable=1, ddfs was not enabled.");
       ddfs_enable = 0;
     } else {
-      NXPLOG_UCIHAL_D("Apply DDFS_TONE_CONFIG: ddfs_tone_config = { %zu bytes }", retlen);
+      NXPLOG_UCIHAL_D("Apply DDFS_TONE_CONFIG: ddfs_tone_config = { %zu bytes }", (*ddfs_tone).size());
 
-      ret = nxpucihal_ctrl.uwb_chip->apply_calibration(EXTCAL_PARAM_DDFS_TONE_CONFIG, 0, ddfs_tone, (size_t)retlen);
+      tHAL_UWB_STATUS ret = nxpucihal_ctrl.uwb_chip->apply_calibration(EXTCAL_PARAM_DDFS_TONE_CONFIG,
+        /*ch=*/0, (*ddfs_tone).data(), (*ddfs_tone).size());
       if (ret != UWBSTATUS_SUCCESS) {
         NXPLOG_UCIHAL_E("Failed to apply DDFS_TONE_CONFIG, ddfs was not enabled.");
         ddfs_enable = 0;
@@ -717,16 +706,13 @@
   }
 
   // TX_BASE_BAND_CONTROL
-  {
-    uint8_t flag = 0;
-    if (ddfs_enable)
-      flag |= 0x01;
-    if (dc_suppress)
-      flag |= 0x02;
-    ret = nxpucihal_ctrl.uwb_chip->apply_calibration(EXTCAL_PARAM_TX_BASE_BAND_CONTROL, 0, &flag, 1);
-    if (ret) {
-      NXPLOG_UCIHAL_E("Failed to apply TX_BASE_BAND_CONTROL");
-    }
+  uint8_t flag = 0;
+  if (ddfs_enable) { flag |= 0x01; }
+  if (dc_suppress) { flag |= 0x02; }
+  tHAL_UWB_STATUS ret = nxpucihal_ctrl.uwb_chip->apply_calibration(EXTCAL_PARAM_TX_BASE_BAND_CONTROL,
+    /*ch=*/0, &flag, 1);
+  if (ret) {
+    NXPLOG_UCIHAL_E("Failed to apply TX_BASE_BAND_CONTROL");
   }
 }
 
@@ -741,16 +727,20 @@
 void phNxpUciHal_extcal_handle_coreinit(void)
 {
   // read rx_aantenna_mask, tx_antenna_mask
-  uint8_t rx_antenna_mask_n = 0x1;
-  uint8_t tx_antenna_mask_n = 0x1;
-  if (!NxpConfig_GetNum("cal.rx_antenna_mask", &rx_antenna_mask_n, 1)) {
-      NXPLOG_UCIHAL_E("cal.rx_antenna_mask is not specified, use default 0x%x", rx_antenna_mask_n);
+  auto res = NxpConfig_GetNum<uint8_t>("cal.rx_antenna_mask");
+  if (!res.has_value()) {
+    NXPLOG_UCIHAL_W("cal.tx_antenna_mask is not specified, use default value.");
   }
-  if (!NxpConfig_GetNum("cal.tx_antenna_mask", &tx_antenna_mask_n, 1)) {
-      NXPLOG_UCIHAL_E("cal.tx_antenna_mask is not specified, use default 0x%x", tx_antenna_mask_n);
+  nxpucihal_ctrl.cal_rx_antenna_mask = res.value_or(0x01);
+
+  res = NxpConfig_GetNum<uint8_t>("cal.tx_antenna_mask");
+  if (!res.has_value()) {
+      NXPLOG_UCIHAL_W("cal.tx_antenna_mask is not specified, use default value.");
   }
-  nxpucihal_ctrl.cal_rx_antenna_mask = rx_antenna_mask_n;
-  nxpucihal_ctrl.cal_tx_antenna_mask = tx_antenna_mask_n;
+  nxpucihal_ctrl.cal_tx_antenna_mask = res.value_or(0x01);
+
+  NXPLOG_UCIHAL_D("tx_antenna_mask=0x%x, rx_antenna_mask=0x%x",
+    nxpucihal_ctrl.cal_tx_antenna_mask, nxpucihal_ctrl.cal_rx_antenna_mask);
 
   extcal_do_xtal();
   extcal_do_ant_delay();
@@ -799,24 +789,23 @@
     phNxpUciHal_resetRuntimeSettings();
 
     // Load ExtraCal restrictions
-    uint16_t mask= 0;
-    if (NxpConfig_GetNum("cal.restricted_channels", &mask, sizeof(mask))) {
+    uint16_t mask = NxpConfig_GetNum<uint16_t>("cal.restricted_channels").value_or(0);
+    if (mask != 0) {
       NXPLOG_UCIHAL_D("Restriction flag, restricted channel mask=0x%x", mask);
       rt_set->restricted_channel_mask = mask;
     }
 
-    uint8_t uwb_disable = 0;
-    if (NxpConfig_GetNum("cal.uwb_disable", &uwb_disable, sizeof(uwb_disable))) {
-      NXPLOG_UCIHAL_D("Restriction flag, uwb_disable=%u", uwb_disable);
-      rt_set->uwb_enable = !uwb_disable;
+    if (NxpConfig_GetBool("cal.uwb_disable").value_or(false)) {
+      NXPLOG_UCIHAL_D("Restriction flag, uwb_disable set");
+      rt_set->uwb_enable = 0;
     }
 
     // Apply COUNTRY_CODE_CAPS
-    uint8_t cc_caps[UCI_MAX_DATA_LEN];
-    size_t retlen = 0;
-    if (NxpConfig_GetByteArray(NAME_NXP_UWB_COUNTRY_CODE_CAPS, cc_caps, sizeof(cc_caps), &retlen) && retlen) {
+    std::optional<std::span<const uint8_t>> cc_caps =
+      NxpConfig_GetByteArray(NAME_NXP_UWB_COUNTRY_CODE_CAPS);
+    if (cc_caps.has_value()) {
       NXPLOG_UCIHAL_D("COUNTRY_CODE_CAPS is provided.");
-      phNxpUciHal_applyCountryCaps(country_code, cc_caps, retlen);
+      phNxpUciHal_applyCountryCaps(country_code, (*cc_caps).data(), (*cc_caps).size());
     }
 
     // Check country code validity
@@ -1008,15 +997,12 @@
   }
 
   // Append UWB_VENDOR_CAPABILITY from configuration files
-  {
-    std::array<uint8_t, NXP_MAX_CONFIG_STRING_LEN> buffer;
-    size_t retlen = 0;
-    if (NxpConfig_GetByteArray(NAME_UWB_VENDOR_CAPABILITY, buffer.data(),
-                               buffer.size(), &retlen) && retlen) {
-      auto vendorTlvs = decodeTlvBytes({}, buffer.data(), retlen);
-      for (auto const& [key, val] : vendorTlvs) {
-        tlvs[key] = val;
-      }
+  std::optional<std::span<const uint8_t>> vcaps =
+    NxpConfig_GetByteArray(NAME_UWB_VENDOR_CAPABILITY);
+  if (vcaps.has_value()) {
+    auto vendorTlvs = decodeTlvBytes(/*ext_ids=*/{}, (*vcaps).data(), (*vcaps).size());
+    for (auto const& [key, val] : vendorTlvs) {
+      tlvs[key] = val;
     }
   }
 
diff --git a/halimpl/hal/sessionTrack.cc b/halimpl/hal/sessionTrack.cc
index d580e26..6a7a96a 100644
--- a/halimpl/hal/sessionTrack.cc
+++ b/halimpl/hal/sessionTrack.cc
@@ -1,6 +1,8 @@
 #include <bit>
 #include <mutex>
 #include <random>
+#include <optional>
+#include <span>
 #include <thread>
 #include <unordered_map>
 #include <vector>
@@ -109,7 +111,7 @@
   bool calibration_delayed_;
   std::atomic<PowerState> power_state_;
   bool idle_timer_started_;
-  unsigned long idle_timeout_ms_;
+  uint32_t idle_timeout_ms_;
   bool override_sts_index_for_ccc_;
 
   std::thread worker_thread_;
@@ -119,34 +121,27 @@
 
 public:
   SessionTrack() :
-    auto_suspend_enabled_(false),
-    delete_ursk_ccc_enabled_(false),
-    calibration_delayed_(false),
-    power_state_(PowerState::IDLE),
-    idle_timer_started_(false),
-    idle_timeout_ms_(kAutoSuspendTimeoutDefaultMs_),
-    override_sts_index_for_ccc_(true)
-  {
+      calibration_delayed_(false),
+      power_state_(PowerState::IDLE),
+      idle_timer_started_(false)  {
     sessions_.clear();
 
     msgq_ = std::make_unique<MessageQueue<SessionTrackMsg>>("SessionTrack");
     worker_thread_ = std::thread(&SessionTrack::PowerManagerWorker, this);
 
-    unsigned long numval = 0;
-
-    if (NxpConfig_GetNum(NAME_DELETE_URSK_FOR_CCC_SESSION, &numval, sizeof(numval)) && numval) {
-      delete_ursk_ccc_enabled_ = true;
-    }
+    delete_ursk_ccc_enabled_ =
+      NxpConfig_GetBool(NAME_DELETE_URSK_FOR_CCC_SESSION).value_or(false);
 
     // Default on
-    if (NxpConfig_GetNum(NAME_OVERRIDE_STS_INDEX_FOR_CCC_SESSION, &numval, sizeof(numval)) && !numval) {
-      override_sts_index_for_ccc_ = false;
-    }
+    override_sts_index_for_ccc_ =
+      NxpConfig_GetBool(NAME_OVERRIDE_STS_INDEX_FOR_CCC_SESSION).value_or(true);
 
-    if (NxpConfig_GetNum(NAME_AUTO_SUSPEND_ENABLE, &numval, sizeof(numval)) && numval) {
-      auto_suspend_enabled_ = true;
+    auto_suspend_enabled_ =
+      NxpConfig_GetBool(NAME_AUTO_SUSPEND_ENABLE).value_or(false);
 
-      NxpConfig_GetNum(NAME_AUTO_SUSPEND_TIMEOUT_MS, &idle_timeout_ms_, sizeof(idle_timeout_ms_));
+    if (auto_suspend_enabled_) {
+      idle_timeout_ms_ = NxpConfig_GetNum<uint32_t>(
+        NAME_AUTO_SUSPEND_TIMEOUT_MS).value_or(kAutoSuspendTimeoutDefaultMs_);
 
       // Idle timer is only activated when AUTO_SUSPEND_ENABLED=1
       // device suspend won't be triggered when it's not activated.
@@ -567,7 +562,7 @@
     if (!auto_suspend_enabled_)
       return;
 
-    NXPLOG_UCIHAL_D("SessionTrack: refresh idle timer, %lums", idle_timeout_ms_);
+    NXPLOG_UCIHAL_D("SessionTrack: refresh idle timer, %ums", idle_timeout_ms_);
     if (idle_timer_started_) {
       if (phOsalUwb_Timer_Stop(idle_timer_) != UWBSTATUS_SUCCESS) {
         NXPLOG_UCIHAL_E("SessionTrack: idle timer stop failed");
diff --git a/halimpl/log/phNxpLog.cc b/halimpl/log/phNxpLog.cc
index 7fc5091..6bd1619 100644
--- a/halimpl/log/phNxpLog.cc
+++ b/halimpl/log/phNxpLog.cc
@@ -15,13 +15,16 @@
  */
 
 #define LOG_TAG "NxpUwbHal"
-#include "phNxpLog.h"
-#include "phNxpConfig.h"
+
+#include <string>
+
 #include <cutils/properties.h>
 #include <log/log.h>
-#include <stdio.h>
-#include <string.h>
 
+#include "phNxpLog.h"
+#include "phNxpConfig.h"
+
+// TODO: use constexpr and move to header
 const char* NXPLOG_ITEM_EXTNS = "NxpExtns";
 const char* NXPLOG_ITEM_UCIHAL = "NxpUwbHal";
 const char* NXPLOG_ITEM_UCIX = "NxpUciX";
@@ -29,31 +32,17 @@
 const char* NXPLOG_ITEM_FWDNLD = "NxpFwDnld";
 const char* NXPLOG_ITEM_TML = "NxpUwbTml";
 
-#ifdef NXP_HCI_REQ
-const char* NXPLOG_ITEM_HCPX = "NxpHcpX";
-const char* NXPLOG_ITEM_HCPR = "NxpHcpR";
-#endif /*NXP_HCI_REQ*/
-
 /* global log level structure */
 uci_log_level_t gLog_level;
 
-/*******************************************************************************
- *
- * Function         phNxpLog_SetGlobalLogLevel
- *
- * Description      Sets the global log level for all modules.
- *                  This value is set by Android property
- *uwb.nxp_log_level_global.
- *                  If value can be overridden by module log level.
- *
- * Returns          The value of global log level
- *
- ******************************************************************************/
-static uint8_t phNxpLog_SetGlobalLogLevel(void) {
+namespace {
+
+uint8_t phNxpLog_SetGlobalLogLevel(void) {
   uint8_t level = NXPLOG_DEFAULT_LOGLEVEL;
   unsigned long num = 0;
   char valueStr[PROPERTY_VALUE_MAX] = {0};
 
+  // TODO: use property_get_int32()
   int len = property_get(PROP_NAME_NXPLOG_GLOBAL_LOGLEVEL, valueStr, "");
   if (len > 0) {
     // let Android property override .conf variable
@@ -64,148 +53,65 @@
   return level;
 }
 
-/*******************************************************************************
- *
- * Function         phNxpLog_SetHALLogLevel
- *
- * Description      Sets the HAL layer log level.
- *
- * Returns          void
- *
- ******************************************************************************/
-static void phNxpLog_SetHALLogLevel(uint8_t level) {
-  unsigned long num = 0;
-  int len;
-  char valueStr[PROPERTY_VALUE_MAX] = {0};
-
-  if (NxpConfig_GetNum(NAME_NXPLOG_HAL_LOGLEVEL, &num, sizeof(num))) {
-    gLog_level.hal_log_level =
-        (level > (unsigned char)num) ? level : (unsigned char)num;
-    ;
+// TODO: add helper function for reading property + configuration
+void phNxpLog_SetHALLogLevel(uint8_t level) {
+  int32_t prop_level = property_get_int32(PROP_NAME_NXPLOG_HAL_LOGLEVEL, 0);
+  if (prop_level > 0) {
+    gLog_level.hal_log_level = prop_level;
+    return;
   }
-
-  len = property_get(PROP_NAME_NXPLOG_HAL_LOGLEVEL, valueStr, "");
-  if (len > 0) {
-    /* let Android property override .conf variable */
-    sscanf(valueStr, "%lu", &num);
-
-    gLog_level.hal_log_level = (unsigned char)num;
-  }
+  uint8_t conf_level = NxpConfig_GetNum<uint8_t>(NAME_NXPLOG_HAL_LOGLEVEL).value_or(0);
+  gLog_level.hal_log_level = std::max(level, conf_level);
 }
 
-/*******************************************************************************
- *
- * Function         phNxpLog_SetExtnsLogLevel
- *
- * Description      Sets the Extensions layer log level.
- *
- * Returns          void
- *
- ******************************************************************************/
-static void phNxpLog_SetExtnsLogLevel(uint8_t level) {
-  unsigned long num = 0;
-  int len;
-  char valueStr[PROPERTY_VALUE_MAX] = {0};
-  if (NxpConfig_GetNum(NAME_NXPLOG_EXTNS_LOGLEVEL, &num, sizeof(num))) {
-    gLog_level.extns_log_level =
-        (level > (unsigned char)num) ? level : (unsigned char)num;
-    ;
+void phNxpLog_SetExtnsLogLevel(uint8_t level) {
+  int32_t prop_level = property_get_int32(PROP_NAME_NXPLOG_EXTNS_LOGLEVEL, 0);
+  if (prop_level > 0) {
+    gLog_level.extns_log_level = prop_level;
+    return;
   }
-
-  len = property_get(PROP_NAME_NXPLOG_EXTNS_LOGLEVEL, valueStr, "");
-  if (len > 0) {
-    /* let Android property override .conf variable */
-    sscanf(valueStr, "%lu", &num);
-    gLog_level.extns_log_level = (unsigned char)num;
-  }
+  uint8_t conf_level = NxpConfig_GetNum<uint8_t>(NAME_NXPLOG_EXTNS_LOGLEVEL).value_or(0);
+  gLog_level.extns_log_level = std::max(level, conf_level);
 }
 
-/*******************************************************************************
- *
- * Function         phNxpLog_SetTmlLogLevel
- *
- * Description      Sets the Tml layer log level.
- *
- * Returns          void
- *
- ******************************************************************************/
-static void phNxpLog_SetTmlLogLevel(uint8_t level) {
-  unsigned long num = 0;
-  int len;
-  char valueStr[PROPERTY_VALUE_MAX] = {0};
-  if (NxpConfig_GetNum(NAME_NXPLOG_TML_LOGLEVEL, &num, sizeof(num))) {
-    gLog_level.tml_log_level =
-        (level > (unsigned char)num) ? level : (unsigned char)num;
-    ;
+void phNxpLog_SetTmlLogLevel(uint8_t level) {
+  int32_t prop_level = property_get_int32(PROP_NAME_NXPLOG_TML_LOGLEVEL, 0);
+  if (prop_level > 0) {
+    gLog_level.tml_log_level = prop_level;
+    return;
   }
 
-  len = property_get(PROP_NAME_NXPLOG_TML_LOGLEVEL, valueStr, "");
-  if (len > 0) {
-    /* let Android property override .conf variable */
-    sscanf(valueStr, "%lu", &num);
-    gLog_level.tml_log_level = (unsigned char)num;
-  }
+  uint8_t conf_level = NxpConfig_GetNum<uint8_t>(NAME_NXPLOG_TML_LOGLEVEL).value_or(0);
+  gLog_level.tml_log_level = std::max(level, conf_level);
 }
 
-/*******************************************************************************
- *
- * Function         phNxpLog_SetDnldLogLevel
- *
- * Description      Sets the FW download layer log level.
- *
- * Returns          void
- *
- ******************************************************************************/
-static void phNxpLog_SetDnldLogLevel(uint8_t level) {
-  unsigned long num = 0;
-  int len;
-  char valueStr[PROPERTY_VALUE_MAX] = {0};
-  if (NxpConfig_GetNum(NAME_NXPLOG_FWDNLD_LOGLEVEL, &num, sizeof(num))) {
-    gLog_level.dnld_log_level =
-        (level > (unsigned char)num) ? level : (unsigned char)num;
-    ;
+void phNxpLog_SetDnldLogLevel(uint8_t level) {
+  int32_t prop_level = property_get_int32(PROP_NAME_NXPLOG_FWDNLD_LOGLEVEL, 0);
+  if (prop_level > 0) {
+    gLog_level.dnld_log_level = prop_level;
+    return;
   }
 
-  len = property_get(PROP_NAME_NXPLOG_FWDNLD_LOGLEVEL, valueStr, "");
-  if (len > 0) {
-    /* let Android property override .conf variable */
-    sscanf(valueStr, "%lu", &num);
-    gLog_level.dnld_log_level = (unsigned char)num;
-  }
+  uint8_t conf_level = NxpConfig_GetNum<uint8_t>(NAME_NXPLOG_FWDNLD_LOGLEVEL).value_or(0);
+  gLog_level.dnld_log_level = std::max(level, conf_level);
 }
 
-/*******************************************************************************
- *
- * Function         phNxpLog_SetUciTxLogLevel
- *
- * Description      Sets the UCI transaction layer log level.
- *
- * Returns          void
- *
- ******************************************************************************/
-static void phNxpLog_SetUciTxLogLevel(uint8_t level) {
-  unsigned long num = 0;
-  int len;
-  char valueStr[PROPERTY_VALUE_MAX] = {0};
-  if (NxpConfig_GetNum(NAME_NXPLOG_UCIX_LOGLEVEL, &num, sizeof(num))) {
-    gLog_level.ucix_log_level =
-        (level > (unsigned char)num) ? level : (unsigned char)num;
-  }
-  if (NxpConfig_GetNum(NAME_NXPLOG_UCIR_LOGLEVEL, &num, sizeof(num))) {
-    gLog_level.ucir_log_level =
-        (level > (unsigned char)num) ? level : (unsigned char)num;
-    ;
+void phNxpLog_SetUciTxLogLevel(uint8_t level) {
+  int32_t prop_level = property_get_int32(PROP_NAME_NXPLOG_UCI_LOGLEVEL, 0);
+  if (prop_level > 0) {
+    gLog_level.ucix_log_level = prop_level;
+    gLog_level.ucir_log_level = prop_level;
+    return;
   }
 
-  len = property_get(PROP_NAME_NXPLOG_UCI_LOGLEVEL, valueStr, "");
-  if (len > 0) {
-    /* let Android property override .conf variable */
-    sscanf(valueStr, "%lu", &num);
-    gLog_level.ucix_log_level = (unsigned char)num;
-    gLog_level.ucir_log_level = (unsigned char)num;
-  }
+  uint8_t conf_level_x = NxpConfig_GetNum<uint8_t>(NAME_NXPLOG_UCIX_LOGLEVEL).value_or(0);
+  uint8_t conf_level_r = NxpConfig_GetNum<uint8_t>(NAME_NXPLOG_UCIR_LOGLEVEL).value_or(0);
+  gLog_level.ucix_log_level = std::max(level, conf_level_x);
+  gLog_level.ucir_log_level = std::max(level, conf_level_r);
 }
 
+}   // namespace
+
 /******************************************************************************
  * Function         phNxpLog_InitializeLogLevel
  *
diff --git a/halimpl/utils/phNxpConfig.cc b/halimpl/utils/phNxpConfig.cc
index 86774e9..c03c9f9 100644
--- a/halimpl/utils/phNxpConfig.cc
+++ b/halimpl/utils/phNxpConfig.cc
@@ -28,9 +28,12 @@
 #include <iomanip>
 #include <list>
 #include <memory>
+#include <optional>
+#include <span>
 #include <sstream>
 #include <sstream>
 #include <string>
+#include <string_view>
 #include <unordered_map>
 #include <unordered_set>
 #include <vector>
@@ -47,6 +50,7 @@
 
 namespace {
 
+// TODO: use constexpr
 static const char default_nxp_config_path[] = "/vendor/etc/libuwb-nxp.conf";
 static const char country_code_config_name[] = "libuwb-countrycode.conf";
 static const char nxp_uci_config_file[] = "libuwb-uci.conf";
@@ -67,6 +71,7 @@
 static const char prop_name_revision[] = "persist.vendor.uwb.cal.revision";
 static const char prop_default_revision[] = "defaultrevision";
 
+// TODO: remove this out
 using namespace::std;
 
 class uwbParam
@@ -108,6 +113,8 @@
 class CUwbNxpConfig
 {
 public:
+    using HashType = unordered_map<std::string, uwbParam>;
+
     CUwbNxpConfig();
     CUwbNxpConfig(const char *filepath);
 
@@ -127,7 +134,7 @@
         mValidFile = false;
     }
 
-    const uwbParam*    find(const char* p_name) const;
+    const uwbParam* find(std::string_view key) const;
     void    setCountry(const string& strCountry);
     const char* getFilePath() const {
         return mFilePath.c_str();
@@ -135,7 +142,7 @@
 
     void dump() const;
 
-    const unordered_map<string, uwbParam>& get_data() const {
+    const HashType& get_data() const {
         return m_map;
     }
 
@@ -146,7 +153,7 @@
     std::filesystem::path mFilePath;
     bool mFactoryFile;
 
-    unordered_map<string, uwbParam> m_map;
+    HashType m_map;
 };
 
 /*******************************************************************************
@@ -462,21 +469,12 @@
     return *this;
 }
 
-/*******************************************************************************
-**
-** Function:    CUwbNxpConfig::find()
-**
-** Description: search if a setting exist in the setting array
-**
-** Returns:     pointer to the setting object
-**
-*******************************************************************************/
-const uwbParam* CUwbNxpConfig::find(const char* p_name) const
+const uwbParam* CUwbNxpConfig::find(std::string_view key) const
 {
-    const auto it = m_map.find(p_name);
-
+    // TODO: how can we use the same hash function for string and string_view?
+    const auto it = m_map.find(std::string(key));
     if (it == m_map.cend()) {
-        return NULL;
+        return nullptr;
     }
     return &it->second;
 }
@@ -656,10 +654,7 @@
     void deinit();
     bool setCountryCode(const char country_code[2]);
 
-    const uwbParam* find(const char *name)  const;
-    bool    getValue(const char* name, char* pValue, size_t len) const;
-    bool    getValue(const char* name, unsigned long& rValue) const;
-    bool    getValue(const char* name, uint8_t* pValue, size_t len, size_t* readlen) const;
+    const uwbParam* find(std::string_view key)  const;
 private:
     // default_nxp_config_path
     CUwbNxpConfig mMainConfig;
@@ -798,39 +793,34 @@
     evaluateExtraConfPaths();
 
     // re-evaluate with "<extid>"
-    char extid_value[PROPERTY_VALUE_MAX];
-    if (!NxpConfig_GetStr(extid_config_name, extid_value, sizeof(extid_value))) {
-        strcpy(extid_value, extid_default_value);
-    }
-    mExtraConfSpecifiers.mCurExtid = extid_value;
+    mExtraConfSpecifiers.mCurExtid =
+        NxpConfig_GetStr(extid_config_name).value_or(extid_default_value);
     evaluateExtraConfPaths();
 
-    ALOGI("Provided specifiers: sku=[%s] revision=[%s] extid=[%s]", sku_value, revision_value, extid_value);
+    ALOGI("Provided specifiers: sku=[%s] revision=[%s] extid=[%s]", sku_value, revision_value,
+        mExtraConfSpecifiers.mCurExtid.c_str());
 
     // Pick one libuwb-countrycode.conf with the highest VERSION number
     // from multiple directories specified by COUNTRY_CODE_CAP_FILE_LOCATION
-    size_t arrLen = 0;
-    if (NxpConfig_GetStrArrayLen(NAME_COUNTRY_CODE_CAP_FILE_LOCATION, &arrLen) && arrLen > 0) {
-        constexpr size_t loc_max_len = 260;
-        auto loc = make_unique<char[]>(loc_max_len);
-        int version, max_version = -1;
+    std::optional<size_t> arrLen = NxpConfig_GetStrArrayLen(NAME_COUNTRY_CODE_CAP_FILE_LOCATION);
+    if (arrLen.has_value() && *arrLen > 0) {
+        int max_version = -1;
         string strPickedPath;
         bool foundCapFile = false;
         CUwbNxpConfig pickedConfig;
 
-        for (int i = 0; i < arrLen; i++) {
-            if (!NxpConfig_GetStrArrayVal(NAME_COUNTRY_CODE_CAP_FILE_LOCATION, i, loc.get(), loc_max_len)) {
-                continue;
-            }
-            string strPath(loc.get());
-            strPath += country_code_config_name;
+        for (auto i = 0; i < *arrLen; i++) {
+            std::optional<std::string_view> loc = NxpConfig_GetStrArrayVal(NAME_COUNTRY_CODE_CAP_FILE_LOCATION, i);
+            if (!loc.has_value()) { continue;  }
 
+            string strPath(*loc);
+            strPath += country_code_config_name;
             ALOGV("Try to load %s", strPath.c_str());
 
             CUwbNxpConfig config(strPath.c_str());
 
             const uwbParam *param = config.find(NAME_NXP_COUNTRY_CODE_VERSION);
-            version = param ? atoi(param->str_value()) : -2;
+            int version = param ? atoi(param->str_value()) : -2;
             if (version > max_version) {
                 foundCapFile = true;
                 pickedConfig = move(config);
@@ -885,71 +875,29 @@
     return evaluateExtraConfPaths();
 }
 
-const uwbParam* CascadeConfig::find(const char *name) const
+const uwbParam* CascadeConfig::find(std::string_view key) const
 {
     const uwbParam* param = NULL;
 
-    param = mCapsConfig.find(name);
+    param = mCapsConfig.find(key);
     if (param)
       return param;
 
     for (auto it = mExtraConfig.rbegin(); it != mExtraConfig.rend(); it++) {
         auto &config = it->second;
-        param = config.find(name);
+        param = config.find(key);
         if (param)
             break;
     }
     if (!param) {
-        param = mMainConfig.find(name);
+        param = mMainConfig.find(key);
     }
     if (!param) {
-        param = mUciConfig.find(name);
+        param = mUciConfig.find(key);
     }
     return param;
 }
 
-// TODO: move these getValue() helpers out of the class
-bool CascadeConfig::getValue(const char* name, char* pValue, size_t len) const
-{
-    const uwbParam *param = find(name);
-    if (!param)
-        return false;
-    if (param->getType() != uwbParam::type::STRING)
-        return false;
-    if (len < (param->str_len() + 1))
-        return false;
-
-    strncpy(pValue, param->str_value(), len);
-    return true;
-}
-
-bool CascadeConfig::getValue(const char* name, uint8_t* pValue, size_t len, size_t* readlen) const
-{
-    const uwbParam *param = find(name);
-    if (!param)
-        return false;
-    if (param->getType() != uwbParam::type::BYTEARRAY)
-        return false;
-    if (len < param->arr_len())
-        return false;
-    memcpy(pValue, param->arr_value(), param->arr_len());
-    if (readlen)
-        *readlen = param->arr_len();
-    return true;
-}
-
-bool CascadeConfig::getValue(const char* name, unsigned long& rValue) const
-{
-    const uwbParam *param = find(name);
-    if (!param)
-        return false;
-    if (param->getType() != uwbParam::type::NUMBER)
-        return false;
-
-    rValue = param->numValue();
-    return true;
-}
-
 }   // namespace
 
 static CascadeConfig gConfig;
@@ -971,104 +919,60 @@
     return gConfig.setCountryCode(country_code);
 }
 
-/*******************************************************************************
-**
-** Function:    NxpConfig_GetStr
-**
-** Description: API function for getting a string value of a setting
-**
-** Returns:     True if found, otherwise False.
-**
-*******************************************************************************/
-bool NxpConfig_GetStr(const char* name, char* pValue, size_t len)
+std::optional<std::string_view> NxpConfig_GetStr(std::string_view key)
 {
-    return gConfig.getValue(name, pValue, len);
-}
-
-/*******************************************************************************
-**
-** Function:    NxpConfig_GetByteArray()
-**
-** Description: Read byte array value from the config file.
-**
-** Parameters:
-**              name    - name of the config param to read.
-**              pValue  - pointer to input buffer.
-**              bufflen - input buffer length.
-**              len     - out parameter to return the number of bytes read from config file,
-**                        return -1 in case bufflen is not enough.
-**
-** Returns:     TRUE[1] if config param name is found in the config file, else FALSE[0]
-**
-*******************************************************************************/
-bool NxpConfig_GetByteArray(const char* name, uint8_t* pValue, size_t bufflen, size_t* len)
-{
-    return gConfig.getValue(name, pValue, bufflen, len);
-}
-
-/*******************************************************************************
-**
-** Function:    NxpConfig_GetNum
-**
-** Description: API function for getting a numerical value of a setting
-**
-** Returns:     true, if successful
-**
-*******************************************************************************/
-bool NxpConfig_GetNum(const char* name, void* pValue, size_t len)
-{
-    if ((name == nullptr) || (pValue == nullptr)){
-        ALOGE("[%s] Invalid arguments", __func__);
-        return false;
+    const uwbParam *param = gConfig.find(key);
+    if (param == nullptr || param->getType() != uwbParam::type::STRING) {
+        return std::nullopt;
     }
-    const uwbParam* pParam = gConfig.find(name);
+    return param->str_value();
+}
+
+std::optional<std::span<const uint8_t>> NxpConfig_GetByteArray(std::string_view key)
+{
+    const uwbParam *param = gConfig.find(key);
+    if (param == nullptr || param->getType() != uwbParam::type::BYTEARRAY) {
+        return std::nullopt;
+    }
+    return std::span{param->arr_value(), param->arr_len()};
+}
+
+std::optional<uint64_t> NxpConfig_GetUint64(std::string_view key)
+{
+    const uwbParam* pParam = gConfig.find(key);
 
     if ((pParam == nullptr) || (pParam->getType() != uwbParam::type::NUMBER)) {
-        ALOGE("Config:%s not found in the config file", name);
-        return false;
+        return std::nullopt;
     }
-
-    unsigned long v = pParam->numValue();
-    switch (len)
-    {
-    case sizeof(unsigned long):
-        *(static_cast<unsigned long*>(pValue)) = (unsigned long)v;
-        break;
-    case sizeof(unsigned short):
-        *(static_cast<unsigned short*>(pValue)) = (unsigned short)v;
-        break;
-    case sizeof(unsigned char):
-        *(static_cast<unsigned char*> (pValue)) = (unsigned char)v;
-        break;
-    default:
-        ALOGE("[%s] unsupported length:%zu", __func__, len);
-        return false;
-    }
-    return true;
+    return pParam->numValue();
 }
 
-// Get the length of a 'string-array' type parameter
-bool NxpConfig_GetStrArrayLen(const char* name, size_t* pLen)
+std::optional<bool> NxpConfig_GetBool(std::string_view key)
 {
-    const uwbParam* param = gConfig.find(name);
-    if (!param || param->getType() != uwbParam::type::STRINGARRAY)
-        return false;
-
-    *pLen = param->str_arr_len();
-    return true;
+    const uwbParam* pParam = gConfig.find(key);
+    if (pParam == nullptr || pParam->getType() != uwbParam::type::NUMBER) {
+        return std::nullopt;
+    }
+    return pParam->numValue();
 }
 
-// Get a string value from 'string-array' type parameters, index zero-based
-bool NxpConfig_GetStrArrayVal(const char* name, int index, char* pValue, size_t len)
+std::optional<size_t> NxpConfig_GetStrArrayLen(std::string_view key)
 {
-    const uwbParam* param = gConfig.find(name);
-    if (!param || param->getType() != uwbParam::type::STRINGARRAY)
-        return false;
-    if (index < 0 || index >= param->str_arr_len())
-        return false;
+    const uwbParam* param = gConfig.find(key);
+    if (param == nullptr || param->getType() != uwbParam::type::STRINGARRAY) {
+        return std::nullopt;
+    }
+    return param->str_arr_len();
+}
 
-    if (len < param->str_arr_elem_len(index) + 1)
-        return false;
-    strncpy(pValue, param->str_arr_elem(index), len);
-    return true;
+std::optional<std::string_view> NxpConfig_GetStrArrayVal(std::string_view key, int idx)
+{
+    const uwbParam* param = gConfig.find(key);
+    if (param == nullptr || param->getType() != uwbParam::type::STRINGARRAY) {
+        return std::nullopt;
+    }
+    if (idx < 0 || idx >= param->str_arr_len()) {
+        return std::nullopt;
+    }
+    return param->str_arr_elem(idx);
 }
diff --git a/halimpl/utils/phNxpConfig.h b/halimpl/utils/phNxpConfig.h
index 030fc03..c276736 100644
--- a/halimpl/utils/phNxpConfig.h
+++ b/halimpl/utils/phNxpConfig.h
@@ -22,22 +22,41 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <limits>
+#include <optional>
+#include <span>
+#include <string_view>
+
+#include "phNxpLog.h"
 
 void NxpConfig_Init(void);
 void NxpConfig_Deinit(void);
 bool NxpConfig_SetCountryCode(const char country_code[2]);
 
-// TODO: use std::optional as return type.
-// TODO: use std::string_view instead of const char*.
-// TODO: add GetBool().
-// TODO: use template for GetNum() (uint8_t, uint16_t, uint32_t).
-bool NxpConfig_GetStr(const char* name, char* p_value, size_t len);
-bool NxpConfig_GetNum(const char* name, void* p_value, size_t len);
-bool NxpConfig_GetByteArray(const char* name, uint8_t* pValue, size_t bufflen, size_t *len);
+std::optional<std::string_view> NxpConfig_GetStr(std::string_view key);
 
-bool NxpConfig_GetStrArrayLen(const char* name, size_t* pLen);
-bool NxpConfig_GetStrArrayVal(const char* name, int index, char* pValue, size_t len);
+std::optional<std::span<const uint8_t>> NxpConfig_GetByteArray(std::string_view key);
 
+std::optional<uint64_t> NxpConfig_GetUint64(std::string_view key);
+
+template <typename T>
+inline std::optional<T> NxpConfig_GetNum(std::string_view key) {
+    static_assert(std::is_integral<T>::value);
+    auto res = NxpConfig_GetUint64(key);
+    if (res.has_value() && *res > std::numeric_limits<T>::max()) {
+        std::string strkey(key);
+        NXPLOG_UCIHAL_W("Config %s overflow", strkey.c_str());
+    }
+    return res;
+}
+
+// Returns true or false if key is existed as a number type parameter.
+std::optional<bool> NxpConfig_GetBool(std::string_view key);
+
+std::optional<size_t> NxpConfig_GetStrArrayLen(std::string_view key);
+std::optional<std::string_view> NxpConfig_GetStrArrayVal(std::string_view key, int idx);
+
+// TODO: use constexpr
 /* libuwb-nxp.conf parameters */
 #define NAME_UWB_BOARD_VARIANT_CONFIG "UWB_BOARD_VARIANT_CONFIG"
 #define NAME_UWB_BOARD_VARIANT_VERSION "UWB_BOARD_VARIANT_VERSION"