2009-02-05 19:32:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of the announcement primitive
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 00:15:42 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rimeannouncement
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-02-05 19:32:01 +00:00
|
|
|
#include "net/rime/announcement.h"
|
|
|
|
#include "lib/list.h"
|
|
|
|
#include "sys/cc.h"
|
|
|
|
|
|
|
|
LIST(announcements);
|
|
|
|
|
|
|
|
static void (* listen_callback)(int time);
|
2010-03-19 13:16:11 +00:00
|
|
|
static announcement_observer observer_callback;
|
2009-02-05 19:32:01 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
announcement_init(void)
|
|
|
|
{
|
|
|
|
list_init(announcements);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-03-25 08:49:56 +00:00
|
|
|
announcement_register(struct announcement *a, uint16_t id,
|
2009-02-05 19:32:01 +00:00
|
|
|
announcement_callback_t callback)
|
|
|
|
{
|
|
|
|
a->id = id;
|
2010-03-25 08:49:56 +00:00
|
|
|
a->has_value = 0;
|
2009-02-05 19:32:01 +00:00
|
|
|
a->callback = callback;
|
|
|
|
list_add(announcements, a);
|
|
|
|
if(observer_callback) {
|
2010-03-25 08:49:56 +00:00
|
|
|
observer_callback(a->id, a->has_value,
|
|
|
|
a->value, 0, ANNOUNCEMENT_BUMP);
|
2009-02-05 19:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
announcement_remove(struct announcement *a)
|
|
|
|
{
|
|
|
|
list_remove(announcements, a);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-03-25 08:49:56 +00:00
|
|
|
announcement_remove_value(struct announcement *a)
|
2009-02-05 19:32:01 +00:00
|
|
|
{
|
2010-03-25 08:49:56 +00:00
|
|
|
a->has_value = 0;
|
2009-02-05 19:32:01 +00:00
|
|
|
if(observer_callback) {
|
2010-03-25 08:49:56 +00:00
|
|
|
observer_callback(a->id, 0, 0, 0, ANNOUNCEMENT_NOBUMP);
|
2009-02-05 19:32:01 +00:00
|
|
|
}
|
2010-03-25 08:49:56 +00:00
|
|
|
|
2009-02-05 19:32:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-03-25 08:49:56 +00:00
|
|
|
announcement_set_value(struct announcement *a, uint16_t value)
|
2009-02-05 19:32:01 +00:00
|
|
|
{
|
2010-03-25 08:49:56 +00:00
|
|
|
uint16_t oldvalue = a->value;
|
2010-06-15 19:22:25 +00:00
|
|
|
|
2010-03-25 08:49:56 +00:00
|
|
|
a->has_value = 1;
|
|
|
|
a->value = value;
|
2009-02-05 19:32:01 +00:00
|
|
|
if(observer_callback) {
|
2010-03-25 08:49:56 +00:00
|
|
|
observer_callback(a->id, a->has_value,
|
|
|
|
value, oldvalue, ANNOUNCEMENT_NOBUMP);
|
2010-03-19 13:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
announcement_bump(struct announcement *a)
|
|
|
|
{
|
|
|
|
if(observer_callback) {
|
2010-03-25 08:49:56 +00:00
|
|
|
observer_callback(a->id, a->has_value,
|
|
|
|
a->value, a->value, ANNOUNCEMENT_BUMP);
|
2009-02-05 19:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
announcement_listen(int time)
|
|
|
|
{
|
|
|
|
if(listen_callback) {
|
|
|
|
listen_callback(time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
announcement_register_listen_callback(void (*callback)(int time))
|
|
|
|
{
|
|
|
|
listen_callback = callback;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2010-03-19 13:16:11 +00:00
|
|
|
announcement_register_observer_callback(announcement_observer callback)
|
2009-02-05 19:32:01 +00:00
|
|
|
{
|
|
|
|
observer_callback = callback;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct announcement *
|
|
|
|
announcement_list(void)
|
|
|
|
{
|
|
|
|
return list_head(announcements);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2013-12-12 22:58:52 +00:00
|
|
|
announcement_heard(const linkaddr_t *from, uint16_t id, uint16_t value)
|
2009-02-05 19:32:01 +00:00
|
|
|
{
|
|
|
|
struct announcement *a;
|
2010-06-15 19:22:25 +00:00
|
|
|
for(a = list_head(announcements); a != NULL; a = list_item_next(a)) {
|
2009-02-05 19:32:01 +00:00
|
|
|
if(a->id == id) {
|
|
|
|
if(a->callback != NULL) {
|
|
|
|
a->callback(a, from, id, value);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|