Repository navigation
Expand file tree
/
Copy pathDevice.cpp
More file actions
229 lines (198 loc) · 5.19 KB
/
Copy pathDevice.cpp
File metadata and controls
229 lines (198 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Device.cpp
*
Copyright (c) 2013 Collin Kidder, Michael Neuweiler, Charles Galpin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "Device.h"
#include "DeviceManager.h"
/**
* Constructor - initialize class variables
*/
Device::Device()
{
prefsHandler = NULL;
commonName = "Generic Device";
deviceConfiguration = NULL;
ready = false;
running = false;
powerOn = false;
}
/**
* Destructor
*/
Device::~Device()
{
}
/**
* Called during initialization of the device.
* May be called multiple times e.g. when recovering from an error
* or disabling and re-enabling the device.
*/
void Device::setup()
{
tickHandler.detach(this);
ready = false;
running = false;
powerOn = false;
loadConfiguration();
Logger::info(this, "device started");
}
/**
* Called during tear-down of the device.
* May be called multiple times e.g. when disabling the device.
*/
void Device::tearDown()
{
tickHandler.detach(this);
ready = false;
running = false;
powerOn = false;
Logger::info(this, "device stopped");
}
/**
* Retrieve the common name of the device.
*/
char* Device::getCommonName()
{
return commonName;
}
/**
* Handle a timer event - called by the TickHandler
*/
void Device::handleTick()
{
}
/**
* Enable the device in the preferences and activate it
*/
void Device::enable()
{
if (isEnabled()) {
return;
}
if (prefsHandler != NULL && prefsHandler->setEnabled(true)) {
prefsHandler->forceCacheWrite(); //just in case someone power cycles quickly
Logger::info(this, "Successfully enabled device %s.(%#x)", commonName, getId());
}
setup();
}
/**
* Deactivate the device and disable it in the preferences
*/
void Device::disable()
{
if (!isEnabled()) {
return;
}
if (prefsHandler != NULL && prefsHandler->setEnabled(false)) {
prefsHandler->forceCacheWrite(); //just in case someone power cycles quickly
Logger::info(this, "Successfully disabled device %s.(%#x)", commonName, getId());
}
tearDown();
}
/**
* Returns if the device is enabled via preferences
*/
bool Device::isEnabled()
{
if (prefsHandler == NULL) {
return true;
}
return prefsHandler->isEnabled();
}
/**
* Is the device itself ready for operation ?
*/
bool Device::isReady()
{
return ready;
}
/**
* Is the device reporting that it's running ?
*/
bool Device::isRunning()
{
return running;
}
/**
* Handle incoming messages from the DeviceManager. A message might
* indicate a change in the system state, a command to reload the configuration
* or other actions.
*/
void Device::handleMessage(uint32_t msgType, void* message)
{
switch (msgType) {
case MSG_SOFT_FAULT:
//TODO: implement action/method for soft fault
break;
case MSG_HARD_FAULT:
//TODO: implement action/method for hard fault
break;
case MSG_DISABLE:
disable();
break;
case MSG_ENABLE:
enable();
break;
case MSG_STATE_CHANGE:
Status::SystemState *state = (Status::SystemState *) message;
handleStateChange(state[0], state[1]);
break;
}
}
/**
* React on state changes.
* Subclasses may overwrite the method but should call the parent.
*/
void Device::handleStateChange(Status::SystemState oldState, Status::SystemState newState)
{
switch (newState) {
case Status::init:
this->setup();
break;
case Status::error: // stop all devices in case of an error
this->tearDown();
break;
case Status::shutdown: // stop all devices
this->tearDown();
break;
}
}
DeviceType Device::getType()
{
return DEVICE_NONE;
}
DeviceId Device::getId()
{
return INVALID;
}
void Device::loadConfiguration()
{
}
void Device::saveConfiguration()
{
}
DeviceConfiguration *Device::getConfiguration()
{
return this->deviceConfiguration;
}
void Device::setConfiguration(DeviceConfiguration *configuration)
{
this->deviceConfiguration = configuration;
}