Contiki 2.6

announcement.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rimeannouncement
00003  * @{
00004  */
00005 
00006 /*
00007  * Copyright (c) 2008, Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of the Institute nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * This file is part of the Contiki operating system.
00035  *
00036  * $Id: announcement.c,v 1.6 2010/06/15 19:22:25 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Implementation of the announcement primitive
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include "net/rime/announcement.h"
00047 #include "lib/list.h"
00048 #include "sys/cc.h"
00049 
00050 LIST(announcements);
00051 
00052 static void (* listen_callback)(int time);
00053 static announcement_observer observer_callback;
00054 
00055 /*---------------------------------------------------------------------------*/
00056 void
00057 announcement_init(void)
00058 {
00059   list_init(announcements);
00060 }
00061 /*---------------------------------------------------------------------------*/
00062 void
00063 announcement_register(struct announcement *a, uint16_t id,
00064                       announcement_callback_t callback)
00065 {
00066   a->id = id;
00067   a->has_value = 0;
00068   a->callback = callback;
00069   list_add(announcements, a);
00070   if(observer_callback) {
00071     observer_callback(a->id, a->has_value,
00072                       a->value, 0, ANNOUNCEMENT_BUMP);
00073   }
00074 }
00075 /*---------------------------------------------------------------------------*/
00076 void
00077 announcement_remove(struct announcement *a)
00078 {
00079   list_remove(announcements, a);
00080 }
00081 /*---------------------------------------------------------------------------*/
00082 void
00083 announcement_remove_value(struct announcement *a)
00084 {
00085   a->has_value = 0;
00086   if(observer_callback) {
00087     observer_callback(a->id, 0, 0, 0, ANNOUNCEMENT_NOBUMP);
00088   }
00089 
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 void
00093 announcement_set_value(struct announcement *a, uint16_t value)
00094 {
00095   uint16_t oldvalue = a->value;
00096 
00097   a->has_value = 1;
00098   a->value = value;
00099   if(observer_callback) {
00100     observer_callback(a->id, a->has_value,
00101                       value, oldvalue, ANNOUNCEMENT_NOBUMP);
00102   }
00103 }
00104 /*---------------------------------------------------------------------------*/
00105 void
00106 announcement_bump(struct announcement *a)
00107 {
00108   if(observer_callback) {
00109     observer_callback(a->id, a->has_value,
00110                       a->value, a->value, ANNOUNCEMENT_BUMP);
00111   }
00112 }
00113 /*---------------------------------------------------------------------------*/
00114 void
00115 announcement_listen(int time)
00116 {
00117   if(listen_callback) {
00118     listen_callback(time);
00119   }
00120 }
00121 /*---------------------------------------------------------------------------*/
00122 void
00123 announcement_register_listen_callback(void (*callback)(int time))
00124 {
00125   listen_callback = callback;
00126 }
00127 /*---------------------------------------------------------------------------*/
00128 void
00129 announcement_register_observer_callback(announcement_observer callback)
00130 {
00131   observer_callback = callback;
00132 }
00133 /*---------------------------------------------------------------------------*/
00134 struct announcement *
00135 announcement_list(void)
00136 {
00137   return list_head(announcements);
00138 }
00139 /*---------------------------------------------------------------------------*/
00140 void
00141 announcement_heard(const rimeaddr_t *from, uint16_t id, uint16_t value)
00142 {
00143   struct announcement *a;
00144   for(a = list_head(announcements); a != NULL; a = list_item_next(a)) {
00145     if(a->id == id) {
00146       if(a->callback != NULL) {
00147         a->callback(a, from, id, value);
00148       }
00149       return;
00150     }
00151   }
00152 }
00153 /*---------------------------------------------------------------------------*/
00154 /** @} */