testframework: support for w/o usb logging for testframework

1)support scripting
2)support for command line args which helps in logging w/o usb

Change-Id: I59de8b11fb75dbc254d89106717ffbc68fb43cd3
diff --git a/Android.mk b/Android.mk
index a648c58..c82d8bd 100644
--- a/Android.mk
+++ b/Android.mk
@@ -9,17 +9,23 @@
         src/TestFrameworkApi.cpp \
         src/TestFrameworkCommon.cpp \
         src/TestFrameworkHash.cpp \
-        src/TestFramework.cpp \
         src/TestFrameworkService.cpp
 
 LOCAL_CFLAGS := -DCUSTOM_EVENTS_TESTFRAMEWORK
 
 LOCAL_C_INCLUDES := $(TOP)/vendor/qcom/opensource/testframework
 
-LOCAL_SHARED_LIBRARIES += \
-        libutils \
-        libcutils \
-        libbinder
+LOCAL_SHARED_LIBRARIES += libutils libcutils
+
+ifeq ($(TF_FEATURE_USES_BINDER),true)
+LOCAL_CFLAGS += -DTF_FEATURE_USE_BINDER
+LOCAL_SRC_FILES += src/TestFramework.cpp
+LOCAL_SHARED_LIBRARIES += libbinder
+endif
+
+ifeq ($(call is-android-codename,JELLY_BEAN),true)
+LOCAL_CFLAGS += -DJB
+endif
 
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE:= libtestframework
@@ -34,9 +40,19 @@
 
 LOCAL_C_INCLUDES := vendor/qcom/opensource/testframework
 
-LOCAL_SHARED_LIBRARIES := libtestframework libcutils libutils libbinder
+LOCAL_SHARED_LIBRARIES := libtestframework libcutils libutils
 
 LOCAL_CFLAGS := -DCUSTOM_EVENTS_TESTFRAMEWORK
+
+ifeq ($(TF_FEATURE_USES_BINDER),true)
+LOCAL_CFLAGS += -DTF_FEATURE_USE_BINDER
+LOCAL_SHARED_LIBRARIES += libbinder
+endif
+
+ifeq ($(call is-android-codename,JELLY_BEAN),true)
+LOCAL_CFLAGS += -DJB
+endif
+
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE:= testframeworkservice
 
diff --git a/src/TFSShell.cpp b/src/TFSShell.cpp
index 70ff980..0a80400 100644
--- a/src/TFSShell.cpp
+++ b/src/TFSShell.cpp
@@ -48,6 +48,7 @@
 #include <binder/IPCThreadState.h>
 #include <binder/ProcessState.h>
 #include "TestFrameworkService.h"
+#include "TestFramework.h"
 #include "TFSShell.h"
 
 #define TFS_VERSION "TF v2.0"
@@ -58,11 +59,13 @@
 TFSShell::TFSShell() {
     mBuf = (char *) malloc(sizeof(char) * MAX_BUFLEN);
     mPath = (char *) malloc(sizeof(char) * MAX_PATHLEN);
+    mCmds =  (char *) malloc(sizeof(char) * MAX_PATHLEN);
     if(mPath) {
         mPath[0] = '/';
         mPath[1] = '\0';
     }
     mService = NULL;
+    mQuit = false;
 }
 
 TFSShell::~TFSShell() {
@@ -74,17 +77,16 @@
         free(mPath);
         mPath = NULL;
     }
+    if(mCmds) {
+        free(mCmds);
+        mCmds = NULL;
+    }
 }
 
