blob: 2ef35156ca4de133b7bf36005022e3a7eab41ce9 [file] [log] [blame]
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -07001/******************************************************************************
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
Sharvil Nanavatiae973d52016-06-20 19:16:12 -070023#include <errno.h>
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070024#include <semaphore.h>
Sharvil Nanavatiae973d52016-06-20 19:16:12 -070025#include <unistd.h>
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070026
27#define WAIT(callback) \
28 do { \
29 sem_t *semaphore = callbacks_get_semaphore(#callback); \
Sharvil Nanavatiae973d52016-06-20 19:16:12 -070030 TEMP_FAILURE_RETRY(sem_wait(semaphore)); \
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070031 } while (0)
32
33#define CALL_AND_WAIT(expression, callback) \
34 do { \
35 sem_t *semaphore = callbacks_get_semaphore(#callback); \
36 while (!sem_trywait(semaphore)); \
37 expression; \
Sharvil Nanavatiae973d52016-06-20 19:16:12 -070038 TEMP_FAILURE_RETRY(sem_wait(semaphore)); \
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070039 } while(0)
40
41// To be called from every exit point of the callback. This macro
42// takes 0 or 1 arguments, the return value of the callback.
43#define CALLBACK_RET(...) do { \
44 sem_t *semaphore = callbacks_get_semaphore(__func__); \
45 sem_post(semaphore); \
46 return __VA_ARGS__; \
47 } while (0)
48
49void callbacks_init();
50void callbacks_cleanup();
51
52bt_callbacks_t *callbacks_get_adapter_struct();
53btpan_callbacks_t *callbacks_get_pan_struct();
Steve Guc8497632014-10-27 12:59:02 -070054btgatt_callbacks_t *callbacks_get_gatt_struct();
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070055sem_t *callbacks_get_semaphore(const char *name);