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 "base.h"
22
23#include <semaphore.h>
24
25#define WAIT(callback) \
26  do { \
27    sem_t *semaphore = callbacks_get_semaphore(#callback); \
28    sem_wait(semaphore); \
29  } while (0)
30
31#define CALL_AND_WAIT(expression, callback) \
32  do { \
33    sem_t *semaphore = callbacks_get_semaphore(#callback); \
34    while (!sem_trywait(semaphore)); \
35    expression; \
36    sem_wait(semaphore); \
37  } while(0)
38
39// To be called from every exit point of the callback. This macro
40// takes 0 or 1 arguments, the return value of the callback.
41#define CALLBACK_RET(...) do { \
42    sem_t *semaphore = callbacks_get_semaphore(__func__); \
43    sem_post(semaphore); \
44    return __VA_ARGS__; \
45  } while (0)
46
47void callbacks_init();
48void callbacks_cleanup();
49
50bt_callbacks_t *callbacks_get_adapter_struct();
51btpan_callbacks_t *callbacks_get_pan_struct();
52sem_t *callbacks_get_semaphore(const char *name);
53