Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 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 | |
Pavlin Radoslavov | c196f21 | 2015-09-23 20:39:53 -0700 | [diff] [blame] | 24 | #include "osi/include/fixed_queue.h" |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 25 | |
Jakub Pawlowski | 2b56e01 | 2016-05-26 10:13:40 -0700 | [diff] [blame] | 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 30 | #define DISPATCHER_NAME_MAX 16 |
| 31 | |
| 32 | typedef struct data_dispatcher_t data_dispatcher_t; |
Nitin Arora | a74fcdf | 2015-03-24 17:15:50 -0700 | [diff] [blame] | 33 | typedef uintptr_t data_dispatcher_type_t; |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 34 | |
| 35 | // Creates a new data dispatcher object, with the given name. |
| 36 | // The returned object must be freed by calling |data_dispatcher_free|. |
| 37 | // Returns NULL on failure. |name| may not be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame^] | 38 | data_dispatcher_t* data_dispatcher_new(const char* name); |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 39 | |
| 40 | // Frees a data dispatcher object created by |data_dispatcher_new|. |
| 41 | // |data_dispatcher| may be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame^] | 42 | void data_dispatcher_free(data_dispatcher_t* dispatcher); |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 43 | |
| 44 | // Registers |type| and |queue| with the data dispatcher so that data |
| 45 | // sent under |type| ends up in |queue|. If |type| is already registered, |
| 46 | // it is replaced. If |queue| is NULL, the existing registration is |
| 47 | // removed, if it exists. |dispatcher| may not be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame^] | 48 | void data_dispatcher_register(data_dispatcher_t* dispatcher, |
| 49 | data_dispatcher_type_t type, |
| 50 | fixed_queue_t* queue); |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 51 | |
| 52 | // Registers a default queue to send data to when there is not a specific |
| 53 | // type/queue relationship registered. If a default queue is already registered, |
| 54 | // it is replaced. If |queue| is NULL, the existing registration is |
| 55 | // removed, if it exists. |dispatcher| may not be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame^] | 56 | void data_dispatcher_register_default(data_dispatcher_t* dispatcher, |
| 57 | fixed_queue_t* queue); |
Zach Johnson | 3605c80 | 2014-07-29 16:25:02 -0700 | [diff] [blame] | 58 | |
| 59 | // Dispatches |data| to the queue registered for |type|. If no such registration |
| 60 | // exists, it is dispatched to the default queue if it exists. |
| 61 | // Neither |dispatcher| nor |data| may be NULL. |
| 62 | // Returns true if data dispatch was successful. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame^] | 63 | bool data_dispatcher_dispatch(data_dispatcher_t* dispatcher, |
| 64 | data_dispatcher_type_t type, void* data); |
Jakub Pawlowski | 2b56e01 | 2016-05-26 10:13:40 -0700 | [diff] [blame] | 65 | |
| 66 | #ifdef __cplusplus |
| 67 | } |
| 68 | #endif |