-int TFSShell::MainLoop(ITestFrameworkService *service) {
-    char *cmds = NULL;
-    bool quit = false;
-    char *arg = NULL;
-    FILE *fp = NULL;
+int TFSShell::MainLoop(ITestFrameworkService *service, char *script) {
     int ret = -1;
-    int n = 0, fd = -1, fd2 = -1;
-    int len = 0, arglen = 0;
-    int total = 0;
+    int arglen = 0;
+    FILE *fp = stdin;
 
     if(!mBuf) {
         return -1;
@@ -92,379 +94,432 @@
 
     mService = service;
 
-    cmds =  (char *) malloc(sizeof(char) * MAX_PATHLEN);
+    if(script) {
+        fp = fopen(script, "rt");
+        if(NULL == fp) {
+            TFSTtyPrint("Script does not exist");
+            return -1;
+        }
+    }
 
-    while(!quit) {
+    while(!mQuit && !feof(fp)) {
         TFSTtyPrint(PROMPT);
         ret = -1;
-        fgets(mBuf, MAX_BUFLEN, stdin);
+        fgets(mBuf, MAX_BUFLEN, fp);
         arglen = strlen(mBuf);
         if(arglen > 0 && mBuf[arglen-1] == '\n') {
             mBuf[arglen-1] = '\0';
         }
-        arg = TFSGetCmdArg(mBuf, arglen);
-        switch(mBuf[0]) {
-        case 'h':
-        case 'H':
-            //help
-            if(arglen != 4 || strncmp(mBuf, "help", 4)) {
-                break;
-            }
-
-            TFSCmdHint("help");
-            ret = 0;
-            break;
-
-        case 'v':
-        case 'V':
-            TFSTtyPrint(TFS_VERSION);
-            ret = 0;
-            break;
-
-        case 'e':
-        case 'E':
-            //exit
-            if(arglen == 4 && !strncmp(mBuf, "exit", 4)) {
-                property_set("debug.tf.exit", "1");
-                quit = true;
-                ret = 0;
-            }
-            else if(arglen == 9 && !strncmp(mBuf, "eventtype", 9)) {
-                arg = TFSGetCmdArg(arg+arglen, arglen);
-                //eventtype <type>
-                if(arg) {
-                    arg[arglen] = '\0';
-                    property_set("debug.tf.eventtype", arg);
-                }
-                else {
-                    property_get("debug.tf.eventtype", mBuf, "0");
-                    TFSTtyPrint(mBuf, strlen(mBuf));
-                }
-                ret = 0;
-            }
-            break;
-
-        case 'k':
-            if(arglen != 8 || strncmp(mBuf, "kfilters", 4)) {
-                break;
-            }
-            //kfilters [available|list|set <filters>]
-            //filters := !|+<subsystem>:<event>
-            arg = TFSGetCmdArg(arg+arglen, arglen);
-            if(NULL == arg) {
-                //list
-                fd = open(NODE_STR(set_event), O_RDONLY);
-                while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
-                    mBuf[n-1] = '\0';
-                    TFSTtyPrint(mBuf, n);
-                }
-                close(fd);
-            }
-            else if(arg[0] == 'l') {
-                //list
-                fd = open(NODE_STR(set_event), O_RDONLY);
-                while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
-                    mBuf[n-1] = '\0';
-                    TFSTtyPrint(mBuf, n);
-                }
-                close(fd);
-            }
-            else if(arg[0] == 's') {
-                //set <filters>
-                arg = TFSGetCmdArg(arg+arglen, arglen);
-                if(arg) {
-                    char filter[MAX_FILTERLEN];
-                    if(arg[0] == '+') {
-                        arg = TFSGetCmdArg(arg+1, arglen);
-                        fd = open(NODE_STR(set_event), O_WRONLY);
-                    }
-                    else {
-                        fd = open(NODE_STR(set_event), O_WRONLY|O_TRUNC);
-                    }
-                    if(arg) {
-                        arg[arglen] = '\0';
-                    }
-                    arg = TFSGetFilterArg(arg, arglen);
-                    while(arg) {
-                        strlcpy(filter, arg, TFS_MIN(arglen+1, MAX_FILTERLEN));
-                        n = write(fd, filter, arglen);
-                        arg = TFSGetFilterArg(arg+arglen, arglen);
-                    }
-                    close(fd);
-                }
-                else {
-                    //empty filters
-                    fd = open(NODE_STR(set_event), O_WRONLY|O_TRUNC);
-                    close(fd);
-                }
-            }
-            else if(arg[0] == 'a') {
-                //available events
-                fd = open(NODE_STR(available_events), O_RDONLY);
-                while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
-                    mBuf[n-1] = '\0';
-                    TFSTtyPrint(mBuf, n);
-                }
-                close(fd);
-            }
-            else {
-                TFSCmdHint("kfilters");
-            }
-            ret = 0;
-            break;
-
-        case 'f':
-            if(arglen != 7 || strncmp(mBuf, "filters", 7)) {
-                break;
-            }
-            //filters [available|list|set <filters>]
-            arg = TFSGetCmdArg(arg+arglen, arglen);
-            if(NULL == arg) {
-                property_get("debug.tf.filters", mBuf, "");
-                TFSTtyPrint(mBuf, strlen(mBuf));
-            }
-            else if(arg[0] == 's' || arg[0] == 's') {
-                //ufilters [available|list|set <filters>]
-                //filters := !|+<subsystem>:<event>
-                bool bRemove = false;
-                arg = (char *) TFSGetCmdArg(arg+arglen, arglen);
-                if(arg) {
-                    char filters[MAX_FILTERLEN];
-                    char * str = NULL;
-                    filters[0] = '\0';
-                    if(arg[0] == '+') {
-                        arg = (char *)TFSGetCmdArg(arg+1, arglen);
-                        if(arg) {
-                            arg[arglen] = '\0';
-                        }
-                        property_get("debug.tf.filters", filters, NULL);
-                        arg = TFSGetFilterArg(arg, arglen);
-                        while(arg) {
-                            bRemove = false;
-                            arg[arglen] = '\0';
-                            if (arg[0] == '!') {
-                                arg++;
-                                arglen--;
-                                bRemove = true;
-                            }
-                            str = strstr(filters, arg);
-                            if (str) {
-                                if (bRemove) {
-                                    if ((filters < str) && (*(str-1) == '%'))
-                                        strlcpy(str-1, str+arglen, MAX_FILTERLEN - (str - filters));
-                                    else if(filters+strlen(filters) > str+arglen && *(str+arglen) == '%') {
-                                        strlcpy(str, str+arglen+1, MAX_FILTERLEN - (str - filters));
-                                    }
-                                    else
-                                        strlcpy(str, str+arglen, MAX_FILTERLEN);
-                                }
-                            }
-                            else if (!bRemove) {
-                                if (filters[0] != '\0')
-                                    strlcat(filters, "%", MAX_FILTERLEN);
-                                strlcat(filters, arg, MAX_FILTERLEN);
-                            }
-                            arg = TFSGetFilterArg(arg+arglen+1, arglen);
-                        }
-                    }
-                    else {
-                        arg[arglen] = '\0';
-                        filters[0] = '\0';
-                        arg = TFSGetFilterArg(arg, arglen);
-                        while(arg) {
-                            arg[arglen] = '\0';
-                            if (arg[0] == '!') {
-                                arg++;
-                                arglen--;
-                            }
-                            else {
-                                str = strstr(filters, arg);
-                                if (!str) {
-                                    if (filters[0] != '\0')
-                                        strlcat(filters, "%", MAX_FILTERLEN);
-                                    strlcat(filters, arg, MAX_FILTERLEN);
-                                }
-                            }
-                            arg = TFSGetFilterArg(arg+arglen+1, arglen);
-                        }
-                    }
-                    property_set("debug.tf.filters", filters);
-                }
-                else {
-                    property_set("debug.tf.filters", "$.$");
-                }
-            }
-            else if(arg[0] == 'l') {
-                property_get("debug.tf.filters", mBuf, "");
-                TFSTtyPrint(mBuf, strlen(mBuf));
-            }
-            else if(arg[0] == 'a') {
-                //not supported yet
-                property_get("debug.tf.filters", mBuf, "");
-                TFSTtyPrint(mBuf, strlen(mBuf));
-            }
-            else {
-                TFSCmdHint("filters");
-            }
-            ret = 0;
-            break;
-
-        case 's':
-            if(!strncmp(mBuf, "start", 5)) {
-                //start
-                arg = TFSGetCmdArg(arg+arglen, arglen);
-                if(arg && (arg[0] == 'l' || arg[0] == 'L'))
-                    property_set("debug.tf.enable", "logcat");
-                else if(arg && (arg[0] == '+'))
-                    property_set("debug.tf.enable", "1");
-                else if(!arg) {
-                    fd = open(NODE_STR(trace), O_WRONLY|O_TRUNC);
-                    close(fd);
-                    property_set("debug.tf.enable", "1");
-                }
-                else
-                    TFSCmdHint("start");
-
-                if(mService) {
-                    mService->TFSUpdate(START);
-                }
-                ret = 0;
-            }
-            else if(!strncmp(mBuf, "stop", 4)) {
-                //stop
-                property_set("debug.tf.enable", "0");
-                if(mService) {
-                    mService->TFSUpdate(STOP);
-                }
-                ret = 0;
-            }
-            else if(!strncmp(mBuf, "size", 4)) {
-                //size
-                arg = TFSGetCmdArg(arg+arglen, arglen);
-                if(arg) {
-                    arg[arglen] = '\0';
-                    fd = open(NODE_STR(buffer_size_kb), O_WRONLY|O_TRUNC);
-                    n = write(fd, arg, arglen);
-                    close(fd);
-                }
-                else {
-                    fd = open(NODE_STR(buffer_size_kb), O_RDONLY);
-                    n = read(fd, mBuf, MAX_BUFLEN);
-                    mBuf[n-1] = '\0';
-                    TFSTtyPrint(mBuf, n);
-                    close(fd);
-                }
-                ret = 0;
-            }
-            break;
-
-        case 'd':
-            if(arglen != 4 || strncmp(mBuf, "dump", 4)) {
-                break;
-            }
-
-            total = 0;
-
-            //dump [path]
-            arg = TFSGetCmdArg(arg+arglen, arglen);
-
-            if(arg && !strncmp("clear", arg, 5) && (arglen == 5)) {
-                fd = open(NODE_STR(trace), O_WRONLY|O_TRUNC);
-                close(fd);
-                ret = 0;
-                break;
-            }
-
-            if(NULL != arg) {
-                arg[arglen] = '\0';
-                if(mPath) {
-                    chdir(mPath);
-                }
-                fd2 = open(arg, O_WRONLY|O_CREAT|O_TRUNC,
-                           S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
-            }
-
-            fd = open(NODE_STR(trace), O_RDONLY);
-            while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
-                if(NULL == arg) {
-                    TFSTtyPrint(mBuf, n);
-                }
-                else {
-                    if(fd2 >= 0) {
-                        write(fd2, mBuf, n);
-                        total += n;
-                    }
-                    else {
-                        TFSTtyPrint("could not dump into file");
-                        break;
-                    }
-                }
-            }
-
-            if(fd2 >= 0) {
-                snprintf(mBuf, MAX_BUFLEN, "written %d bytes...\n", total);
-                TFSTtyPrint(mBuf);
-            }
-
-            close(fd);
-            close(fd2);
-            fd2 = -1;
-            ret = 0;
-            break;
-
-        case 'c':
-            if(!strncmp(mBuf, "cd", 2) && arglen==2 && mPath && cmds) {
-                arg = TFSGetCmdArg(arg+arglen, arglen);
-                if(arg) {
-                    arg[arglen] = '\0';
-                    snprintf(cmds, MAX_PATHLEN, "cd %s;cd %s;pwd", mPath, arg);
-                    fp = popen(cmds, "r");
-                    if(fp) {
-                        while ( fgets( mPath, MAX_PATHLEN, fp));
-                        pclose(fp);
-                    }
-                    int l = strlen(mPath);
-                    mPath[l-1] = '\0';
-                }
-                ret = 0;
-            }
-            break;
-
-        case 'p':
-            if(!strncmp(mBuf, "pwd", 3) && arglen==3 && mPath) {
-                TFSTtyPrint(mPath);
-                ret = 0;
-            }
-            break;
-
-        case '\n':
-            ret = 0;
-            break;
-
-        default:
-            break;
-        }
-
-        if(ret < 0 && mPath && cmds) {
-            snprintf(cmds, MAX_PATHLEN, "cd %s;%s", mPath, mBuf);
-            fp = popen(cmds, "r");
-            if(fp) {
-                while ( fgets( mBuf, MAX_BUFLEN, fp)) {
-                    TFSTtyPrint(mBuf);
-                }
-                pclose(fp);
-            }
-        }
-    }
-
-    if(cmds) {
-        free(cmds);
-        cmds = NULL;
+        ret = RunCmd(mBuf, arglen);
     }
 
     return 0;
 }
 
