00001 /** 00002 * \addtogroup compower 00003 * @{ 00004 */ 00005 00006 /* 00007 * Copyright (c) 2009, 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: compower.c,v 1.5 2010/06/14 19:19:17 adamdunkels Exp $ 00037 */ 00038 00039 /** 00040 * \file 00041 * Communication power accounting module 00042 * \author 00043 * Adam Dunkels <adam@sics.se> 00044 */ 00045 00046 #include "contiki-conf.h" 00047 #include "sys/energest.h" 00048 #include "sys/compower.h" 00049 #include "net/packetbuf.h" 00050 00051 struct compower_activity compower_idle_activity; 00052 00053 /*---------------------------------------------------------------------------*/ 00054 void 00055 compower_init(void) 00056 { 00057 compower_clear(&compower_idle_activity); 00058 } 00059 /*---------------------------------------------------------------------------*/ 00060 void 00061 compower_accumulate(struct compower_activity *e) 00062 { 00063 static uint32_t last_listen, last_transmit; 00064 uint32_t listen, transmit; 00065 00066 listen = energest_type_time(ENERGEST_TYPE_LISTEN); 00067 e->listen += listen - last_listen; 00068 last_listen = listen; 00069 00070 transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT); 00071 e->transmit += transmit - last_transmit; 00072 last_transmit = transmit; 00073 } 00074 /*---------------------------------------------------------------------------*/ 00075 void 00076 compower_clear(struct compower_activity *e) 00077 { 00078 e->listen = e->transmit = 0; 00079 } 00080 /*---------------------------------------------------------------------------*/ 00081 void 00082 compower_attrconv(struct compower_activity *e) 00083 { 00084 packetbuf_set_attr(PACKETBUF_ATTR_LISTEN_TIME, 00085 packetbuf_attr(PACKETBUF_ATTR_LISTEN_TIME) + e->listen); 00086 packetbuf_set_attr(PACKETBUF_ATTR_TRANSMIT_TIME, 00087 packetbuf_attr(PACKETBUF_ATTR_TRANSMIT_TIME) + e->transmit); 00088 } 00089 /*---------------------------------------------------------------------------*/ 00090 void 00091 compower_accumulate_attrs(struct compower_activity *e) 00092 { 00093 e->listen += packetbuf_attr(PACKETBUF_ATTR_LISTEN_TIME); 00094 e->transmit += packetbuf_attr(PACKETBUF_ATTR_TRANSMIT_TIME); 00095 } 00096 /*---------------------------------------------------------------------------*/ 00097 /** @} */