|
Packit |
34410b |
/*
|
|
Packit |
34410b |
*
|
|
Packit |
34410b |
* BlueZ - Bluetooth protocol stack for Linux
|
|
Packit |
34410b |
*
|
|
Packit |
34410b |
* Copyright (C) 2014 Intel Corporation. All rights reserved.
|
|
Packit |
34410b |
*
|
|
Packit |
34410b |
*
|
|
Packit |
34410b |
* This library is free software; you can redistribute it and/or
|
|
Packit |
34410b |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
34410b |
* License as published by the Free Software Foundation; either
|
|
Packit |
34410b |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
34410b |
*
|
|
Packit |
34410b |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
34410b |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
34410b |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
34410b |
* Lesser General Public License for more details.
|
|
Packit |
34410b |
*
|
|
Packit |
34410b |
*/
|
|
Packit |
34410b |
|
|
Packit |
34410b |
#include <stdlib.h>
|
|
Packit |
34410b |
|
|
Packit |
34410b |
#include "mainloop.h"
|
|
Packit |
34410b |
#include "util.h"
|
|
Packit |
34410b |
#include "timeout.h"
|
|
Packit |
34410b |
|
|
Packit |
34410b |
struct timeout_data {
|
|
Packit |
34410b |
int id;
|
|
Packit |
34410b |
timeout_func_t func;
|
|
Packit |
34410b |
timeout_destroy_func_t destroy;
|
|
Packit |
34410b |
unsigned int timeout;
|
|
Packit |
34410b |
void *user_data;
|
|
Packit |
34410b |
};
|
|
Packit |
34410b |
|
|
Packit |
34410b |
static void timeout_callback(int id, void *user_data)
|
|
Packit |
34410b |
{
|
|
Packit |
34410b |
struct timeout_data *data = user_data;
|
|
Packit |
34410b |
|
|
Packit |
34410b |
if (data->func(data->user_data) &&
|
|
Packit |
34410b |
!mainloop_modify_timeout(data->id, data->timeout))
|
|
Packit |
34410b |
return;
|
|
Packit |
34410b |
|
|
Packit |
34410b |
mainloop_remove_timeout(data->id);
|
|
Packit |
34410b |
}
|
|
Packit |
34410b |
|
|
Packit |
34410b |
static void timeout_destroy(void *user_data)
|
|
Packit |
34410b |
{
|
|
Packit |
34410b |
struct timeout_data *data = user_data;
|
|
Packit |
34410b |
|
|
Packit |
34410b |
if (data->destroy)
|
|
Packit |
34410b |
data->destroy(data->user_data);
|
|
Packit |
34410b |
|
|
Packit |
34410b |
free(data);
|
|
Packit |
34410b |
}
|
|
Packit |
34410b |
|
|
Packit |
34410b |
unsigned int timeout_add(unsigned int timeout, timeout_func_t func,
|
|
Packit |
34410b |
void *user_data, timeout_destroy_func_t destroy)
|
|
Packit |
34410b |
{
|
|
Packit |
34410b |
struct timeout_data *data;
|
|
Packit |
34410b |
|
|
Packit |
34410b |
data = new0(struct timeout_data, 1);
|
|
Packit |
34410b |
data->func = func;
|
|
Packit |
34410b |
data->user_data = user_data;
|
|
Packit |
34410b |
data->timeout = timeout;
|
|
Packit |
34410b |
data->destroy = destroy;
|
|
Packit |
34410b |
|
|
Packit |
34410b |
data->id = mainloop_add_timeout(timeout, timeout_callback, data,
|
|
Packit |
34410b |
timeout_destroy);
|
|
Packit |
34410b |
if (data->id < 0) {
|
|
Packit |
34410b |
free(data);
|
|
Packit |
34410b |
return 0;
|
|
Packit |
34410b |
}
|
|
Packit |
34410b |
|
|
Packit |
34410b |
return (unsigned int) data->id;
|
|
Packit |
34410b |
}
|
|
Packit |
34410b |
|
|
Packit |
34410b |
void timeout_remove(unsigned int id)
|
|
Packit |
34410b |
{
|
|
Packit |
34410b |
if (!id)
|
|
Packit |
34410b |
return;
|
|
Packit |
34410b |
|
|
Packit |
34410b |
mainloop_remove_timeout((int) id);
|
|
Packit |
34410b |
}
|