+int TFSShell::RunCmd(char *buf, int arglen) {
+    int ret = -1;
+    char *arg = NULL;
+    FILE *fp = NULL;
+    int n = 0, fd = -1, fd2 = -1;
+    int len = 0;
+    int total = 0;
+
+    arg = TFSGetCmdArg(buf, arglen);
+    switch(buf[0]) {
+    case  '#':
+        //comment, ignore
+        ret = 0;
+        break;
+
+    case 'h':
+    case 'H':
+        //help
+        if(arglen != 4 || strncmp(buf, "help", 4)) {
+            break;
+        }
+
+        TFSCmdHint("help");
+        ret = 0;
+        break;
+
+    case 'v':
+    case 'V':
+        TFSTtyPrint(TFS_VERSION);
+        ret = 0;
+        break;
+
+    case 'e':
+    case 'E':
+        //exit
+        if(arglen == 4 && !strncmp(buf, "exit", 4)) {
+            property_set("debug.tf.exit", "1");
+            mQuit = true;
+            ret = 0;
+        }
+        else if(arglen == 9 && !strncmp(buf, "eventtype", 9)) {
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            //eventtype <type>
+            if(arg) {
+                arg[arglen] = '\0';
+                property_set("debug.tf.eventtype", arg);
+            }
+            else {
+                property_get("debug.tf.eventtype", mBuf, "0");
+                TFSTtyPrint(mBuf, strlen(mBuf));
+            }
+            ret = 0;
+        }
+        break;
+
+    case 'k':
+        if(arglen != 8 || strncmp(buf, "kfilters", 4)) {
+            break;
+        }
+        //kfilters [available|list|set <filters>]
+        //filters := !|+<subsystem>:<event>
+        arg = TFSGetCmdArg(arg+arglen, arglen);
+        if(NULL == arg) {
+            //list
+            fd = open(NODE_STR(set_event), O_RDONLY);
+            while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
+                mBuf[n-1] = '\0';
+                TFSTtyPrint(mBuf, n);
+            }
+            close(fd);
+        }
+        else if(arg[0] == 'l') {
+            //list
+            fd = open(NODE_STR(set_event), O_RDONLY);
+            while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
+                mBuf[n-1] = '\0';
+                TFSTtyPrint(mBuf, n);
+            }
+            close(fd);
+        }
+        else if(arg[0] == 's') {
+            //set <filters>
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            if(arg) {
+                char filter[MAX_FILTERLEN];
+                if(arg[0] == '+') {
+                    arg = TFSGetCmdArg(arg+1, arglen);
+                    fd = open(NODE_STR(set_event), O_WRONLY);
+                }
+                else {
+                    fd = open(NODE_STR(set_event), O_WRONLY|O_TRUNC);
+                }
+                if(arg) {
+                    arg[arglen] = '\0';
+                }
+                arg = TFSGetFilterArg(arg, arglen);
+                while(arg) {
+                    strlcpy(filter, arg, TFS_MIN(arglen+1, MAX_FILTERLEN));
+                    n = write(fd, filter, arglen);
+                    arg = TFSGetFilterArg(arg+arglen, arglen);
+                }
+                close(fd);
+            }
+            else {
+                //empty filters
+                fd = open(NODE_STR(set_event), O_WRONLY|O_TRUNC);
+                close(fd);
+            }
+        }
+        else if(arg[0] == 'a') {
+            //available events
+            fd = open(NODE_STR(available_events), O_RDONLY);
+            while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
+                mBuf[n-1] = '\0';
+                TFSTtyPrint(mBuf, n);
+            }
+            close(fd);
+        }
+        else {
+            TFSCmdHint("kfilters");
+        }
+        ret = 0;
+        break;
+
+    case 'f':
+        if(arglen != 7 || strncmp(buf, "filters", 7)) {
+            break;
+        }
+        //filters [available|list|set <filters>]
+        arg = TFSGetCmdArg(arg+arglen, arglen);
+        if(NULL == arg) {
+            property_get("debug.tf.filters", mBuf, "");
+            TFSTtyPrint(mBuf, strlen(mBuf));
+        }
+        else if(arg[0] == 's' || arg[0] == 's') {
+            //ufilters [available|list|set <filters>]
+            //filters := !|+<subsystem>:<event>
+            bool bRemove = false;
+            arg = (char *) TFSGetCmdArg(arg+arglen, arglen);
+            if(arg) {
+                char filters[MAX_FILTERLEN];
+                char * str = NULL;
+                filters[0] = '\0';
+                if(arg[0] == '+') {
+                    arg = (char *)TFSGetCmdArg(arg+1, arglen);
+                    if(arg) {
+                        arg[arglen] = '\0';
+                    }
+                    property_get("debug.tf.filters", filters, NULL);
+                    arg = TFSGetFilterArg(arg, arglen);
+                    while(arg) {
+                        bRemove = false;
+                        arg[arglen] = '\0';
+                        if (arg[0] == '!') {
+                            arg++;
+                            arglen--;
+                            bRemove = true;
+                        }
+                        str = strstr(filters, arg);
+                        if (str) {
+                            if (bRemove) {
+                                if ((filters < str) && (*(str-1) == '%'))
+                                    strlcpy(str-1, str+arglen, MAX_FILTERLEN - (str - filters));
+                                else if(filters+strlen(filters) > str+arglen && *(str+arglen) == '%') {
+                                    strlcpy(str, str+arglen+1, MAX_FILTERLEN - (str - filters));
+                                }
+                                else
+                                    strlcpy(str, str+arglen, MAX_FILTERLEN);
+                            }
+                        }
+                        else if (!bRemove) {
+                            if (filters[0] != '\0')
+                                strlcat(filters, "%", MAX_FILTERLEN);
+                            strlcat(filters, arg, MAX_FILTERLEN);
+                        }
+                        arg = TFSGetFilterArg(arg+arglen+1, arglen);
+                    }
+                }
+                else {
+                    arg[arglen] = '\0';
+                    filters[0] = '\0';
+                    arg = TFSGetFilterArg(arg, arglen);
+                    while(arg) {
+                        arg[arglen] = '\0';
+                        if (arg[0] == '!') {
+                            arg++;
+                            arglen--;
+                        }
+                        else {
+                            str = strstr(filters, arg);
+                            if (!str) {
+                                if (filters[0] != '\0')
+                                    strlcat(filters, "%", MAX_FILTERLEN);
+                                strlcat(filters, arg, MAX_FILTERLEN);
+                            }
+                        }
+                        arg = TFSGetFilterArg(arg+arglen+1, arglen);
+                    }
+                }
+                property_set("debug.tf.filters", filters);
+            }
+            else {
+                property_set("debug.tf.filters", "$.$");
+            }
+        }
+        else if(arg[0] == 'l') {
+            property_get("debug.tf.filters", mBuf, "");
+            TFSTtyPrint(mBuf, strlen(mBuf));
+        }
+        else if(arg[0] == 'a') {
+            //not supported yet
+            property_get("debug.tf.filters", mBuf, "");
+            TFSTtyPrint(mBuf, strlen(mBuf));
+        }
+        else {
+            TFSCmdHint("filters");
+        }
+        ret = 0;
+        break;
+
+    case 's':
+        if(!strncmp(buf, "start", 5)) {
+            //start
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            if(arg && (arg[0] == 'l' || arg[0] == 'L'))
+                property_set("debug.tf.enable", "logcat");
+            else if(arg && (arg[0] == '+'))
+                property_set("debug.tf.enable", "1");
+            else if(!arg) {
+                fd = open(NODE_STR(trace), O_WRONLY|O_TRUNC);
+                close(fd);
+                property_set("debug.tf.enable", "1");
+            }
+            else
+                TFSCmdHint("start");
+
+            if(mService) {
+                mService->TFSUpdate(START);
+            }
+            ret = 0;
+        }
+        else if(!strncmp(buf, "stop", 4)) {
+            //stop
+            property_set("debug.tf.enable", "0");
+            if(mService) {
+                mService->TFSUpdate(STOP);
+            }
+            ret = 0;
+        }
+        else if(!strncmp(buf, "size", 4)) {
+            //size
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            if(arg) {
+                arg[arglen] = '\0';
+                fd = open(NODE_STR(buffer_size_kb), O_WRONLY|O_TRUNC);
+                n = write(fd, arg, arglen);
+                close(fd);
+            }
+            else {
+                fd = open(NODE_STR(buffer_size_kb), O_RDONLY);
+                n = read(fd, mBuf, MAX_BUFLEN);
+                mBuf[n-1] = '\0';
+                TFSTtyPrint(mBuf, n);
+                close(fd);
+            }
+            ret = 0;
+        }
+        if(!strncmp(buf, "sleep", 5)) {
+            //sleep
+            unsigned long time = 0;
+            int sec = 0;
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            if(arg) {
+                time = strtoul(arg, NULL, 10);
+                sec = time/1000;
+                time = time - sec * 1000;
+                TFSTtyPrint("sleeping...\n");
+                if(sec) {
+                    sleep(sec);
+                }
+                if(time) {
+                    usleep(time*1000);
+                }
+                TFSTtyPrint("done sleeping\n");
+            }
+            else
+                TFSCmdHint("sleep");
+
+            ret = 0;
+        }
+
+        break;
+
+    case 'd':
+        if(arglen != 4 || strncmp(buf, "dump", 4)) {
+            break;
+        }
+
+        total = 0;
+
+        //dump [path]
+        arg = TFSGetCmdArg(arg+arglen, arglen);
+
+        if(arg && !strncmp("clear", arg, 5) && (arglen == 5)) {
+            fd = open(NODE_STR(trace), O_WRONLY|O_TRUNC);
+            close(fd);
+            ret = 0;
+            break;
+        }
+
+        if(NULL != arg) {
+            arg[arglen] = '\0';
+            if(mPath) {
+                chdir(mPath);
+            }
+            fd2 = open(arg, O_WRONLY|O_CREAT|O_TRUNC,
+                       S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+        }
+
+        fd = open(NODE_STR(trace), O_RDONLY);
+        while ((n = read(fd, mBuf, MAX_BUFLEN)) > 0) {
+            if(NULL == arg) {
+                TFSTtyPrint(mBuf, n);
+            }
+            else {
+                if(fd2 >= 0) {
+                    write(fd2, mBuf, n);
+                    total += n;
+                }
+                else {
+                    TFSTtyPrint("could not dump into file");
+                    break;
+                }
+            }
+        }
+
+        if(fd2 >= 0) {
+            snprintf(mBuf, MAX_BUFLEN, "written %d bytes...\n", total);
+            TFSTtyPrint(mBuf);
+        }
+
+        close(fd);
+        close(fd2);
+        fd2 = -1;
+        ret = 0;
+        break;
+
+    case 'c':
+        if(!strncmp(buf, "cd", 2) && arglen==2 && mPath && mCmds) {
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            if(arg) {
+                arg[arglen] = '\0';
+                snprintf(mCmds, MAX_PATHLEN, "cd %s;cd %s;pwd", mPath, arg);
+                fp = popen(mCmds, "r");
+                if(fp) {
+                    while ( fgets( mPath, MAX_PATHLEN, fp));
+                    pclose(fp);
+                }
+                int l = strlen(mPath);
+                mPath[l-1] = '\0';
+            }
+            ret = 0;
+        }
+        break;
+
+    case 'p':
+        if(!strncmp(buf, "pwd", 3) && arglen==3 && mPath) {
+            TFSTtyPrint(mPath);
+            ret = 0;
+        }
+        if(arglen == 11 && !strncmp(buf, "probeperiod", 11)) {
+            arg = TFSGetCmdArg(arg+arglen, arglen);
+            if(arg) {
+                property_set("debug.tf.probeperiod", arg);
+            }
+            else {
+                property_get("debug.tf.probeperiod", mBuf, "");
+                TFSTtyPrint(mBuf, strlen(mBuf));
+            }
+            ret = 0;
+        }
+        break;
+
+    case '\n':
+        ret = 0;
+        break;
+
+    default:
+        break;
+    }
+
+    if(ret < 0 && mPath && mCmds) {
+        snprintf(mCmds, MAX_PATHLEN, "cd %s;%s", mPath, buf);
+        fp = popen(mCmds, "r");
+        if(fp) {
+            while ( fgets( buf, MAX_BUFLEN, fp)) {
+                TFSTtyPrint(buf);
+            }
+            pclose(fp);
+        }
+    }
+
+    return ret;
+}
 
 int TFSShell::TFSTtyPrint(const char *buf, int len) {
     int ret = 0;
@@ -559,6 +614,7 @@
     TFSCmdHint("kfilters");
     TFSCmdHint("start");
     TFSCmdHint("stop");
+    TFSCmdHint("sleep");
     TFSCmdHint("dump");
     TFSCmdHint("eventtype");
     TFSCmdHint("version");
@@ -611,6 +667,9 @@
             TFSTtyPrint("size [<size-in-kb>] : displays current buffer size\n"
                         "                    <size-in-kb> sets to this size\n\n");
         }
