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_osi_data_dispatcher" 20 21 #include "osi/include/data_dispatcher.h" 22 23 #include <assert.h> 24 25 #include "osi/include/allocator.h" 26 #include "osi/include/hash_functions.h" 27 #include "osi/include/hash_map.h" 28 #include "osi/include/osi.h" 29 #include "osi/include/log.h" 30 31 #define DEFAULT_TABLE_BUCKETS 10 32 33 struct data_dispatcher_t { 34 char *name; 35 hash_map_t *dispatch_table; 36 fixed_queue_t *default_queue; // We don't own this queue 37 }; 38 39 data_dispatcher_t *data_dispatcher_new(const char *name) { 40 assert(name != NULL); 41 42 data_dispatcher_t *ret = osi_calloc(sizeof(data_dispatcher_t)); 43 44 ret->dispatch_table = hash_map_new(DEFAULT_TABLE_BUCKETS, hash_function_naive, NULL, NULL, NULL); 45 if (!ret->dispatch_table) { 46 LOG_ERROR(LOG_TAG, "%s unable to create dispatch table.", __func__); 47 goto error; 48 } 49 50 ret->name = osi_strdup(name); 51 if (!ret->name) { 52 LOG_ERROR(LOG_TAG, "%s unable to duplicate provided name.", __func__); 53 goto error; 54 } 55 56 return ret; 57 58 error:; 59 data_dispatcher_free(ret); 60 return NULL; 61 } 62 63 void data_dispatcher_free(data_dispatcher_t *dispatcher) { 64 if (!dispatcher) 65 return; 66 67 hash_map_free(dispatcher->dispatch_table); 68 osi_free(dispatcher->name); 69 osi_free(dispatcher); 70 } 71 72 void data_dispatcher_register(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, fixed_queue_t *queue) { 73 assert(dispatcher != NULL); 74 75 hash_map_erase(dispatcher->dispatch_table, (void *)type); 76 if (queue) 77 hash_map_set(dispatcher->dispatch_table, (void *)type, queue); 78 } 79 80 void data_dispatcher_register_default(data_dispatcher_t *dispatcher, fixed_queue_t *queue) { 81 assert(dispatcher != NULL); 82 83 dispatcher->default_queue = queue; 84 } 85 86 bool data_dispatcher_dispatch(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, void *data) { 87 assert(dispatcher != NULL); 88 assert(data != NULL); 89 90 fixed_queue_t *queue = hash_map_get(dispatcher->dispatch_table, (void *)type); 91 if (!queue) 92 queue = dispatcher->default_queue; 93 94 if (queue) 95 fixed_queue_enqueue(queue, data); 96 else 97 LOG_WARN(LOG_TAG, "%s has no handler for type (%zd) in data dispatcher named: %s", __func__, type, dispatcher->name); 98 99 return queue != NULL; 100 } 101