00001 /** 00002 * \addtogroup rimelinkestimate 00003 * @{ 00004 */ 00005 /* 00006 * Copyright (c) 2010, Swedish Institute of Computer Science. 00007 * All rights reserved. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 2. Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the distribution. 00017 * 3. Neither the name of the Institute nor the names of its contributors 00018 * may be used to endorse or promote products derived from this software 00019 * without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 * 00033 * This file is part of the Contiki operating system. 00034 * 00035 * $Id: collect-link-estimate.c,v 1.6 2011/01/09 21:14:22 adamdunkels Exp $ 00036 */ 00037 00038 /** 00039 * \file 00040 * Implementation of Collect link estimate based on ETX 00041 * \author 00042 * Adam Dunkels <adam@sics.se> 00043 */ 00044 00045 #include "net/rime/collect.h" 00046 #include "net/rime/collect-link-estimate.h" 00047 00048 #define INITIAL_LINK_ESTIMATE 16 00049 00050 #define COLLECT_LINK_ESTIMATE_ALPHA ((3 * (COLLECT_LINK_ESTIMATE_UNIT)) / 8) 00051 00052 #define MAX_ESTIMATES 255 00053 00054 #define DEBUG 0 00055 #if DEBUG 00056 #include <stdio.h> 00057 #define PRINTF(...) printf(__VA_ARGS__) 00058 #else 00059 #define PRINTF(...) 00060 #endif 00061 00062 /*---------------------------------------------------------------------------*/ 00063 void 00064 collect_link_estimate_new(struct collect_link_estimate *le) 00065 { 00066 le->num_estimates = 0; 00067 le->etx_accumulator = COLLECT_LINK_ESTIMATE_UNIT; 00068 } 00069 /*---------------------------------------------------------------------------*/ 00070 void 00071 collect_link_estimate_update_tx(struct collect_link_estimate *le, uint8_t tx) 00072 { 00073 if(tx == 0) { 00074 /* printf("ERROR tx == 0\n");*/ 00075 return; 00076 } 00077 if(le != NULL) { 00078 if(le->num_estimates == 0) { 00079 le->etx_accumulator = tx * COLLECT_LINK_ESTIMATE_UNIT; 00080 } 00081 00082 if(le->num_estimates < MAX_ESTIMATES) { 00083 le->num_estimates++; 00084 } 00085 00086 le->etx_accumulator = (((uint32_t)tx * COLLECT_LINK_ESTIMATE_UNIT) * 00087 COLLECT_LINK_ESTIMATE_ALPHA + 00088 le->etx_accumulator * (COLLECT_LINK_ESTIMATE_UNIT - 00089 COLLECT_LINK_ESTIMATE_ALPHA)) / 00090 COLLECT_LINK_ESTIMATE_UNIT; 00091 00092 } 00093 } 00094 /*---------------------------------------------------------------------------*/ 00095 void 00096 collect_link_estimate_update_tx_fail(struct collect_link_estimate *le, 00097 uint8_t tx) 00098 { 00099 collect_link_estimate_update_tx(le, tx * 2); 00100 } 00101 /*---------------------------------------------------------------------------*/ 00102 void 00103 collect_link_estimate_update_rx(struct collect_link_estimate *n) 00104 { 00105 00106 } 00107 /*---------------------------------------------------------------------------*/ 00108 uint16_t 00109 collect_link_estimate(struct collect_link_estimate *le) 00110 { 00111 if(le->num_estimates == 0) { 00112 return INITIAL_LINK_ESTIMATE * COLLECT_LINK_ESTIMATE_UNIT; 00113 } 00114 00115 return le->etx_accumulator; 00116 } 00117 /*---------------------------------------------------------------------------*/ 00118 int 00119 collect_link_estimate_num_estimates(struct collect_link_estimate *le) 00120 { 00121 if(le != NULL) { 00122 return le->num_estimates; 00123 } 00124 return 0; 00125 } 00126 /*---------------------------------------------------------------------------*/ 00127 /** @} */