RDK Documentation (Open Sourced RDK Components)
TRMMgr.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 using namespace std;
21 
22 
23 #include "TRMMgr.h"
24 
25 /*Helper header*/
26 #include "Helper.h"
27 #include "Debug.h"
28 
29 /*C++ include */
30 #include <stdint.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <map>
34 #include <string>
35 #include <string.h>
36 #include <uuid/uuid.h>
37 #include <list>
38 #include <vector>
39 #include <time.h>
40 #include <sys/time.h>
41 #include <stddef.h>
42 #include <sys/syscall.h>
43 #include <unistd.h>
44 #include <iostream>
45 #include <cstdio>
46 
47 
48 /* IARM include */
49 #include "libIBus.h"
50 static IARM_Result_t _GetTRMDiagInfo(void *arg);
51 
52 /* TRM include */
53 #include "trm/Messages.h"
54 #include "trm/MessageProcessor.h"
55 #include "trm/Activity.h"
56 #include "trm/JsonEncoder.h"
57 #include "trm/JsonDecoder.h"
58 #include "safec_lib.h"
59 
60 /* For connection sync */
61 static char responseMsg[MAX_PAYLOAD_LEN];
62 static pthread_mutex_t trmMgr_mutex;
63 
65 {
66 public:
67  TRMMgrLock_(void) {
68  pthread_mutex_lock(&trmMgr_mutex);
69  }
70  ~TRMMgrLock_(void) {
71  pthread_mutex_unlock(&trmMgr_mutex);
72  }
73 };
74 
75 #define TRMMgrLock() TRMMgrLock_ t()
76 
77 
78 
79 /**************************************************************************
80 Function name : Get TRMMessage
81 
82 Arguments : TRMMessageData_t
83 
84 Description : Frame the Requested TRM Diag Message and send it to Client.
85 **************************************************************************/
86 static IARM_Result_t _GetTRMDiagInfo(void *arg)
87 {
88  TRMDiagInfo_t *infoParam = (TRMDiagInfo_t *)arg;
89 
90 
91  DIAG_TRACE(("Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
92 
93  TRMMgrLock();
94 
95  uint32_t length = 0;
96  uint32_t errCount = 0;
97  bool ret= false;
98  errno_t safec_rc = -1;
99 
100  safec_rc = memset_s(responseMsg, sizeof(responseMsg), 0, sizeof(responseMsg));
101  ERR_CHK(safec_rc);
102 
103  if (NULL == infoParam)
104  {
105  DIAG_WARN(("Error in %s : NULL Arguments Passed \r\n",__FUNCTION__));
106  return IARM_RESULT_INVALID_PARAM;
107  }
108 
109  /* Initialize the parameters */
110  infoParam->numOfTuner = 0;
111  infoParam->numOfTRMError = 0;
112  infoParam->bufLen = 0;
113  infoParam->retCode = TRMMgr_ERR_NONE;
114 
115 
116  switch (infoParam->msgType)
117  {
118  case TRMMgr_MSG_TYPE_GET_NUM_IN_BAND_TUNERS:
119  {
120  DIAG_WARN(("%s : GET_NUM_IN_BAND_TUNERS \r\n",__FUNCTION__));
121  #ifdef NUM_OF_TUNERS
122  infoParam->numOfTuner = NUM_OF_TUNERS;
123  #else
124  infoParam->numOfTRMError = 6;
125  infoParam->retCode = TRMMgr_ERR_FAILED;
126  #endif
127  DIAG_TRACE(("%s : Num of Tuner Supported = %d \r\n",__FUNCTION__,infoParam->numOfTuner));
128  }
129  break;
130 
131  case TRMMgr_MSG_TYPE_GET_CONN_DEVICE_IDS:
132  {
133  DIAG_WARN(("%s : GET_CONN_DEVICE_IDS \r\n",__FUNCTION__));
134 
135  ret = TRMMgrHelperImpl::getInstance().getConnectedDeviceIdList(&length,&responseMsg[0]);
136  if(ret)
137  {
138  infoParam->bufLen = length;
139  safec_rc = memcpy_s(infoParam->buf, sizeof(infoParam->buf), responseMsg, length);
140  if(safec_rc != EOK)
141  {
142  ERR_CHK(safec_rc);
143  infoParam->retCode = TRMMgr_ERR_FAILED;
144  infoParam->bufLen = 0;
145  }
146 
147  DIAG_TRACE(("%s() Length of TRM Message is %d \r\n",__FUNCTION__,length));
148  }
149  else
150  {
151  infoParam->retCode = TRMMgr_ERR_FAILED;
152  DIAG_WARN(("Failed to Get Diag Info for GET_CONN_DEVICE_IDS \r\n"));
153  }
154  }
155  break;
156 
157  case TRMMgr_MSG_TYPE_GET_TUNER_RESERVATION:
158  {
159  DIAG_WARN(("%s : GET_TUNER_RESERVATION \r\n",__FUNCTION__));
160 
161  ret = TRMMgrHelperImpl::getInstance().getTunerReservationsList(&length,&responseMsg[0]);
162  if(ret)
163  {
164  infoParam->bufLen = length;
165  safec_rc = memcpy_s(infoParam->buf, sizeof(infoParam->buf), responseMsg, length);
166  if(safec_rc != EOK)
167  {
168  ERR_CHK(safec_rc);
169  infoParam->retCode = TRMMgr_ERR_FAILED;
170  infoParam->bufLen = 0;
171  }
172 
173 
174  DIAG_TRACE(("%s() Length of TRM Message is %d \r\n",__FUNCTION__,length));
175  }
176  else
177  {
178  infoParam->retCode = TRMMgr_ERR_FAILED;
179  DIAG_WARN(("Failed to Get Diag Info for GET_TUNER_RESERVATION \r\n"));
180  }
181  }
182  break;
183 
184  case TRMMgr_MSG_TYPE_GET_NUM_TRM_ERRORS:
185  {
186  DIAG_WARN(("%s : GET_NUM_TRM_ERRORS \r\n",__FUNCTION__));
187  ret = TRMMgrHelperImpl::getInstance().getNumofTRMErrors(&errCount);
188  if(ret)
189  {
190  infoParam->numOfTRMError = errCount;
191  DIAG_TRACE(("%s() Num of TRM Errors are %d \r\n",__FUNCTION__,errCount));
192  }
193  else
194  {
195  infoParam->retCode = TRMMgr_ERR_FAILED;
196  DIAG_WARN(("Failed to Get Diag Info for GET_CONNECTION_ERRORS \r\n"));
197  }
198  }
199  break;
200 
201  case TRMMgr_MSG_TYPE_GET_CONNECTION_ERRORS:
202  {
203  DIAG_WARN(("%s : GET_CONNECTION_ERRORS \r\n",__FUNCTION__));
204  ret = TRMMgrHelperImpl::getInstance().getTRMConnectionEventList(&length,&responseMsg[0]);
205  if(ret)
206  {
207  infoParam->bufLen = length;
208  safec_rc = memcpy_s(infoParam->buf, sizeof(infoParam->buf), responseMsg, length);
209  if(safec_rc != EOK)
210  {
211  ERR_CHK(safec_rc);
212  infoParam->retCode = TRMMgr_ERR_FAILED;
213  infoParam->bufLen = 0;
214  }
215 
216 
217  DIAG_TRACE(("%s() Length of TRM Message is %d \r\n",__FUNCTION__,length));
218  }
219  else
220  {
221  infoParam->retCode = TRMMgr_ERR_FAILED;
222  DIAG_WARN(("Failed to Get Diag Info for GET_CONNECTION_ERRORS \r\n"));
223  }
224 
225  }
226  break;
227 
228  case TRMMgr_MSG_TYPE_GET_TUNER_CONFLICTS:
229  {
230  DIAG_WARN(("%s : GET_TUNER_CONFLICTS \r\n",__FUNCTION__));
231 
232  if(ret)
233  {
234  infoParam->bufLen = length;
235  safec_rc = memcpy_s(infoParam->buf, sizeof(infoParam->buf), responseMsg, length);
236  if(safec_rc != EOK)
237  {
238  ERR_CHK(safec_rc);
239  infoParam->retCode = TRMMgr_ERR_FAILED;
240  infoParam->bufLen = 0;
241  }
242 
243 
244  DIAG_TRACE(("%s() Length of TRM Message is %d \r\n",__FUNCTION__,length));
245 
246  }
247  else
248  {
249  infoParam->retCode = TRMMgr_ERR_FAILED;
250  DIAG_WARN(("Failed to Get Diag Info for GET_TUNER_CONFLICTS \r\n"));
251  }
252  }
253  break;
254 
255  case TRMMgr_MSG_TYPE_GET_TRM_VERSION:
256  {
257  DIAG_WARN(("%s : GET_TRM_VERSION \r\n",__FUNCTION__));
258 
259  bool ret = TRMMgrHelperImpl::getInstance().getVersion(&length,&responseMsg[0]);
260 
261  if(ret)
262  {
263  infoParam->bufLen = length;
264  safec_rc = memcpy_s(infoParam->buf, sizeof(infoParam->buf), responseMsg, length);
265  if(safec_rc != EOK)
266  {
267  ERR_CHK(safec_rc);
268  infoParam->retCode = TRMMgr_ERR_FAILED;
269  infoParam->bufLen = 0;
270  }
271 
272  DIAG_TRACE(("%s() Length of TRM Message is %d \r\n",__FUNCTION__,length));
273  }
274  else
275  {
276  DIAG_WARN(("%s() Failed to Get Diag Info for GET_TRM_VERSION \r\n",__FUNCTION__));
277  }
278  }
279  break;
280 
281  default:
282  {
283  DIAG_WARN(("%s Error: Diag Info not supported \r\n",__FUNCTION__));
284  infoParam->retCode = TRMMgr_ERR_INVALID_PARAM;
285  }
286  break;
287  }
288 
289  DIAG_TRACE(("Exit %s():%d with %s \r\n" , __FUNCTION__, __LINE__,ret?"sucess":"failure"));
290 
291  return IARM_RESULT_SUCCESS;
292 }
293 
294 
295 /**************************************************************************
296 Function name : TRM Manager Main routine
297 
298 Description : Initialize with IARM and start the helper routine to get the
299 TRM Message.
300 **************************************************************************/
301 int main(int argc, char *argv[])
302 {
303 
304  uint32_t length = 0;
305  errno_t safec_rc = -1;
306 
307  /* Line Buffering*/
308  setvbuf(stdout, NULL, _IOLBF, 0);
309 
310 
311  DIAG_TRACE(("TRM Manager Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
312 
313 
314  /* Initialization with IARM and Register RPC for the clients */
315  IARM_Bus_Init(IARM_BUS_TRMMGR_NAME);
317  IARM_Bus_RegisterCall(IARM_BUS_TRMMGR_API_GetTRMDiagInfo,_GetTRMDiagInfo);
318 
319  /*Mutex Init*/
320  pthread_mutex_init(&trmMgr_mutex, NULL);
321 
322  /* Init with TRMMgr helper Implementation */
324 
325  /* Get TRM Version */
326  safec_rc = memset_s(responseMsg, sizeof(responseMsg), 0 , sizeof(responseMsg));
327  ERR_CHK(safec_rc);
328 
329  bool ret = TRMMgrHelperImpl::getInstance().getVersion(&length,&responseMsg[0]);
330 
331  while(1)
332  {
333  DIAG_WARN(("HeartBeat : TRM Manager !!\r\n"));
334  sleep(300);
335  }
336 
338  IARM_Bus_Term();
339 
340  DIAG_TRACE(("TRM Manager Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
341 
342  return 0;
343 }
344 
_TRMDiagInfo_t
Definition: TRMMgr.h:65
IARM_Bus_Term
IARM_Result_t IARM_Bus_Term(void)
This API is used to terminate the IARM-Bus library.
TRMMgrHelperImpl::getInstance
static TRMMgrHelperImpl & getInstance()
Instance of TRMMgrHelperImpl object.
Definition: Helper.cpp:182
TRMMgrHelperImpl::init
static void init()
Init and connect with TRM.
Definition: Helper.cpp:155
TRMMgrLock_
Definition: TRMMgr.cpp:64
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.
IARM_Bus_Disconnect
IARM_Result_t IARM_Bus_Disconnect(void)
This API disconnect Application from IARM Bus so the application will not receive any IARM event or R...
libIBus.h
RDK IARM-Bus API Declarations.
Messages.h
TRMMgrHelperImpl::getConnectedDeviceIdList
bool getConnectedDeviceIdList(uint32_t *length, char *responseMsg)
Send a TRM Request to get all connected Device IDs Get the Response and send to TRM Mgr.
Definition: Helper.cpp:303
TRMMgrHelperImpl::getTRMConnectionEventList
bool getTRMConnectionEventList(uint32_t *length, char *responseMsg)
Send a TRM Request to get all connected Device IDs Get the Response and send to TRM Mgr.
Definition: Helper.cpp:430
TRMMgrHelperImpl::getTunerReservationsList
bool getTunerReservationsList(uint32_t *length, char *responseMsg)
Send a TRM Request to get all Reservation Get the Response and send to TRM Mgr.
Definition: Helper.cpp:257
TRMMgrHelperImpl::getVersion
bool getVersion(uint32_t *length, char *responseMsg)
Get TRM Version Number.
Definition: Helper.cpp:194
IARM_Bus_Connect
IARM_Result_t IARM_Bus_Connect(void)
This API is used to connect application to the IARM bus daemon. After connected, the application can ...
Definition: iarmMgrMocks.cpp:33
IARM_Bus_Init
IARM_Result_t IARM_Bus_Init(const char *name)
This API is used to initialize the IARM-Bus library.
Definition: iarmMgrMocks.cpp:38