Contiki 2.6

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          if(c->buf) {
00117         queuebuf_to_packetbuf(c->buf);
00118         unicast_send(&c->c, &c->receiver);
00119         stunicast_set_timer(c, CLOCK_SECOND);
00120   }
00121   /*  if(c->u->sent != NULL) {
00122     c->u->sent(c);
00123     }*/
00124 }
00125 /*---------------------------------------------------------------------------*/
00126 void
00127 stunicast_set_timer(struct stunicast_conn *c, clock_time_t t)
00128 {
00129   ctimer_set(&c->t, t, send, c);
00130 }
00131 /*---------------------------------------------------------------------------*/
00132 int
00133 stunicast_send_stubborn(struct stunicast_conn *c, const rimeaddr_t *receiver,
00134                   clock_time_t rxmittime)
00135 {
00136   if(c->buf != NULL) {
00137     queuebuf_free(c->buf);
00138   }
00139   c->buf = queuebuf_new_from_packetbuf();
00140   if(c->buf == NULL) {
00141     return 0;
00142   }
00143   rimeaddr_copy(&c->receiver, receiver);
00144   ctimer_set(&c->t, rxmittime, send, c);
00145 
00146   PRINTF("%d.%d: stunicast_send_stubborn to %d.%d\n",
00147          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00148          c->receiver.u8[0],c->receiver.u8[1]);
00149   unicast_send(&c->c, &c->receiver);
00150   /*  if(c->u->sent != NULL) {
00151     c->u->sent(c);
00152     }*/
00153   
00154   return 1;
00155   
00156 }
00157 /*---------------------------------------------------------------------------*/
00158 int
00159 stunicast_send(struct stunicast_conn *c, const rimeaddr_t *receiver)
00160 {
00161   PRINTF("%d.%d: stunicast_send to %d.%d\n",
00162          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00163          receiver->u8[0], receiver->u8[1]);
00164   return unicast_send(&c->c, receiver);
00165 }
00166 /*---------------------------------------------------------------------------*/
00167 void
00168 stunicast_cancel(struct stunicast_conn *c)
00169 {
00170   ctimer_stop(&c->t);
00171   if(c->buf != NULL) {
00172     queuebuf_free(c->buf);
00173     c->buf = NULL;
00174   }
00175 }
00176 /*---------------------------------------------------------------------------*/
00177 /** @} */