2016-09-26 13:37:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "bt_defs.h"
|
|
|
|
#include "bt_trace.h"
|
|
|
|
#include "alarm.h"
|
|
|
|
#include "allocator.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "freertos/FreeRTOSConfig.h"
|
|
|
|
#include "freertos/xtensa_api.h"
|
|
|
|
#include "rom/ets_sys.h"
|
2017-04-14 10:49:30 +00:00
|
|
|
#include "btc_task.h"
|
|
|
|
#include "btc_alarm.h"
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-12-07 06:11:40 +00:00
|
|
|
#define RTC_TIMER_TICKS_TO_MS(ticks) (((ticks/625)<<1) + (ticks-(ticks/625)*625)/312)
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
#define BT_ALARM_START_WAIT_TICKS 100
|
|
|
|
#define BT_ALARM_STOP_WAIT_TICKS 100
|
|
|
|
#define BT_ALARM_FREE_WAIT_TICKS 100
|
|
|
|
#define BT_ALARM_CHG_PERIOD_WAIT_TICKS 100
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
enum {
|
|
|
|
ALARM_STATE_IDLE,
|
|
|
|
ALARM_STATE_OPEN,
|
|
|
|
};
|
|
|
|
|
|
|
|
static osi_mutex_t alarm_mutex;
|
|
|
|
static int alarm_state;
|
|
|
|
|
2016-09-26 13:37:39 +00:00
|
|
|
static struct alarm_t alarm_cbs[ALARM_CBS_NUM];
|
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
static int alarm_free(osi_alarm_t *alarm);
|
|
|
|
|
|
|
|
int osi_alarm_create_mux(void)
|
|
|
|
{
|
|
|
|
if (alarm_state != ALARM_STATE_IDLE) {
|
|
|
|
LOG_WARN("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
osi_mutex_new(&alarm_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int osi_alarm_delete_mux(void)
|
|
|
|
{
|
|
|
|
if (alarm_state != ALARM_STATE_IDLE) {
|
|
|
|
LOG_WARN("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
osi_mutex_free(&alarm_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:37:39 +00:00
|
|
|
void osi_alarm_init(void)
|
|
|
|
{
|
2017-03-09 12:46:19 +00:00
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
|
|
|
|
osi_mutex_lock(&alarm_mutex);
|
|
|
|
if (alarm_state != ALARM_STATE_IDLE) {
|
|
|
|
LOG_WARN("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
memset(alarm_cbs, 0x00, sizeof(alarm_cbs));
|
|
|
|
alarm_state = ALARM_STATE_OPEN;
|
|
|
|
|
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void osi_alarm_deinit(void)
|
|
|
|
{
|
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
|
|
|
|
osi_mutex_lock(&alarm_mutex);
|
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_WARN("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ALARM_CBS_NUM; i++) {
|
|
|
|
if (alarm_cbs[i].alarm_hdl != NULL) {
|
|
|
|
alarm_free(&alarm_cbs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alarm_state = ALARM_STATE_IDLE;
|
|
|
|
|
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct alarm_t *alarm_cbs_lookfor_available(void)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
int i;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (i = 0; i < ALARM_CBS_NUM; i++) {
|
|
|
|
if (alarm_cbs[i].alarm_hdl == NULL) { //available
|
2016-12-07 06:11:40 +00:00
|
|
|
LOG_DEBUG("%s %d %p\n", __func__, i, &alarm_cbs[i]);
|
2016-11-24 18:10:15 +00:00
|
|
|
return &alarm_cbs[i];
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return NULL;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void alarm_cb_handler(TimerHandle_t xTimer)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
struct alarm_t *alarm;
|
|
|
|
if (!xTimer) {
|
2016-12-12 20:10:44 +00:00
|
|
|
LOG_ERROR("TimerName: NULL\n");
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-14 10:49:30 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
alarm = pvTimerGetTimerID(xTimer);
|
2016-12-07 06:11:40 +00:00
|
|
|
LOG_DEBUG("TimerID %p, Name %s\n", alarm, pcTimerGetTimerName(xTimer));
|
2017-04-14 10:49:30 +00:00
|
|
|
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_alarm_args_t arg;
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_ALARM;
|
|
|
|
arg.cb = alarm->cb;
|
|
|
|
arg.cb_data = alarm->cb_data;
|
|
|
|
btc_transfer_context(&msg, &arg, sizeof(btc_alarm_args_t), NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
osi_alarm_t *osi_alarm_new(char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire)
|
|
|
|
{
|
2017-03-09 12:46:19 +00:00
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
|
|
|
|
struct alarm_t *timer_id = NULL;
|
|
|
|
|
|
|
|
osi_mutex_lock(&alarm_mutex);
|
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
timer_id = NULL;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2017-03-09 12:46:19 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
timer_id = alarm_cbs_lookfor_available();
|
2017-03-09 12:46:19 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (!timer_id) {
|
2017-03-09 12:46:19 +00:00
|
|
|
LOG_ERROR("%s alarm_cbs exhausted\n", __func__);
|
|
|
|
timer_id = NULL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer_expire == 0) {
|
|
|
|
timer_expire = 1000;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
TimerHandle_t t = xTimerCreate(alarm_name, timer_expire / portTICK_PERIOD_MS, pdFALSE, timer_id, alarm_cb_handler);
|
2016-11-24 18:10:15 +00:00
|
|
|
if (!t) {
|
2017-03-09 12:46:19 +00:00
|
|
|
LOG_ERROR("%s failed to create timer\n", __func__);
|
|
|
|
timer_id = NULL;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timer_id->alarm_hdl = t;
|
|
|
|
timer_id->cb = callback;
|
|
|
|
timer_id->cb_data = data;
|
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
2016-11-24 18:10:15 +00:00
|
|
|
return timer_id;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
static int alarm_free(osi_alarm_t *alarm)
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
2017-03-09 12:46:19 +00:00
|
|
|
if (!alarm || alarm->alarm_hdl == NULL) {
|
2016-11-24 18:10:15 +00:00
|
|
|
LOG_ERROR("%s null\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (xTimerDelete(alarm->alarm_hdl, BT_ALARM_FREE_WAIT_TICKS) != pdPASS) {
|
2017-03-09 12:46:19 +00:00
|
|
|
LOG_ERROR("%s alarm delete error\n", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
return -2;
|
|
|
|
}
|
2017-03-09 12:46:19 +00:00
|
|
|
|
|
|
|
memset(alarm, 0, sizeof(osi_alarm_t));
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
int osi_alarm_free(osi_alarm_t *alarm)
|
|
|
|
{
|
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
osi_mutex_lock(&alarm_mutex);
|
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
ret = -3;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
alarm_free(alarm);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
return ret;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
int osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout)
|
|
|
|
{
|
2017-03-09 12:46:19 +00:00
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
osi_mutex_lock(&alarm_mutex);
|
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
ret = -3;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alarm || alarm->alarm_hdl == NULL) {
|
2016-11-24 18:10:15 +00:00
|
|
|
LOG_ERROR("%s null\n", __func__);
|
2017-03-09 12:46:19 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2017-03-09 12:46:19 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (xTimerChangePeriod(alarm->alarm_hdl, timeout / portTICK_PERIOD_MS, BT_ALARM_CHG_PERIOD_WAIT_TICKS) != pdPASS) {
|
|
|
|
LOG_ERROR("%s chg period error\n", __func__);
|
2017-03-09 12:46:19 +00:00
|
|
|
ret = -2;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xTimerStart(alarm->alarm_hdl, BT_ALARM_START_WAIT_TICKS) != pdPASS) {
|
|
|
|
LOG_ERROR("%s start error\n", __func__);
|
2017-03-09 12:46:19 +00:00
|
|
|
ret = -2;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
return ret;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
int osi_alarm_cancel(osi_alarm_t *alarm)
|
|
|
|
{
|
2017-03-09 12:46:19 +00:00
|
|
|
int ret = 0;
|
|
|
|
osi_mutex_lock(&alarm_mutex);
|
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
ret = -3;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alarm || alarm->alarm_hdl == NULL) {
|
2016-11-24 18:10:15 +00:00
|
|
|
LOG_ERROR("%s null\n", __func__);
|
2017-03-09 12:46:19 +00:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (xTimerStop(alarm->alarm_hdl, BT_ALARM_STOP_WAIT_TICKS) != pdPASS) {
|
2017-03-09 12:46:19 +00:00
|
|
|
LOG_ERROR("%s failed to stop timer\n", __func__);
|
|
|
|
ret = -2;
|
|
|
|
goto end;
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2017-03-09 12:46:19 +00:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
return ret;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static uint32_t alarm_current_tick(void)
|
|
|
|
{
|
|
|
|
return xTaskGetTickCount();
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: this is not accurate
|
|
|
|
// max return value: 0xffffffff / 312 = 13765920 = 0xD20D20
|
2016-11-24 18:10:15 +00:00
|
|
|
period_ms_t osi_alarm_now(void)
|
|
|
|
{
|
|
|
|
return RTC_TIMER_TICKS_TO_MS((alarm_current_tick()));
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
/* TODO: use FreeRTOS timer.c implement ??? */
|
|
|
|
return 0xffffffff;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pre-condition: 0 <= t1, t2 <= 0xD20D20
|
|
|
|
// return value: 0<= ret <=0XD20D20
|
2016-11-24 18:10:15 +00:00
|
|
|
period_ms_t osi_alarm_time_diff(period_ms_t t1, period_ms_t t2)
|
|
|
|
{
|
2016-09-26 13:37:39 +00:00
|
|
|
#define MAX_ALARM_TIME_MS (0xD20D20)
|
2016-11-24 18:10:15 +00:00
|
|
|
int32_t diff = (int32_t)(t1) - (int32_t)(t2);
|
|
|
|
if (diff < 0) {
|
|
|
|
diff += MAX_ALARM_TIME_MS;
|
|
|
|
}
|
|
|
|
return (period_ms_t)diff;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|