Adding polling based support for testing proximity sensor 70/32670/4
authorVibhor Gaur <vibhor.gaur@samsung.com>
Mon, 22 Dec 2014 12:45:52 +0000 (18:15 +0530)
committerVibhor Gaur <vibhor.gaur@samsung.com>
Mon, 22 Dec 2014 12:46:04 +0000 (18:16 +0530)
Change-Id: I0a5c82eb75a615348a348f56bf9254bc0d4f17c9

test/src/proxi.c

index 9ac965f..70185bd 100644 (file)
  * limitations under the License.
  *
  */
-
 #include <time.h>
 #include <glib.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <sensor_internal.h>
 #include <stdbool.h>
-#include <string.h>
+#include <sensor_common.h>
+#include <unistd.h>
 
 static GMainLoop *mainloop;
 
@@ -35,11 +35,17 @@ void callback(unsigned int event_type, sensor_event_data_t *event, void *user_da
 
 void printformat()
 {
-       printf("Usage : ./proxi <event> <interval>(optional)\n\n");
-       printf("event:\n");
-       printf("EVENT_CHANGE_STATE\n");
-       printf("EVENT_STATE_REPORT_ON_TIME\n");
-       printf("EVENT_DISTANCE_DATA_REPORT_ON_TIME\n");
+       printf("Usage : ./proxi <mode>(optional) <event> <interval>(optional)\n\n");
+
+       printf("mode:");
+       printf("[-p]\n");
+       printf("p is for polling based,default mode is event driven\n");
+
+       printf("event:");
+       printf("[EVENT_CHANGE_STATE] ");
+       printf("[EVENT_STATE_REPORT_ON_TIME] ");
+       printf("[EVENT_DISTANCE_DATA_REPORT_ON_TIME]\n");
+
        printf("interval:\n");
        printf("The time interval should be entered based on the sampling frequency supported by proximity driver on the device in ms.If no value for sensor is entered default value by the driver will be used.\n");
 }
@@ -48,68 +54,118 @@ int main(int argc,char **argv)
 {
        int result, handle, start_handle, stop_handle;
        unsigned int event;
-
        mainloop = g_main_loop_new(NULL, FALSE);
-       sensor_type_t type = PROXIMITY_SENSOR;
        event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t));
        event_condition->cond_op = CONDITION_EQUAL;
-       event_condition->cond_value1 = 100;
+       sensor_type_t type = PROXIMITY_SENSOR;
 
-       if (argc != 2 && argc != 3) {
+       if (argc != 2 && argc != 3 && argc!=4) {
                printformat();
                free(event_condition);
                return 0;
        }
 
-       if (strcmp(argv[1], "EVENT_CHANGE_STATE") == 0) {
-               event = PROXIMITY_EVENT_CHANGE_STATE;
-       }
-       else if (strcmp(argv[1], "EVENT_STATE_REPORT_ON_TIME") == 0) {
-               event = PROXIMITY_EVENT_STATE_REPORT_ON_TIME;
-       }
-       else if (strcmp(argv[1], "EVENT_DISTANCE_DATA_REPORT_ON_TIME") == 0) {
-               event = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
-       }
-       else {
-               printformat();
-               free(event_condition);
-               return 0;
+       else if (argc>=3 && strcmp(argv[1], "-p") == 0) {
+               printf("Polling based\n");
+
+               if (strcmp(argv[2], "EVENT_CHANGE_STATE") == 0) {
+                       event = PROXIMITY_BASE_DATA_SET;
+               }
+               else if (strcmp(argv[2], "EVENT_STATE_REPORT_ON_TIME") == 0) {
+                       event = PROXIMITY_DISTANCE_DATA_SET;
+               }
+               else if (strcmp(argv[2], "EVENT_DISTANCE_DATA_REPORT_ON_TIME") == 0) {
+                       event = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
+               }
+               else {
+                       printformat();
+                       free(event_condition);
+                       return 0;
+               }
+
+               handle = sf_connect(type);
+               result = sf_start(handle, 1);
+
+               if (result < 0) {
+                       printf("Can't start proximity SENSOR\n");
+                       printf("Error\n\n\n\n");
+                       return -1;
+               }
+
+               sensor_data_t data;
+
+               while(1) {
+                       result = sf_get_data(handle, event , &data);
+                       printf("Proximity [%6.6f]\n\n", data.values[0]);
+                       usleep(100000);
+               }
+
+               result = sf_disconnect(handle);
+
+               if (result < 0) {
+                       printf("Can't disconnect proximity sensor\n");
+                       printf("Error\n\n\n\n");
+                       return -1;
+               }
        }
 
-       if (argc == 3)
-               event_condition->cond_value1 = atof(argv[2]);
+       else if (argc == 2 || argc ==3) {
+               printf("Event based\n");
+
+               if (strcmp(argv[1], "EVENT_CHANGE_STATE") == 0) {
+                       event = PROXIMITY_EVENT_CHANGE_STATE;
+               }
+               else if (strcmp(argv[1], "EVENT_STATE_REPORT_ON_TIME") == 0) {
+                       event = PROXIMITY_EVENT_STATE_REPORT_ON_TIME;
+               }
+               else if (strcmp(argv[1], "EVENT_DISTANCE_DATA_REPORT_ON_TIME") == 0) {
+                       event = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
+               }
+               else {
+                       printformat();
+                       free(event_condition);
+                       return 0;
+               }
+
+               event_condition->cond_value1 = 100;
+               if (argc == 3)
+                       event_condition->cond_value1 = atof(argv[2]);
+
+               handle = sf_connect(type);
+               result = sf_register_event(handle, event, event_condition, callback, NULL);
+
+               if (result < 0)
+                       printf("Can't register proximity\n");
+
+               start_handle = sf_start(handle,0);
+
+               if (start_handle < 0) {
+                       printf("Error\n\n\n\n");
+                       sf_unregister_event(handle, event);
+                       sf_disconnect(handle);
+                       return -1;
+               }
+
+               g_main_loop_run(mainloop);
+               g_main_loop_unref(mainloop);
 
-       handle = sf_connect(type);
-       result = sf_register_event(handle, event, event_condition, callback, NULL);
+               sf_unregister_event(handle, event);
 
-       if (result < 0)
-               printf("Can't register proximity sensor\n");
+               stop_handle = sf_stop(handle);
 
-       start_handle = sf_start(handle,0);
+               if (stop_handle < 0) {
+                       printf("Error\n\n");
+                       return -1;
+               }
 
-       if (start_handle < 0) {
-               printf("Error\n\n\n\n");
-               sf_unregister_event(handle, event);
                sf_disconnect(handle);
-               return -1;
+               free(event_condition);
        }
 
-       g_main_loop_run(mainloop);
-       g_main_loop_unref(mainloop);
-
-       sf_unregister_event(handle, event);
-
-       stop_handle = sf_stop(handle);
-
-       if (stop_handle < 0) {
-               printf("Failed to stop proximity sensor\n\n");
-               return -1;
+       else {
+               printformat();
        }
 
-       sf_disconnect(handle);
-
-       free(event_condition);
-
        return 0;
 }