Refactor btsnoop and stack config into modules

Moves stack config out of the combined bte_config, and into
its own module.

Makes btsnoop more self sufficient and removes uneccessary
levels of indirection.

Refactor logging slightly into a (temporary) module to disassociate
from the direct calls from config. Eliminates some useless stuff in
the module as well.
diff --git a/btif/src/bluetooth.c b/btif/src/bluetooth.c
index 1b693b7..f248e08 100644
--- a/btif/src/bluetooth.c
+++ b/btif/src/bluetooth.c
@@ -44,6 +44,7 @@
 #define LOG_TAG "bluedroid"
 
 #include "btif_api.h"
+#include "btsnoop.h"
 #include "bt_utils.h"
 #include "osi.h"
 #include "stack_manager.h"
@@ -385,11 +386,11 @@
 {
     ALOGI("config_hci_snoop_log");
 
-    /* sanity check */
-    if (interface_ready() == FALSE)
+    if (!interface_ready())
         return BT_STATUS_NOT_READY;
 
-    return btif_config_hci_snoop_log(enable);
+    btsnoop_get_interface()->set_api_wants_to_log(enable);
+    return BT_STATUS_SUCCESS;
 }
 
 static int set_os_callouts(bt_os_callouts_t *callouts) {
diff --git a/btif/src/btif_core.c b/btif/src/btif_core.c
index afb21ec..93f24a0 100644
--- a/btif/src/btif_core.c
+++ b/btif/src/btif_core.c
@@ -1331,18 +1331,3 @@
   future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
 }
 
-
-/*******************************************************************************
-**
-** Function         btif_config_hci_snoop_log
-**
-** Description      enable or disable HCI snoop log
-**
-** Returns          bt_status_t
-**
-*******************************************************************************/
-bt_status_t btif_config_hci_snoop_log(uint8_t enable)
-{
-    bte_main_config_hci_logging(enable != 0, !stack_manager_get_interface()->get_stack_is_running());
-    return BT_STATUS_SUCCESS;
-}
diff --git a/hci/Android.mk b/hci/Android.mk
index 190abcc..048652c 100644
--- a/hci/Android.mk
+++ b/hci/Android.mk
@@ -29,6 +29,7 @@
 LOCAL_C_INCLUDES += \
     $(LOCAL_PATH)/include \
     $(LOCAL_PATH)/../include \
+    $(LOCAL_PATH)/../btcore/include \
     $(LOCAL_PATH)/../gki/common \
     $(LOCAL_PATH)/../gki/ulinux \
     $(LOCAL_PATH)/../osi/include \
@@ -48,6 +49,7 @@
 LOCAL_C_INCLUDES := \
     $(LOCAL_PATH)/include \
     $(LOCAL_PATH)/../include \
+    $(LOCAL_PATH)/../btcore/include \
     $(LOCAL_PATH)/../gki/common \
     $(LOCAL_PATH)/../gki/ulinux \
     $(LOCAL_PATH)/../osi/include \
diff --git a/hci/include/btsnoop.h b/hci/include/btsnoop.h
index 05ff791..49df9ee 100644
--- a/hci/include/btsnoop.h
+++ b/hci/include/btsnoop.h
@@ -23,12 +23,10 @@
 #include "bt_types.h"
 
 typedef struct btsnoop_t {
-  // Sets the logging path for btsnoop to the provided |path|.
-  void (*set_logging_path)(const char *path);
-
-  // Turns btsnoop logging on or off, depending on |value|. If
-  // you are turning btsnoop on, you must have set a logging path.
-  void (*set_is_running)(bool value);
+  // Inform btsnoop whether the API desires to log. If |value| is true.
+  // logging will be enabled. Otherwise it defers to the value from the
+  // config file.
+  void (*set_api_wants_to_log)(bool value);
 
   // Capture |packet| and dump it to the btsnoop logs. If |is_received| is
   // true, the packet is marked as incoming. Otherwise, the packet is marked
@@ -36,4 +34,5 @@
   void (*capture)(const BT_HDR *packet, bool is_received);
 } btsnoop_t;
 
