summaryrefslogtreecommitdiffstats
path: root/gki/ulinux/gki_ulinux.c
blob: a512af910b1d0f01ef2af2c7f9b865ea4d2449da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/******************************************************************************
 *
 *  Copyright (C) 2009-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "bt_gki"

#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <time.h>

#include "btcore/include/module.h"
#include "gki/ulinux/gki_int.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#include "bt_utils.h"

tGKI_CB gki_cb;

static future_t *init(void) {
  memset(&gki_cb, 0, sizeof(gki_cb));

  pthread_mutexattr_t attr;
  pthread_mutexattr_init(&attr);
  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  pthread_mutex_init(&gki_cb.lock, &attr);

  gki_buffer_init();
  return NULL;
}

static future_t *clean_up(void) {
  gki_buffer_cleanup();

  pthread_mutex_destroy(&gki_cb.lock);
  return NULL;
}

// Temp module until GKI dies
const module_t gki_module = {
  .name = GKI_MODULE,
  .init = init,
  .start_up = NULL,
  .shut_down = NULL,
  .clean_up = clean_up,
  .dependencies = {
    NULL
  }
};

UINT32 GKI_get_os_tick_count(void) {
  struct timespec timespec;
  time_now_timespec(&timespec);
  return (timespec.tv_sec * 1000) + (timespec.tv_nsec / 1000000);
}

// Sleep the calling thread unconditionally for |timeout_ms| milliseconds.
void GKI_delay(UINT32 timeout_ms) {
  struct timespec delay;
  delay.tv_sec = timeout_ms / 1000;
  delay.tv_nsec = 1000 * 1000 * (timeout_ms % 1000);

  int err;
  do {
    err = TEMP_FAILURE_RETRY(nanosleep(&delay, &delay));
  } while (err == -1 && errno == EINTR);
}

void GKI_enable(void) {
  pthread_mutex_unlock(&gki_cb.lock);
}

void GKI_disable(void) {
  pthread_mutex_lock(&gki_cb.lock);
}