2016-09-14 07:16:04 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
2019-10-25 00:43:08 +02:00
|
|
|
* Copyright (C) 2016-2019 Intel Corporation. All rights reserved.
|
2016-09-14 07:16:04 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2018-10-26 21:33:01 +02:00
|
|
|
#include "src/watchlist.h"
|
2016-09-14 07:16:04 +02:00
|
|
|
|
|
|
|
static bool watchlist_item_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct watchlist_item *item = a;
|
|
|
|
uint32_t id = L_PTR_TO_UINT(b);
|
|
|
|
|
|
|
|
return item->id == id;
|
|
|
|
}
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
static void __watchlist_item_free(struct watchlist *watchlist,
|
|
|
|
struct watchlist_item *item)
|
2016-09-14 07:16:04 +02:00
|
|
|
{
|
|
|
|
if (item->destroy)
|
|
|
|
item->destroy(item->notify_data);
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
if (watchlist->ops && watchlist->ops->item_free)
|
|
|
|
watchlist->ops->item_free(item);
|
|
|
|
else
|
|
|
|
l_free(item);
|
2016-09-14 07:16:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
struct watchlist *watchlist_new(const struct watchlist_ops *ops)
|
2016-09-14 07:16:04 +02:00
|
|
|
{
|
|
|
|
struct watchlist *watchlist;
|
|
|
|
|
|
|
|
watchlist = l_new(struct watchlist, 1);
|
|
|
|
watchlist->items = l_queue_new();
|
2017-09-01 00:57:16 +02:00
|
|
|
watchlist->ops = ops;
|
2016-09-14 07:16:04 +02:00
|
|
|
return watchlist;
|
|
|
|
}
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
void watchlist_init(struct watchlist *watchlist,
|
|
|
|
const struct watchlist_ops *ops)
|
2016-09-14 07:16:04 +02:00
|
|
|
{
|
|
|
|
watchlist->next_id = 0;
|
|
|
|
watchlist->items = l_queue_new();
|
2017-09-01 00:57:16 +02:00
|
|
|
watchlist->ops = ops;
|
2016-09-14 07:16:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
unsigned int watchlist_link(struct watchlist *watchlist,
|
|
|
|
struct watchlist_item *item,
|
|
|
|
void *notify, void *notify_data,
|
2016-09-14 07:16:04 +02:00
|
|
|
watchlist_item_destroy_func_t destroy)
|
|
|
|
{
|
|
|
|
item->id = ++watchlist->next_id;
|
|
|
|
item->notify = notify;
|
|
|
|
item->notify_data = notify_data;
|
|
|
|
item->destroy = destroy;
|
|
|
|
|
|
|
|
l_queue_push_tail(watchlist->items, item);
|
|
|
|
|
|
|
|
return item->id;
|
|
|
|
}
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
unsigned int watchlist_add(struct watchlist *watchlist, void *notify,
|
|
|
|
void *notify_data,
|
|
|
|
watchlist_item_destroy_func_t destroy)
|
|
|
|
{
|
|
|
|
struct watchlist_item *item;
|
|
|
|
|
|
|
|
item = l_new(struct watchlist_item, 1);
|
|
|
|
return watchlist_link(watchlist, item, notify, notify_data, destroy);
|
|
|
|
}
|
|
|
|
|
2016-09-14 07:16:04 +02:00
|
|
|
bool watchlist_remove(struct watchlist *watchlist, unsigned int id)
|
|
|
|
{
|
|
|
|
struct watchlist_item *item;
|
|
|
|
|
2016-09-22 22:28:29 +02:00
|
|
|
if (watchlist->in_notify) {
|
|
|
|
item = l_queue_find(watchlist->items, watchlist_item_match,
|
|
|
|
L_UINT_TO_PTR(id));
|
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
item->id = 0; /* Mark stale */
|
|
|
|
watchlist->stale_items = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-14 07:16:04 +02:00
|
|
|
item = l_queue_remove_if(watchlist->items, watchlist_item_match,
|
|
|
|
L_UINT_TO_PTR(id));
|
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
__watchlist_item_free(watchlist, item);
|
2016-09-14 07:16:04 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-01 00:57:16 +02:00
|
|
|
static void __watchlist_clear(struct watchlist *watchlist)
|
|
|
|
{
|
|
|
|
struct watchlist_item *item;
|
|
|
|
|
|
|
|
while ((item = l_queue_pop_head(watchlist->items)))
|
|
|
|
__watchlist_item_free(watchlist, item);
|
|
|
|
}
|
|
|
|
|
2016-09-14 07:16:04 +02:00
|
|
|
void watchlist_destroy(struct watchlist *watchlist)
|
|
|
|
{
|
2017-09-01 00:57:16 +02:00
|
|
|
__watchlist_clear(watchlist);
|
2017-10-19 02:29:24 +02:00
|
|
|
l_queue_destroy(watchlist->items, NULL);
|
2016-09-14 07:16:04 +02:00
|
|
|
watchlist->items = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void watchlist_free(struct watchlist *watchlist)
|
|
|
|
{
|
2017-09-01 00:57:16 +02:00
|
|
|
__watchlist_clear(watchlist);
|
2017-10-19 02:29:24 +02:00
|
|
|
l_queue_destroy(watchlist->items, NULL);
|
2016-09-14 07:16:04 +02:00
|
|
|
l_free(watchlist);
|
|
|
|
}
|
2016-09-22 22:28:29 +02:00
|
|
|
|
|
|
|
void __watchlist_prune_stale(struct watchlist *watchlist)
|
|
|
|
{
|
|
|
|
struct watchlist_item *item;
|
|
|
|
|
|
|
|
while ((item = l_queue_remove_if(watchlist->items, watchlist_item_match,
|
2017-09-01 00:57:16 +02:00
|
|
|
L_UINT_TO_PTR(0))))
|
|
|
|
__watchlist_item_free(watchlist, item);
|
2016-09-22 22:28:29 +02:00
|
|
|
|
|
|
|
watchlist->stale_items = false;
|
|
|
|
}
|