OSDN Git Service

DO NOT MERGE Use POSIX timer API for wake alarms instead of OSI callouts.
[android-x86/system-bt.git] / gki / ulinux / gki_ulinux.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2012 Broadcom Corporation
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 #define LOG_TAG "bt_gki"
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <pthread.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include "btcore/include/module.h"
28 #include "gki/ulinux/gki_int.h"
29 #include "osi/include/log.h"
30 #include "osi/include/osi.h"
31
32 tGKI_CB gki_cb;
33
34 static future_t *init(void) {
35   memset(&gki_cb, 0, sizeof(gki_cb));
36
37   pthread_mutexattr_t attr;
38   pthread_mutexattr_init(&attr);
39   pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
40   pthread_mutex_init(&gki_cb.lock, &attr);
41
42   gki_buffer_init();
43   return NULL;
44 }
45
46 static future_t *clean_up(void) {
47   gki_buffer_cleanup();
48
49   pthread_mutex_destroy(&gki_cb.lock);
50   return NULL;
51 }
52
53 // Temp module until GKI dies
54 const module_t gki_module = {
55   .name = GKI_MODULE,
56   .init = init,
57   .start_up = NULL,
58   .shut_down = NULL,
59   .clean_up = clean_up,
60   .dependencies = {
61     NULL
62   }
63 };
64
65 UINT32 GKI_get_os_tick_count(void) {
66   struct timespec timespec;
67   clock_gettime(CLOCK_BOOTTIME, &timespec);
68   return (timespec.tv_sec * 1000) + (timespec.tv_nsec / 1000000);
69 }
70
71 // Sleep the calling thread unconditionally for |timeout_ms| milliseconds.
72 void GKI_delay(UINT32 timeout_ms) {
73   struct timespec delay;
74   delay.tv_sec = timeout_ms / 1000;
75   delay.tv_nsec = 1000 * 1000 * (timeout_ms % 1000);
76
77   int err;
78   do {
79     err = nanosleep(&delay, &delay);
80   } while (err == -1 && errno == EINTR);
81 }
82
83 void GKI_enable(void) {
84   pthread_mutex_unlock(&gki_cb.lock);
85 }
86
87 void GKI_disable(void) {
88   pthread_mutex_lock(&gki_cb.lock);
89 }