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 #pragma once
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 #include "fixed_queue.h"
25 
26 #define DISPATCHER_NAME_MAX 16
27 
28 typedef struct data_dispatcher_t data_dispatcher_t;
29 typedef uintptr_t data_dispatcher_type_t;
30 
31 // Creates a new data dispatcher object, with the given name.
32 // The returned object must be freed by calling |data_dispatcher_free|.
33 // Returns NULL on failure. |name| may not be NULL.
34 data_dispatcher_t *data_dispatcher_new(const char *name);
35 
36 // Frees a data dispatcher object created by |data_dispatcher_new|.
37 // |data_dispatcher| may be NULL.
38 void data_dispatcher_free(data_dispatcher_t *dispatcher);
39 
40 // Registers |type| and |queue| with the data dispatcher so that data
41 // sent under |type| ends up in |queue|. If |type| is already registered,
42 // it is replaced. If |queue| is NULL, the existing registration is
43 // removed, if it exists. |dispatcher| may not be NULL.
44 void data_dispatcher_register(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, fixed_queue_t *queue);
45 
46 // Registers a default queue to send data to when there is not a specific
47 // type/queue relationship registered. If a default queue is already registered,
48 // it is replaced. If |queue| is NULL, the existing registration is
49 // removed, if it exists. |dispatcher| may not be NULL.
50 void data_dispatcher_register_default(data_dispatcher_t *dispatcher, fixed_queue_t *queue);
51 
52 // Dispatches |data| to the queue registered for |type|. If no such registration
53 // exists, it is dispatched to the default queue if it exists.
54 // Neither |dispatcher| nor |data| may be NULL.
55 // Returns true if data dispatch was successful.
56 bool data_dispatcher_dispatch(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, void *data);
57