StateEnergyCommunicationComponent.java 4.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

21
package de.tud.kom.p2psim.impl.energy.components;
22
23
24
25
26
27

import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyCommunicationComponent;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
import de.tud.kom.p2psim.api.energy.EnergyState;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
28
import de.tud.kom.p2psim.impl.energy.DefaultEnergyState;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import de.tudarmstadt.maki.simonstrator.api.Message;
import de.tudarmstadt.maki.simonstrator.api.Time;

/**
 * This is the implementation of a communication component that does care about
 * a state - it just consumes energy as soon as something is being sent or
 * received.
 * 
 * 
 * @author Christoph Muenker
 * @version 1.0, 07.02.2013
 */
public class StateEnergyCommunicationComponent implements
		EnergyCommunicationComponent {

	private PhyType phy;

	private EnergyEventListener energyModel;

	/**
	 * States supported by this component
	 */
	private final EnergyState SEND, IDLE, RECV, OFF, SLEEP;

	private EnergyState currentState;

	private long lastStateChange;

	/*
	 * Parameters from SWB-B23 Datasheet - Broadcom BCM4329 WLAN+BT Solution
	 */

	public static double volt = 3.3; // in volt

	public static double sendAmp = 0.2525; // in ampere

	public static double recvAmp = 0.07783; // in ampere

	public static double idleAmp = 0.0004909; // in ampere

	public static double sleepAmp = 0.000125; // in ampere

	/**
	 * Create a stateless Component
	 * 
	 * @param phy
	 */
	public StateEnergyCommunicationComponent(PhyType phy) {
		this.phy = phy;

		IDLE = new DefaultEnergyState("IDLE", (idleAmp * volt) * 1000000);
		SEND = new DefaultEnergyState("SEND", (sendAmp * volt) * 1000000);
		RECV = new DefaultEnergyState("RECV", (recvAmp * volt) * 1000000);
		OFF = new DefaultEnergyState("OFF", 0);
		SLEEP = new DefaultEnergyState("SLEEP", (sleepAmp * volt) * 1000000);

		this.currentState = IDLE;
		this.lastStateChange = Time.getCurrentTime();
	}

	@Override
90
	public boolean turnOff() {
91
92
93
		if (!currentState.equals(OFF)) {
			doStateChange(OFF);
		}
94
		return true;
95
96
97
98
	}

	@Override
	public boolean turnOn() {
99
		if (energyModel.componentCanBeActivated(this)) {
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
			if (!currentState.equals(IDLE)) {
				doStateChange(IDLE);
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean isOn() {
		return !currentState.equals(OFF);
	}

	private void doStateChange(EnergyState newState) {
		long timeSpentInState = Time.getCurrentTime() - lastStateChange;
		timeSpentInState = Math.max(0, timeSpentInState);
116
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
		currentState = newState;
		lastStateChange = Time.getCurrentTime();
	}

	/**
	 * Just to ensure that the energy calculation is correct, once you rely on
	 * the energy level in an analyzer you should call this method prior to
	 * querying the battery.
	 */
	public void doFakeStateChange() {
		doStateChange(currentState);
	}

	@Override
	public void receive(long duration, Message msg, boolean isBroadcast,
			boolean isIntendedReceiver) {

		assert !currentState.equals(OFF);
		doStateChange(RECV);
		// we do this change manually, because we can save the event for the end
		// receive!
138
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(RECV, duration));		
139
140
141
142
143
144
145
146
147
148
149
		currentState = IDLE;
		lastStateChange = Time.getCurrentTime() + duration;
	}

	@Override
	public void send(long duration, Message msg, boolean isBroadcast) {

		assert !currentState.equals(OFF);
		doStateChange(SEND);
		// we do this change manually, because we can save the event for the end
		// receive!
150
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(SEND, duration));		
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
		currentState = IDLE;
		lastStateChange = Time.getCurrentTime() + duration;
	}

	@Override
	public ComponentType getType() {
		return ComponentType.COMMUNICATION;
	}

	@Override
	public PhyType getPhyType() {
		return phy;
	}

	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;
	}

	@Override
	public void eventOccurred(Object content, int type) {
		// will not happen!
	}

	@Override
	public String toString() {
		return phy.toString();
	}

}