+#define BTSNOOP_MODULE "btsnoop_module"
 const btsnoop_t *btsnoop_get_interface();
diff --git a/hci/src/btsnoop.c b/hci/src/btsnoop.c
index c16e26c..703c522 100644
--- a/hci/src/btsnoop.c
+++ b/hci/src/btsnoop.c
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  Copyright (C) 2009-2012 Broadcom Corporation
+ *  Copyright (C) 2014 Google, Inc.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -18,24 +18,21 @@
 
 #define LOG_TAG "btsnoop"
 
-#include <arpa/inet.h>
 #include <assert.h>
-#include <ctype.h>
 #include <cutils/log.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <unistd.h>
 
 #include "btsnoop.h"
 #include "bt_types.h"
-#include "bt_utils.h"
 #include "hci_layer.h"
+#include "stack_config.h"
 
 typedef enum {
   kCommandPacket = 1,
@@ -47,15 +44,121 @@
 // Epoch in microseconds since 01/01/0000.
 static const uint64_t BTSNOOP_EPOCH_DELTA = 0x00dcddb30f2f8000ULL;
 
-// File descriptor for btsnoop file.
-static int hci_btsnoop_fd = -1;
-static bool is_logging = false;
-static const char *logging_path;
+static const stack_config_t *stack_config;
 
+static int logfile_fd = INVALID_FD;
+static bool module_started;
+static bool is_logging;
+static bool logging_enabled_via_api;
+
+// TODO(zachoverflow): merge btsnoop and btsnoop_net together
 void btsnoop_net_open();
 void btsnoop_net_close();
 void btsnoop_net_write(const void *data, size_t length);
 
+static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received);
+static void update_logging();
+
+// Module lifecycle functions
+
+static future_t *start_up(void) {
+  module_started = true;
+  update_logging();
+
+  return NULL;
+}
+
+static future_t *shut_down(void) {
+  module_started = false;
+  update_logging();
+
+  return NULL;
+}
+
+const module_t btsnoop_module = {
+  .name = BTSNOOP_MODULE,
+  .init = NULL,
+  .start_up = start_up,
+  .shut_down = shut_down,
+  .clean_up = NULL,
+  .dependencies = {
+    STACK_CONFIG_MODULE,
+    NULL
+  }
+};
+
+// Interface functions
+
+static void set_api_wants_to_log(bool value) {
+  logging_enabled_via_api = value;
+  update_logging();
+}
+
+static void capture(const BT_HDR *buffer, bool is_received) {
+  const uint8_t *p = buffer->data + buffer->offset;
+
+  if (logfile_fd == INVALID_FD)
+    return;
+
+  switch (buffer->event & MSG_EVT_MASK) {
+    case MSG_HC_TO_STACK_HCI_EVT:
+      btsnoop_write_packet(kEventPacket, p, false);
+      break;
+    case MSG_HC_TO_STACK_HCI_ACL:
+    case MSG_STACK_TO_HC_HCI_ACL:
+      btsnoop_write_packet(kAclPacket, p, is_received);
+      break;
+    case MSG_HC_TO_STACK_HCI_SCO:
+    case MSG_STACK_TO_HC_HCI_SCO:
+      btsnoop_write_packet(kScoPacket, p, is_received);
+      break;
+    case MSG_STACK_TO_HC_HCI_CMD:
+      btsnoop_write_packet(kCommandPacket, p, true);
+      break;
+  }
+}
+
+static const btsnoop_t interface = {
+  set_api_wants_to_log,
+  capture
+};
+
+const btsnoop_t *btsnoop_get_interface() {
+  stack_config = stack_config_get_interface();
+  return &interface;
+}
+
+// Internal functions
+
+static void update_logging() {
+  bool should_log = module_started &&
+    (logging_enabled_via_api || stack_config->get_btsnoop_turned_on());
+
+  if (should_log == is_logging)
+    return;
+
+  is_logging = should_log;
+  if (should_log) {
+    btsnoop_net_open();
+
+    const char *path = stack_config->get_btsnoop_log_path();
+    logfile_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+
+    if (logfile_fd == INVALID_FD) {
+      ALOGE("%s unable to open '%s': %s", __func__, path, strerror(errno));
+      return;
+    }
+
+    write(logfile_fd, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16);
+  } else {
+    if (logfile_fd != INVALID_FD)
+      close(logfile_fd);
+
+    logfile_fd = INVALID_FD;
+    btsnoop_net_close();
+  }
+}
+
 static uint64_t btsnoop_timestamp(void) {
   struct timeval tv;
   gettimeofday(&tv, NULL);
@@ -68,8 +171,8 @@
 }
 
 static void btsnoop_write(const void *data, size_t length) {
-  if (hci_btsnoop_fd != -1)
-    write(hci_btsnoop_fd, data, length);
+  if (logfile_fd != INVALID_FD)
+    write(logfile_fd, data, length);
 
   btsnoop_net_write(data, length);
 }