+        else if(buf[1] == 'l') {
+            TFSTtyPrint("sleep [<time-in-ms>] : sleeps for <time-in-ms>\n");
+        }
         break;
     case 'f':
         TFSTtyPrint("filters [set [<uspacefilters>]|available|list] : userspace filters\n"
diff --git a/src/TFSShell.h b/src/TFSShell.h
index d325d0f..779a272 100644
--- a/src/TFSShell.h
+++ b/src/TFSShell.h
@@ -35,7 +35,8 @@
     TFSShell();
     ~TFSShell();
 
-    int MainLoop(ITestFrameworkService *service);
+    int MainLoop(ITestFrameworkService *service, char *script = NULL);
+    int RunCmd(char *buf, int arglen);
     static int TFSTtyPrint(const char *buf, int len = 0);
 
 private:
@@ -49,8 +50,11 @@
     static const int MAX_PATHLEN = 1024;
     static const int MAX_BUFLEN = 8192;
 
+    bool mQuit;
+
     char *mPath;
     char *mBuf;
+    char *mCmds;
     ITestFrameworkService *mService;
 };
 
diff --git a/src/TestFramework.h b/src/TestFramework.h
index 0a99225..fd7115b 100644
--- a/src/TestFramework.h
+++ b/src/TestFramework.h
@@ -44,6 +44,14 @@
 
 using namespace android;
 
