forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEDControl.cpp
297 lines (262 loc) · 12 KB
/
LEDControl.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "LEDControl.h"
#include <algorithm>
#include "rdk/iarmmgrs-hal/pwrMgr.h"
#include "UtilsJsonRpc.h"
#include "UtilsIarm.h"
#include "dsFPD.h"
#define API_VERSION_NUMBER_MAJOR 1
#define API_VERSION_NUMBER_MINOR 0
#define API_VERSION_NUMBER_PATCH 1
#define FPD_LED_DEVICE_NONE "NONE"
#define FPD_LED_DEVICE_ACTIVE "ACTIVE"
#define FPD_LED_DEVICE_STANDBY "STANDBY"
#define FPD_LED_DEVICE_WPS_CONNECTING "WPS_CONNECTING"
#define FPD_LED_DEVICE_WPS_CONNECTED "WPS_CONNECTED"
#define FPD_LED_DEVICE_WPS_ERROR "WPS_ERROR"
#define FPD_LED_DEVICE_FACTORY_RESET "FACTORY_RESET"
#define FPD_LED_DEVICE_USB_UPGRADE "USB_UPGRADE"
#define FPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR "DOWNLOAD_ERROR"
namespace WPEFramework
{
namespace {
static Plugin::Metadata<Plugin::LEDControl> metadata(
// Version (Major, Minor, Patch)
API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH,
// Preconditions
{},
// Terminations
{},
// Controls
{}
);
}
namespace Plugin
{
SERVICE_REGISTRATION(LEDControl, API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH);
LEDControl* LEDControl::_instance = nullptr;
LEDControl::LEDControl()
: PluginHost::JSONRPC(),
m_isPlatInitialized (false)
{
LOGWARN("ctor");
LEDControl::_instance = this;
Register("getSupportedLEDStates", &LEDControl::getSupportedLEDStates, this);
Register("getLEDState", &LEDControl::getLEDState, this);
Register("setLEDState", &LEDControl::setLEDState, this);
}
LEDControl::~LEDControl()
{
}
const string LEDControl::Initialize(PluginHost::IShell* /* service */)
{
string msg;
if (!m_isPlatInitialized){
LOGINFO("Doing plat init");
if (dsERR_NONE != dsFPInit()){
msg = "dsFPInit failed";
LOGERR("dsFPInit failed");
return msg;
}
m_isPlatInitialized = true;
}
// On success return empty, to indicate there is no error text.
return msg;
}
void LEDControl::Deinitialize(PluginHost::IShell* /* service */)
{
LEDControl::_instance = nullptr;
if (m_isPlatInitialized){
LOGINFO("Doing plat uninit");
dsFPTerm();
m_isPlatInitialized = false;
}
}
void LEDControl::setResponseArray(JsonObject& response, const char* key, const vector<string>& items)
{
JsonArray arr;
for(auto& i : items) arr.Add(JsonValue(i));
response[key] = arr;
string json;
response.ToString(json);
}
/**
* @brief This method returns all the led states supported by the platform.
*
* @param: None.
* @return Returns the success code of underlying method.
*/
uint32_t LEDControl::getSupportedLEDStates(const JsonObject& parameters, JsonObject& response)
{
bool success = false;
vector<string> supportedLEDStates;
try {
unsigned int states = dsFPD_LED_DEVICE_NONE;
dsError_t err = dsFPGetSupportedLEDStates (&states);
if (!err) {
if(!states)supportedLEDStates.emplace_back(FPD_LED_DEVICE_NONE);
if(states & (1<<dsFPD_LED_DEVICE_ACTIVE))supportedLEDStates.emplace_back(FPD_LED_DEVICE_ACTIVE);
if(states & (1<<dsFPD_LED_DEVICE_STANDBY))supportedLEDStates.emplace_back(FPD_LED_DEVICE_STANDBY);
if(states & (1<<dsFPD_LED_DEVICE_WPS_CONNECTING))supportedLEDStates.emplace_back(FPD_LED_DEVICE_WPS_CONNECTING);
if(states & (1<<dsFPD_LED_DEVICE_WPS_CONNECTED))supportedLEDStates.emplace_back(FPD_LED_DEVICE_WPS_CONNECTED);
if(states & (1<<dsFPD_LED_DEVICE_WPS_ERROR))supportedLEDStates.emplace_back(FPD_LED_DEVICE_WPS_ERROR);
if(states & (1<<dsFPD_LED_DEVICE_FACTORY_RESET))supportedLEDStates.emplace_back(FPD_LED_DEVICE_FACTORY_RESET);
if(states & (1<<dsFPD_LED_DEVICE_USB_UPGRADE))supportedLEDStates.emplace_back(FPD_LED_DEVICE_USB_UPGRADE);
if(states & (1<<dsFPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR))supportedLEDStates.emplace_back(FPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR);
success = true;
} else {
LOGERR("dsFPGetSupportedLEDStates returned error %d", err);
}
} catch (...){
LOGERR("Exception in supportedLEDStates");
}
setResponseArray(response, "supportedLEDStates", supportedLEDStates);
returnResponse(success);
}
/**
* @brief This method returns current led state of the platform.
*
* @param: None.
* @return Returns the success code of underlying method.
*/
uint32_t LEDControl::getLEDState(const JsonObject& parameters, JsonObject& response)
{
bool success = false;
try
{
dsFPDLedState_t state;
dsError_t err = dsFPGetLEDState (&state);
if (!err) {
success = true;
switch (state) {
case dsFPD_LED_DEVICE_NONE:
response["state"] = FPD_LED_DEVICE_NONE;
break;
case dsFPD_LED_DEVICE_ACTIVE:
response["state"] = FPD_LED_DEVICE_ACTIVE;
break;
case dsFPD_LED_DEVICE_STANDBY:
response["state"] = FPD_LED_DEVICE_STANDBY;
break;
case dsFPD_LED_DEVICE_WPS_CONNECTING:
response["state"] = FPD_LED_DEVICE_WPS_CONNECTING;
break;
case dsFPD_LED_DEVICE_WPS_CONNECTED:
response["state"] = FPD_LED_DEVICE_WPS_CONNECTED;
break;
case dsFPD_LED_DEVICE_WPS_ERROR:
response["state"] = FPD_LED_DEVICE_WPS_ERROR;
break;
case dsFPD_LED_DEVICE_FACTORY_RESET:
response["state"] = FPD_LED_DEVICE_FACTORY_RESET;
break;
case dsFPD_LED_DEVICE_USB_UPGRADE:
response["state"] = FPD_LED_DEVICE_USB_UPGRADE;
break;
case dsFPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR:
response["state"] = FPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR;
break;
default :
LOGERR("Unsupported LEDState %d", state);
LOGTRACEMETHODFIN();
return WPEFramework::Core::ERROR_BAD_REQUEST;
}
} else {
LOGERR("dsFPGetLEDState returned error %d", err);
LOGTRACEMETHODFIN();
return WPEFramework::Core::ERROR_ILLEGAL_STATE;
}
}
catch(...)
{
LOGERR("Exception in dsFPGetLEDState");
LOGTRACEMETHODFIN();
return WPEFramework::Core::ERROR_ILLEGAL_STATE;
}
returnResponse(success);
}
/**
* @brief This method changes the current led state to the one, mentioned by the user.
*
* @param[in] led state to apply.
* @return Returns the success code of underlying method.
* @ingroup SERVMGR_FRONTPANEL_API
*/
uint32_t LEDControl::setLEDState(const JsonObject& parameters, JsonObject& response)
{
LOGINFOMETHOD();
returnIfParamNotFound(parameters, "state");
string strLedState = parameters["state"].String();
bool success = false;
try
{
dsFPDLedState_t state = dsFPD_LED_DEVICE_NONE;
if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_ACTIVE, strlen(FPD_LED_DEVICE_ACTIVE)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_ACTIVE)) ){
state = dsFPD_LED_DEVICE_ACTIVE;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_STANDBY, strlen(FPD_LED_DEVICE_STANDBY)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_STANDBY)) ){
state = dsFPD_LED_DEVICE_STANDBY;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_WPS_CONNECTING, strlen(FPD_LED_DEVICE_WPS_CONNECTING)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_WPS_CONNECTING))){
state = dsFPD_LED_DEVICE_WPS_CONNECTING;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_WPS_CONNECTED, strlen(FPD_LED_DEVICE_WPS_CONNECTED)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_WPS_CONNECTED)) ){
state = dsFPD_LED_DEVICE_WPS_CONNECTED;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_WPS_ERROR, strlen(FPD_LED_DEVICE_WPS_ERROR)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_WPS_ERROR)) ){
state = dsFPD_LED_DEVICE_WPS_ERROR;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_FACTORY_RESET, strlen(FPD_LED_DEVICE_FACTORY_RESET)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_FACTORY_RESET)) ){
state = dsFPD_LED_DEVICE_FACTORY_RESET;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_USB_UPGRADE, strlen(FPD_LED_DEVICE_USB_UPGRADE)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_USB_UPGRADE)) ){
state = dsFPD_LED_DEVICE_USB_UPGRADE;
} else if (0==strncmp(strLedState.c_str(), FPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR, strlen(FPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR)) &&
(strlen(strLedState.c_str()) == strlen(FPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR)) ){
state = dsFPD_LED_DEVICE_SOFTWARE_DOWNLOAD_ERROR;
} else {
//Invalid parameter
LOGERR("UNKNOWN state : %s", strLedState.c_str());
LOGTRACEMETHODFIN();
return WPEFramework::Core::ERROR_BAD_REQUEST;
}
if (dsFPD_LED_DEVICE_NONE!=state) {
LOGINFO("dsFPSetLEDState state:%s state:%d", strLedState.c_str(), state);
dsError_t err = dsFPSetLEDState (state);
if (!err) {
success = true;
} else {
LOGERR("dsFPSetLEDState returned error %d", err);
LOGTRACEMETHODFIN();
return WPEFramework::Core::ERROR_ILLEGAL_STATE;
}
}
}
catch (...)
{
LOGERR("Exception in dsFPSetLEDState");
LOGTRACEMETHODFIN();
return WPEFramework::Core::ERROR_ILLEGAL_STATE;
}
returnResponse(success);
}
} // namespace Plugin
} // namespace WPEFramework