Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2014 Google, Inc.
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 #define LOG_TAG "bt_profile_manager"
     20 
     21 #include <assert.h>
     22 #include <stdbool.h>
     23 
     24 #include "btcore/include/module.h"
     25 #include "profile/include/manager.h"
     26 #include "osi/include/future.h"
     27 #include "osi/include/hash_functions.h"
     28 #include "osi/include/hash_map.h"
     29 #include "osi/include/log.h"
     30 #include "osi/include/osi.h"
     31 
     32 static const size_t number_of_profile_buckets = 15;
     33 
     34 static bool initialized;
     35 static hash_map_t *profile_map;
     36 
     37 // Lifecycle management functions
     38 
     39 static future_t *init(void) {
     40   profile_map = hash_map_new(
     41     number_of_profile_buckets,
     42     hash_function_string,
     43     NULL,
     44     NULL,
     45     NULL);
     46 
     47   initialized = true;
     48   return NULL;
     49 }
     50 
     51 static future_t *clean_up(void) {
     52   initialized = false;
     53 
     54   hash_map_free(profile_map);
     55   profile_map = NULL;
     56 
     57   return NULL;
     58 }
     59 
     60 EXPORT_SYMBOL const module_t profile_manager_module = {
     61   .name = PROFILE_MANAGER_MODULE,
     62   .init = init,
     63   .start_up = NULL,
     64   .shut_down = NULL,
     65   .clean_up = clean_up,
     66   .dependencies = {
     67     NULL
     68   }
     69 };
     70 
     71 // Interface functions
     72 
     73 void profile_register(const profile_t *profile) {
     74   assert(initialized);
     75   assert(profile != NULL);
     76   assert(profile->name != NULL);
     77   assert(!hash_map_has_key(profile_map, profile->name));
     78 
     79   hash_map_set(profile_map, profile->name, (void *) profile);
     80 }
     81 
     82 const profile_t *profile_by_name(const char *name) {
     83   assert(initialized);
     84   assert(name != NULL);
     85 
     86   return (profile_t *)hash_map_get(profile_map, name);
     87 }
     88