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