RDK Documentation (Open Sourced RDK Components)
Remote.cpp
1 /*
2  * If not stated otherwise in this file or this component's license 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 <string.h>
21 #include "libIBus.h"
22 #include "libIBusDaemon.h"
23 #include "irMgr.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <unistd.h>
28 
29 #include <linux/input.h>
30 #include <linux/uinput.h>
31 #include <fcntl.h>
32 #include "XFCIrKeyCodes.h"
33 
34 #define IR_REMOTE_APP_INIT_STR "IrRemoteApp"
35 
36 
37 #define IOCTL_CALL(fd, keybit, keyvalue) \
38  if (ioctl (fd, keybit, keyvalue) < 0) {\
39  printf("Failed at %d = %d",keyvalue , errno); \
40  }
41 
42 int gPipefd[2];
43 int uinp_fd;
44 struct uinput_user_dev uinp;
45 struct input_event event;
46 
47 static void _evtHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len);
48 static void KeyProcessHandler(int,int);
49 static void IRkeyhandler(int,int);
50 static void SendKeyToKernel(int);
51 static void RegisterDevice();
52 
53 int main(int argc, char** argv)
54 {
55 
56  (void) argc;
57  (void) argv;
58 
59  char Init_Str[] = IR_REMOTE_APP_INIT_STR;
60 
61  IARM_Bus_Init(Init_Str);
64  RegisterDevice();
65 
66  while(1)
67  {
68  sleep(5);
69  }
70 }
71 
72 void KeyProcessHandler(int KeyType,int KeyCode)
73 {
74 
75  switch (KeyType)
76  {
77  case KET_KEYDOWN:
78  printf("KET_KEYDOWN\n");
79  IRkeyhandler(KeyType, KeyCode);
80  break;
81  case KET_KEYUP:
82  // IRkeyhandler(KeyType, KeyCode);
83  break;
84  case KET_KEYREPEAT:
85  // IRkeyhandler(KeyCode, KeyType);
86  break;
87  default:
88  /* TBD: Support for mouse and keypad */
89  break;
90  }
91 
92 
93 }
94 
95 //Open uinput device node and register this application
96 void RegisterDevice()
97 {
98 
99  uinp_fd = open("/dev/uinput", O_WRONLY|O_NDELAY);
100 
101  if(uinp_fd < 0)
102  {
103  printf("Unable to open /dev/uinput\n");
104  return;
105  }
106 
107  memset(&uinp,0,sizeof(uinp));
108 
109  snprintf(uinp.name, UINPUT_MAX_NAME_SIZE, "Ir Keyboard");
110  uinp.id.bustype = BUS_USB;
111  uinp.id.version = 1;
112  uinp.id.vendor = 0x1234;
113  uinp.id.product = 0xfedc;
114 
115  write(uinp_fd, &uinp, sizeof(uinp));
116 
117  //ioctl(uinp_fd, UI_DEV_CREATE)
118  /****** Setup the uinput keyboard device section: **********/
119 
120  IOCTL_CALL(uinp_fd, UI_SET_EVBIT, EV_KEY);
121  IOCTL_CALL(uinp_fd, UI_SET_EVBIT, EV_SYN);
122  //ioctl(uinp_fd, UI_SET_EVBIT, EV_REP);
123 
124  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_H);
125  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_R);
126  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_P);
127  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_U);
128  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_D);
129  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_S);
130  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_F);
131  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_X);
132  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_Z);
133  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_UP);
134  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_DOWN);
135  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_LEFT);
136  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_RIGHT);
137  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_SPACE);
138  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_ESC);
139 
140  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_0);
141  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_1);
142  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_2);
143  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_3);
144  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_4);
145  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_5);
146  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_6);
147  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_7);
148  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_8);
149  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_9);
150 
151  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_VOLUMEUP);
152  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_VOLUMEDOWN);
153  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_MUTE);
154 
155  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_F2);
156  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_F3);
157  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_F5);
158 
159  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_ENTER);
160  IOCTL_CALL(uinp_fd, UI_SET_KEYBIT, KEY_BACKSPACE);
161 
162  IOCTL_CALL(uinp_fd, UI_DEV_CREATE, NULL);
163 
164  sleep(2);
165 }
166 
167 void IRkeyhandler(int KeyType,int KeyCode)
168 {
169  (void) KeyType;
170 
171  //test check
172  switch(KeyCode)
173  {
174  case 192:
175  SendKeyToKernel(KEY_H);
176  break;
177  case KED_ARROWUP:
178  SendKeyToKernel(KEY_UP);
179  break;
180 
181  case KED_ARROWDOWN:
182  SendKeyToKernel(KEY_DOWN);
183  break;
184 
185  case KED_ARROWLEFT:
186  SendKeyToKernel(KEY_LEFT);
187  break;
188 
189  case KED_ARROWRIGHT:
190  SendKeyToKernel(KEY_RIGHT);
191  break;
192  case 133:
193  SendKeyToKernel(KEY_SPACE);
194  break;
195  case 135:
196  SendKeyToKernel(KEY_ESC);
197  break;
198  case KED_DIGIT0 :
199  SendKeyToKernel(KEY_0);
200  break;
201  case KED_DIGIT1 :
202  SendKeyToKernel(KEY_1);
203  break;
204  case KED_DIGIT2 :
205  SendKeyToKernel(KEY_2);
206  break;
207  case KED_DIGIT3 :
208  SendKeyToKernel(KEY_3);
209  break;
210  case KED_DIGIT4 :
211  SendKeyToKernel(KEY_4);
212  break;
213  case KED_DIGIT5 :
214  SendKeyToKernel(KEY_5);
215  break;
216  case KED_DIGIT6 :
217  SendKeyToKernel(KEY_6);
218  break;
219  case KED_DIGIT7 :
220  SendKeyToKernel(KEY_7);
221  break;
222  case KED_DIGIT8 :
223  SendKeyToKernel(KEY_8);
224  break;
225  case KED_DIGIT9 :
226  SendKeyToKernel(KEY_9);
227  break;
228  case KED_CHANNELUP :
229  SendKeyToKernel(KEY_U);
230  break;
231  case KED_CHANNELDOWN :
232  SendKeyToKernel(KEY_D);
233  break;
234  case KED_VOLUMEUP :
235  SendKeyToKernel(KEY_VOLUMEUP);
236  break;
237  case KED_VOLUMEDOWN :
238  SendKeyToKernel(KEY_VOLUMEDOWN);
239  break;
240  case KED_MUTE:
241  SendKeyToKernel(KEY_MUTE);
242  break;
243  case KED_PLAY:
244  SendKeyToKernel(KEY_P);
245  break;
246  case KED_PAUSE:
247  SendKeyToKernel(KEY_P);
248  break;
249  case KED_LAST:
250  SendKeyToKernel(KEY_ENTER);
251  break;
252  case KED_FASTFORWARD:
253  SendKeyToKernel(KEY_F);
254  break;
255  case KED_REWIND:
256  SendKeyToKernel(KEY_R);
257  break;
258  case KED_STOP:
259  SendKeyToKernel(KEY_STOP);
260  break;
261  case KED_REPLAY:
262  SendKeyToKernel(KEY_S);
263  break;
264  case KED_PAGEUP:
265  SendKeyToKernel(KEY_Z);
266  break;
267  case KED_PAGEDOWN:
268  SendKeyToKernel(KEY_X);
269  break;
270  default:
271  break;
272  }
273 
274 }
275 
276 //send Events to kernel
277 void SendKeyToKernel(int code)
278 {
279  printf("Inside SendKeytoKernel\n");
280  memset(&event, 0, sizeof(event));
281  //gettimeofday(&event.time, NULL);
282 
283  //Send key down event
284  event.type = EV_KEY;
285  event.code = code;
286  event.value = 1;
287  write(uinp_fd, &event, sizeof(event));
288 
289  memset(&event, 0, sizeof(event));
290  event.type = EV_KEY;
291  event.code = code;
292  event.value = 0;
293  write(uinp_fd, &event, sizeof(event));
294 
295  event.type = EV_SYN;
296  event.code = SYN_REPORT;
297  event.value = 1;
298  write(uinp_fd, &event, sizeof(event));
299 
300  printf("keyevent send to the kernel\n");
301 
302 }
303 
304 //IR remote integration
305 void _evtHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len)
306 {
307  (void) len;
308 
309  if (strcmp(owner, IARM_BUS_IRMGR_NAME) == 0)
310  {
311  switch (eventId) {
313  {
315  int keyCode = irEventData->data.irkey.keyCode;
316  int keyType = irEventData->data.irkey.keyType;
317  printf("Receiver Get IR KeyCode: %d-",keyCode);
318  KeyProcessHandler(keyType, keyCode);
319  }
320  break;
321  default:
322  break;
323  }
324 
325  }
326 }
327 
328 
329 
330 
331 
332 
IARM_BUS_IRMGR_EVENT_IRKEY
@ IARM_BUS_IRMGR_EVENT_IRKEY
Definition: irMgr.h:220
IARM_Bus_RegisterEventHandler
IARM_Result_t IARM_Bus_RegisterEventHandler(const char *ownerName, IARM_EventId_t eventId, IARM_EventHandler_t handler)
This API register to listen to event and provide the callback function for event notification....
Definition: iarmMgrMocks.cpp:43
libIBus.h
RDK IARM-Bus API Declarations.
IARM_BUS_IRMGR_NAME
#define IARM_BUS_IRMGR_NAME
Definition: irMgr.h:216
keyType
Definition: reset.c:85
irMgr.h
IARM-Bus IR Manager API.
_IRMgr_EventData_t
Definition: irMgr.h:235
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