RDK Documentation (Open Sourced RDK Components)
main.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 2019 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 <iostream>
21 #include <pthread.h>
22 #include <glib.h>
23 #include "lighteningcastservice.h"
24 
25 
26 rtRemoteEnvironment* env;
27 pthread_t lcast_thread;
28 static GMainLoop *gLoop = nullptr;
30 
31 
32 
33 static gboolean pumpRemoteObjectQueue(gpointer data)
34 {
35 // printf("### %s : %s : %d ### \n",__FILE__,__func__,__LINE__);
36  rtError err;
37  GSource *source = (GSource *)data;
38  do {
39  g_source_set_ready_time(source, -1);
40  err = rtRemoteProcessSingleItem();
41  } while (err == RT_OK);
42  if (err != RT_OK && err != RT_ERROR_QUEUE_EMPTY) {
43  //printf("rtRemoteProcessSingleItem() returned %s", rtStrError(err));
44  return G_SOURCE_CONTINUE;
45  }
46  return G_SOURCE_CONTINUE;
47 }
48 
49 static GSource *attachRtRemoteSource()
50 {
51 // printf("### %s : %s : %d ###\n",__FILE__,__func__,__LINE__);
52  static GSourceFuncs g_sourceFuncs =
53  {
54  nullptr, // prepare
55  nullptr, // check
56  [](GSource *source, GSourceFunc callback, gpointer data) -> gboolean // dispatch
57  {
58  if (g_source_get_ready_time(source) != -1) {
59  g_source_set_ready_time(source, -1);
60  return callback(data);
61  }
62  return G_SOURCE_CONTINUE;
63  },
64  nullptr, // finalize
65  nullptr, // closure_callback
66  nullptr, // closure_marshall
67  };
68  GSource *source = g_source_new(&g_sourceFuncs, sizeof(GSource));
69  g_source_set_name(source, "RT Remote Event dispatcher");
70  g_source_set_can_recurse(source, TRUE);
71  g_source_set_callback(source, pumpRemoteObjectQueue, source, nullptr);
72  g_source_set_priority(source, G_PRIORITY_HIGH);
73 
74  rtError e = rtRemoteRegisterQueueReadyHandler(env, [](void *data) -> void {
75  GSource *source = (GSource *)data;
76  g_source_set_ready_time(source, 0);
77  }, source);
78 
79  if (e != RT_OK)
80  {
81  printf("Failed to register queue handler: %d", e);
82  g_source_destroy(source);
83  return nullptr;
84  }
85  g_source_attach(source, NULL);
86  return source;
87 }
88 
89 void * lcastPump(void*) {
90 
91  gLoop = g_main_loop_new( NULL , FALSE);
92  GSource *remoteSource = attachRtRemoteSource();
93 
94  if (!remoteSource)
95  printf("Failed to attach rt remote source");
96 
97  g_main_loop_run(gLoop);
98  g_source_unref(remoteSource);
99 }
100 
101 void lcastExit() {
102  printf("LighteningCastService Exit!!!\n");
103  g_main_loop_quit(gLoop);
104  pthread_join( lcast_thread, NULL );
105  rtError e = rtRemoteShutdown();
106  if (e != RT_OK){
107  printf("rtRemoteShutdown failed: %s \n", rtStrError(e));
108  }
109 }
110 
111 
112 int main() {
113  rtError err;
114  char* objName;
115 
116  env = rtEnvironmentGetGlobal();
117  err = rtRemoteInit(env);
118 
119  printf("LighteningCastService!!! Func: %s\n",__func__);
120 
121  if (err != RT_OK){
122  printf("rtRemoteinit Failed\n");
123  return 1;
124  }
125 
126  pthread_create(&lcast_thread, NULL, lcastPump, NULL);
127 
128  objName = getenv("PX_WAYLAND_CLIENT_REMOTE_OBJECT_NAME");
129  if(!objName) objName = "com.comcast.lighteningcast";
130 
131  lCastObj = new rtLighteningCastRemoteObject("com.comcast.lighteningcast");
132  err = rtRemoteRegisterObject(env, objName, lCastObj);
133  if (err != RT_OK){
134  printf("rtRemoteRegisterObject for %s failed! error:%s !\n", objName, rtStrError(err));
135  return 1;
136  }
137 
138  printf("LIGHTENINGCAST>>>>\n");
139  std::string appName;
140  std::string args;
141  int options;
142  bool exit_lcast = false;
143 
144  while(!exit_lcast) {
145  printf("Select Options:\n");
146  printf("1) launch Application\n2) stop Application\n3) query Application status\n4) Exit\n");
147  options = 0;
148  scanf(" %d",&options);
149  while(getchar() != '\n');
150 
151  switch (options)
152  {
153  case 1:
154  printf("\nLaunch Application\nApplication Name : ");
155  std::getline(std::cin,appName);
156  printf("Launch Params:\n");
157  std::getline(std::cin,args);
158  lCastObj->launchApplication(appName.c_str(),args.c_str());
159  break;
160 
161  case 2:
162  printf("\nStop Application\nApplication Name : ");
163  std::getline(std::cin,appName);
164  lCastObj->stopApplication(appName.c_str(),NULL);
165  break;
166 
167  case 3:
168  printf("\nQuery Application Status\nApplication Name : ");
169  std::getline(std::cin,appName);
170  lCastObj->getApplicationState(appName.c_str(),NULL);
171  break;
172 
173  case 4:
174  printf("\nEXIT\n");
175  lcastExit();
176  exit_lcast = true;
177  break;
178 
179  default:
180  printf("Invalid Option!!!\n");
181  break;
182  }
183  }
184 
185  return 0;
186 }
rtLighteningCastRemoteObject
Definition: lighteningcastservice.h:29
TRUE
#define TRUE
Defines for TRUE/FALSE/ENABLE flags.
Definition: wifi_common_hal.h:199