2007-03-19 00:30:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-05-22 20:53:04 +00:00
|
|
|
* Header file for the energy estimation mechanism
|
2007-03-19 00:30:13 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2013-11-24 15:57:08 +00:00
|
|
|
#ifndef ENERGEST_H_
|
|
|
|
#define ENERGEST_H_
|
2007-03-19 00:30:13 +00:00
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
#include "contiki.h"
|
|
|
|
|
2017-11-01 23:10:56 +00:00
|
|
|
#ifndef ENERGEST_CONF_ON
|
|
|
|
/* Energest is disabled by default */
|
|
|
|
#define ENERGEST_CONF_ON 0
|
|
|
|
#endif /* ENERGEST_CONF_ON */
|
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
#ifndef ENERGEST_CURRENT_TIME
|
|
|
|
#ifdef ENERGEST_CONF_CURRENT_TIME
|
|
|
|
#define ENERGEST_CURRENT_TIME ENERGEST_CONF_CURRENT_TIME
|
|
|
|
#else
|
|
|
|
#define ENERGEST_CURRENT_TIME RTIMER_NOW
|
|
|
|
#define ENERGEST_SECOND RTIMER_SECOND
|
2017-10-31 18:54:23 +00:00
|
|
|
#define ENERGEST_TIME_T rtimer_clock_t
|
2017-10-12 22:08:53 +00:00
|
|
|
#endif /* ENERGEST_CONF_TIME */
|
|
|
|
#endif /* ENERGEST_TIME */
|
|
|
|
|
2017-10-31 18:54:23 +00:00
|
|
|
#ifndef ENERGEST_TIME_T
|
|
|
|
#ifdef ENERGEST_CONF_TIME_T
|
|
|
|
#define ENERGEST_TIME_T ENERGEST_CONF_TIME_T
|
|
|
|
#else
|
|
|
|
#define ENERGEST_TIME_T rtimer_clock_t
|
|
|
|
#endif /* ENERGEST_CONF_TIME_T */
|
|
|
|
#endif /* ENERGEST_TIME_T */
|
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
#ifndef ENERGEST_SECOND
|
|
|
|
#ifdef ENERGEST_CONF_SECOND
|
|
|
|
#define ENERGEST_SECOND ENERGEST_CONF_SECOND
|
|
|
|
#else /* ENERGEST_CONF_SECOND */
|
|
|
|
#define ENERGEST_SECOND RTIMER_SECOND
|
|
|
|
#endif /* ENERGEST_CONF_SECOND */
|
|
|
|
#endif /* ENERGEST_SECOND */
|
2007-05-22 20:53:04 +00:00
|
|
|
|
2017-10-31 18:54:23 +00:00
|
|
|
#ifndef ENERGEST_GET_TOTAL_TIME
|
|
|
|
#ifdef ENERGEST_CONF_GET_TOTAL_TIME
|
|
|
|
#define ENERGEST_GET_TOTAL_TIME ENERGEST_CONF_GET_TOTAL_TIME
|
|
|
|
#else /* ENERGEST_CONF_GET_TOTAL_TIME */
|
|
|
|
#define ENERGEST_GET_TOTAL_TIME energest_get_total_time
|
|
|
|
#endif /* ENERGEST_CONF_GET_TOTAL_TIME */
|
|
|
|
#endif /* ENERGEST_GET_TOTAL_TIME */
|
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
/*
|
|
|
|
* Optional support for more energest types.
|
|
|
|
*
|
|
|
|
* #define ENERGEST_CONF_PLATFORM_ADDITIONS TYPE_NAME1, TYPE_NAME2
|
|
|
|
*
|
|
|
|
* #define ENERGEST_CONF_ADDITIONS TYPE_NAME3, TYPE_NAME4
|
|
|
|
*/
|
|
|
|
typedef enum energest_type {
|
2007-03-19 00:30:13 +00:00
|
|
|
ENERGEST_TYPE_CPU,
|
2007-05-15 07:54:03 +00:00
|
|
|
ENERGEST_TYPE_LPM,
|
2017-10-31 18:54:23 +00:00
|
|
|
ENERGEST_TYPE_DEEP_LPM,
|
2007-03-19 00:30:13 +00:00
|
|
|
ENERGEST_TYPE_TRANSMIT,
|
2007-10-25 09:30:39 +00:00
|
|
|
ENERGEST_TYPE_LISTEN,
|
2008-09-29 11:44:37 +00:00
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
#ifdef ENERGEST_CONF_PLATFORM_ADDITIONS
|
|
|
|
ENERGEST_CONF_PLATFORM_ADDITIONS,
|
|
|
|
#endif /* ENERGEST_CONF_PLATFORM_ADDITIONS */
|
2009-05-11 15:26:24 +00:00
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
#ifdef ENERGEST_CONF_ADDITIONS
|
|
|
|
ENERGEST_CONF_ADDITIONS,
|
|
|
|
#endif /* ENERGEST_CONF_ADDITIONS */
|
2008-09-29 11:44:37 +00:00
|
|
|
|
2007-03-19 00:30:13 +00:00
|
|
|
ENERGEST_TYPE_MAX
|
2017-10-12 22:08:53 +00:00
|
|
|
} energest_type_t;
|
2007-03-19 00:30:13 +00:00
|
|
|
|
|
|
|
void energest_init(void);
|
2008-06-02 13:12:07 +00:00
|
|
|
void energest_flush(void);
|
2007-03-19 00:30:13 +00:00
|
|
|
|
2017-10-31 18:54:23 +00:00
|
|
|
uint64_t ENERGEST_GET_TOTAL_TIME(void);
|
|
|
|
|
2007-03-19 00:30:13 +00:00
|
|
|
#if ENERGEST_CONF_ON
|
2017-10-12 22:08:53 +00:00
|
|
|
|
|
|
|
extern uint64_t energest_total_time[ENERGEST_TYPE_MAX];
|
2017-10-31 18:54:23 +00:00
|
|
|
extern ENERGEST_TIME_T energest_current_time[ENERGEST_TYPE_MAX];
|
2008-06-02 13:12:07 +00:00
|
|
|
extern unsigned char energest_current_mode[ENERGEST_TYPE_MAX];
|
2007-03-19 00:30:13 +00:00
|
|
|
|
2017-10-12 22:08:53 +00:00
|
|
|
static inline uint64_t
|
|
|
|
energest_type_time(energest_type_t type)
|
|
|
|
{
|
|
|
|
return energest_total_time[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
energest_type_set(energest_type_t type, uint64_t value)
|
|
|
|
{
|
|
|
|
energest_total_time[type] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
energest_on(energest_type_t type)
|
|
|
|
{
|
|
|
|
if(energest_current_mode[type] == 0) {
|
|
|
|
energest_current_time[type] = ENERGEST_CURRENT_TIME();
|
|
|
|
energest_current_mode[type] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define ENERGEST_ON(type) energest_on(type)
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
energest_off(energest_type_t type)
|
|
|
|
{
|
|
|
|
if(energest_current_mode[type] != 0) {
|
2017-10-31 18:54:23 +00:00
|
|
|
energest_total_time[type] +=
|
|
|
|
(ENERGEST_TIME_T)(ENERGEST_CURRENT_TIME() - energest_current_time[type]);
|
2017-10-12 22:08:53 +00:00
|
|
|
energest_current_mode[type] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define ENERGEST_OFF(type) energest_off(type)
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
energest_switch(energest_type_t type_off, energest_type_t type_on)
|
|
|
|
{
|
2017-10-31 18:54:23 +00:00
|
|
|
ENERGEST_TIME_T energest_local_variable_now = ENERGEST_CURRENT_TIME();
|
2017-10-12 22:08:53 +00:00
|
|
|
if(energest_current_mode[type_off] != 0) {
|
2017-10-31 18:54:23 +00:00
|
|
|
energest_total_time[type_off] += (ENERGEST_TIME_T)
|
|
|
|
(energest_local_variable_now - energest_current_time[type_off]);
|
2017-10-12 22:08:53 +00:00
|
|
|
energest_current_mode[type_off] = 0;
|
|
|
|
}
|
|
|
|
if(energest_current_mode[type_on] == 0) {
|
|
|
|
energest_current_time[type_on] = energest_local_variable_now;
|
|
|
|
energest_current_mode[type_on] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define ENERGEST_SWITCH(type_off, type_on) energest_switch(type_off, type_on)
|
2008-01-14 16:18:39 +00:00
|
|
|
|
2007-03-19 00:30:13 +00:00
|
|
|
#else /* ENERGEST_CONF_ON */
|
2017-10-12 22:08:53 +00:00
|
|
|
|
|
|
|
static inline uint64_t energest_type_time(energest_type_t type) { return 0; }
|
|
|
|
|
|
|
|
static inline void energest_type_set(energest_type_t type, uint64_t time) { }
|
|
|
|
|
|
|
|
static inline void energest_on(energest_type_t type) { }
|
|
|
|
|
|
|
|
static inline void energest_off(energest_type_t type) { }
|
|
|
|
|
|
|
|
static inline void energest_switch(energest_type_t type_off,
|
|
|
|
energest_type_t type_on)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-03-19 00:30:13 +00:00
|
|
|
#define ENERGEST_ON(type) do { } while(0)
|
|
|
|
#define ENERGEST_OFF(type) do { } while(0)
|
2015-09-03 13:53:10 +00:00
|
|
|
#define ENERGEST_SWITCH(type_off, type_on) do { } while(0)
|
2017-10-12 22:08:53 +00:00
|
|
|
|
2007-03-19 00:30:13 +00:00
|
|
|
#endif /* ENERGEST_CONF_ON */
|
|
|
|
|
2013-11-24 15:57:08 +00:00
|
|
|
#endif /* ENERGEST_H_ */
|