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