00001 /** 00002 * \addtogroup rimestbroadcast 00003 * @{ 00004 */ 00005 00006 /* 00007 * Copyright (c) 2006, 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: stbroadcast.c,v 1.3 2009/11/08 19:40:18 adamdunkels Exp $ 00037 */ 00038 00039 /** 00040 * \file 00041 * Implementation of the Rime module Stubborn Anonymous 00042 * BroadCast (stbroadcast) 00043 * \author 00044 * Adam Dunkels <adam@sics.se> 00045 */ 00046 00047 #include "net/rime/stbroadcast.h" 00048 #include "net/rime.h" 00049 #include <string.h> 00050 00051 /*---------------------------------------------------------------------------*/ 00052 static void 00053 recv_from_broadcast(struct broadcast_conn *broadcast, const rimeaddr_t *sender) 00054 { 00055 register struct stbroadcast_conn *c = (struct stbroadcast_conn *)broadcast; 00056 /* DEBUGF(3, "stbroadcast: recv_from_broadcast from %d\n", from_id);*/ 00057 if(c->u->recv != NULL) { 00058 c->u->recv(c); 00059 } 00060 } 00061 /*---------------------------------------------------------------------------*/ 00062 static const struct broadcast_callbacks stbroadcast = {recv_from_broadcast}; 00063 /*---------------------------------------------------------------------------*/ 00064 void 00065 stbroadcast_open(struct stbroadcast_conn *c, uint16_t channel, 00066 const struct stbroadcast_callbacks *u) 00067 { 00068 broadcast_open(&c->c, channel, &stbroadcast); 00069 c->u = u; 00070 } 00071 /*---------------------------------------------------------------------------*/ 00072 void 00073 stbroadcast_close(struct stbroadcast_conn *c) 00074 { 00075 broadcast_close(&c->c); 00076 ctimer_stop(&c->t); 00077 } 00078 /*---------------------------------------------------------------------------*/ 00079 static void 00080 send(void *ptr) 00081 { 00082 struct stbroadcast_conn *c = ptr; 00083 00084 /* DEBUGF(3, "stbroadcast: send()\n");*/ 00085 queuebuf_to_packetbuf(c->buf); 00086 broadcast_send(&c->c); 00087 ctimer_reset(&c->t); 00088 if(c->u->sent != NULL) { 00089 c->u->sent(c); 00090 } 00091 } 00092 /*---------------------------------------------------------------------------*/ 00093 void 00094 stbroadcast_set_timer(struct stbroadcast_conn *c, clock_time_t t) 00095 { 00096 ctimer_set(&c->t, t, send, c); 00097 } 00098 /*---------------------------------------------------------------------------*/ 00099 int 00100 stbroadcast_send_stubborn(struct stbroadcast_conn *c, clock_time_t t) 00101 { 00102 if(c->buf != NULL) { 00103 queuebuf_free(c->buf); 00104 } 00105 c->buf = queuebuf_new_from_packetbuf(); 00106 if(c->buf == NULL) { 00107 return 0; 00108 } 00109 send(c); 00110 stbroadcast_set_timer(c, t); 00111 return 1; 00112 00113 } 00114 /*---------------------------------------------------------------------------*/ 00115 void 00116 stbroadcast_cancel(struct stbroadcast_conn *c) 00117 { 00118 ctimer_stop(&c->t); 00119 } 00120 /*---------------------------------------------------------------------------*/ 00121 /** @} */