Contiki-NG
energest.h
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  */
32 
33 /**
34  * \file
35  * Header file for the energy estimation mechanism
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #ifndef ENERGEST_H_
41 #define ENERGEST_H_
42 
43 #include "contiki.h"
44 
45 #ifndef ENERGEST_CONF_ON
46 /* Energest is disabled by default */
47 #define ENERGEST_CONF_ON 0
48 #endif /* ENERGEST_CONF_ON */
49 
50 #ifndef ENERGEST_CURRENT_TIME
51 #ifdef ENERGEST_CONF_CURRENT_TIME
52 #define ENERGEST_CURRENT_TIME ENERGEST_CONF_CURRENT_TIME
53 #else
54 #define ENERGEST_CURRENT_TIME RTIMER_NOW
55 #define ENERGEST_SECOND RTIMER_SECOND
56 #define ENERGEST_TIME_T rtimer_clock_t
57 #endif /* ENERGEST_CONF_TIME */
58 #endif /* ENERGEST_TIME */
59 
60 #ifndef ENERGEST_TIME_T
61 #ifdef ENERGEST_CONF_TIME_T
62 #define ENERGEST_TIME_T ENERGEST_CONF_TIME_T
63 #else
64 #define ENERGEST_TIME_T rtimer_clock_t
65 #endif /* ENERGEST_CONF_TIME_T */
66 #endif /* ENERGEST_TIME_T */
67 
68 #ifndef ENERGEST_SECOND
69 #ifdef ENERGEST_CONF_SECOND
70 #define ENERGEST_SECOND ENERGEST_CONF_SECOND
71 #else /* ENERGEST_CONF_SECOND */
72 #define ENERGEST_SECOND RTIMER_SECOND
73 #endif /* ENERGEST_CONF_SECOND */
74 #endif /* ENERGEST_SECOND */
75 
76 #ifndef ENERGEST_GET_TOTAL_TIME
77 #ifdef ENERGEST_CONF_GET_TOTAL_TIME
78 #define ENERGEST_GET_TOTAL_TIME ENERGEST_CONF_GET_TOTAL_TIME
79 #else /* ENERGEST_CONF_GET_TOTAL_TIME */
80 #define ENERGEST_GET_TOTAL_TIME energest_get_total_time
81 #endif /* ENERGEST_CONF_GET_TOTAL_TIME */
82 #endif /* ENERGEST_GET_TOTAL_TIME */
83 
84 /*
85  * Optional support for more energest types.
86  *
87  * #define ENERGEST_CONF_PLATFORM_ADDITIONS TYPE_NAME1, TYPE_NAME2
88  *
89  * #define ENERGEST_CONF_ADDITIONS TYPE_NAME3, TYPE_NAME4
90  */
91 typedef enum energest_type {
92  ENERGEST_TYPE_CPU,
93  ENERGEST_TYPE_LPM,
94  ENERGEST_TYPE_DEEP_LPM,
95  ENERGEST_TYPE_TRANSMIT,
96  ENERGEST_TYPE_LISTEN,
97 
98 #ifdef ENERGEST_CONF_PLATFORM_ADDITIONS
99  ENERGEST_CONF_PLATFORM_ADDITIONS,
100 #endif /* ENERGEST_CONF_PLATFORM_ADDITIONS */
101 
102 #ifdef ENERGEST_CONF_ADDITIONS
103  ENERGEST_CONF_ADDITIONS,
104 #endif /* ENERGEST_CONF_ADDITIONS */
105 
106  ENERGEST_TYPE_MAX
107 } energest_type_t;
108 
109 void energest_init(void);
110 void energest_flush(void);
111 
112 uint64_t ENERGEST_GET_TOTAL_TIME(void);
113 
114 #if ENERGEST_CONF_ON
115 
116 extern uint64_t energest_total_time[ENERGEST_TYPE_MAX];
117 extern ENERGEST_TIME_T energest_current_time[ENERGEST_TYPE_MAX];
118 extern unsigned char energest_current_mode[ENERGEST_TYPE_MAX];
119 
120 static inline uint64_t
121 energest_type_time(energest_type_t type)
122 {
123  return energest_total_time[type];
124 }
125 
126 static inline void
127 energest_type_set(energest_type_t type, uint64_t value)
128 {
129  energest_total_time[type] = value;
130 }
131 
132 static inline void
133 energest_on(energest_type_t type)
134 {
135  if(energest_current_mode[type] == 0) {
136  energest_current_time[type] = ENERGEST_CURRENT_TIME();
137  energest_current_mode[type] = 1;
138  }
139 }
140 #define ENERGEST_ON(type) energest_on(type)
141 
142 static inline void
143 energest_off(energest_type_t type)
144 {
145  if(energest_current_mode[type] != 0) {
146  energest_total_time[type] +=
147  (ENERGEST_TIME_T)(ENERGEST_CURRENT_TIME() - energest_current_time[type]);
148  energest_current_mode[type] = 0;
149  }
150 }
151 #define ENERGEST_OFF(type) energest_off(type)
152 
153 static inline void
154 energest_switch(energest_type_t type_off, energest_type_t type_on)
155 {
156  ENERGEST_TIME_T energest_local_variable_now = ENERGEST_CURRENT_TIME();
157  if(energest_current_mode[type_off] != 0) {
158  energest_total_time[type_off] += (ENERGEST_TIME_T)
159  (energest_local_variable_now - energest_current_time[type_off]);
160  energest_current_mode[type_off] = 0;
161  }
162  if(energest_current_mode[type_on] == 0) {
163  energest_current_time[type_on] = energest_local_variable_now;
164  energest_current_mode[type_on] = 1;
165  }
166 }
167 #define ENERGEST_SWITCH(type_off, type_on) energest_switch(type_off, type_on)
168 
169 #else /* ENERGEST_CONF_ON */
170 
171 static inline uint64_t energest_type_time(energest_type_t type) { return 0; }
172 
173 static inline void energest_type_set(energest_type_t type, uint64_t time) { }
174 
175 static inline void energest_on(energest_type_t type) { }
176 
177 static inline void energest_off(energest_type_t type) { }
178 
179 static inline void energest_switch(energest_type_t type_off,
180  energest_type_t type_on)
181 {
182 }
183 
184 #define ENERGEST_ON(type) do { } while(0)
185 #define ENERGEST_OFF(type) do { } while(0)
186 #define ENERGEST_SWITCH(type_off, type_on) do { } while(0)
187 
188 #endif /* ENERGEST_CONF_ON */
189 
190 #endif /* ENERGEST_H_ */