Tizen 2.0 Release
[adaptation/intel_mfld/sensor-plugins-mfld-blackbay.git] / src / test_programs / client.cpp
1 /* Medfield sensor plugins
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; version 2.1.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301USA
16  */
17
18 #include <sensor.h>
19 #include <sensor_accel.h>
20 #include <sensor_light.h>
21 #include <sensor_geomag.h>
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <glib.h>
27
28
29 #define LOGERR(fmt,args...) fprintf(stderr,fmt "\n",##args)
30 #define LOGENTRY(format,args...) LOGERR("%s " format,__func__,##args)
31
32 static int sf_handle;
33
34 static void sensor_callback(unsigned int type, sensor_event_data_t *event, void *data)
35 {
36      LOGENTRY("");
37      if (type == ACCELEROMETER_EVENT_ROTATION_CHECK) {
38           LOGERR("ACCELEROMETER_EVENT_ROTATION_CHECK");
39           int  cb_event_data = (*(int *)(event->event_data));
40           if (cb_event_data == ROTATION_LANDSCAPE_LEFT )
41                LOGERR("ROTATION_LANDSCAPE_LEFT\n");
42           else if (cb_event_data == ROTATION_LANDSCAPE_RIGHT)
43                LOGERR("ROTATION_LANDSCAPE_RIGHT\n");
44           else if (cb_event_data == ROTATION_PORTRAIT_TOP )
45                LOGERR("ROTATION_PORTRAIT_TOP\n");
46           else if (cb_event_data == ROTATION_PORTRAIT_BTM )
47                LOGERR("ROTATION_PORTRAIT_BTM\n");
48      }
49
50      else if (type == LIGHT_EVENT_CHANGE_LEVEL ) {
51           int  cb_event_data = (*(int *)(event->event_data));
52           LOGERR("LIGHT_EVENT_CHANGE_LEVEL %d",cb_event_data);
53      }
54
55      else if (type == PROXIMITY_EVENT_CHANGE_STATE) {
56           int  cb_event_data = (*(int *)(event->event_data));
57           LOGERR("PROXIMITY_EVENT_CHANGE_STATE %d",cb_event_data);
58      }
59      else {
60           sensor_data_t data;
61           memcpy(&data,event->event_data,event->event_data_size);
62           for (int i = 0; i < data.values_num; i++)
63                LOGERR("%d: %f",i,data.values[i]);
64      }
65
66
67 }
68
69 static int connect_sfsvc(sensor_type_t sensor, int data_event, int report_event)
70 {
71      int sf_state = -1;
72      int err = 0;
73
74      LOGERR("Here");
75      sensor_properties_t props;
76      sf_get_properties(sensor, &props);
77      LOGERR("Sensor name: %s", props.sensor_name);
78
79      sensor_data_t data;
80      //memset(&data,0,sizeof(data));
81
82      /* connect with sensor fw */
83      LOGERR("connect with sensor fw");
84      sf_handle = sf_connect(sensor);
85      if (sf_handle < 0) {
86           LOGERR("sensor attach fail");
87           return -1;
88      }
89      sf_state = sf_start(sf_handle, 0);
90      if (sf_state < 0) {
91           LOGERR("sensor attach fail");
92           sf_disconnect(sf_handle);
93           sf_handle = -1;
94           return -2;
95      }
96      err =  sf_start(sf_handle, 0);
97      sleep(1);
98      if (err == 0) {
99           err = sf_get_data(sf_handle,data_event, &data);
100           if (err == 0) {
101                LOGERR("sf_get_data values:", data.values[0]);
102                for (int i =0; i <data.values_num; i++)
103                     LOGERR("%d: %f\n",i, data.values[i]);
104                LOGERR("Values end");
105                err = sf_register_event(sf_handle,report_event,NULL,sensor_callback,NULL);
106                if (err)
107                     LOGERR("Error registering for event %d",err);
108           } else {
109                LOGERR("sf_get_data value fail", err);
110           }
111      } else {
112           LOGERR("sf_start failed %d", err);
113      }
114
115
116      return 0;
117 }
118
119 int main(int argc, char **argv)
120 {
121      GMainLoop *mainloop;
122      const char *errormsg = "Give sensor to listen (accel, light, proxi, compass, gyro)";
123      mainloop = g_main_loop_new (NULL, FALSE);
124      if (argc == 1) {
125           LOGERR("%s",errormsg);
126           return 1;
127      }
128
129      if (strcmp(argv[1],"accel") == 0) {
130           connect_sfsvc(ACCELEROMETER_SENSOR, ACCELEROMETER_BASE_DATA_SET, ACCELEROMETER_EVENT_ROTATION_CHECK );
131      }
132
133      else if (strcmp(argv[1],"light") ==  0)
134           connect_sfsvc(LIGHT_SENSOR, LIGHT_BASE_DATA_SET,LIGHT_EVENT_CHANGE_LEVEL );
135      else if (strcmp(argv[1],"proxi") ==  0)
136           connect_sfsvc(PROXIMITY_SENSOR, PROXIMITY_BASE_DATA_SET,PROXIMITY_EVENT_CHANGE_STATE );
137      else if (strcmp(argv[1],"compass") ==  0)
138           connect_sfsvc(GEOMAGNETIC_SENSOR, GEOMAGNETIC_RAW_DATA_SET,GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME );
139      else if (strcmp(argv[1],"gyro") ==  0)
140           connect_sfsvc(GYROSCOPE_SENSOR, GYRO_BASE_DATA_SET,GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME );
141      else {
142           LOGERR("%s",errormsg);
143           return 1;
144      }
145
146      g_main_loop_run(mainloop);
147      int err = sf_disconnect(sf_handle);
148      LOGERR("Disconnect error %d",err);
149      g_main_loop_unref(mainloop);
150 }