]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/osi/alarm.c
component/bt: Merge branch 'master' into feature/btdm_a2dp
[esp-idf] / components / bt / bluedroid / osi / alarm.c
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 #include <stdlib.h>
19 #include <string.h>
20 #include <stdbool.h>
21 #include "bt_defs.h"
22 #include "bt_trace.h"
23 #include "alarm.h"
24 #include "allocator.h"
25 #include "list.h"
26 #include "thread.h"
27 #include "freertos/FreeRTOSConfig.h"
28 #include "freertos/xtensa_api.h"
29 #include "rom/ets_sys.h"
30
31 #define RTC_TIMER_TICKS_TO_MS(ticks)            (((ticks/625)<<1) + (ticks-(ticks/625)*625)/312)
32
33
34 #define BT_ALARM_START_WAIT_TICKS   100
35 #define BT_ALARM_STOP_WAIT_TICKS    100
36 #define BT_ALARM_FREE_WAIT_TICKS    100
37 #define BT_ALARM_CHG_PERIOD_WAIT_TICKS  100
38
39 static struct alarm_t alarm_cbs[ALARM_CBS_NUM];
40
41 void osi_alarm_init(void)
42 {
43     memset(&alarm_cbs[0], 0x00, sizeof(alarm_cbs));
44 }
45
46 static struct alarm_t *alarm_cbs_lookfor_available(void)
47 {
48     int i;
49
50     for (i = 0; i < ALARM_CBS_NUM; i++) {
51         if (alarm_cbs[i].alarm_hdl == NULL) { //available
52             LOG_DEBUG("%s %d %p\n", __func__, i, &alarm_cbs[i]);
53             return &alarm_cbs[i];
54         }
55     }
56
57     return NULL;
58 }
59
60 static void alarm_cb_handler(TimerHandle_t xTimer)
61 {
62     struct alarm_t *alarm;
63
64     if (!xTimer) {
65         LOG_ERROR("TimerName: NULL\n");
66         return;
67     }
68
69     alarm = pvTimerGetTimerID(xTimer);
70     LOG_DEBUG("TimerID %p, Name %s\n", alarm, pcTimerGetTimerName(xTimer));
71     if (alarm->cb) {
72         alarm->cb(alarm->cb_data);
73     }
74 }
75
76 osi_alarm_t *osi_alarm_new(char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire, bool is_periodic)
77 {
78     struct alarm_t *timer_id;
79     TimerHandle_t t;
80
81     if (timer_expire == 0) {
82         timer_expire = 1000;
83     }
84
85     /* TODO mutex lock */
86     timer_id = alarm_cbs_lookfor_available();
87     if (!timer_id) {
88         LOG_ERROR("%s full\n", __func__);
89         return NULL;
90     }
91
92     portBASE_TYPE auto_reload = is_periodic ? pdTRUE : pdFALSE;
93     t = xTimerCreate(alarm_name, timer_expire / portTICK_PERIOD_MS, auto_reload, timer_id, alarm_cb_handler);
94     if (!t) {
95         LOG_ERROR("%s error\n", __func__);
96         return NULL;
97     }
98
99     timer_id->alarm_hdl = t;
100     timer_id->cb = callback;
101     timer_id->cb_data = data;
102     /* TODO mutex unlock */
103
104     return timer_id;
105 }
106
107 int osi_alarm_free(osi_alarm_t *alarm)
108 {
109     if (!alarm) {
110         LOG_ERROR("%s null\n", __func__);
111         return -1;
112     }
113
114     if (xTimerDelete(alarm->alarm_hdl, BT_ALARM_FREE_WAIT_TICKS) != pdPASS) {
115         LOG_ERROR("%s error\n", __func__);
116         return -2;
117     }
118
119     /* TODO mutex lock */
120     memset(alarm, 0x00, sizeof(osi_alarm_t));
121     /* TODO mutex unlock */
122
123     return 0;
124 }
125
126
127 int osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout)
128 {
129     if (!alarm) {
130         LOG_ERROR("%s null\n", __func__);
131         return -1;
132     }
133
134     if (xTimerChangePeriod(alarm->alarm_hdl, timeout / portTICK_PERIOD_MS, BT_ALARM_CHG_PERIOD_WAIT_TICKS) != pdPASS) {
135         LOG_ERROR("%s chg period error\n", __func__);
136         return -2;
137     }
138
139     if (xTimerStart(alarm->alarm_hdl, BT_ALARM_START_WAIT_TICKS) != pdPASS) {
140         LOG_ERROR("%s start error\n", __func__);
141         return -3;
142     }
143
144     return 0;
145 }
146
147
148 int osi_alarm_cancel(osi_alarm_t *alarm)
149 {
150     if (!alarm) {
151         LOG_ERROR("%s null\n", __func__);
152         return -1;
153     }
154
155     if (xTimerStop(alarm->alarm_hdl, BT_ALARM_STOP_WAIT_TICKS) != pdPASS) {
156         LOG_ERROR("%s error\n", __func__);
157         return -2;
158     }
159
160     return 0;
161 }
162
163 static uint32_t alarm_current_tick(void)
164 {
165     return xTaskGetTickCount();
166 }
167
168 // todo: this is not accurate
169 // max return value: 0xffffffff / 312 = 13765920 = 0xD20D20
170 period_ms_t osi_alarm_now(void)
171 {
172     return RTC_TIMER_TICKS_TO_MS((alarm_current_tick()));
173 }
174
175 period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm)
176 {
177     /* TODO: use FreeRTOS timer.c implement ??? */
178     return 0xffffffff;
179 }
180
181 // pre-condition: 0 <= t1, t2 <= 0xD20D20
182 // return value: 0<= ret <=0XD20D20
183 period_ms_t osi_alarm_time_diff(period_ms_t t1, period_ms_t t2)
184 {
185 #define MAX_ALARM_TIME_MS     (0xD20D20)
186     int32_t diff = (int32_t)(t1) - (int32_t)(t2);
187     if (diff < 0) {
188         diff += MAX_ALARM_TIME_MS;
189     }
190     return (period_ms_t)diff;
191 }