@@ -117,86 +220,3 @@
   btsnoop_write(&type, 1);
   btsnoop_write(packet, length_he - 1);
 }
-
-static void btsnoop_open(const char *p_path) {
-  assert(p_path != NULL);
-  assert(*p_path != '\0');
-
-  btsnoop_net_open();
-
-  if (hci_btsnoop_fd != -1) {
-    ALOGE("%s btsnoop log file is already open.", __func__);
-    return;
-  }
-
-  hci_btsnoop_fd = open(p_path,
-                        O_WRONLY | O_CREAT | O_TRUNC,
-                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
-
-  if (hci_btsnoop_fd == -1) {
-    ALOGE("%s unable to open '%s': %s", __func__, p_path, strerror(errno));
-    return;
-  }
-
-  write(hci_btsnoop_fd, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16);
-}
-
-static void btsnoop_close(void) {
-  if (hci_btsnoop_fd != -1)
-    close(hci_btsnoop_fd);
-  hci_btsnoop_fd = -1;
-
-  btsnoop_net_close();
-}
-
-static void btsnoop_set_logging_path(const char *path) {
-  assert(!is_logging);
-  logging_path = path;
-}
-
-static void btsnoop_set_is_running(bool should_log) {
-  if (should_log == is_logging)
-    return;
-
-  is_logging = should_log;
-  if (should_log) {
-    assert(logging_path != NULL);
-    btsnoop_open(logging_path);
-  } else {
-    btsnoop_close();
-  }
-}
-
-static void btsnoop_capture(const BT_HDR *buffer, bool is_received) {
-  const uint8_t *p = buffer->data + buffer->offset;
-
-  if (hci_btsnoop_fd == -1)
-    return;
-
-  switch (buffer->event & MSG_EVT_MASK) {
-    case MSG_HC_TO_STACK_HCI_EVT:
-      btsnoop_write_packet(kEventPacket, p, false);
-      break;
-    case MSG_HC_TO_STACK_HCI_ACL:
-    case MSG_STACK_TO_HC_HCI_ACL:
-      btsnoop_write_packet(kAclPacket, p, is_received);
-      break;
-    case MSG_HC_TO_STACK_HCI_SCO:
-    case MSG_STACK_TO_HC_HCI_SCO:
-      btsnoop_write_packet(kScoPacket, p, is_received);
-      break;
-    case MSG_STACK_TO_HC_HCI_CMD:
-      btsnoop_write_packet(kCommandPacket, p, true);
-      break;
-  }
-}
-
-static const btsnoop_t interface = {
-  btsnoop_set_logging_path,
-  btsnoop_set_is_running,
-  btsnoop_capture
-};
-
-const btsnoop_t *btsnoop_get_interface() {
-  return &interface;
-}
diff --git a/include/bt_trace.h b/include/bt_trace.h
index 0b802fc..d7acce3 100644
--- a/include/bt_trace.h
+++ b/include/bt_trace.h
@@ -18,6 +18,8 @@
 
 #pragma once
 
+#define BTE_LOGMSG_MODULE "bte_logmsg_module"
+
 /* BTE tracing IDs for debug purposes */
 /* LayerIDs for stack */
 #define BTTRC_ID_STK_GKI                   1
@@ -435,5 +437,3 @@
 
 /* External declaration for appl_trace_level here to avoid to add the declaration in all the files using APPL_TRACExxx macros */
 extern UINT8 appl_trace_level;
-
-void BTE_InitTraceLevels(void);
diff --git a/include/stack_config.h b/include/stack_config.h
new file mode 100644
index 0000000..5da6926
--- /dev/null
+++ b/include/stack_config.h
@@ -0,0 +1,34 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#pragma once
+
+#include <stdbool.h>
+
+#include "config.h"
+#include "module.h"
+
+typedef struct {
+  const char *(*get_btsnoop_log_path)(void);
+  bool (*get_btsnoop_turned_on)(void);
+  bool (*get_trace_config_enabled)(void);
+  config_t *(*get_all)(void);
+} stack_config_t;
+
+#define STACK_CONFIG_MODULE "stack_config_module"
+const stack_config_t *stack_config_get_interface();
diff --git a/main/Android.mk b/main/Android.mk
index b25d828..da5365c 100644
--- a/main/Android.mk
+++ b/main/Android.mk
@@ -15,7 +15,8 @@
 	bte_main.c \
 	bte_init.c \
 	bte_logmsg.c \
-	bte_conf.c
+	bte_conf.c \
+	stack_config.c
 
 # BTIF
 LOCAL_SRC_FILES += \
diff --git a/main/bte_conf.c b/main/bte_conf.c
index 63ace86..38607f8 100644
--- a/main/bte_conf.c
+++ b/main/bte_conf.c
@@ -26,35 +26,6 @@
 #include "bta_api.h"
 #include "config.h"
 
-// TODO: eliminate these global variables.
-extern char hci_logfile[256];
-extern BOOLEAN hci_logging_enabled;
-extern BOOLEAN hci_save_log;
-extern BOOLEAN trace_conf_enabled;
-void bte_trace_conf_config(const config_t *config);
-
-// Reads the stack configuration file and populates global variables with
-// the contents of the file.
-void bte_load_conf(const char *path) {
-  assert(path != NULL);
-
-  ALOGI("%s attempt to load stack conf from %s", __func__, path);
-
-  config_t *config = config_new(path);
-  if (!config) {
-    ALOGI("%s file >%s< not found", __func__, path);
-    return;
-  }
-
-  strlcpy(hci_logfile, config_get_string(config, CONFIG_DEFAULT_SECTION, "BtSnoopFileName", ""), sizeof(hci_logfile));
-  hci_logging_enabled = config_get_bool(config, CONFIG_DEFAULT_SECTION, "BtSnoopLogOutput", false);
-  hci_save_log = config_get_bool(config, CONFIG_DEFAULT_SECTION, "BtSnoopSaveLog", false);
-  trace_conf_enabled = config_get_bool(config, CONFIG_DEFAULT_SECTION, "TraceConf", false);
-
-  bte_trace_conf_config(config);
-  config_free(config);
-}
-
 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
 extern int btm_ble_tx_power[BTM_BLE_ADV_TX_POWER_MAX + 1];
 void bte_load_ble_conf(const char* path)
diff --git a/main/bte_logmsg.c b/main/bte_logmsg.c
index 416c5ca..034ede7 100644
--- a/main/bte_logmsg.c
+++ b/main/bte_logmsg.c
@@ -32,6 +32,7 @@
 #include "config.h"
 #include "gki.h"
 #include "l2c_api.h"
+#include "stack_config.h"
 
 #if (RFCOMM_INCLUDED==TRUE)
 #include "port_api.h"
@@ -103,8 +104,6 @@
 
 #define MSG_BUFFER_OFFSET 0
 
-bool trace_conf_enabled = false;
-
 /* LayerIDs for BTA, currently everything maps onto appl_trace_level */
 static const char * const bt_layer_tags[] = {
   "bt-btif",
@@ -231,16 +230,6 @@
 
 static const UINT16 bttrc_map_size = sizeof(bttrc_set_level_map)/sizeof(tBTTRC_FUNC_MAP);
 
-void bte_trace_conf_config(const config_t *config) {
-  assert(config != NULL);
-
-  for (tBTTRC_FUNC_MAP *functions = &bttrc_set_level_map[0]; functions->trc_name; ++functions) {
-    int value = config_get_int(config, CONFIG_DEFAULT_SECTION, functions->trc_name, -1);
-    if (value != -1)
-      functions->trace_level = value;
-  }
-}
-
 void LogMsg(uint32_t trace_set_mask, const char *fmt_str, ...) {
   static char buffer[BTE_LOG_BUF_SIZE];
   int trace_layer = TRACE_GET_LAYER(trace_set_mask);
@@ -272,36 +261,6 @@
   }
 }
 
-/********************************************************************************
- **
- **    Function Name:     BTE_InitTraceLevels
- **
- **    Purpose:           This function can be used to set the boot time reading it from the
- **                       platform.
- **                       WARNING: it is called under BTU context and it blocks the BTU task
- **                       till it returns (sync call)
- **
- **    Input Parameters:  None, platform to provide levels
- **    Returns:
- **                       Newly set levels, if any!
- **
- *********************************************************************************/
-void BTE_InitTraceLevels(void) {
-  //  Read and set trace levels by calling the different XXX_SetTraceLevel().
-  if (trace_conf_enabled == false) {
-    ALOGI("[bttrc] using compile default trace settings");
-    return;
-  }
-
-  tBTTRC_FUNC_MAP *p_f_map = (tBTTRC_FUNC_MAP *)&bttrc_set_level_map[0];
-  while (p_f_map->trc_name != NULL) {
-    ALOGI("BTE_InitTraceLevels -- %s", p_f_map->trc_name);
-    if (p_f_map->p_f)
-      p_f_map->p_f(p_f_map->trace_level);
-    p_f_map++;
-  }
-}
-
 /* this function should go into BTAPP_DM for example */
 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level) {
   if (new_level != 0xFF)
@@ -323,3 +282,40 @@
 
   return btu_cb.trace_level;
 }