+#ifdef JB
+#define TF_LOGE(...) ALOGE(__VA_ARGS__)
+#define TF_LOGD(...) ALOGD(__VA_ARGS__)
+#else
+#define TF_LOGE(...) LOGE(__VA_ARGS__)
+#define TF_LOGD(...) LOGD(__VA_ARGS__)
+#endif
+
 enum {
     TF_WRITE_BUF = 1,
     TF_WRITE_FMT_MSG = 2,
diff --git a/src/TestFrameworkApi.cpp b/src/TestFrameworkApi.cpp
index 3797266..caca5f8 100644
--- a/src/TestFrameworkApi.cpp
+++ b/src/TestFrameworkApi.cpp
@@ -116,7 +116,9 @@
     int ret = 0;
 
     if (TfIsServiceRunning()) {
+        #ifdef TF_FEATURE_USE_BINDER
         BpTestFramework::Connect(mTfDispacther);
+        #endif
         if (mTfDispacther != 0) {
             mTfDispacther->DispatchTurnOn(TF_TESTFRAMEWORK);
         }
@@ -267,10 +269,13 @@
 
     //probe frequency set to 1, so lets not probe
     //any params whatever set initially they will be used
-    if (mProbeFreq <= 1) {
+    //let it update only initially..
+    if (mProbeFreq == 1) {
+        mProbeFreq--;
+    }
+    else if (mProbeFreq <= 0) {
         return mLogType;
     }
-
     time_now = ns2ms(systemTime());
     timeout = (time_now - tfTs >= mProbeFreq);
 
@@ -278,8 +283,9 @@
     //everytime, one in few milliseconds fetch data from tf service
     if (timeout) {
         if (TfIsServiceRunning()) {
+            #ifdef TF_FEATURE_USE_BINDER
             BpTestFramework::Connect(mTfDispacther);
-
+            #endif
             if (mTfDispacther != 0) {
                 mTfDispacther->DispatchGetInfo(mLogType, mEventType,
                                               mOpenInterval, mClosedInterval);
diff --git a/src/TestFrameworkCommon.cpp b/src/TestFrameworkCommon.cpp
index 14c182e..3be214c 100644
--- a/src/TestFrameworkCommon.cpp
+++ b/src/TestFrameworkCommon.cpp
@@ -58,7 +58,7 @@
 #define STR(x) _STR(x)
 
 TestFrameworkCommon::TestFrameworkCommon() {
-    LOGD("TestFrameworkCommon created");
+    TF_LOGD("TestFrameworkCommon created");
     mTfInitDone = false;
     mTfTracerFd = -1;
     mTfMarkerFd = -1;
@@ -111,9 +111,6 @@
         return -1;
     }
 
-    iov[0].iov_base = (void *) evArg;
-    iov[0].iov_len = strlen(evArg)+1;
-
     switch(evType)
     {
         case TF_EVENT_START:
@@ -138,12 +135,15 @@
             evid[0] = '\0';
     }
 
-    num = 2;
-    iov[1].iov_base = (void *) evid;
-    iov[1].iov_len = strlen(evid)+1;
+    num = 1;
+    //iov[0].iov_base = (void *) evArg;
+    //iov[0].iov_len = strlen(evArg)+1;
+    //iov[1].iov_base = (void *) evid;
+    //iov[1].iov_len = strlen(evid)+1;
 
     if (evArg) {
-        ret = writev(mTfMarkerFd, iov, num);
+        ret = ::write(mTfMarkerFd, evArg, strlen(evArg));
+        //ret = writev(mTfMarkerFd, iov, num);
     }
 
     return ret;
@@ -191,10 +191,10 @@
     if (mTfTracerFd >= 0) {
         lseek(mTfTracerFd, 0, SEEK_SET);
         write(mTfTracerFd, "1", 1);
-        LOGD("TfEnable");
+        TF_LOGD("TfEnable");
     }
     else {
-      LOGE("tf_turnon: could not open trace file");
+      TF_LOGE("tf_turnon: could not open trace file");
     }
 }
 
@@ -202,10 +202,10 @@
     if (mTfTracerFd >= 0) {
         lseek(mTfTracerFd, 0, SEEK_SET);
         write(mTfTracerFd, "0", 1);
-        LOGD("TfDisable");
+        TF_LOGD("TfDisable");
     }
     else {
-        LOGE("tf_turnoff: could not open trace file");
+        TF_LOGE("tf_turnoff: could not open trace file");
     }
 }
 
@@ -218,6 +218,11 @@
         }
     }
 
+    int pf = TfGetPropertyProbeFreq();
+    if ((1 != pf) || (0 != mProbeFreq)) {
+        mProbeFreq = pf;
+    }
+
     if (updatetype != SYNC && mTurnOnByApi) {
         //start, stop commands can close api turned on logging
         mTurnOnByApi = false;
@@ -437,7 +442,7 @@
         strlcat(path, "/tracing/trace_marker", sizeof(path));
         errno = 0;
         mTfMarkerFd = open(path, O_WRONLY);
-        LOGV("tf_marker opened=%d", mTfMarkerFd);
+        TF_LOGD("tf_marker opened=%d", mTfMarkerFd);
     }
 
     return mTfMarkerFd;
@@ -455,7 +460,7 @@
         strlcat(path, "/tracing/tracing_on", sizeof(path));
         errno = 0;
         mTfTracerFd = open(path, O_RDWR);
-        LOGV("tf_tracer opened=%d", mTfTracerFd);
+        TF_LOGD("tf_tracer opened=%d", mTfTracerFd);
     }
 
     return mTfTracerFd;
diff --git a/src/TestFrameworkHash.cpp b/src/TestFrameworkHash.cpp
index 5d944a5..6753d71 100644
--- a/src/TestFrameworkHash.cpp
+++ b/src/TestFrameworkHash.cpp
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <utils/Log.h>
 #include "TestFrameworkHash.h"
+#include "TestFramework.h"
 
 /* table load factor, i.e. how full can it get before we resize */
 //#define LOAD_NUMER  3       // 75%
@@ -208,7 +209,7 @@
             {
                 if (!resizeHash(tableSize * 2)) {
                     /* don't really have a way to indicate failure */
-                    LOGE("TF hash resize failure\n");
+                    TF_LOGE("TF hash resize failure\n");
                 }
             }
 
diff --git a/src/TestFrameworkService.cpp b/src/TestFrameworkService.cpp
index 01ed95c..d13c265 100644
--- a/src/TestFrameworkService.cpp
+++ b/src/TestFrameworkService.cpp
@@ -64,13 +64,21 @@
 
 ITestFrameworkService *TestFrameworkService::RunTestFrameworkService() {
     TestFrameworkService *service =  new TestFrameworkService();
+#ifdef TF_FEATURE_USE_BINDER
     defaultServiceManager()->addService(ITestFrameworkService::descriptor, service);
     ProcessState::self()->startThreadPool();
+#endif
     return (ITestFrameworkService *)service;
 }
 
+void TestFrameworkService::StopTestFrameworkService() {
+    //stub for now
+    return;
+}
+
+
 TestFrameworkService::TestFrameworkService() {
-    LOGD("TestFrameworkService created");
+    TF_LOGD("TestFrameworkService created");
     mNextConnId = 1;
     pthread_mutex_init(&mMutex, 0);
     TfInit();
@@ -78,7 +86,7 @@
 
 TestFrameworkService::~TestFrameworkService() {
     pthread_mutex_destroy(&mMutex);
-    LOGD("TestFrameworkService destroyed");
+    TF_LOGD("TestFrameworkService destroyed");
 }
 
 status_t TestFrameworkService::onTransact(uint32_t code,
@@ -86,6 +94,7 @@
                                           android::Parcel *reply,
                                           uint32_t flags) {
     status_t ret = android::NO_ERROR;
+#ifdef TF_FEATURE_USE_BINDER
     int evType = 0;
     int status = 0;
 
@@ -147,6 +156,7 @@
         }
         break;
     }
+#endif
 
     return ret;
 }
