50 #include "deepSleepMgr.h"
51 #include "deepSleepInternal.h"
52 #include "libIBusDaemon.h"
53 #include "deepsleeplogger.h"
55 #include "comcastIrKeyCodes.h"
63 static void _eventHandler(
const char *owner, IARM_EventId_t eventId,
void *data,
size_t len);
64 static IARM_Result_t _DeepSleepWakeup(
void *arg);
65 static IARM_Result_t _SetDeepSleepTimer(
void *arg);
66 static IARM_Result_t _GetDeepSleepStatus(
void *arg);
67 static IARM_Result_t _GetLastWakeupReason(
void *arg);
68 static IARM_Result_t _GetLastWakeupKeyCode(
void *arg);
70 static gboolean heartbeatMsg(gpointer data);
71 static gboolean deep_sleep_delay_timer_fn(gpointer data);
74 static uint32_t deep_sleep_delay_timeout = 0;
75 static uint32_t deep_sleep_wakeup_timer = 0;
76 static bool nwStandbyMode_gs =
false;
77 GMainLoop *deepSleepMgr_Loop = NULL;
78 static guint dsleep_delay_event_src = 0;
80 static gboolean isLxcRestart = 0;
82 IARM_Result_t DeepSleepMgr_Start(
int argc,
char *argv[])
84 char *settingsFile = NULL;
85 if (argc == 2) settingsFile = argv[1];
87 setvbuf(stdout, NULL, _IOLBF, 0);
103 #ifdef ENABLE_DEEPSLEEP_FPLED_HANDLING
108 LOG(
"Exception Caught during [device::Manager::Initialize]\r\n");
116 deepSleepMgr_Loop = g_main_loop_new ( NULL , FALSE );
117 if(deepSleepMgr_Loop != NULL){
118 g_timeout_add_seconds (300 , heartbeatMsg , deepSleepMgr_Loop);
125 return IARM_RESULT_SUCCESS;
128 IARM_Result_t DeepSleepMgr_Loop()
134 LOG(
"Deep Sleep Manager Init with Power State %d\r\n",param.curState);
137 if(deepSleepMgr_Loop)
139 g_main_loop_run (deepSleepMgr_Loop);
140 g_main_loop_unref(deepSleepMgr_Loop);
143 return IARM_RESULT_SUCCESS;
146 IARM_Result_t DeepSleepMgr_Stop(
void)
148 if(deepSleepMgr_Loop)
150 g_main_loop_quit(deepSleepMgr_Loop);
159 return IARM_RESULT_SUCCESS;
163 static gboolean heartbeatMsg(gpointer data)
167 LOG(
"I-ARM Deep Sleep Mgr: HeartBeat at %s\r\n", ctime(&curr));
172 static void _eventHandler(
const char *owner, IARM_EventId_t eventId,
void *data,
size_t len)
182 #if !defined (_DISABLE_SCHD_REBOOT_AT_DEEPSLEEP)
184 system(
"echo 0 > /opt/.rebootFlag");
185 system(
" echo `/bin/timestamp` ------------- Reboot timer expired while in Deep Sleep --------------- >> /opt/logs/receiver.log");
186 system(
"sleep 5; /rebootNow.sh -s DeepSleepMgr -o 'Rebooting the box due to reboot timer expired while in Deep Sleep...'");
193 LOG(
"Event IARM_BUS_PWRMGR_EVENT_MODECHANGED: State Changed %d -- > %d\r\n",
194 param->data.state.curState, param->data.state.newState);
196 if(IARM_BUS_PWRMGR_POWERSTATE_STANDBY_DEEP_SLEEP == param->data.state.newState)
198 LOG(
"GOT EVENT TO GO TO DEEP SLEEP \r\n");
202 deep_sleep_wakeup_timer = param->data.state.deep_sleep_timeout;
204 uint32_t SleepTimeInSec = 0;
208 fpST = fopen(
"/tmp/deepSleepTimer",
"r");
211 if(0 > fscanf(fpST,
"%d",&SleepTimeInSec))
213 __TIMESTAMP();LOG(
"Error: fscanf on SleepTimeInSec failed");
217 deep_sleep_delay_timeout = SleepTimeInSec ;
218 __TIMESTAMP();LOG(
" /tmp/ override Deep Sleep Time is %d \r\n",deep_sleep_delay_timeout);
223 LOG(
"Deep Sleep wakeup time value is %d Secs.. \r\n",deep_sleep_wakeup_timer);
224 if (deep_sleep_delay_timeout) {
226 LOG(
"Schedule Deep SLeep After %d Sec.. \r\n",deep_sleep_delay_timeout);
227 dsleep_delay_event_src = g_timeout_add_seconds ((guint) deep_sleep_delay_timeout,deep_sleep_delay_timer_fn,deepSleepMgr_Loop);
230 LOG(
"Enter to Deep sleep Mode..stop Receiver with sleep 10 before DS \r\n");
232 if ((stat(
"/lib/systemd/system/lxc.service", &buf) == 0) && (stat(
"/opt/lxc_service_disabled",&buf) !=0))
234 LOG(
"stopping lxc service\r\n");
235 system(
"systemctl stop lxc.service");
240 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
241 LOG(
"stopping xre-receiver service\r\n");
242 system(
"systemctl stop xre-receiver.service");
243 LOG(
"stopping wpeframework service\r\n");
244 system(
"systemctl stop wpeframework.service");
246 LOG(
"Skipping Stopping service while entering DEEPSLEEP.\n");
248 #ifdef ENABLE_LLAMA_PLATCO_SKY_XIONE
249 LOG(
"Update the Deepsleep marker to splunk.\n");
250 system(
"sh /lib/rdk/alertSystem.sh deepSleepMgrMain SYST_INFO_devicetoDS");
253 #ifndef ENABLE_LLAMA_PLATCO
254 LOG(
"Unmounting SDcard partition\r\n");
255 system(
"sh /lib/rdk/disk_checkV2 deepsleep ON");
257 LOG(
"Skipping Unmounting SDcard partition.\r\n");
260 #ifdef ENABLE_DEEPSLEEP_FPLED_HANDLING
261 __TIMESTAMP();LOG(
"FrontPanelConfig::fPTerm\n");
264 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
265 LOG(
"Enter to Deep sleep Mode..stop fog service before DS \r\n");
266 system(
"systemctl stop fog.service");
273 LOG(
"Device entering Deep sleep Mode.. \r\n");
275 #ifdef ENABLE_LLAMA_PLATCO_SKY_XIONE
276 nwStandbyMode_gs = param->data.state.nwStandbyMode;
277 LOG(
"\nCalling PLAT_DS_SetDeepSleep with nwStandbyMode: %s\n",
278 nwStandbyMode_gs?(
"Enabled"):(
"Disabled"));
280 LOG(
"Device entered to Deep sleep Mode.. \r\n");
281 status = PLAT_DS_SetDeepSleep(deep_sleep_wakeup_timer,&userWakeup, nwStandbyMode_gs);
283 LOG(
"Device resumed from Deep sleep Mode. \r\n");
291 LOG(
"ERROR: Device failed to enter into Deep sleep Mode.. \r\n");
308 LOG(
"Resumed due to user action. Sending KED_DEEPSLEEP_WAKEUP. \r\n");
310 eventData.data.irkey.keyType = KET_KEYDOWN;
311 eventData.data.irkey.keyCode = KED_DEEPSLEEP_WAKEUP;
312 eventData.data.irkey.isFP = 0;
316 eventData.data.irkey.keyType = KET_KEYUP;
320 LOG(
"Resumed without user action. Not sending KED_DEEPSLEEP_WAKEUP. \r\n");
323 #ifdef USE_WAKEUP_TIMER_EVT
324 DeepSleep_WakeupReason_t wakeupReason = DEEPSLEEP_WAKEUPREASON_UNKNOWN;
325 int reasonStatus = PLAT_DS_GetLastWakeupReason(&wakeupReason);
326 if (DEEPSLEEP_WAKEUPREASON_TIMER == wakeupReason){
327 LOG(
"Calling IARM_BUS_PWRMGR_API_handleDeepsleepTimeoutWakeup on wakeupReason:%d \n", wakeupReason);
330 #endif //End of USE_WAKEUP_TIMER_EVT
343 static IARM_Result_t _DeepSleepWakeup(
void *arg)
345 IARM_Bus_CommonAPI_PowerPreChange_Param_t *param = (IARM_Bus_CommonAPI_PowerPreChange_Param_t *) arg;
347 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
348 char* syscommand =
"systemctl restart mocadriver.service &";
351 LOG(
"RPC IARM_BUS_COMMON_API_DeepSleepWakeup : State Changed %d -- > %d\r" , param->curState , param->newState);
354 if( (IARM_BUS_PWRMGR_POWERSTATE_STANDBY_DEEP_SLEEP == param->curState) &&
355 (IARM_BUS_PWRMGR_POWERSTATE_STANDBY_DEEP_SLEEP != param->newState))
357 LOG(
"GOT EVENT TO EXIT FROM DEEP SLEEP \r\n");
358 #ifdef ENABLE_DEEPSLEEP_FPLED_HANDLING
359 __TIMESTAMP();LOG(
"FrontPanelConfig::fPInit\n");
363 PLAT_DS_DeepSleepWakeup();
366 if(dsleep_delay_event_src)
368 g_source_remove(dsleep_delay_event_src);
369 dsleep_delay_event_src = 0;
372 if(IsDeviceInDeepSleep)
375 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
376 LOG(
"Restarting Moca Service After Waking up from Deep Sleep.. \r\n");
379 #ifndef ENABLE_LLAMA_PLATCO
380 LOG(
"Mounting SDcard partition After Waking up from Deep Sleep..\r\n");
381 system(
"sh /lib/rdk/disk_checkV2 deepsleep OFF");
383 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
384 LOG(
"Restarting fog Service After Waking up from Deep Sleep.. \r\n");
385 system(
"systemctl restart fog.service &");
389 LOG(
"Restarting Lxc Service After Waking up from Deep Sleep\r\n");
390 system(
"systemctl restart lxc.service");
395 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
396 LOG(
"Restarting WPEFramework Service After Waking up from Deep Sleep\r\n");
397 system(
"systemctl restart wpeframework.service");
398 LOG(
"Restarting Receiver Service After Waking up from Deep Sleep\r\n");
399 system(
"systemctl restart xre-receiver.service");
401 LOG(
"Skipping restart of Services in Sky Platform\n");
408 LOG(
"Device woke up from Deep sleep Mode.. \r\n");
410 return IARM_RESULT_SUCCESS;
413 static IARM_Result_t _GetDeepSleepStatus(
void *arg)
415 int *status = (
int *)arg;
416 *status = IsDeviceInDeepSleep;
417 return IARM_RESULT_SUCCESS;
421 static IARM_Result_t _SetDeepSleepTimer(
void *arg)
423 IARM_Bus_DeepSleepMgr_SetDeepSleepTimer_Param_t *param = (IARM_Bus_DeepSleepMgr_SetDeepSleepTimer_Param_t *)arg;
427 LOG(
"Deep sleep timer set to : %d Seconds \r\n", param->timeout);
428 deep_sleep_delay_timeout = param->timeout;
429 return IARM_RESULT_SUCCESS;
431 return IARM_RESULT_IPCCORE_FAIL;
435 static gboolean deep_sleep_delay_timer_fn(gpointer data)
440 LOG(
"Deep Sleep Timer Expires :Enter to Deep sleep Mode..stop Receiver with sleep 10 before DS \r\n");
443 if ((stat(
"/lib/systemd/system/lxc.service", &buf) == 0) && (stat(
"/opt/lxc_service_disabled",&buf) !=0))
445 system(
"systemctl stop lxc.service");
449 #ifndef ENABLE_LLAMA_PLATCO_SKY_XIONE
450 system(
"systemctl stop xre-receiver.service");
451 system(
"systemctl stop wpeframework.service");
453 LOG(
"Skiping Stopping of services in Sky Llama Platform\n");
457 status = PLAT_DS_SetDeepSleep(deep_sleep_wakeup_timer,&userWakeup, nwStandbyMode_gs);
460 LOG(
"deep_sleep_delay_timer_fn: Failed to enter deepsleep state \n");
462 #ifdef USE_WAKEUP_TIMER_EVT
464 DeepSleep_WakeupReason_t wakeupReason = DEEPSLEEP_WAKEUPREASON_UNKNOWN;
465 int reasonStatus = PLAT_DS_GetLastWakeupReason(&wakeupReason);
466 if (DEEPSLEEP_WAKEUPREASON_TIMER == wakeupReason){
467 LOG(
"Calling IARM_BUS_PWRMGR_API_handleDeepsleepTimeoutWakeup on wakeupReason:%d \n", wakeupReason);
470 #endif //End of USE_WAKEUP_TIMER_EVT
474 static IARM_Result_t _GetLastWakeupReason(
void *arg)
476 DeepSleep_WakeupReason_t *wakeupReason = (DeepSleep_WakeupReason_t *)arg;
477 int status = PLAT_DS_GetLastWakeupReason(wakeupReason);
478 return IARM_RESULT_SUCCESS;
481 static IARM_Result_t _GetLastWakeupKeyCode(
void *arg)
483 IARM_Bus_DeepSleepMgr_WakeupKeyCode_Param_t *wakeupKeyCode = (IARM_Bus_DeepSleepMgr_WakeupKeyCode_Param_t *)arg;
484 int status = PLAT_DS_GetLastWakeupKeyCode(wakeupKeyCode);
485 return (IARM_Result_t)status;