+
+static void load_levels_from_config(const config_t *config) {
+  assert(config != NULL);
+
+  for (tBTTRC_FUNC_MAP *functions = &bttrc_set_level_map[0]; functions->trc_name; ++functions) {
+    ALOGI("BTE_InitTraceLevels -- %s", functions->trc_name);
+    int value = config_get_int(config, CONFIG_DEFAULT_SECTION, functions->trc_name, -1);
+    if (value != -1)
+      functions->trace_level = value;
+
+    if (functions->p_f)
+      functions->p_f(functions->trace_level);
+  }
+}
+
+static future_t *init(void) {
+  const stack_config_t *stack_config = stack_config_get_interface();
+  if (!stack_config->get_trace_config_enabled()) {
+    ALOGI("[bttrc] using compile default trace settings");
+    return NULL;
+  }
+
+  load_levels_from_config(stack_config->get_all());
+  return NULL;
+}
+
+const module_t bte_logmsg_module = {
+  .name = BTE_LOGMSG_MODULE,
+  .init = init,
+  .start_up = NULL,
+  .shut_down = NULL,
+  .clean_up = NULL,
+  .dependencies = {
+    STACK_CONFIG_MODULE,
+    NULL
+  }
+};
diff --git a/main/bte_main.c b/main/bte_main.c
index e9edb9e..0c5209a 100755
--- a/main/bte_main.c
+++ b/main/bte_main.c
@@ -48,40 +48,28 @@
 #include "hash_functions.h"
 #include "hash_map.h"
 #include "hci_layer.h"
