From 76a9ab6ab7f0b28aa10a5a581d8e086f85727fab Mon Sep 17 00:00:00 2001
From: "June R. Tate-Gans" <jtgans@google.com>
Date: Tue, 6 Jan 2015 17:38:29 -0800
Subject: [PATCH] First pass at the bluedroid profile manager.

---
 system/doc/log_tags.md           |  1 +
 system/profile/Android.mk        | 41 +++++++++++++++
 system/profile/include/manager.h | 41 +++++++++++++++
 system/profile/src/manager.c     | 87 ++++++++++++++++++++++++++++++++
 4 files changed, 170 insertions(+)
 create mode 100644 system/profile/Android.mk
 create mode 100644 system/profile/include/manager.h
 create mode 100644 system/profile/src/manager.c

diff --git a/system/doc/log_tags.md b/system/doc/log_tags.md
index 9524d51f0d2..b1b6016d32f 100644
--- a/system/doc/log_tags.md
+++ b/system/doc/log_tags.md
@@ -44,6 +44,7 @@ This document lists all of the log tags used by bluedroid.
 * bt_osi_data_dispatcher
 * bt_osi_reactor
 * bt_osi_socket
+* bt_profile_manager
 * bt_sdp_client
 * btsnoop
 * btsnoop_net
diff --git a/system/profile/Android.mk b/system/profile/Android.mk
new file mode 100644
index 00000000000..5683d7fbfe4
--- /dev/null
+++ b/system/profile/Android.mk
@@ -0,0 +1,41 @@
+ ##############################################################################
+ #
+ #  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.
+ #
+ ##############################################################################
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_C_INCLUDES := \
+    $(LOCAL_PATH)/.. \
+    $(LOCAL_PATH)/include \
+    $(LOCAL_PATH)/../btcore/include \
+    $(LOCAL_PATH)/../include \
+    $(LOCAL_PATH)/../osi/include \
+    $(bdroid_C_INCLUDES)
+
+LOCAL_SRC_FILES := \
+    src/manager.c
+
+LOCAL_CFLAGS := $(bdroid_CFLAGS)
+LOCAL_CONLYFLAGS := $(bdroid_CONLYFLAGS)
+LOCAL_MODULE := libbtprofile
+LOCAL_MODULE_TAGS := optional
+LOCAL_SHARED_LIBRARIES := libc liblog
+LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/system/profile/include/manager.h b/system/profile/include/manager.h
new file mode 100644
index 00000000000..587369fc430
--- /dev/null
+++ b/system/profile/include/manager.h
@@ -0,0 +1,41 @@
+/******************************************************************************
+ *
+ *  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
+
+static const char PROFILE_MANAGER_MODULE[] = "profile_manager_module";
+
+typedef enum {
+  PROFILE_POWER_ACTIVE,
+  PROFILE_POWER_HOLD,
+  PROFILE_POWER_SNIFF,
+  PROFILE_POWER_PARK
+} profile_power_level_t;
+
+typedef struct profile_t {
+  const char *name;
+  profile_power_level_t lowest_acceptable_power_mode;
+  bool in_use;
+} profile_t;
+
+// Registers a given Bluetooth |profile| with the manager.
+void profile_register(const profile_t *profile);
+
+// Looks up a previously registered profile by |name|. If no profile was
+// registered by the given |name|, then this function returns NULL.
+const profile_t *profile_by_name(const char *name);
diff --git a/system/profile/src/manager.c b/system/profile/src/manager.c
new file mode 100644
index 00000000000..9e56b655e07
--- /dev/null
+++ b/system/profile/src/manager.c
@@ -0,0 +1,87 @@
+/******************************************************************************
+ *
+ *  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_profile_manager"
+
+#include <assert.h>
+#include <stdbool.h>
+
+#include "btcore/include/module.h"
+#include "profile/include/manager.h"
+#include "osi/include/future.h"
+#include "osi/include/hash_functions.h"
+#include "osi/include/hash_map.h"
+#include "osi/include/log.h"
+#include "osi/include/osi.h"
+
+static const size_t number_of_profile_buckets = 15;
+
+static bool initialized;
+static hash_map_t *profile_map;
+
+// Lifecycle management functions
+
+static future_t *init(void) {
+  profile_map = hash_map_new(
+    number_of_profile_buckets,
+    hash_function_string,
+    NULL,
+    NULL,
+    NULL);
+
+  initialized = true;
+  return NULL;
+}
+
+static future_t *clean_up(void) {
+  initialized = false;
+
+  hash_map_free(profile_map);
+  profile_map = NULL;
+
+  return NULL;
+}
+
+const module_t profile_manager_module = {
+  .name = PROFILE_MANAGER_MODULE,
+  .init = init,
+  .start_up = NULL,
+  .shut_down = NULL,
+  .clean_up = clean_up,
+  .dependencies = {
+    NULL
+  }
+};
+
+// Interface functions
+
+void profile_register(const profile_t *profile) {
+  assert(initialized);
+  assert(profile != NULL);
+  assert(profile->name != NULL);
+  assert(!hash_map_has_key(profile_map, profile->name));
+
+  hash_map_set(profile_map, profile->name, (void *) profile);
+}
+
+const profile_t *profile_by_name(const char *name) {
+  assert(initialized);
+  assert(name != NULL);
+
+  return (profile_t *)hash_map_get(profile_map, name);
+}
-- 
GitLab