RDK Documentation (Open Sourced RDK Components)
irMgrMain.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 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 * @defgroup iarmmgrs
22 * @{
23 * @defgroup ir
24 * @{
25 **/
26 
27 
28 #include "irMgrInternal.h"
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include "plat_ir.h"
37 #include "libIBus.h"
38 #ifdef __cplusplus
39 }
40 #endif
41 #ifdef RDK_LOGGER_ENABLED
42 
43 int b_rdk_logger_enabled = 0;
44 
45 void logCallback(const char *buff)
46 {
47  LOG("%s",buff);
48 }
49 
50 #endif
51 #include "irMgr.h"
52 #include "cap.h"
53 #ifdef ENABLE_SD_NOTIFY
54 #include <systemd/sd-daemon.h>
55 #endif
56 
57 #define RFC_BUFFER_SIZE 100
58 int get_irmgr_RFC_config()
59 {
60  int is_enabled = 0;
61  const char * cmd = "tr181Set -g Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.XRRemoteAsLinuxDevice.Enable 2>&1 1>/dev/null";
62  /*Note: the above redirection is necessary because tr181 prints the value of parameter to stderr instead of stdout. */
63  printf("%s: getting RFC config using command \"%s\"\n", __FUNCTION__, cmd);
64  FILE * pipe = popen(cmd, "r");
65  if(pipe)
66  {
67  char buffer[RFC_BUFFER_SIZE];
68  if(NULL == fgets(buffer, RFC_BUFFER_SIZE, pipe))
69  {
70  printf("%s: could not parse output of command <%s>.\n", __FUNCTION__, cmd);
71  }
72  else
73  {
74  printf("%s: read output \"%s\"\n", __FUNCTION__, buffer);
75  if(0 == strncasecmp(buffer, "true", 4))
76  {
77  is_enabled = 1;
78  }
79  }
80  }
81  printf("%s: the feature is %s.\n", __FUNCTION__, (1 == is_enabled? "enabled" : "disabled"));
82  pclose(pipe);
83  return is_enabled;
84 }
85 
86 static bool drop_root()
87 {
88  bool ret = false,retval = false;
89  cap_user appcaps = {{0, 0, 0, '\0', 0, 0, 0, '\0'}};
90  ret = isBlocklisted();
91  if(ret)
92  {
93  LOG("NonRoot feature is disabled\n");
94  }
95  else
96  {
97  LOG("NonRoot feature is enabled\n");
98  appcaps.caps = NULL;
99  appcaps.user_name = NULL;
100  if(init_capability() != NULL) {
101  if(drop_root_caps(&appcaps) != -1) {
102  if(update_process_caps(&appcaps) != -1) {
103  read_capability(&appcaps);
104  retval = true;
105  }
106  }
107  }
108  }
109  return retval;
110 }
111 
112 
113 int main(int argc, char *argv[])
114 {
115  const char* debugConfigFile = NULL;
116  int itr=0;
117 
118  while (itr < argc)
119  {
120  if(strcmp(argv[itr],"--debugconfig")==0)
121  {
122  itr++;
123  if (itr < argc)
124  {
125  debugConfigFile = argv[itr];
126  }
127  else
128  {
129  break;
130  }
131  }
132  itr++;
133  }
134 
135 #ifdef RDK_LOGGER_ENABLED
136 
137  if(rdk_logger_init(debugConfigFile) == 0) b_rdk_logger_enabled = 1;
138  IARM_Bus_RegisterForLog(logCallback);
139 
140 #endif
141  if(!drop_root())
142  {
143  LOG("drop_root function failed!\n");
144  }
145 
146 
147  int inputEnabled = get_irmgr_RFC_config();
148  if(0 == inputEnabled)
149  {
150  //Dev-friendly testing aid in case TR181 method isn't available/functional.
151  //TODO: This needs to be removed once TR181 method is verified to be fully functional
152  inputEnabled = (access("/opt/remote_input_enable", F_OK) == 0);
153  }
154 #ifdef ENABLE_LINUX_REMOTE_KEYS
155  inputEnabled = 1;
156  printf("%s: Linux key proceesing enabbled default for RNE build\n",__FUNCTION__);
157 #endif
158  if (inputEnabled) {
159  UINPUT_init();
161  }
162 
163  IRMgr_Start(argc, argv);
164  #ifdef ENABLE_SD_NOTIFY
165  sd_notifyf(0, "READY=1\n"
166  "STATUS=IRMgr is Successfully Initialized\n"
167  "MAINPID=%lu", (unsigned long) getpid());
168  #endif
169 
170 #ifdef PID_FILE_PATH
171 #define xstr(s) str(s)
172 #define str(s) #s
173  // write pidfile because sd_notify() does not work inside container
174  IARM_Bus_WritePIDFile(xstr(PID_FILE_PATH) "/irmgr.pid");
175 #endif
176 
177  IRMgr_Loop();
178  IRMgr_Stop();
179 
180  if (inputEnabled) {
181  UINPUT_term();
182  }
183  return 0;
184 }
185 
186 
187 
188 
189 /** @} */
190 /** @} */
UINPUT_init
int UINPUT_init(void)
uinput module init.
Definition: uinput.c:225
IRMgr_Register_uinput
IARM_Result_t IRMgr_Register_uinput(uinput_dispatcher_t f)
register a uinput dispatcher.
Definition: irMgr.c:145
IRMgr_Start
IARM_Result_t IRMgr_Start(int argc, char *argv[])
Starts the IR manager.
Definition: irMgr.c:151
IRMgr_Stop
IARM_Result_t IRMgr_Stop(void)
Terminates the IR manager.
Definition: irMgr.c:235
IARM_Bus_WritePIDFile
void IARM_Bus_WritePIDFile(const char *path)
Write PID file.
irMgrInternal.h
IARM-Bus IR Manager Internal API.
libIBus.h
RDK IARM-Bus API Declarations.
UINPUT_GetDispatcher
uinput_dispatcher_t UINPUT_GetDispatcher(void)
get the dispather that will listen for IARM IR
Definition: uinput.c:325
IRMgr_Loop
IARM_Result_t IRMgr_Loop()
Loop to keep the IR manager alive.
Definition: irMgr.c:221
irMgr.h
IARM-Bus IR Manager API.
rdk_logger_init
rdk_Error rdk_logger_init(const char *debugConfigFile)
Initialize the logger. Sets up the environment variable storage by parsing debug configuration file t...
Definition: rdk_logger_init.c:57
UINPUT_term
int UINPUT_term(void)
uinput module term.
Definition: uinput.c:339