+#include "module.h"
 #include "osi.h"
+#include "stack_config.h"
 #include "thread.h"
 
 /*******************************************************************************
 **  Constants & Macros
 *******************************************************************************/
 
-/* Run-time configuration file */
-#ifndef BTE_STACK_CONF_FILE
-#define BTE_STACK_CONF_FILE "/etc/bluetooth/bt_stack.conf"
-#endif
 /* Run-time configuration file for BLE*/
 #ifndef BTE_BLE_STACK_CONF_FILE
 #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
 #endif
 
-/* if not specified in .txt file then use this as default  */
-#ifndef HCI_LOGGING_FILENAME
-#define HCI_LOGGING_FILENAME  "/data/misc/bluedroid/btsnoop_hci.log"
-#endif
-
 /******************************************************************************
 **  Variables
 ******************************************************************************/
-BOOLEAN hci_logging_enabled = FALSE;    /* by default, turn hci log off */
-BOOLEAN hci_logging_config = FALSE;    /* configured from bluetooth framework */
-BOOLEAN hci_save_log = FALSE; /* save a copy of the log before starting again */
-char hci_logfile[256] = HCI_LOGGING_FILENAME;
 
 /*******************************************************************************
 **  Static variables
 *******************************************************************************/
 static const hci_t *hci;
