Contiki-NG
Loading...
Searching...
No Matches
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#if ENERGEST_CONF_ON
78#ifdef ENERGEST_CONF_GET_TOTAL_TIME
79#define ENERGEST_GET_TOTAL_TIME ENERGEST_CONF_GET_TOTAL_TIME
80#else /* ENERGEST_CONF_GET_TOTAL_TIME */
81#define ENERGEST_GET_TOTAL_TIME energest_get_total_time
82#endif /* ENERGEST_CONF_GET_TOTAL_TIME */
83#else /* ENERGEST_CONF_ON */
84#define ENERGEST_GET_TOTAL_TIME()
85#endif /* ENERGEST_CONF_ON */
86#endif /* ENERGEST_GET_TOTAL_TIME */
87
88/*
89 * Optional support for more energest types.
90 *
91 * #define ENERGEST_CONF_PLATFORM_ADDITIONS TYPE_NAME1, TYPE_NAME2
92 *
93 * #define ENERGEST_CONF_ADDITIONS TYPE_NAME3, TYPE_NAME4
94 */
95typedef enum energest_type {
96 ENERGEST_TYPE_CPU,
97 ENERGEST_TYPE_LPM,
98 ENERGEST_TYPE_DEEP_LPM,
99 ENERGEST_TYPE_TRANSMIT,
100 ENERGEST_TYPE_LISTEN,
101
102#ifdef ENERGEST_CONF_PLATFORM_ADDITIONS
103 ENERGEST_CONF_PLATFORM_ADDITIONS,
104#endif /* ENERGEST_CONF_PLATFORM_ADDITIONS */
105
106#ifdef ENERGEST_CONF_ADDITIONS
107 ENERGEST_CONF_ADDITIONS,
108#endif /* ENERGEST_CONF_ADDITIONS */
109
110 ENERGEST_TYPE_MAX
111} energest_type_t;
112
113#if ENERGEST_CONF_ON
114
115void energest_init(void);
116void energest_flush(void);
117
118uint64_t ENERGEST_GET_TOTAL_TIME(void);
119
120extern uint64_t energest_total_time[ENERGEST_TYPE_MAX];
121extern ENERGEST_TIME_T energest_current_time[ENERGEST_TYPE_MAX];
122extern bool energest_current_mode[ENERGEST_TYPE_MAX];
123
124static inline uint64_t
125energest_type_time(energest_type_t type)
126{
127 return energest_total_time[type];
128}
129
130static inline void
131energest_type_set(energest_type_t type, uint64_t value)
132{
133 energest_total_time[type] = value;
134}
135
136static inline void
137energest_on(energest_type_t type)
138{
139 if(!energest_current_mode[type]) {
140 energest_current_time[type] = ENERGEST_CURRENT_TIME();
141 energest_current_mode[type] = true;
142 }
143}
144#define ENERGEST_ON(type) energest_on(type)
145
146static inline void
147energest_off(energest_type_t type)
148{
149 if(energest_current_mode[type]) {
150 energest_total_time[type] +=
151 (ENERGEST_TIME_T)(ENERGEST_CURRENT_TIME() - energest_current_time[type]);
152 energest_current_mode[type] = false;
153 }
154}
155#define ENERGEST_OFF(type) energest_off(type)
156
157static inline void
158energest_switch(energest_type_t type_off, energest_type_t type_on)
159{
160 ENERGEST_TIME_T energest_local_variable_now = ENERGEST_CURRENT_TIME();
161 if(energest_current_mode[type_off]) {
162 energest_total_time[type_off] += (ENERGEST_TIME_T)
163 (energest_local_variable_now - energest_current_time[type_off]);
164 energest_current_mode[type_off] = false;
165 }
166 if(!energest_current_mode[type_on]) {
167 energest_current_time[type_on] = energest_local_variable_now;
168 energest_current_mode[type_on] = true;
169 }
170}
171#define ENERGEST_SWITCH(type_off, type_on) energest_switch(type_off, type_on)
172
173#else /* ENERGEST_CONF_ON */
174
175static inline void energest_init(void) { }
176
177static inline void energest_flush(void) { }
178
179static inline uint64_t energest_type_time(energest_type_t type) { return 0; }
180
181static inline void energest_type_set(energest_type_t type, uint64_t time) { }
182
183static inline void energest_on(energest_type_t type) { }
184
185static inline void energest_off(energest_type_t type) { }
186
187static inline void energest_switch(energest_type_t type_off,
188 energest_type_t type_on)
189{
190}
191
192#define ENERGEST_ON(type) do { } while(0)
193#define ENERGEST_OFF(type) do { } while(0)
194#define ENERGEST_SWITCH(type_off, type_on) do { } while(0)
195
196#endif /* ENERGEST_CONF_ON */
197
198#endif /* ENERGEST_H_ */