RDK Documentation (Open Sourced RDK Components)
sleepTimer.c
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include <glib.h>
21 #include <stdio.h>
22 #include <pthread.h>
23 #include "libIBus.h"
24 #include "pwrlogger.h"
25 #include "pwrMgr.h"
26 #include "pwrMgrInternal.h"
27 #ifdef SLEEPTIMER_USE_CECSTANDBY
28 #include "CecIARMBusMgr.h"
29 #endif
30 #include "safec_lib.h"
31 
32 static GMainLoop *mainloop = NULL;
33 static guint timerSource = 0;
34 static time_t timerEnd = 0;
35 static pthread_mutex_t _lock = PTHREAD_MUTEX_INITIALIZER;
36 #define LOCK() pthread_mutex_lock(&_lock)
37 #define UNLOCK() pthread_mutex_unlock(&_lock)
38 
39 #ifdef SLEEPTIMER_USE_CECSTANDBY
40 static void SendCECStandby(void)
41 {
42  errno_t rc = -1;
44  unsigned char buf[] = {0x30, 0x36}; //standby msg, from TUNER to TV
45  memset(&dataToSend, 0, sizeof(dataToSend));
46  dataToSend.length = sizeof(buf);
47  rc = memcpy_s(dataToSend.data,sizeof(dataToSend.data), buf, dataToSend.length);
48  if(rc!=EOK)
49  {
50  ERR_CHK(rc);
51  }
52  LOG("SleepTimer send CEC Standby\r\n");
53  IARM_Bus_Call(IARM_BUS_CECMGR_NAME,IARM_BUS_CECMGR_API_Send,(void *)&dataToSend, sizeof(dataToSend));
54 }
55 #endif
56 
57 static gboolean _onSleepTimerFire(void *)
58 {
59  LOCK();
60  LOG("SleepTimer %d start at %ld, fired at %ld, set standby\r\n", timerSource, timerEnd, time(NULL));
61  timerEnd = 0;
62  timerSource = 0;
63  UNLOCK();
64 #ifdef SLEEPTIMER_USE_CECSTANDBY
65  SendCECStandby();
66 #else
67  LOG("No STANDBY action implemented\r\n");
68 #endif
69  return FALSE;
70 }
71 
72 static IARM_Result_t _SetSleepTimer(void *arg)
73 {
75 
76  if(param != NULL)
77  {
78  LOCK();
79  if (param->start == 1) {
80  time(&timerEnd);
81  timerEnd += (time_t)param->time;
82  LOG("SleepTimer (re)set :%.3f, curr source=%d, to fire at %ld\r\n", param->time, timerSource, timerEnd);
83  if (timerSource) {
84  g_source_remove(timerSource);
85  timerSource = 0;
86  }
87  timerSource = g_timeout_add_seconds((guint)(param->time), _onSleepTimerFire, NULL);
88  }
89  else if (param->start == 0) {
90  LOG("SleepTimer %d cancelled\r\n", timerSource);
91  if (timerSource) {
92  g_source_remove(timerSource);
93  }
94  timerSource = 0;
95  timerEnd = 0;
96  }
97  UNLOCK();
98  return IARM_RESULT_SUCCESS;
99  }
100  return IARM_RESULT_IPCCORE_FAIL;
101 }
102 
103 static IARM_Result_t _GetSleepTimer(void *arg)
104 {
106  param->time = 0.;
107  param->start = 0;
108  LOCK();
109  if (timerSource) {
110  time_t now = time(NULL);
111  param->time = (double)((now < timerEnd) ? (timerEnd - now) :0);
112  param->start = (param->time ? 1 : 0);
113  }
114  UNLOCK();
115  LOG("SleepTimer %d has %.3lf remaining\r\n", param->start, param->time); //CID:128053,127752,97127,95718 - Print_Args
116  return IARM_RESULT_SUCCESS;
117 }
118 
119 void IARM_Bus_PWRMGR_RegisterSleepTimerAPIs(void *context)
120 {
121  LOG("SleepTimer PwrMgr register APIs\r\n");
122  mainloop = (GMainLoop*)context;
125 }
126 
_IARM_Bus_PWRMgr_SleepTimer_Param_t::start
int start
Definition: pwrMgr.h:210
pwrMgrInternal.h
IARM_Bus_Call
IARM_Result_t IARM_Bus_Call(const char *ownerName, const char *methodName, void *arg, size_t argLen)
This API is used to Invoke RPC method by its application name and method name.
Definition: iarm_bus.c:57
IARM_BUS_CECMGR_NAME
#define IARM_BUS_CECMGR_NAME
Definition: CecIARMBusMgr.h:41
IARM_BUS_PWRMGR_API_SetSleepTimer
#define IARM_BUS_PWRMGR_API_SetSleepTimer
Definition: pwrMgr.h:202
IARM_Bus_RegisterCall
IARM_Result_t IARM_Bus_RegisterCall(const char *methodName, IARM_BusCall_t handler)
This API is used to register an RPC method that can be invoked by other applications.
libIBus.h
RDK IARM-Bus API Declarations.
pwrMgr.h
IARM-Bus Power Manager Public API.
IARM_BUS_PWRMGR_API_GetSleepTimer
#define IARM_BUS_PWRMGR_API_GetSleepTimer
Definition: pwrMgr.h:203
_IARM_Bus_PWRMgr_SleepTimer_Param_t
Structure which holds the sleep timer information.
Definition: pwrMgr.h:208
_IARM_Bus_PWRMgr_SleepTimer_Param_t::time
double time
Definition: pwrMgr.h:209
_IARM_Bus_CECMgr_Send_Param_t
Definition: CecIARMBusMgr.h:71