-static const btsnoop_t *btsnoop;
 static const hci_callbacks_t hci_callbacks;
 // Lock to serialize shutdown requests from upper layer.
 static pthread_mutex_t shutdown_lock;
@@ -120,8 +108,6 @@
     if (!hci)
       ALOGE("%s could not get hci layer interface.", __func__);
 
-    btsnoop = btsnoop_get_interface();
-
     btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
     if (btu_hci_msg_queue == NULL) {
       ALOGE("%s unable to allocate hci message queue.", __func__);
@@ -130,10 +116,10 @@
 
     data_dispatcher_register_default(hci->upward_dispatcher, btu_hci_msg_queue);
 
-    bte_load_conf(BTE_STACK_CONF_FILE);
 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
     bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
 #endif
+    module_init(get_module(STACK_CONFIG_MODULE));
 
 #if (BTTRC_INCLUDED == TRUE)
     /* Initialize trace feature */
@@ -141,7 +127,6 @@
 #endif
 
     pthread_mutex_init(&shutdown_lock, NULL);
-    btsnoop->set_logging_path(hci_logfile);
 }
 
 /******************************************************************************
@@ -160,6 +145,8 @@
 
     btu_hci_msg_queue = NULL;
 
+    module_clean_up(get_module(STACK_CONFIG_MODULE));
+
     pthread_mutex_destroy(&shutdown_lock);
     GKI_shutdown();
 }
@@ -180,8 +167,8 @@
 
 //    BTU_StartUp();
 
-    btsnoop->set_is_running(hci_logging_enabled || hci_logging_config);
     assert(hci->start_up_async(btif_local_bd_addr.address, &hci_callbacks));
+    module_start_up(get_module(BTSNOOP_MODULE));
 }
 
 /******************************************************************************
@@ -198,11 +185,11 @@
 {
     APPL_TRACE_DEBUG("%s", __FUNCTION__);
 
+    module_shut_down(get_module(BTSNOOP_MODULE));
     if (hci) {
       // Shutdown is not thread safe and must be protected.
       pthread_mutex_lock(&shutdown_lock);
 
-      btsnoop->set_is_running(false);
       hci->shut_down();
 
       pthread_mutex_unlock(&shutdown_lock);
@@ -213,21 +200,6 @@
 
 /******************************************************************************
 **
-** Function         bte_main_config_hci_logging
-**
-** Description      enable or disable HIC snoop logging
-**
-** Returns          None
-**
-******************************************************************************/
-void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled)
-{
-  hci_logging_config = enable;
-  btsnoop->set_is_running((hci_logging_config || hci_logging_enabled) && !bt_disabled);
-}
-
-/******************************************************************************
-**
 ** Function         bte_main_postload_cfg
 **
 ** Description      BTE MAIN API - Stack postload configuration
diff --git a/main/stack_config.c b/main/stack_config.c
new file mode 100644
index 0000000..a568c23
--- /dev/null
+++ b/main/stack_config.c
@@ -0,0 +1,93 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#define LOG_TAG "bt_stack_config"
+
+#include <assert.h>
+#include <utils/Log.h>
+
+#include "future.h"
+#include "stack_config.h"
+
+const char *BTSNOOP_LOG_PATH_KEY = "BtSnoopFileName";
+const char *BTSNOOP_TURNED_ON_KEY = "BtSnoopLogOutput";
+const char *TRACE_CONFIG_ENABLED_KEY = "TraceConf";
+
+static config_t *config;
+
+// Module lifecycle functions
+
+static future_t *init() {
+  const char *path = "/etc/bluetooth/bt_stack.conf";
+  assert(path != NULL);
+
+  ALOGI("%s attempt to load stack conf from %s", __func__, path);
+
+  config = config_new(path);
+  if (!config) {
+    ALOGI("%s file >%s< not found", __func__, path);
+    return future_new_immediate(FUTURE_FAIL);
+  }
+
+  return future_new_immediate(FUTURE_SUCCESS);
+}
+
+static future_t *clean_up() {
+  config_free(config);
+  return future_new_immediate(FUTURE_SUCCESS);
+}
+
+const module_t stack_config_module = {
+  .name = STACK_CONFIG_MODULE,
+  .init = init,
+  .start_up = NULL,
+  .shut_down = NULL,
+  .clean_up = clean_up,
+  .dependencies = {
+    NULL
+  }
+};
+
+// Interface functions
+
+static const char *get_btsnoop_log_path(void) {
+  return config_get_string(config, CONFIG_DEFAULT_SECTION, BTSNOOP_LOG_PATH_KEY, "/data/misc/bluedroid/btsnoop_hci.log");
+}
+
+static bool get_btsnoop_turned_on(void) {
+  return config_get_bool(config, CONFIG_DEFAULT_SECTION, BTSNOOP_TURNED_ON_KEY, false);
+}
+
+static bool get_trace_config_enabled(void) {
+  return config_get_bool(config, CONFIG_DEFAULT_SECTION, TRACE_CONFIG_ENABLED_KEY, false);
+}
+
+static config_t *get_all(void) {
+  return config;
+}
+
+const stack_config_t interface = {
+  get_btsnoop_log_path,
+  get_btsnoop_turned_on,
+  get_trace_config_enabled,
+  get_all
+};
+
+const stack_config_t *stack_config_get_interface() {
+  return &interface;
+}
diff --git a/stack/Android.mk b/stack/Android.mk
index b0d9c23..616ee07 100644
--- a/stack/Android.mk
+++ b/stack/Android.mk
@@ -17,6 +17,7 @@
                    $(LOCAL_PATH)/sdp \
                    $(LOCAL_PATH)/smp \
                    $(LOCAL_PATH)/srvc \
+                   $(LOCAL_PATH)/../btcore/include \
                    $(LOCAL_PATH)/../vnd/include \
                    $(LOCAL_PATH)/../vnd/ble \
                    $(LOCAL_PATH)/../btif/include \
diff --git a/stack/btu/btu_task.c b/stack/btu/btu_task.c
index d520039..ae0617a 100644
--- a/stack/btu/btu_task.c
+++ b/stack/btu/btu_task.c
@@ -39,10 +39,7 @@
 #include "hash_map.h"
 #include "hcimsgs.h"
 #include "l2c_int.h"
-#include "btu.h"
-#include "bt_utils.h"
-#include <sys/prctl.h>
-
+#include "module.h"
 #include "osi.h"
 #include "sdpint.h"
 #include "thread.h"
@@ -426,7 +423,7 @@
    * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
    */
 #if ( BT_USE_TRACES==TRUE )
-  BTE_InitTraceLevels();
+  module_init(get_module(BTE_LOGMSG_MODULE));
 #endif
 
   // Inform the bt jni thread initialization is ok.