eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

watchlist.c (3421B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2016-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <ell/ell.h>
     28 
     29 #include "src/watchlist.h"
     30 
     31 static bool watchlist_item_match(const void *a, const void *b)
     32 {
     33 	const struct watchlist_item *item = a;
     34 	uint32_t id = L_PTR_TO_UINT(b);
     35 
     36 	return item->id == id;
     37 }
     38 
     39 static void watchlist_item_free(struct watchlist *watchlist,
     40 						struct watchlist_item *item)
     41 {
     42 	if (item->destroy)
     43 		item->destroy(item->notify_data);
     44 
     45 	if (watchlist->ops && watchlist->ops->item_free)
     46 		watchlist->ops->item_free(item);
     47 	else
     48 		l_free(item);
     49 }
     50 
     51 void watchlist_init(struct watchlist *watchlist,
     52 					const struct watchlist_ops *ops)
     53 {
     54 	watchlist->next_id = 0;
     55 	watchlist->items = l_queue_new();
     56 	watchlist->ops = ops;
     57 }
     58 
     59 unsigned int watchlist_link(struct watchlist *watchlist,
     60 					struct watchlist_item *item,
     61 					void *notify, void *notify_data,
     62 					watchlist_item_destroy_func_t destroy)
     63 {
     64 	item->id = ++watchlist->next_id;
     65 	item->notify = notify;
     66 	item->notify_data = notify_data;
     67 	item->destroy = destroy;
     68 
     69 	l_queue_push_tail(watchlist->items, item);
     70 
     71 	return item->id;
     72 }
     73 
     74 unsigned int watchlist_add(struct watchlist *watchlist, void *notify,
     75 					void *notify_data,
     76 					watchlist_item_destroy_func_t destroy)
     77 {
     78 	struct watchlist_item *item;
     79 
     80 	item = l_new(struct watchlist_item, 1);
     81 	return watchlist_link(watchlist, item, notify, notify_data, destroy);
     82 }
     83 
     84 bool watchlist_remove(struct watchlist *watchlist, unsigned int id)
     85 {
     86 	struct watchlist_item *item;
     87 
     88 	if (watchlist->in_notify) {
     89 		item = l_queue_find(watchlist->items, watchlist_item_match,
     90 							L_UINT_TO_PTR(id));
     91 		if (!item)
     92 			return false;
     93 
     94 		item->id = 0;	/* Mark stale */
     95 		watchlist->stale_items = true;
     96 
     97 		return true;
     98 	}
     99 
    100 	item = l_queue_remove_if(watchlist->items, watchlist_item_match,
    101 							L_UINT_TO_PTR(id));
    102 	if (!item)
    103 		return false;
    104 
    105 	watchlist_item_free(watchlist, item);
    106 
    107 	return true;
    108 }
    109 
    110 static void watchlist_clear(struct watchlist *watchlist)
    111 {
    112 	struct watchlist_item *item;
    113 
    114 	while ((item = l_queue_pop_head(watchlist->items)))
    115 		watchlist_item_free(watchlist, item);
    116 }
    117 
    118 void watchlist_destroy(struct watchlist *watchlist)
    119 {
    120 	if (watchlist->in_notify) {
    121 		watchlist->pending_destroy = true;
    122 		return;
    123 	}
    124 
    125 	watchlist_clear(watchlist);
    126 	l_queue_destroy(watchlist->items, NULL);
    127 	watchlist->items = NULL;
    128 }
    129 
    130 void __watchlist_prune_stale(struct watchlist *watchlist)
    131 {
    132 	struct watchlist_item *item;
    133 
    134 	while ((item = l_queue_remove_if(watchlist->items, watchlist_item_match,
    135 							L_UINT_TO_PTR(0))))
    136 		watchlist_item_free(watchlist, item);
    137 
    138 	watchlist->stale_items = false;
    139 }