@@ -167,7 +177,7 @@
         }
     }
     else {
-      LOGE("TFS: mount debugfs failed, errno=%d", errno);
+      TF_LOGE("TFS: mount debugfs failed, errno=%d", errno);
     }
 
     if(ret < 0) {
@@ -182,7 +192,7 @@
                "write /sys/kernel/debug/tracing/tracing_on 0");
     }
     //testframework init, open tracer, marker, etc.
-    LOGD("tfhash: TFSInit");
+    TF_LOGD("tfhash: TFSInit");
     ret = TfInit();
 
     property_set("debug.tf.servicerunning", "1");
diff --git a/src/TestFrameworkService.h b/src/TestFrameworkService.h
index 3bb8a82..240b741 100644
--- a/src/TestFrameworkService.h
+++ b/src/TestFrameworkService.h
@@ -45,24 +45,34 @@
 #define TRACING_BASE "/sys/kernel/debug/tracing/"
 #define NODE_STR(x) TRACING_BASE#x
 
-class ITestFrameworkService: public IInterface
+class ITestFrameworkService
+                           #ifdef TF_FEATURE_USE_BINDER
+                           : public IInterface
+                           #endif
 {
 public:
     DECLARE_META_INTERFACE(TestFrameworkService)
 
     virtual int TFSInit() = 0;
     virtual bool TFSUpdate(int updatetype = SYNC) = 0;
+    virtual void StopTestFrameworkService() = 0;
 };
 
 class BnTestFrameworkService : public BnInterface<ITestFrameworkService>
 {
 };
 
