RDK Documentation (Open Sourced RDK Components)
dsHost.cpp
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 2016 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 
21 
22 /**
23 * @defgroup devicesettings
24 * @{
25 * @defgroup rpc
26 * @{
27 **/
28 
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <pthread.h>
37 #include <iostream>
38 #include "hostPersistence.hpp"
39 #include <sstream>
40 #include "iarmUtil.h"
41 #include "libIARM.h"
42 #include "libIBus.h"
43 #include "dsRpc.h"
44 #include "dsError.h"
45 #include "dsTypes.h"
46 #include "dsHost.h"
47 #include <dlfcn.h>
48 
49 #include "safec_lib.h"
50 #include "dsMgr.h"
51 
52 static int m_isInitialized = 0;
53 static pthread_mutex_t hostLock = PTHREAD_MUTEX_INITIALIZER;
54 
55 #define IARM_BUS_Lock(lock) pthread_mutex_lock(&hostLock)
56 #define IARM_BUS_Unlock(lock) pthread_mutex_unlock(&hostLock)
57 
58 
59 IARM_Result_t _dsGetPreferredSleepMode(void *arg);
60 IARM_Result_t _dsSetPreferredSleepMode(void *arg);
61 IARM_Result_t _dsGetCPUTemperature(void *arg);
62 IARM_Result_t _dsGetVersion(void *arg);
63 IARM_Result_t _dsSetVersion(void *arg);
64 IARM_Result_t _dsGetSocIDFromSDK(void *arg);
65 IARM_Result_t _dsGetHostEDID(void *arg);
66 
67 
68 static dsSleepMode_t _SleepMode = dsHOST_SLEEP_MODE_LIGHT;
69 
70 using namespace std;
71 
72 static string enumToString( dsSleepMode_t mode );
73 static dsSleepMode_t stringToEnum ( string mode );
74 
75 #define DSHAL_API_VERSION_MAJOR_DEFAULT 1
76 #define DSHAL_API_VERSION_MINOR_DEFAULT 0
77 
78 
79 IARM_Result_t dsHostMgr_init()
80 {
81 
82  try{
83  string mode="LIGHT_SLEEP";
84  mode = device::HostPersistence::getInstance().getProperty("Power.Mode",mode);
85  printf("Sleep mode Persistent value is -> %s\n",mode.c_str());
86  _SleepMode = stringToEnum(mode);
87  }
88  catch(...)
89  {
90  printf("Error in Reading the Power Mode Persisent \r\n");
91  }
92 
93  if (!m_isInitialized) {
94  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsGetPreferredSleepMode,_dsGetPreferredSleepMode);
95  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsSetPreferredSleepMode,_dsSetPreferredSleepMode);
96  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsGetCPUTemperature,_dsGetCPUTemperature);
97  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsGetVersion,_dsGetVersion);
98  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsSetVersion,_dsSetVersion);
99  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsGetSocIDFromSDK,_dsGetSocIDFromSDK);
100  IARM_Bus_RegisterCall(IARM_BUS_DSMGR_API_dsGetHostEDID,_dsGetHostEDID);
101 
102 
103  uint32_t halVersion = 0x10000;
104  if (dsERR_NONE != dsGetVersion(&halVersion))
105  {
106  halVersion = dsHAL_APIVER(DSHAL_API_VERSION_MAJOR_DEFAULT, DSHAL_API_VERSION_MINOR_DEFAULT);
107  }
108  __TIMESTAMP();printf("DS HAL Version is - %d.%d \r\n",dsHAL_APIVER_MAJOR(halVersion),dsHAL_APIVER_MINOR(halVersion));
109 
110  m_isInitialized = 1;
111  }
112  return IARM_RESULT_SUCCESS;
113 }
114 
115 IARM_Result_t dsHostMgr_term()
116 {
117  return IARM_RESULT_SUCCESS;
118 }
119 
120 IARM_Result_t _dsSetPreferredSleepMode(void *arg)
121 {
122  IARM_Result_t ret = IARM_RESULT_SUCCESS;
123  _DEBUG_ENTER();
124  IARM_BUS_Lock(lock);
125 
127  printf("_dsSetPreferredSleepMode called with the mode - %s \r\n", enumToString(param->mode).c_str());
128  try{
129 
130  device::HostPersistence::getInstance().persistHostProperty("Power.Mode",enumToString(param->mode));
131  _SleepMode = param->mode;
132  IARM_Bus_DSMgr_EventData_t eventData;
133  eventData.data.sleepModeInfo.sleepMode = _SleepMode;
134  IARM_Bus_BroadcastEvent(IARM_BUS_DSMGR_NAME,(IARM_EventId_t)IARM_BUS_DSMGR_EVENT_SLEEP_MODE_CHANGED,(void *)&eventData, sizeof(eventData));
135  printf("callaing IARM_BUS_DSMGR_EVENT_SLEEP_MODE_CHANGED :%d \n",_SleepMode);
136  ret = IARM_RESULT_SUCCESS;
137  }
138  catch(...)
139  {
140  printf("Error in Persisting the Power Mode\r\n");
141  }
142  IARM_BUS_Unlock(lock);
143  return ret;
144 }
145 
146 IARM_Result_t _dsGetPreferredSleepMode(void *arg)
147 {
148  IARM_Result_t ret = IARM_RESULT_SUCCESS;
149 
150  _DEBUG_ENTER();
151 
152  IARM_BUS_Lock(lock);
153 
155  param->mode =_SleepMode;
156 
157  printf("_dsGetPreferredSleepMode: Mode - %s \r\n", enumToString(param->mode).c_str());
158 
159  IARM_BUS_Unlock(lock);
160  return ret;
161 }
162 
163 string enumToString( dsSleepMode_t mode )
164 {
165  string ret;
166  switch(mode)
167  {
169  ret = "LIGHT_SLEEP";
170  break;
172  ret = "DEEP_SLEEP";
173  break;
174  default:
175  ret = "LIGHT_SLEEP";
176  }
177  return ret;
178 }
179 
180 dsSleepMode_t stringToEnum (string mode )
181 {
182  if(mode == "LIGHT_SLEEP")
183  {
185  }
186  else if(mode == "DEEP_SLEEP")
187  {
188  return dsHOST_SLEEP_MODE_DEEP;
189  }
190  return dsHOST_SLEEP_MODE_LIGHT;
191 }
192 
193 IARM_Result_t _dsGetCPUTemperature(void *arg)
194 {
195 
196  _DEBUG_ENTER();
197  IARM_Result_t ret = IARM_RESULT_SUCCESS;
198 
199 
200  #ifdef HAS_THERMAL_API
201  IARM_BUS_Lock(lock);
202 
203  dsCPUThermalParam *param = (dsCPUThermalParam *)arg;
204  float temperature = 45.0;
205  dsError_t retValue = dsERR_NONE;
206 
207  retValue = dsGetCPUTemperature(&temperature);
208  if (retValue == dsERR_NONE)
209  {
210  param->temperature = temperature;
211  }
212 
213  printf("Current temperature in SRV is: %+7.2fC\n", param->temperature);
214 
215  IARM_BUS_Unlock(lock);
216  #endif
217 
218  return ret;
219 }
220 
221 
222 IARM_Result_t _dsGetVersion(void *arg)
223 {
224 
225  _DEBUG_ENTER();
226 
227  IARM_BUS_Lock(lock);
228 
229  IARM_Result_t ret = IARM_RESULT_SUCCESS;
230  dsError_t retValue = dsERR_NONE;
231  dsVesrionParam *param = (dsVesrionParam *)arg;
232  uint32_t ver = 0x10000;
233 
234  retValue = dsGetVersion(&ver);
235  if (retValue == dsERR_NONE)
236  {
237  param->versionNumber = ver;
238  }
239  else
240  {
241  param->versionNumber = dsHAL_APIVER(DSHAL_API_VERSION_MAJOR_DEFAULT, DSHAL_API_VERSION_MINOR_DEFAULT);
242  }
243 
244  __TIMESTAMP();printf("DS HAL Version - %d.%d \r\n",dsHAL_APIVER_MAJOR(param->versionNumber),dsHAL_APIVER_MINOR(param->versionNumber));
245 
246  IARM_BUS_Unlock(lock);
247 
248  return ret;
249 }
250 
251 
252 IARM_Result_t _dsSetVersion(void *arg)
253 {
254 
255  _DEBUG_ENTER();
256 
257  IARM_BUS_Lock(lock);
258 
259  IARM_Result_t ret = IARM_RESULT_SUCCESS;
260 
261  dsError_t retValue = dsERR_NONE;
262  dsVesrionParam *param = (dsVesrionParam *)arg;
263 
264  retValue = dsSetVersion(param->versionNumber);
265  if (retValue != dsERR_NONE)
266  {
267  __TIMESTAMP();printf("Error in Setting versionNumber: 0x%08X \r\n",param->versionNumber);
268  }
269 
270  IARM_BUS_Unlock(lock);
271 
272  return ret;
273 }
274 
275 IARM_Result_t _dsGetSocIDFromSDK(void *arg)
276 {
278  IARM_Result_t ret = IARM_RESULT_SUCCESS;
279  _DEBUG_ENTER();
280  IARM_BUS_Lock(lock);
281 
282  printf("dsSRV:_dsGetSocIDFromSDK\r\n");
283  typedef dsError_t (*dsGetSocIDFromSDK_t)(char* socID);
284  static dsGetSocIDFromSDK_t func = NULL;
285  if (func == NULL) {
286  void *dllib = dlopen(RDK_DSHAL_NAME, RTLD_LAZY);
287 
288  if (dllib != NULL) {
289  func = (dsGetSocIDFromSDK_t) dlsym(dllib, "dsGetSocIDFromSDK");
290  if (func != NULL) {
291  printf("dsSRV: dsGetSocIDFromSDK(char* socID) is defined and loaded\r\n");
292  }
293  else {
294  printf("dsSRV: dsGetSocIDFromSDK(char* socID) is not defined\r\n");
295  }
296  dlclose(dllib); //CID:80469,163396,163400,163407 - Resource leak
297  }
298  else {
299  printf("dsSRV: Opening RDK_DSHAL_NAME [%s] failed\r\n", RDK_DSHAL_NAME);
300  }
301  }
302 
303  if (param != NULL) {
304  param->result = dsERR_GENERAL;
305 
306  if (func != NULL) {
307  param->result = func(param->socID);
308  }
309  }
310  printf("dsSRV: _dsGetSocIDFromSDK SocID : %s\n",param->socID);
311 
312  IARM_BUS_Unlock(lock);
313 
314  return IARM_RESULT_SUCCESS;
315 }
316 
317 IARM_Result_t _dsGetHostEDID(void *arg)
318 {
319 
320 #ifndef RDK_DSHAL_NAME
321 #warning "RDK_DSHAL_NAME is not defined"
322 #define RDK_DSHAL_NAME "RDK_DSHAL_NAME is not defined"
323 #endif
324  errno_t rc = -1;
325  _DEBUG_ENTER();
326 
327  IARM_BUS_Lock(lock);
328 
329  printf("dsSRV::getHostEDID \r\n");
330 
331  typedef dsError_t (*dsGetHostEDID_t)(unsigned char *edid, int *length);
332  static dsGetHostEDID_t func = 0;
333  if (func == 0) {
334  void *dllib = dlopen(RDK_DSHAL_NAME, RTLD_LAZY);
335  if (dllib) {
336  func = (dsGetHostEDID_t) dlsym(dllib, "dsGetHostEDID");
337  if (func) {
338  printf("dsGetHostEDID(void) is defined and loaded\r\n");
339  }
340  else {
341  printf("dsGetHostEDID(void) is not defined\r\n");
342  }
343  dlclose(dllib);
344  }
345  else {
346  printf("Opening RDK_DSHAL_NAME [%s] failed\r\n", RDK_DSHAL_NAME);
347  }
348  }
349 
351 
352  if (func != 0) {
353  unsigned char edidBytes[EDID_MAX_DATA_SIZE] ;
354  int length = 0;
355  dsError_t ret = func(edidBytes, &length);
356  if (ret == dsERR_NONE && length <= 1024) {
357  printf("dsSRV ::getHostEDID returns %d bytes\r\n", length);
358  rc = memcpy_s(param->bytes,sizeof(param->bytes),edidBytes,EDID_MAX_DATA_SIZE);
359  if(rc!=EOK){
360  ERR_CHK(rc);
361  }
362  param->length = length;
363  }
364  param->result = ret;
365  }
366  else {
367  param->result = dsERR_OPERATION_NOT_SUPPORTED;
368  }
369 
370  IARM_BUS_Unlock(lock);
371 
372  return IARM_RESULT_SUCCESS;
373 }
374 
375 
376 /** @} */
377 /** @} */
dsHOST_SLEEP_MODE_DEEP
@ dsHOST_SLEEP_MODE_DEEP
Definition: dsTypes.h:922
dsSetVersion
dsError_t dsSetVersion(uint32_t versionNumber)
Allows the application to set the runtime version of the dsHAL.
Definition: dsHost.cpp:139
dsSleepMode_t
enum _dsSleepMode_t dsSleepMode_t
dsTypes.h
Device Settings HAL types.
_dsCPUThermalParam
Definition: dsRpc.h:694
device::HostPersistence::persistHostProperty
void persistHostProperty(const std::string &key, const std::string &value)
Definition: hostPersistence.cpp:273
dsError.h
Device Settings HAL error codes.
dsGetCPUTemperature
dsError_t dsGetCPUTemperature(float *cpuTemperature)
This function gets the CPU temperature in centrigade.
Definition: dsHost.cpp:88
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.
dsERR_GENERAL
@ dsERR_GENERAL
Definition: dsError.h:86
dsERR_NONE
@ dsERR_NONE
Definition: dsError.h:85
IARM_BUS_DSMGR_EVENT_SLEEP_MODE_CHANGED
@ IARM_BUS_DSMGR_EVENT_SLEEP_MODE_CHANGED
Definition: dsMgr.h:74
_dsDisplayGetEDIDBytesParam_t
Definition: dsRpc.h:613
IARM_Bus_BroadcastEvent
IARM_Result_t IARM_Bus_BroadcastEvent(const char *ownerName, IARM_EventId_t eventId, void *data, size_t len)
This API is used to publish an Asynchronous event to all IARM client registered for this perticular e...
libIBus.h
RDK IARM-Bus API Declarations.
dsHAL_APIVER_MINOR
#define dsHAL_APIVER_MINOR(x)
Definition: dsTypes.h:1161
_dsPreferredSleepMode
Definition: dsRpc.h:689
dsHAL_APIVER
#define dsHAL_APIVER(major, minor)
Definition: dsTypes.h:1157
dsGetVersion
dsError_t dsGetVersion(uint32_t *versionNumber)
Get DS HAL API Version.
Definition: dsHost.cpp:114
dsHOST_SLEEP_MODE_LIGHT
@ dsHOST_SLEEP_MODE_LIGHT
Definition: dsTypes.h:921
dsERR_OPERATION_NOT_SUPPORTED
@ dsERR_OPERATION_NOT_SUPPORTED
Definition: dsError.h:89
_dsVesrionParam
Definition: dsRpc.h:699
dsHAL_APIVER_MAJOR
#define dsHAL_APIVER_MAJOR(x)
Definition: dsTypes.h:1159
device::HostPersistence::getInstance
static HostPersistence & getInstance(void)
Definition: hostPersistence.cpp:122
dsError_t
dsError_t
Device Settings API Error return codes.
Definition: dsError.h:84
_DSMgr_EventData_t
Definition: dsMgr.h:81
_dsGetSocIDFromSDKParam_t
Definition: dsRpc.h:877