Contiki 2.5
energest.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: energest.c,v 1.2 2008/09/29 11:44:37 joxe Exp $
32  */
33 
34 /**
35  * \file
36  * Implementation of the energy estimation module
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 #include "sys/energest.h"
42 #include "contiki-conf.h"
43 
44 #if ENERGEST_CONF_ON
45 
46 int energest_total_count;
47 energest_t energest_total_time[ENERGEST_TYPE_MAX];
48 rtimer_clock_t energest_current_time[ENERGEST_TYPE_MAX];
49 #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
50 energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVICE_LEVELS];
51 #endif
52 unsigned char energest_current_mode[ENERGEST_TYPE_MAX];
53 
54 /*---------------------------------------------------------------------------*/
55 void
56 energest_init(void)
57 {
58  int i;
59  for(i = 0; i < ENERGEST_TYPE_MAX; ++i) {
60  energest_total_time[i].current = energest_current_time[i] = 0;
61  energest_current_mode[i] = 0;
62  }
63 #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
64  for(i = 0; i < ENERGEST_CONF_LEVELDEVICE_LEVELS; ++i) {
65  energest_leveldevice_current_leveltime[i].current = 0;
66  }
67 #endif
68 }
69 /*---------------------------------------------------------------------------*/
70 unsigned long
71 energest_type_time(int type)
72 {
73  /* Note: does not support ENERGEST_CONF_LEVELDEVICE_LEVELS! */
74 #ifndef ENERGEST_CONF_LEVELDEVICE_LEVELS
75  if(energest_current_mode[type]) {
76  rtimer_clock_t now = RTIMER_NOW();
77  energest_total_time[type].current += (rtimer_clock_t)
78  (now - energest_current_time[type]);
79  energest_current_time[type] = now;
80  }
81 #endif /* ENERGEST_CONF_LEVELDEVICE_LEVELS */
82  return energest_total_time[type].current;
83 }
84 /*---------------------------------------------------------------------------*/
85 unsigned long
86 energest_leveldevice_leveltime(int powerlevel)
87 {
88 #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
89  return energest_leveldevice_current_leveltime[powerlevel].current;
90 #else
91  return 0;
92 #endif
93 }
94 /*---------------------------------------------------------------------------*/
95 void
96 energest_type_set(int type, unsigned long val)
97 {
98  energest_total_time[type].current = val;
99 }
100 /*---------------------------------------------------------------------------*/
101 /* Note: does not support ENERGEST_CONF_LEVELDEVICE_LEVELS! */
102 void
103 energest_flush(void)
104 {
105  rtimer_clock_t now;
106  int i;
107  for(i = 0; i < ENERGEST_TYPE_MAX; i++) {
108  if(energest_current_mode[i]) {
109  now = RTIMER_NOW();
110  energest_total_time[i].current += (rtimer_clock_t)
111  (now - energest_current_time[i]);
112  energest_current_time[i] = now;
113  }
114  }
115 }
116 /*---------------------------------------------------------------------------*/
117 #else /* ENERGEST_CONF_ON */
118 void energest_type_set(int type, unsigned long val) {}
119 void energest_init(void) {}
120 unsigned long energest_type_time(int type) { return 0; }
121 void energest_flush(void) {}
122 #endif /* ENERGEST_CONF_ON */