-class TestFrameworkService : public BnTestFrameworkService,
+class TestFrameworkService :
+                             #ifdef TF_FEATURE_USE_BINDER
+                             public BnTestFrameworkService,
+                             #else
+                             public ITestFrameworkService,
+                             #endif
                              public TestFrameworkCommon
 {
 public:
     static ITestFrameworkService *RunTestFrameworkService();
+    void StopTestFrameworkService();
     TestFrameworkService();
     virtual ~TestFrameworkService();
 
diff --git a/src/TestFrameworkServiceMain.cpp b/src/TestFrameworkServiceMain.cpp
index 54c8fcb..4106487 100644
--- a/src/TestFrameworkServiceMain.cpp
+++ b/src/TestFrameworkServiceMain.cpp
@@ -30,12 +30,82 @@
 #define LOG_TAG "TestFrameworkServiceMain"
 
 #include <cutils/properties.h>
+#include <signal.h>
 #include "TestFrameworkService.h"
+#include "TestFramework.h"
 #include "TFSShell.h"
 
 static pthread_mutex_t mMutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t mCond = PTHREAD_COND_INITIALIZER;
 static pthread_t mThreadID;
+static volatile int recvedInterrupt = 0;
+
+class TFSCmdArgParser
+{
+public:
+    TFSCmdArgParser(int argc, char *argv[]);
+    ~TFSCmdArgParser();
+    const char *GetNext(int &len);
+
+private:
+    char **mArgv;
+    int mArgc;
+    int mArgLen;
+    int mArgIdx;
+    int mCmdBoundaryIdx;
+};
+
+TFSCmdArgParser::TFSCmdArgParser(int argc, char *argv[]) {
+    mArgv = argv;
+    mArgc = argc;
+    mArgIdx = 1;
+    mCmdBoundaryIdx = 0;
+    mArgLen = 0;
+}
+
+TFSCmdArgParser::~TFSCmdArgParser() {
+}
+
+const char *TFSCmdArgParser::GetNext(int &len) {
+    int j = 0;
+    int i = mArgIdx;
+    char *start = NULL;
+    len = 0;
+
+    do {
+        if (mArgIdx >= mArgc) {
+            start = NULL;
+            return start;
+        }
+
+        start = mArgv[i] + mCmdBoundaryIdx;
+
+        if (mArgv[i]) {
+            if (0 == mCmdBoundaryIdx)
+                mArgLen = strlen(mArgv[i]);
+            for(j=mCmdBoundaryIdx; j < mArgLen; j++) {
+                if ((mArgv[i][j] == ';') || (mArgv[i][j] == '@')) {
+                    len = j - mCmdBoundaryIdx;
+                    mCmdBoundaryIdx = j+1;
+                    mArgv[i][j] = '\0';
+                    break;
+                }
+            }
+        }
+
+        if (j >= mCmdBoundaryIdx) {
+            len = j - mCmdBoundaryIdx;
+            mCmdBoundaryIdx = 0;
+            mArgIdx++;
+        }
+    }while(0 == len);
+    return start;
+}
+
+static void ExitService(int sig) {
+    TFSShell::TFSTtyPrint("Caught signal...\n");
+    recvedInterrupt = 1;
+}
 
 void *TFSSync(void *s) {
     ITestFrameworkService *service;
@@ -54,8 +124,10 @@
     pthread_mutex_unlock(&mMutex);
 
     while(true) {
-        if (!service->TFSUpdate())
+        if (!service->TFSUpdate() || recvedInterrupt) {
+            TFSShell::TFSTtyPrint("Exit..\n");
             break;
+        }
         /*sleep 500ms*/
         usleep(500000);
     }
@@ -81,21 +153,59 @@
     return service;
 }
 
-int main(int argc, char **argv)
+int main(int argc, char *argv[])
 {
     TFSShell sh;
     ITestFrameworkService *service = NULL;
+    const char *buf = NULL;
+    int len = 0;
 
-    service = SpinService();
-    sh.MainLoop(service);
+    //catch the signals
+    signal(SIGINT, ExitService);
+    signal(SIGQUIT, ExitService);
 
-    pthread_mutex_destroy(&mMutex);
-    pthread_cond_destroy(&mCond);
+    TFSCmdArgParser argparser = TFSCmdArgParser(argc, argv);
+    buf = argparser.GetNext(len);
+    if(buf && len>0 && strstr(buf, ".tf")) {
+        ITestFrameworkService *service = NULL;
+        service = TestFrameworkService::RunTestFrameworkService();
+        service->TFSInit();
+        service->TFSUpdate();
+        TFSShell::TFSTtyPrint("Executing Script...\n");
+        sh.MainLoop(service, (char *)buf);
+        service->StopTestFrameworkService();
+    }
+    else if(buf) {
+        ITestFrameworkService *service = NULL;
+        service = TestFrameworkService::RunTestFrameworkService();
+        service->TFSInit();
+        service->TFSUpdate();
+        if (len > 0)
+            sh.RunCmd((char *)buf, len);
+        buf = argparser.GetNext(len);
+        while(buf) {
+            if (len > 0)
+                sh.RunCmd((char *)buf, len);
+            buf = argparser.GetNext(len);
+        }
+        service->StopTestFrameworkService();
+    }
+    else {
+        service = SpinService();
+        TFSShell::TFSTtyPrint("Interactive mode...");
+        sh.MainLoop(service);
 
-    void *dummy;
-    pthread_join(mThreadID, &dummy);
-    status_t err = (status_t) dummy;
+        service->StopTestFrameworkService();
+
+        pthread_mutex_destroy(&mMutex);
+        pthread_cond_destroy(&mCond);
+
+        void *dummy;
+        pthread_join(mThreadID, &dummy);
+        status_t err = (status_t) dummy;
+    }
 
     return 0;
 }
 
+