-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathctrlm_thunder_plugin_powermanager.cpp
More file actions
executable file
·212 lines (177 loc) · 7.98 KB
/
ctrlm_thunder_plugin_powermanager.cpp
File metadata and controls
executable file
·212 lines (177 loc) · 7.98 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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2015 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 "ctrlm_thunder_plugin_powermanager.h"
#include "ctrlm_thunder_log.h"
#include <WPEFramework/core/core.h>
#include <WPEFramework/websocket/websocket.h>
#include <WPEFramework/plugins/plugins.h>
using namespace Thunder;
using namespace PowerManager;
using namespace WPEFramework;
static ctrlm_thunder_plugin_powermanager_t *instance = NULL;
static ctrlm_power_state_t ctrlm_thunder_power_state_map(const string power_string);
static void _on_power_state_changed(ctrlm_thunder_plugin_powermanager_t *plugin, JsonObject params);
ctrlm_thunder_plugin_powermanager_t::ctrlm_thunder_plugin_powermanager_t() : ctrlm_thunder_plugin_t("PowerManager", "org.rdk.PowerManager", 1) {
sem_init(&this->semaphore, 0, 1);
this->registered_events = false;
}
ctrlm_thunder_plugin_powermanager_t::~ctrlm_thunder_plugin_powermanager_t() {
free(instance);
}
ctrlm_thunder_plugin_powermanager_t *ctrlm_thunder_plugin_powermanager_t::get_instance() {
if(instance == NULL) {
instance = new ctrlm_thunder_plugin_powermanager_t;
}
return instance;
}
void ctrlm_thunder_plugin_powermanager_t::on_initial_activation() {
Thunder::Plugin::ctrlm_thunder_plugin_t::on_initial_activation();
}
void ctrlm_thunder_plugin_powermanager_t::on_activation_change(Thunder::plugin_state_t state) {
Thunder::Plugin::ctrlm_thunder_plugin_t::on_activation_change(state);
}
/* root@pioneer-uhd:~# curl --request POST --url http://127.0.0.1:9998/jsonrpc --header 'Content-Type: application/json' --data '{ "jsonrpc": "2.0", "id": 1234567890, "method": "org.rdk.PowerManager.1.getPowerState", "params": {} }'
* {"jsonrpc":"2.0","id":1234567890,"result":{"currentState":"ON","previousState":"DEEP_SLEEP"}}
* */
ctrlm_power_state_t ctrlm_thunder_plugin_powermanager_t::get_power_state() {
JsonObject params, response;
params = {};
ctrlm_power_state_t power_state = CTRLM_POWER_STATE_INVALID;
sem_wait(&this->semaphore);
if(this->call_plugin("getPowerState", (void *)¶ms, (void *)&response)) {
std::string power_string;
const char *query_string = "currentState";
if(response.HasLabel(query_string)) {
string power_string = response[query_string].String();
power_state = ctrlm_thunder_power_state_map(power_string);
XLOGD_DEBUG("power state <%s>", ctrlm_power_state_str(power_state));
} else {
std::string response_string;
response.ToString(response_string);
XLOGD_ERROR("power state response malformed: <%s>", response_string.c_str());
}
}
sem_post(&this->semaphore);
return power_state;
}
/* root@pioneer-uhd:~# curl --request POST --url http://127.0.0.1:9998/jsonrpc --header 'Content-Type: application/json' --data '{ "jsonrpc": "2.0", "id": 1234567890, "method": "org.rdk.PowerManager.1.getNetworkStandbyMode", "params": {} }'
{"jsonrpc":"2.0","id":1234567890,"result":true} */
bool ctrlm_thunder_plugin_powermanager_t::get_networked_standby_mode() {
JsonObject params;
params = {};
bool networked_standby_mode = false;
sem_wait(&this->semaphore);
if(this->call_plugin_boolean("getNetworkStandbyMode", (void *)¶ms, &networked_standby_mode)) {
XLOGD_DEBUG("networked_standby_mode is %s", networked_standby_mode?"TRUE":"FALSE");
} else {
XLOGD_ERROR("getNetworkStandbyMode call failed");
XLOGD_WARN("NOTE: defaulting to NSM disabled. Will load sleep DSP. Wake with voice will not be available");
}
sem_post(&this->semaphore);
return networked_standby_mode;
}
/* root@pioneer-uhd:~# curl --request POST --url http://127.0.0.1:9998/jsonrpc --header 'Content-Type: application/json' --data '{ "jsonrpc": "2.0", "id": 1234567890, "method": "org.rdk.PowerManager.1.getLastWakeupReason", "params": {} }'
{"jsonrpc":"2.0","id":1234567890,"result":"COLDBOOT"} */
bool ctrlm_thunder_plugin_powermanager_t::get_wakeup_reason_voice() {
JsonObject params, response;
params = {};
bool wakeup_reason_voice = false;
sem_wait(&this->semaphore);
if(this->call_plugin("getLastWakeupReason", (void *)¶ms, (void *)&response)) {
wakeup_reason_voice = (0 == strncmp(response["result"].String().c_str(), "VOICE", 5));
XLOGD_DEBUG("voice_wakeup is %s", wakeup_reason_voice?"TRUE":"FALSE");
} else {
XLOGD_ERROR("getLastWakeupReason call failed");
}
sem_post(&this->semaphore);
return wakeup_reason_voice;
}
void ctrlm_thunder_plugin_powermanager_t::on_power_state_changed(const ctrlm_power_state_t ¤t_state, const ctrlm_power_state_t &new_state) {
ctrlm_main_queue_power_state_change_t *msg = (ctrlm_main_queue_power_state_change_t *)g_malloc(sizeof(ctrlm_main_queue_power_state_change_t));
if(NULL == msg) {
XLOGD_FATAL("Out of memory");
g_assert(0);
return;
}
XLOGD_DEBUG("current state %s, new state %s", ctrlm_power_state_str(current_state), ctrlm_power_state_str(new_state));
msg->header.type = CTRLM_MAIN_QUEUE_MSG_TYPE_POWER_STATE_CHANGE;
msg->new_state = new_state;
ctrlm_main_queue_msg_push(msg);
}
bool ctrlm_thunder_plugin_powermanager_t::register_events() {
bool ret = this->registered_events;
if(ret == false) {
auto clientObject = (JSONRPC::LinkType<Core::JSON::IElement>*)this->plugin_client;
if(clientObject) {
ret = true;
uint32_t thunderRet = clientObject->Subscribe<JsonObject>(CALL_TIMEOUT, _T("onPowerModeChanged"), _on_power_state_changed, this);
if(thunderRet != Core::ERROR_NONE) {
XLOGD_ERROR("Thunder subscribe failed <on_power_state_changed>");
ret = false;
}
if(ret) {
this->registered_events = true;
}
} else {
XLOGD_INFO("clientObject is null");
}
}
Thunder::Plugin::ctrlm_thunder_plugin_t::register_events();
return(ret);
}
static void _on_power_state_changed(ctrlm_thunder_plugin_powermanager_t *plugin, JsonObject params) {
ctrlm_power_state_t new_state = CTRLM_POWER_STATE_INVALID;
ctrlm_power_state_t current_state = CTRLM_POWER_STATE_INVALID;
string power_string;
string params_string;
const char *query_string;
query_string = "newState";
if(!params.HasLabel(query_string)) {
params.ToString(params_string);
} else {
power_string = params[query_string].String();
new_state = ctrlm_thunder_power_state_map(power_string);
}
query_string = "currentState";
if(!params.HasLabel(query_string)) {
params.ToString(params_string);
} else {
power_string = params[query_string].String();
current_state = ctrlm_thunder_power_state_map(power_string);
}
if(new_state == CTRLM_POWER_STATE_INVALID || current_state == CTRLM_POWER_STATE_INVALID) {
XLOGD_ERROR("power state params malformed: <%s>", params_string.c_str());
} else {
plugin->on_power_state_changed(current_state, new_state);
}
}
static ctrlm_power_state_t ctrlm_thunder_power_state_map(const string power_string) {
if("ON" == power_string) {
return CTRLM_POWER_STATE_ON;
} else if("OFF" == power_string) {
return CTRLM_POWER_STATE_DEEP_SLEEP;
} else if("STANDBY" == power_string) {
return CTRLM_POWER_STATE_STANDBY;
} else if("DEEP_SLEEP" == power_string) {
return CTRLM_POWER_STATE_DEEP_SLEEP;
} else if("LIGHT_SLEEP" == power_string) {
return CTRLM_POWER_STATE_STANDBY;
}
return CTRLM_POWER_STATE_INVALID;
}