stunicast.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rimestunicast
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: stunicast.c,v 1.6 2010/04/30 07:29:31 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Stubborn unicast
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include "net/rime/stunicast.h"
00047 #include "net/rime.h"
00048 #include <string.h>
00049 
00050 #define DEBUG 0
00051 #if DEBUG
00052 #include <stdio.h>
00053 #define PRINTF(...) printf(__VA_ARGS__)
00054 #else
00055 #define PRINTF(...)
00056 #endif
00057 
00058 /*---------------------------------------------------------------------------*/
00059 static void
00060 recv_from_uc(struct unicast_conn *uc, const rimeaddr_t *from)
00061 {
00062   register struct stunicast_conn *c = (struct stunicast_conn *)uc;
00063   PRINTF("%d.%d: stunicast: recv_from_uc from %d.%d\n",
00064          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00065         from->u8[0], from->u8[1]);
00066   if(c->u->recv != NULL) {
00067     c->u->recv(c, from);
00068   }
00069 }
00070 /*---------------------------------------------------------------------------*/
00071 static void
00072 sent_by_uc(struct unicast_conn *uc, int status, int num_tx)
00073 {
00074   register struct stunicast_conn *c = (struct stunicast_conn *)uc;
00075   PRINTF("%d.%d: stunicast: recv_from_uc from %d.%d\n",
00076          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00077          packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
00078          packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1]);
00079   if(c->u->sent != NULL) {
00080     c->u->sent(c, status, num_tx);
00081   }
00082 }
00083 /*---------------------------------------------------------------------------*/
00084 static const struct unicast_callbacks stunicast = {recv_from_uc,
00085                                                    sent_by_uc};
00086 /*---------------------------------------------------------------------------*/
00087 void
00088 stunicast_open(struct stunicast_conn *c, uint16_t channel,
00089           const struct stunicast_callbacks *u)
00090 {
00091   unicast_open(&c->c, channel, &stunicast);
00092   c->u = u;
00093 }
00094 /*---------------------------------------------------------------------------*/
00095 void
00096 stunicast_close(struct stunicast_conn *c)
00097 {
00098   unicast_close(&c->c);
00099   stunicast_cancel(c);
00100 }
00101 /*---------------------------------------------------------------------------*/
00102 rimeaddr_t *
00103 stunicast_receiver(struct stunicast_conn *c)
00104 {
00105   return &c->receiver;
00106 }
00107 /*---------------------------------------------------------------------------*/
00108 static void
00109 send(void *ptr)
00110 {
00111   struct stunicast_conn *c = ptr;
00112 
00113   PRINTF("%d.%d: stunicast: resend to %d.%d\n",
00114          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00115          c->receiver.u8[0], c->receiver.u8[1]);
00116   queuebuf_to_packetbuf(c->buf);
00117   unicast_send(&c->c, &c->receiver);
00118   stunicast_set_timer(c, CLOCK_SECOND);
00119   /*  if(c->u->sent != NULL) {
00120     c->u->sent(c);
00121     }*/
00122 }
00123 /*---------------------------------------------------------------------------*/
00124 void
00125 stunicast_set_timer(struct stunicast_conn *c, clock_time_t t)
00126 {
00127   ctimer_set(&c->t, t, send, c);
00128 }
00129 /*---------------------------------------------------------------------------*/
00130 int
00131 stunicast_send_stubborn(struct stunicast_conn *c, const rimeaddr_t *receiver,
00132                   clock_time_t rxmittime)
00133 {
00134   if(c->buf != NULL) {
00135     queuebuf_free(c->buf);
00136   }
00137   c->buf = queuebuf_new_from_packetbuf();
00138   if(c->buf == NULL) {
00139     return 0;
00140   }
00141   rimeaddr_copy(&c->receiver, receiver);
00142   ctimer_set(&c->t, rxmittime, send, c);
00143 
00144   PRINTF("%d.%d: stunicast_send_stubborn to %d.%d\n",
00145          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00146          c->receiver.u8[0],c->receiver.u8[1]);
00147   unicast_send(&c->c, &c->receiver);
00148   /*  if(c->u->sent != NULL) {
00149     c->u->sent(c);
00150     }*/
00151   
00152   return 1;
00153   
00154 }
00155 /*---------------------------------------------------------------------------*/
00156 int
00157 stunicast_send(struct stunicast_conn *c, const rimeaddr_t *receiver)
00158 {
00159   PRINTF("%d.%d: stunicast_send to %d.%d\n",
00160          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00161          receiver->u8[0], receiver->u8[1]);
00162   return unicast_send(&c->c, receiver);
00163 }
00164 /*---------------------------------------------------------------------------*/
00165 void
00166 stunicast_cancel(struct stunicast_conn *c)
00167 {
00168   ctimer_stop(&c->t);
00169   if(c->buf != NULL) {
00170     queuebuf_free(c->buf);
00171     c->buf = NULL;
00172   }
00173 }
00174 /*---------------------------------------------------------------------------*/
00175 /** @} */

Generated on Mon Apr 11 14:23:31 2011 for Contiki 2.5 by  doxygen 1.6.1