OSDN Git Service

Shutdown alarm callbacks on stack disable; added OSI module
authorAndre Eisenbach <eisenbach@google.com>
Mon, 18 May 2015 16:41:06 +0000 (09:41 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Mon, 8 Jun 2015 08:27:04 +0000 (01:27 -0700)
Change-Id: Iecf1e2258da012bdac69a4f57d38b12a272e3edd

btcore/Android.mk
btcore/include/osi_module.h [new file with mode: 0644]
btcore/src/osi_module.c [new file with mode: 0644]
btif/src/stack_manager.c
main/bte_main.c
osi/include/alarm.h
osi/src/alarm.c

index 0356650..50006d4 100644 (file)
@@ -33,6 +33,7 @@ LOCAL_SRC_FILES := \
     src/counter.c \
     src/device_class.c \
     src/module.c \
+    src/osi_module.c \
     src/property.c \
     src/uuid.c
 
diff --git a/btcore/include/osi_module.h b/btcore/include/osi_module.h
new file mode 100644 (file)
index 0000000..964a3fa
--- /dev/null
@@ -0,0 +1,21 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2015 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
+
+static const char OSI_MODULE[] = "osi_module";
diff --git a/btcore/src/osi_module.c b/btcore/src/osi_module.c
new file mode 100644 (file)
index 0000000..40c27d5
--- /dev/null
@@ -0,0 +1,44 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2015 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_osi_module"
+
+#include "btcore/include/module.h"
+#include "btcore/include/osi_module.h"
+#include "osi/include/alarm.h"
+#include "osi/include/future.h"
+#include "osi/include/log.h"
+#include "osi/include/osi.h"
+
+future_t *osi_start_up(void) {
+  return NULL;
+}
+
+future_t *osi_shut_down(void) {
+  alarm_shutdown();
+  return NULL;
+}
+
+const module_t osi_module = {
+  .name = OSI_MODULE,
+  .init = NULL,
+  .start_up = osi_start_up,
+  .shut_down = osi_shut_down,
+  .clean_up = NULL,
+  .dependencies = {NULL}
+};
index ff71b08..3b64648 100644 (file)
@@ -24,6 +24,7 @@
 #include "btif_common.h"
 #include "device/include/controller.h"
 #include "btcore/include/module.h"
+#include "btcore/include/osi_module.h"
 #include "osi/include/osi.h"
 #include "osi/include/log.h"
 #include "osi/include/semaphore.h"
@@ -125,6 +126,7 @@ static void event_start_up_stack(UNUSED_ATTR void *context) {
   LOG_DEBUG("%s is bringing up the stack.", __func__);
   hack_future = future_new();
 
+  module_start_up(get_module(OSI_MODULE));
   // Include this for now to put btif config into a shutdown-able state
   module_start_up(get_module(BTIF_CONFIG_MODULE));
   bte_main_enable();
@@ -156,6 +158,8 @@ static void event_shut_down_stack(UNUSED_ATTR void *context) {
 
   future_await(hack_future);
   module_shut_down(get_module(CONTROLLER_MODULE)); // Doesn't do any work, just puts it in a restartable state
+  module_shut_down(get_module(OSI_MODULE));
+
   LOG_DEBUG("%s finished.", __func__);
   btif_thread_post(event_signal_stack_down, NULL);
 }
index 96b41c9..4f6c1b0 100644 (file)
 #include "btsnoop.h"
 #include "bt_utils.h"
 #include "btcore/include/counter.h"
+#include "btcore/include/module.h"
 #include "osi/include/fixed_queue.h"
 #include "osi/include/future.h"
 #include "gki.h"
 #include "osi/include/hash_functions.h"
 #include "osi/include/hash_map.h"
 #include "hci_layer.h"
-#include "btcore/include/module.h"
 #include "osi/include/osi.h"
 #include "osi/include/log.h"
 #include "stack_config.h"
index b27e9a4..5a372dc 100644 (file)
@@ -51,3 +51,7 @@ void alarm_set_periodic(alarm_t *alarm, period_ms_t period, alarm_callback_t cb,
 // will not be called if it hasn't already been called. This function is idempotent.
 // |alarm| may not be NULL.
 void alarm_cancel(alarm_t *alarm);
+
+// Shuts down the alarm dispatch callback. To be called during module/stack
+// shutdown only.
+void alarm_shutdown(void);
index ff62d10..603ca6b 100644 (file)
@@ -71,6 +71,7 @@ static bool timer_set;
 
 // All alarm callbacks are dispatched from |callback_thread|
 static thread_t *callback_thread;
+static bool callback_thread_active;
 static semaphore_t *alarm_expired;
 
 static bool lazy_initialize(void);
@@ -177,6 +178,22 @@ void alarm_cancel(alarm_t *alarm) {
   pthread_mutex_unlock(&alarm->callback_lock);
 }
 
+void alarm_shutdown(void) {
+  callback_thread_active = false;
+  semaphore_post(alarm_expired);
+  thread_free(callback_thread);
+  callback_thread = NULL;
+
+  semaphore_free(alarm_expired);
+  alarm_expired = NULL;
+  timer_delete(&timer);
+
+  list_free(alarms);
+  alarms = NULL;
+
+  pthread_mutex_destroy(&monitor);
+}
+
 static bool lazy_initialize(void) {
   assert(alarms == NULL);
 
@@ -203,6 +220,7 @@ static bool lazy_initialize(void) {
     return false;
   }
 
+  callback_thread_active = true;
   callback_thread = thread_new("alarm_callbacks");
   if (!callback_thread) {
     LOG_ERROR("%s unable to create alarm callback thread.", __func__);
@@ -323,8 +341,10 @@ static void timer_callback(UNUSED_ATTR void *ptr) {
 static void callback_dispatch(UNUSED_ATTR void *context) {
   while (true) {
     semaphore_wait(alarm_expired);
-    pthread_mutex_lock(&monitor);
+    if (!callback_thread_active)
+      break;
 
+    pthread_mutex_lock(&monitor);
     alarm_t *alarm;
 
     // Take into account that the alarm may get cancelled before we get to it.
@@ -360,4 +380,6 @@ static void callback_dispatch(UNUSED_ATTR void *context) {
 
     pthread_mutex_unlock(&alarm->callback_lock);
   }
+
+  LOG_DEBUG("%s Callback thread exited", __func__);
 }