Modify dialserver to run on GFHD100

Disabled Netflix support until it is updated, fixed launching
and status of YouTube.

Change-Id: I211f307f466821e12166f1e078e0ddc4000ede25
diff --git a/src/server/dial_options.h b/src/server/dial_options.h
index 9d67392..6c24a1b 100644
--- a/src/server/dial_options.h
+++ b/src/server/dial_options.h
@@ -26,14 +26,6 @@
 #ifndef DIAL_OPTIONS_H
 #define DIAL_OPTIONS_H
 
-#define DATA_PATH_OPTION "-D"
-#define DATA_PATH_OPTION_LONG "--data-path"
-#define DATA_PATH_DESCRIPTION "Path to netflix secure store"
-
-#define NETFLIX_PATH_OPTION "-N"
-#define NETFLIX_PATH_OPTION_LONG "--netflix-path"
-#define NETFLIX_PATH_DESCRIPTION "Path to Netflix application"
-
 #define FRIENDLY_NAME_OPTION "-F"
 #define FRIENDLY_NAME_OPTION_LONG "--friendly-name"
 #define FRIENDLY_NAME_DESCRIPTION "Device Friendly Name"
@@ -56,16 +48,6 @@
 struct dial_options gDialOptions[] = 
 {
     {
-        DATA_PATH_OPTION,
-        DATA_PATH_OPTION_LONG,
-        DATA_PATH_DESCRIPTION
-    },
-    {
-        NETFLIX_PATH_OPTION,
-        NETFLIX_PATH_OPTION_LONG,
-        NETFLIX_PATH_DESCRIPTION
-    },
-    {
         FRIENDLY_NAME_OPTION,
         FRIENDLY_NAME_OPTION_LONG,
         FRIENDLY_NAME_DESCRIPTION,
diff --git a/src/server/main.c b/src/server/main.c
index aac852e..305bdcd 100644
--- a/src/server/main.c
+++ b/src/server/main.c
@@ -40,29 +40,19 @@
 
 #define BUFSIZE 256 
 
-static char *spAppNetflix = "netflix";      // name of the netflix executable
-static char *spSecret = "12345678910shhhNFLXwashereman";
-static char *spDefaultNetflix = "../../../src/platform/qt/netflix";
-static char *spDefaultData="../../../src/platform/qt/data";
-static char *spNfDataDir = "NF_DATA_DIR=";
-static char *defaultLaunchParam = "source_type=12";
-static char *spDefaultFriendlyName = "DIAL server sample";
-static char *spDefaultModelName = "NOT A VALID MODEL NAME";
-static char *spDefaultUuid = "deadbeef-dead-beef-dead-beefdeadbeef";
-static char spDataDir[BUFSIZE];
-static char spNetflix[BUFSIZE];
+static char *spDefaultFriendlyName = "TV Box";
+static char *spDefaultModelName = "GFHD100";
+static char *spDefaultUuid = "0";
 static char spFriendlyName[BUFSIZE];
 static char spModelName[BUFSIZE];
 static char spUuid[BUFSIZE];
 static int gDialPort;
 
-static char *spAppYouTube = "chrome";
-static char *spAppYouTubeMatch = "chrome.*google-chrome-dial";
-static char *spAppYouTubeExecutable = "/opt/google/chrome/google-chrome";
-static char *spYouTubePS3UserAgent = "--user-agent="
-    "Mozilla/5.0 (PS3; Leanback Shell) AppleWebKit/535.22 (KHTML, like Gecko) "
-    "Chrome/19.0.1048.0 LeanbackShell/01.00.01.73 QA Safari/535.22 Sony PS3/ "
-    "(PS3, , no, CH)";
+static char *spAppNetflix = "netflix";      // name of the netflix executable
+static char *defaultLaunchParam = "source_type=12";
+
+static char *spAppYouTube = "content_shell";
+static char *spAppYouTubeMatch = "www.youtube.com/tv";
 
 // Adding 20 bytes for prepended source_type for Netflix
 static char sQueryParam[DIAL_MAX_PAYLOAD+20];
@@ -190,16 +180,34 @@
   return 0;
 }
 
+/* Running an application is done through the runminiclient script */
 static pid_t runApplication( const char * const args[], DIAL_run_t *run_id ) {
+  const char * const script_args[] = {"/etc/init.d/S99miniclient", "restart", 0};
+
+  /* Write application information to /tmp/runapp */
+  FILE *runapp = fopen("/tmp/runapp","w");
+  if (!runapp) {
+    fprintf(stderr, "Couldn't open /tmp/runapp file\n");
+    return kDIALStatusStopped;
+  }
+  for(int i = 0; args[i]; ++i) {
+    int outputCharacters = fprintf(runapp, "%s ", args[i]);
+    if (outputCharacters<0) {
+      fprintf(stderr, "Error writing to /tmp/runapp file\n");
+    }
+  }
+  fsync(fileno(runapp));
+  fclose(runapp);
+  runapp=NULL;
+
+
   pid_t pid = fork();
   if (pid != -1) {
     if (!pid) { // child
-      putenv(spDataDir);
-      printf("Execute:\n");
-      for(int i = 0; args[i]; ++i) {
-        printf(" %d) %s\n", i, args[i]);
-      }
-      execv(*args, (char * const *) args);
+      execv(*script_args, (char * const *) script_args);
+      // It won't reach this unless there was an error
+      fprintf(stderr, "Error executing %s\n", *script_args);
+      exit(1);
     } else {
       *run_id = (void *)(long)pid; // parent PID
     }
@@ -225,7 +233,7 @@
 static DIALStatus youtube_start(DIALServer *ds, const char *appname,
                                 const char *args, size_t arglen,
                                 DIAL_run_t *run_id, void *callback_data) {
-    printf("\n\n ** LAUNCH YouTube **\n\n");
+    printf("** LAUNCH YouTube **\n");
 
     // TODO(steineldar): Verify that the args string is a valid URL query param.
     for (int n = 0; args[n] != 0; ++n) {
@@ -240,13 +248,14 @@
       }
     }
 
-    char url[512] = {0,}, data[512] = {0,};
-    sprintf( url, "https://www.youtube.com/tv?%s", args);
-    sprintf( data, "--user-data-dir=%s/.config/google-chrome-dial", getenv("HOME") );
+    char url[512] = {0,};
+    int urlLength = snprintf( url, sizeof(url), "https://www.youtube.com/tv?%s", args);
+    if (urlLength>=sizeof(url)) {
+      fprintf(stderr, "Warning, YouTube URL was truncated (%d>=%d)\n", urlLength, sizeof(url));
+    }
 
-    const char * const youtube_args[] = { spAppYouTubeExecutable,
-      spYouTubePS3UserAgent,
-      data, "--app", url, NULL
+    const char * const youtube_args[] = { "youtube",
+      url, NULL
     };
     runApplication( youtube_args, run_id );
 
@@ -262,7 +271,7 @@
 
 static void youtube_stop(DIALServer *ds, const char *appname, DIAL_run_t run_id,
                          void *callback_data) {
-    printf("\n\n ** KILL YouTube **\n\n");
+    printf("** KILL YouTube **\n");
     pid_t pid;
     if ((pid = isAppRunning( spAppYouTube, spAppYouTubeMatch ))) {
         kill(pid, SIGTERM);
@@ -275,11 +284,11 @@
     int shouldRelaunchApp = 0;
     int payloadLen = 0;
     int appPid = 0;
-  
+
     // only launch Netflix if it isn't running
     appPid = isAppRunning( spAppNetflix, NULL );
     shouldRelaunchApp = shouldRelaunch( ds, appname, args );
-  
+
     // construct the payload to determine if it has changed from the previous launch
     payloadLen = strlen(args);
     memset( sQueryParam, 0, DIAL_MAX_PAYLOAD );
@@ -295,17 +304,17 @@
             free( pUrlEncodedParams );
         }
     }
-  
+
     printf("appPid = %s, shouldRelaunch = %s queryParams = %s\n",
           appPid?"TRUE":"FALSE", 
           shouldRelaunchApp?"TRUE":"FALSE",
           sQueryParam );
-  
+
     // if its not running, launch it.  The Netflix application should
     // never be relaunched
     if( !appPid )
     {
-        const char * const netflix_args[] = {spNetflix, "-s", spSecret, "-Q", sQueryParam, 0};
+        const char * const netflix_args[] = {"netflix", "-Q", sQueryParam, 0};
         return runApplication( netflix_args, run_id );
     }
     else return kDIALStatusRunning;
@@ -355,12 +364,6 @@
     memcpy( dest, pSource, strlen(pSource) );
 }
 
-static void setDataDir(char *pData)
-{
-    setValue( spNfDataDir, spDataDir );
-    strcat(spDataDir, pData);
-}
-
 void runDial(void)
 {
     DIALServer *ds;
@@ -368,12 +371,14 @@
     struct DIALAppCallbacks cb_nf = {netflix_start, netflix_stop, netflix_status};
     struct DIALAppCallbacks cb_yt = {youtube_start, youtube_stop, youtube_status};
 
-    DIAL_register_app(ds, "Netflix", &cb_nf, NULL);
+    /* Until we update the Netflix app to version4.0 we must disable Netflix DIAL support */
+    (void)(cb_nf);
+    //DIAL_register_app(ds, "Netflix", &cb_nf, NULL);
     DIAL_register_app(ds, "YouTube", &cb_yt, NULL);
     gDialPort = DIAL_get_port(ds);
     printf("launcher listening on gDialPort %d\n", gDialPort);
     run_ssdp(gDialPort, spFriendlyName, spModelName, spUuid);
-    
+
     DIAL_stop(ds);
 }
 
@@ -381,20 +386,13 @@
 {
     switch(index)
     {
-        case 0: // Data path
-            memset( spDataDir, 0, sizeof(spDataDir) );
-            setDataDir( pOption );
-            break;
-        case 1: // Netflix path
-            setValue( pOption, spNetflix );
-            break;
-        case 2: // Friendly name
+        case 0: // Friendly name
             setValue( pOption, spFriendlyName ); 
             break;
-        case 3: // Model Name
+        case 1: // Model Name
             setValue( pOption, spModelName );
             break;
-        case 4: // UUID 
+        case 2: // UUID
             setValue( pOption, spUuid );
             break;
         default:
@@ -416,8 +414,6 @@
     setValue(spDefaultFriendlyName, spFriendlyName );
     setValue(spDefaultModelName, spModelName );
     setValue(spDefaultUuid, spUuid );
-    setValue(spDefaultNetflix, spNetflix );
-    setDataDir(spDefaultData);
 
     // Process command line options
     // Loop through pairs of command line options.