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 <errno.h> 24 #include <semaphore.h> 25 #include <unistd.h> 26 27 #define WAIT(callback) \ 28 do { \ 29 sem_t *semaphore = callbacks_get_semaphore(#callback); \ 30 TEMP_FAILURE_RETRY(sem_wait(semaphore)); \ 31 } 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; \ 38 TEMP_FAILURE_RETRY(sem_wait(semaphore)); \ 39 } 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 49 void callbacks_init(); 50 void callbacks_cleanup(); 51 52 bt_callbacks_t *callbacks_get_adapter_struct(); 53 btpan_callbacks_t *callbacks_get_pan_struct(); 54 btgatt_callbacks_t *callbacks_get_gatt_struct(); 55 sem_t *callbacks_get_semaphore(const char *name); 56