API change to the announcement module: an annoucement can now be registered without a value. Such announcements are never transmitted to neighbors, until the announcement's value is set.

This commit is contained in:
adamdunkels 2010-03-25 08:49:56 +00:00
parent 20d9f70b09
commit 2559e27414
3 changed files with 32 additions and 25 deletions

View File

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: announcement.c,v 1.4 2010/03/19 13:16:11 adamdunkels Exp $ * $Id: announcement.c,v 1.5 2010/03/25 08:49:56 adamdunkels Exp $
*/ */
/** /**
@ -60,15 +60,16 @@ announcement_init(void)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
announcement_register(struct announcement *a, uint16_t id, uint16_t value, announcement_register(struct announcement *a, uint16_t id,
announcement_callback_t callback) announcement_callback_t callback)
{ {
a->id = id; a->id = id;
a->value = value; a->has_value = 0;
a->callback = callback; a->callback = callback;
list_add(announcements, a); list_add(announcements, a);
if(observer_callback) { if(observer_callback) {
observer_callback(a->id, a->value, 0, ANNOUNCEMENT_BUMP); observer_callback(a->id, a->has_value,
a->value, 0, ANNOUNCEMENT_BUMP);
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -79,21 +80,24 @@ announcement_remove(struct announcement *a)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
announcement_set_value(struct announcement *a, uint16_t value) announcement_remove_value(struct announcement *a)
{ {
uint16_t oldvalue = a->value; a->has_value = 0;
a->value = value;
if(observer_callback) { if(observer_callback) {
observer_callback(a->id, value, oldvalue, ANNOUNCEMENT_NOBUMP); observer_callback(a->id, 0, 0, 0, ANNOUNCEMENT_NOBUMP);
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
announcement_set_id(struct announcement *a, uint16_t id) announcement_set_value(struct announcement *a, uint16_t value)
{ {
a->id = id; uint16_t oldvalue = a->value;
a->has_value = 1;
a->value = value;
if(observer_callback) { if(observer_callback) {
observer_callback(a->id, a->value, a->value, ANNOUNCEMENT_NOBUMP); observer_callback(a->id, a->has_value,
value, oldvalue, ANNOUNCEMENT_NOBUMP);
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -101,7 +105,8 @@ void
announcement_bump(struct announcement *a) announcement_bump(struct announcement *a)
{ {
if(observer_callback) { if(observer_callback) {
observer_callback(a->id, a->value, a->value, ANNOUNCEMENT_BUMP); observer_callback(a->id, a->has_value,
a->value, a->value, ANNOUNCEMENT_BUMP);
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View File

@ -54,7 +54,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: announcement.h,v 1.7 2010/03/19 13:16:11 adamdunkels Exp $ * $Id: announcement.h,v 1.8 2010/03/25 08:49:56 adamdunkels Exp $
*/ */
/** /**
@ -86,6 +86,7 @@ struct announcement {
uint16_t id; uint16_t id;
uint16_t value; uint16_t value;
announcement_callback_t callback; announcement_callback_t callback;
uint8_t has_value;
}; };
/** /**
@ -96,7 +97,6 @@ struct announcement {
* \brief Register an announcement * \brief Register an announcement
* \param a A pointer to a struct announcement * \param a A pointer to a struct announcement
* \param id The identifying number of the announcement * \param id The identifying number of the announcement
* \param value The initial value of the announcement
* \param callback A pointer to a callback function that is called * \param callback A pointer to a callback function that is called
* when an announcement is heard * when an announcement is heard
* *
@ -112,7 +112,7 @@ struct announcement {
* *
*/ */
void announcement_register(struct announcement *a, void announcement_register(struct announcement *a,
uint16_t id, uint16_t value, uint16_t id,
announcement_callback_t callback); announcement_callback_t callback);
/** /**
@ -140,18 +140,18 @@ void announcement_remove(struct announcement *a);
*/ */
void announcement_set_value(struct announcement *a, uint16_t value); void announcement_set_value(struct announcement *a, uint16_t value);
/** /**
* \brief Set the identifier of an announcement * \brief Remove the value of an announcement
* \param a A pointer to a struct announcement that has * \param a A pointer to a struct announcement that has
* previously been registered * previously been registered
* *
* This function sets the identifier of an announcement * This function removes the value of an announcement that
* that has previously been registered with * has previously been registered with
* announcement_register(). * announcement_register().
* *
*/ */
void announcement_set_id(struct announcement *a, uint16_t id); void announcement_remove_value(struct announcement *a);
/** /**
* \brief Bump an announcement * \brief Bump an announcement
@ -244,8 +244,9 @@ enum {
ANNOUNCEMENT_BUMP, ANNOUNCEMENT_BUMP,
}; };
typedef void (* announcement_observer)(uint16_t id, uint16_t newvalue, typedef void (* announcement_observer)(uint16_t id, uint8_t has_value,
uint16_t oldvalue, uint8_t bump); uint16_t newvalue, uint16_t oldvalue,
uint8_t bump);
/** /**
* \brief Register an observer callback with the announcement module * \brief Register an observer callback with the announcement module

View File

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: broadcast-announcement.c,v 1.2 2010/03/22 11:32:29 fros4943 Exp $ * $Id: broadcast-announcement.c,v 1.3 2010/03/25 08:49:56 adamdunkels Exp $
*/ */
/** /**
@ -104,7 +104,7 @@ send_adv(void *ptr)
packetbuf_clear(); packetbuf_clear();
adata = packetbuf_dataptr(); adata = packetbuf_dataptr();
adata->num = 0; adata->num = 0;
for(a = announcement_list(); a != NULL; a = a->next) { for(a = announcement_list(); a != NULL && a->has_value; a = a->next) {
adata->data[adata->num].id = a->id; adata->data[adata->num].id = a->id;
adata->data[adata->num].value = a->value; adata->data[adata->num].value = a->value;
adata->num++; adata->num++;
@ -186,7 +186,8 @@ send_timer(void *ptr)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
new_announcement(uint16_t id, uint16_t newval, uint16_t oldval, uint8_t bump) new_announcement(uint16_t id, uint8_t has_value,
uint16_t newval, uint16_t oldval, uint8_t bump)
{ {
if(bump == ANNOUNCEMENT_BUMP) { if(bump == ANNOUNCEMENT_BUMP) {
c.current_interval = c.initial_interval; c.current_interval = c.initial_interval;