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

Commit 255e261a authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Synchronize Bluetooth stack init and cleanup

Bug: 25972918
Change-Id: Ia4fb4d74f8340862233dc5073596f3082863c941
parent c3fbae1c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ typedef struct {
  void (*init_stack)(void);
  void (*start_up_stack_async)(void);
  void (*shut_down_stack_async)(void);
  void (*clean_up_stack_async)(void);
  void (*clean_up_stack)(void);

  bool (*get_stack_is_running)(void);
} stack_manager_t;
+1 −1
Original line number Diff line number Diff line
@@ -154,7 +154,7 @@ static int disable(void) {
}

static void cleanup(void) {
  stack_manager_get_interface()->clean_up_stack_async();
  stack_manager_get_interface()->clean_up_stack();
}

static int get_adapter_properties(void)
+18 −6
Original line number Diff line number Diff line
@@ -63,7 +63,9 @@ void btif_thread_post(thread_fn func, void *context);

static void init_stack(void) {
  // This is a synchronous process. Post it to the thread though, so
  // state modification only happens there.
  // state modification only happens there. Using the thread to perform
  // all stack operations ensures that the operations are done serially
  // and do not overlap.
  semaphore_t *semaphore = semaphore_new(0);
  thread_post(management_thread, event_init_stack, semaphore);
  semaphore_wait(semaphore);
@@ -78,8 +80,13 @@ static void shut_down_stack_async(void) {
  thread_post(management_thread, event_shut_down_stack, NULL);
}

static void clean_up_stack_async(void) {
  thread_post(management_thread, event_clean_up_stack, NULL);
static void clean_up_stack(void) {
  // This is a synchronous process. Post it to the thread though, so
  // state modification only happens there.
  semaphore_t *semaphore = semaphore_new(0);
  thread_post(management_thread, event_clean_up_stack, semaphore);
  semaphore_wait(semaphore);
  semaphore_free(semaphore);
}

static bool get_stack_is_running(void) {
@@ -174,10 +181,10 @@ static void ensure_stack_is_not_running(void) {
}

// Synchronous function to clean up the stack
static void event_clean_up_stack(UNUSED_ATTR void *context) {
static void event_clean_up_stack(void *context) {
  if (!stack_is_initialized) {
    LOG_DEBUG(LOG_TAG, "%s found the stack already in a clean state.", __func__);
    return;
    goto cleanup;
  }

  ensure_stack_is_not_running();
@@ -195,6 +202,11 @@ static void event_clean_up_stack(UNUSED_ATTR void *context) {
  module_clean_up(get_module(OSI_MODULE));
  module_management_stop();
  LOG_DEBUG(LOG_TAG, "%s finished.", __func__);

cleanup:;
  semaphore_t *semaphore = (semaphore_t *)context;
  if (semaphore)
    semaphore_post(semaphore);
}

static void event_signal_stack_up(UNUSED_ATTR void *context) {
@@ -223,7 +235,7 @@ static const stack_manager_t interface = {
  init_stack,
  start_up_stack_async,
  shut_down_stack_async,
  clean_up_stack_async,
  clean_up_stack,

  get_stack_is_running
};