2016-09-26 09:37:39 -04: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>
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_defs.h"
|
|
|
|
#include "common/bt_trace.h"
|
|
|
|
#include "osi/alarm.h"
|
|
|
|
#include "osi/allocator.h"
|
|
|
|
#include "osi/list.h"
|
2017-11-08 04:12:08 -05:00
|
|
|
#include "esp_timer.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "btc/btc_task.h"
|
|
|
|
#include "btc/btc_alarm.h"
|
|
|
|
#include "osi/mutex.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
typedef struct alarm_t {
|
|
|
|
/* timer id point to here */
|
|
|
|
esp_timer_handle_t alarm_hdl;
|
|
|
|
osi_alarm_callback_t cb;
|
|
|
|
void *cb_data;
|
|
|
|
int64_t deadline_us;
|
|
|
|
} osi_alarm_t;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
enum {
|
|
|
|
ALARM_STATE_IDLE,
|
|
|
|
ALARM_STATE_OPEN,
|
|
|
|
};
|
|
|
|
|
|
|
|
static osi_mutex_t alarm_mutex;
|
|
|
|
static int alarm_state;
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
static struct alarm_t alarm_cbs[ALARM_CBS_NUM];
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
static osi_alarm_err_t alarm_free(osi_alarm_t *alarm);
|
2017-12-25 06:39:37 -05:00
|
|
|
static osi_alarm_err_t alarm_set(osi_alarm_t *alarm, period_ms_t timeout, bool is_periodic);
|
2017-03-09 07:46:19 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
void osi_alarm_init(void)
|
|
|
|
{
|
2017-03-09 07:46:19 -05:00
|
|
|
assert(alarm_mutex != NULL);
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2017-03-09 07:46:19 -05:00
|
|
|
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;
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void osi_alarm_deinit(void)
|
|
|
|
{
|
|
|
|
assert(alarm_mutex != NULL);
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2017-03-09 07:46:19 -05:00
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_WARN("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
goto end;
|
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
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 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct alarm_t *alarm_cbs_lookfor_available(void)
|
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
int i;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
for (i = 0; i < ALARM_CBS_NUM; i++) {
|
|
|
|
if (alarm_cbs[i].alarm_hdl == NULL) { //available
|
2016-12-07 01:11:40 -05:00
|
|
|
LOG_DEBUG("%s %d %p\n", __func__, i, &alarm_cbs[i]);
|
2016-11-24 13:10:15 -05:00
|
|
|
return &alarm_cbs[i];
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
static void alarm_cb_handler(struct alarm_t *alarm)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2017-11-08 04:12:08 -05:00
|
|
|
LOG_DEBUG("TimerID %p\n", alarm);
|
2018-03-28 08:25:57 -04:00
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_WARN("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-14 06:49:30 -04: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 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
osi_alarm_t *osi_alarm_new(const char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2017-03-09 07:46:19 -05:00
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
|
|
|
|
struct alarm_t *timer_id = NULL;
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2017-03-09 07:46:19 -05:00
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
timer_id = NULL;
|
|
|
|
goto end;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
timer_id = alarm_cbs_lookfor_available();
|
2017-03-09 07:46:19 -05:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!timer_id) {
|
2017-03-09 07:46:19 -05:00
|
|
|
LOG_ERROR("%s alarm_cbs exhausted\n", __func__);
|
|
|
|
timer_id = NULL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
esp_timer_create_args_t tca;
|
|
|
|
tca.callback = (esp_timer_cb_t)alarm_cb_handler;
|
|
|
|
tca.arg = timer_id;
|
|
|
|
tca.dispatch_method = ESP_TIMER_TASK;
|
|
|
|
tca.name = alarm_name;
|
2016-11-24 13:10:15 -05:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
timer_id->cb = callback;
|
|
|
|
timer_id->cb_data = data;
|
|
|
|
timer_id->deadline_us = 0;
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
esp_err_t stat = esp_timer_create(&tca, &timer_id->alarm_hdl);
|
|
|
|
if (stat != ESP_OK) {
|
|
|
|
LOG_ERROR("%s failed to create timer, err 0x%x\n", __func__, stat);
|
2017-03-09 07:46:19 -05:00
|
|
|
timer_id = NULL;
|
|
|
|
goto end;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
2016-11-24 13:10:15 -05:00
|
|
|
return timer_id;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
static osi_alarm_err_t alarm_free(osi_alarm_t *alarm)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2017-03-09 07:46:19 -05:00
|
|
|
if (!alarm || alarm->alarm_hdl == NULL) {
|
2016-11-24 13:10:15 -05:00
|
|
|
LOG_ERROR("%s null\n", __func__);
|
2017-11-08 04:12:08 -05:00
|
|
|
return OSI_ALARM_ERR_INVALID_ARG;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
esp_timer_stop(alarm->alarm_hdl);
|
2017-11-08 04:12:08 -05:00
|
|
|
esp_err_t stat = esp_timer_delete(alarm->alarm_hdl);
|
|
|
|
if (stat != ESP_OK) {
|
|
|
|
LOG_ERROR("%s failed to delete timer, err 0x%x\n", __func__, stat);
|
|
|
|
return OSI_ALARM_ERR_FAIL;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
memset(alarm, 0, sizeof(osi_alarm_t));
|
2017-11-08 04:12:08 -05:00
|
|
|
return OSI_ALARM_ERR_PASS;
|
2017-03-09 07:46:19 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
void osi_alarm_free(osi_alarm_t *alarm)
|
2017-03-09 07:46:19 -05:00
|
|
|
{
|
|
|
|
assert(alarm_mutex != NULL);
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2017-03-09 07:46:19 -05:00
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
alarm_free(alarm);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
2017-11-08 04:12:08 -05:00
|
|
|
return;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-12-25 06:39:37 -05:00
|
|
|
static osi_alarm_err_t alarm_set(osi_alarm_t *alarm, period_ms_t timeout, bool is_periodic)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
2017-03-09 07:46:19 -05:00
|
|
|
assert(alarm_mutex != NULL);
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
osi_alarm_err_t ret = OSI_ALARM_ERR_PASS;
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2017-03-09 07:46:19 -05:00
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
2017-11-08 04:12:08 -05:00
|
|
|
ret = OSI_ALARM_ERR_INVALID_STATE;
|
2017-03-09 07:46:19 -05:00
|
|
|
goto end;
|
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
if (!alarm || alarm->alarm_hdl == NULL) {
|
2016-11-24 13:10:15 -05:00
|
|
|
LOG_ERROR("%s null\n", __func__);
|
2017-11-08 04:12:08 -05:00
|
|
|
ret = OSI_ALARM_ERR_INVALID_ARG;
|
2017-03-09 07:46:19 -05:00
|
|
|
goto end;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
int64_t timeout_us = 1000 * (int64_t)timeout;
|
2017-12-25 06:39:37 -05:00
|
|
|
esp_err_t stat;
|
|
|
|
if (is_periodic) {
|
|
|
|
stat = esp_timer_start_periodic(alarm->alarm_hdl, (uint64_t)timeout_us);
|
|
|
|
} else {
|
|
|
|
stat = esp_timer_start_once(alarm->alarm_hdl, (uint64_t)timeout_us);
|
|
|
|
}
|
2017-11-08 04:12:08 -05:00
|
|
|
if (stat != ESP_OK) {
|
|
|
|
LOG_ERROR("%s failed to start timer, err 0x%x\n", __func__, stat);
|
|
|
|
ret = OSI_ALARM_ERR_FAIL;
|
2017-03-09 07:46:19 -05:00
|
|
|
goto end;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2017-12-25 06:39:37 -05:00
|
|
|
alarm->deadline_us = is_periodic ? 0 : (timeout_us + esp_timer_get_time());
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
return ret;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-12-25 06:39:37 -05:00
|
|
|
osi_alarm_err_t osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout)
|
|
|
|
{
|
|
|
|
return alarm_set(alarm, timeout, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
osi_alarm_err_t osi_alarm_set_periodic(osi_alarm_t *alarm, period_ms_t period)
|
|
|
|
{
|
|
|
|
return alarm_set(alarm, period, true);
|
|
|
|
}
|
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
osi_alarm_err_t osi_alarm_cancel(osi_alarm_t *alarm)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
2017-11-08 04:12:08 -05:00
|
|
|
int ret = OSI_ALARM_ERR_PASS;
|
2017-08-17 09:13:45 -04:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2017-03-09 07:46:19 -05:00
|
|
|
if (alarm_state != ALARM_STATE_OPEN) {
|
|
|
|
LOG_ERROR("%s, invalid state %d\n", __func__, alarm_state);
|
2017-11-08 04:12:08 -05:00
|
|
|
ret = OSI_ALARM_ERR_INVALID_STATE;
|
2017-03-09 07:46:19 -05:00
|
|
|
goto end;
|
|
|
|
}
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-03-09 07:46:19 -05:00
|
|
|
if (!alarm || alarm->alarm_hdl == NULL) {
|
2016-11-24 13:10:15 -05:00
|
|
|
LOG_ERROR("%s null\n", __func__);
|
2017-11-08 04:12:08 -05:00
|
|
|
ret = OSI_ALARM_ERR_INVALID_ARG;
|
2017-03-09 07:46:19 -05:00
|
|
|
goto end;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
esp_err_t stat = esp_timer_stop(alarm->alarm_hdl);
|
|
|
|
if (stat != ESP_OK) {
|
|
|
|
LOG_DEBUG("%s failed to stop timer, err 0x%x\n", __func__, stat);
|
|
|
|
ret = OSI_ALARM_ERR_FAIL;
|
2017-03-09 07:46:19 -05:00
|
|
|
goto end;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2017-03-09 07:46:19 -05:00
|
|
|
end:
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
|
|
|
return ret;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm)
|
|
|
|
{
|
2017-11-08 04:12:08 -05:00
|
|
|
assert(alarm_mutex != NULL);
|
|
|
|
int64_t dt_us = 0;
|
2018-03-28 08:25:57 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
osi_mutex_lock(&alarm_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
dt_us = alarm->deadline_us - esp_timer_get_time();
|
|
|
|
osi_mutex_unlock(&alarm_mutex);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-11-08 04:12:08 -05:00
|
|
|
return (dt_us > 0) ? (period_ms_t)(dt_us / 1000) : 0;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
2017-08-17 09:13:45 -04:00
|
|
|
|
|
|
|
uint32_t osi_time_get_os_boottime_ms(void)
|
|
|
|
{
|
2017-11-08 04:12:08 -05:00
|
|
|
return (uint32_t)(esp_timer_get_time() / 1000);
|
2017-08-17 09:13:45 -04:00
|
|
|
}
|