Contiki 2.6
|
00001 00002 /** 00003 * \addtogroup rimeuc 00004 * @{ 00005 */ 00006 00007 /* 00008 * Copyright (c) 2006, Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright 00017 * notice, this list of conditions and the following disclaimer in the 00018 * documentation and/or other materials provided with the distribution. 00019 * 3. Neither the name of the Institute nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00029 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00032 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00033 * SUCH DAMAGE. 00034 * 00035 * This file is part of the Contiki operating system. 00036 * 00037 * $Id: unicast.c,v 1.4 2010/02/23 18:38:05 adamdunkels Exp $ 00038 */ 00039 00040 /** 00041 * \file 00042 * Single-hop unicast 00043 * \author 00044 * Adam Dunkels <adam@sics.se> 00045 */ 00046 00047 #include "net/rime.h" 00048 #include "net/rime/unicast.h" 00049 #include <string.h> 00050 00051 static const struct packetbuf_attrlist attributes[] = 00052 { 00053 UNICAST_ATTRIBUTES 00054 PACKETBUF_ATTR_LAST 00055 }; 00056 00057 #define DEBUG 0 00058 #if DEBUG 00059 #include <stdio.h> 00060 #define PRINTF(...) printf(__VA_ARGS__) 00061 #else 00062 #define PRINTF(...) 00063 #endif 00064 00065 /*---------------------------------------------------------------------------*/ 00066 static void 00067 recv_from_broadcast(struct broadcast_conn *broadcast, const rimeaddr_t *from) 00068 { 00069 struct unicast_conn *c = (struct unicast_conn *)broadcast; 00070 00071 PRINTF("%d.%d: uc: recv_from_broadcast, receiver %d.%d\n", 00072 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], 00073 packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0], 00074 packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]); 00075 if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_node_addr)) { 00076 c->u->recv(c, from); 00077 } 00078 } 00079 /*---------------------------------------------------------------------------*/ 00080 static void 00081 sent_by_broadcast(struct broadcast_conn *broadcast, int status, int num_tx) 00082 { 00083 struct unicast_conn *c = (struct unicast_conn *)broadcast; 00084 00085 PRINTF("%d.%d: uc: sent_by_broadcast, receiver %d.%d\n", 00086 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], 00087 packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0], 00088 packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]); 00089 00090 if(c->u->sent) { 00091 c->u->sent(c, status, num_tx); 00092 } 00093 } 00094 /*---------------------------------------------------------------------------*/ 00095 static const struct broadcast_callbacks uc = {recv_from_broadcast, 00096 sent_by_broadcast}; 00097 /*---------------------------------------------------------------------------*/ 00098 void 00099 unicast_open(struct unicast_conn *c, uint16_t channel, 00100 const struct unicast_callbacks *u) 00101 { 00102 broadcast_open(&c->c, channel, &uc); 00103 c->u = u; 00104 channel_set_attributes(channel, attributes); 00105 } 00106 /*---------------------------------------------------------------------------*/ 00107 void 00108 unicast_close(struct unicast_conn *c) 00109 { 00110 broadcast_close(&c->c); 00111 } 00112 /*---------------------------------------------------------------------------*/ 00113 int 00114 unicast_send(struct unicast_conn *c, const rimeaddr_t *receiver) 00115 { 00116 PRINTF("%d.%d: unicast_send to %d.%d\n", 00117 rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], 00118 receiver->u8[0], receiver->u8[1]); 00119 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, receiver); 00120 return broadcast_send(&c->c); 00121 } 00122 /*---------------------------------------------------------------------------*/ 00123 /** @} */