summaryrefslogtreecommitdiff
path: root/src/modules/eeze/sensor/fake/fake.c
blob: 231f367227c6357d291e521ddd89de186867cd48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <sys/time.h>

#include <Eina.h>
#include <Ecore.h>
#include <Eeze_Sensor.h>
#include "eeze_sensor_private.h"

/* This small Eeze_Sensor module is meant to be used as a test harness for
 * developing. It does not gather any real data from hardware sensors. It uses
 * fixed values for the data, but provides the correct timestamp value.
 */

static int _eeze_sensor_fake_log_dom = -1;

#ifdef ERR
#undef ERR
#endif
#define ERR(...)  EINA_LOG_DOM_ERR(_eeze_sensor_fake_log_dom, __VA_ARGS__)

Eeze_Sensor_Module *esensor_module;

Eina_Bool
fake_init(void)
{
   /* For the fake module we prepare a list with all potential sensors. Even if
    * we only have a small subset at the moment.
    */
   Eeze_Sensor_Type type;

   for (type = 0; type <= EEZE_SENSOR_TYPE_LAST; type++)
     {
        Eeze_Sensor_Obj *obj = calloc(1, sizeof(Eeze_Sensor_Obj));
        obj->type = type;
        esensor_module->sensor_list = eina_list_append(esensor_module->sensor_list, obj);
     }

   return EINA_TRUE;
}

/* We don't have anything to clear when we get unregistered from the core here.
 * This is different in other modules.
 */
Eina_Bool
fake_shutdown(void)
{
   return EINA_TRUE;
}

Eina_Bool
fake_read(Eeze_Sensor_Type sensor_type, Eeze_Sensor_Obj *lobj)
{
   Eeze_Sensor_Obj *obj = NULL;
   struct timeval tv;

   obj = eeze_sensor_obj_get(sensor_type);
   if (obj == NULL)
     {
        ERR("No matching sensor object found in list");
        return EINA_FALSE;
     }

   switch (sensor_type)
     {
      case EEZE_SENSOR_TYPE_ACCELEROMETER:
      case EEZE_SENSOR_TYPE_GRAVITY:
      case EEZE_SENSOR_TYPE_LINEAR_ACCELERATION:
      case EEZE_SENSOR_TYPE_DEVICE_ORIENTATION:
      case EEZE_SENSOR_TYPE_MAGNETIC:
      case EEZE_SENSOR_TYPE_ORIENTATION:
      case EEZE_SENSOR_TYPE_GYROSCOPE:
        /* This is only a test harness so we supply hardcoded values here */
        obj->accuracy = -1;
        obj->data[0] = 7;
        obj->data[1] = 23;
        obj->data[2] = 42;
        gettimeofday(&tv, NULL);
        obj->timestamp = ((tv.tv_sec * 1000000) + tv.tv_usec);
        break;

      case EEZE_SENSOR_TYPE_LIGHT:
      case EEZE_SENSOR_TYPE_PROXIMITY:
      case EEZE_SENSOR_TYPE_BAROMETER:
      case EEZE_SENSOR_TYPE_TEMPERATURE:
        obj->accuracy = -1;
        obj->data[0] = 7;
        gettimeofday(&tv, NULL);
        obj->timestamp = ((tv.tv_sec * 1000000) + tv.tv_usec);
        break;

      default:
        ERR("Not possible to read from this sensor type.");
        free(obj);
        return EINA_FALSE;
     }

   memcpy(lobj, obj, sizeof(Eeze_Sensor_Obj));
   free(obj);

   return EINA_TRUE;
}

Eina_Bool
fake_async_read(Eeze_Sensor_Type sensor_type, void *user_data EINA_UNUSED)
{
   Eeze_Sensor_Obj *obj = NULL;
   struct timeval tv;

   obj = eeze_sensor_obj_get(sensor_type);
   if (obj == NULL)
     {
        ERR("No matching sensor object found in list.");
        return EINA_FALSE;
     }

   /* Default values for sensor objects with three data points */
   obj->accuracy = -1;
   obj->data[0] = 7;
   obj->data[1] = 23;
   obj->data[2] = 42;
   gettimeofday(&tv, NULL);
   obj->timestamp = ((tv.tv_sec * 1000000) + tv.tv_usec);

   switch (sensor_type)
     {
      case EEZE_SENSOR_TYPE_ACCELEROMETER:
        ecore_event_add(EEZE_SENSOR_EVENT_ACCELEROMETER, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_GRAVITY:
        ecore_event_add(EEZE_SENSOR_EVENT_GRAVITY, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_LINEAR_ACCELERATION:
        ecore_event_add(EEZE_SENSOR_EVENT_LINEAR_ACCELERATION, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_DEVICE_ORIENTATION:
        ecore_event_add(EEZE_SENSOR_EVENT_DEVICE_ORIENTATION, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_MAGNETIC:
        ecore_event_add(EEZE_SENSOR_EVENT_MAGNETIC, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_ORIENTATION:
        ecore_event_add(EEZE_SENSOR_EVENT_ORIENTATION, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_GYROSCOPE:
        ecore_event_add(EEZE_SENSOR_EVENT_GYROSCOPE, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_LIGHT:
        /* Reset values that are not used for sensor object with one data point */
        obj->data[1] = 0;
        obj->data[2] = 0;
        ecore_event_add(EEZE_SENSOR_EVENT_LIGHT, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_PROXIMITY:
        obj->data[1] = 0;
        obj->data[2] = 0;
        ecore_event_add(EEZE_SENSOR_EVENT_PROXIMITY, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_BAROMETER:
        obj->data[1] = 0;
        obj->data[2] = 0;
        ecore_event_add(EEZE_SENSOR_EVENT_BAROMETER, obj, NULL, NULL);
        break;
      case EEZE_SENSOR_TYPE_TEMPERATURE:
        obj->data[1] = 0;
        obj->data[2] = 0;
        ecore_event_add(EEZE_SENSOR_EVENT_TEMPERATURE, obj, NULL, NULL);
        break;

      default:
        ERR("Not possible to read from this sensor type.");
        free(obj);
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

/* This function gets called when the module is loaded from the disk. Its the
 * entry point to anything in this module. After setting ourself up we register
 * into the core of eeze sensor to make our functionality available.
 */
Eina_Bool
sensor_fake_init(void)
{

   _eeze_sensor_fake_log_dom = eina_log_domain_register("eeze_sensor_fake", EINA_COLOR_BLUE);
   if (_eeze_sensor_fake_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register 'eeze_sensor_fake' log domain.");
        return EINA_FALSE;
     }

   /* Check to avoid multi-init */
   if (esensor_module) return EINA_FALSE;

   /* Set module function pointer to allow calls into the module */
   esensor_module = calloc(1, sizeof(Eeze_Sensor_Module));
   if (!esensor_module) return EINA_FALSE;

   /* Setup our function pointers to allow the core accessing this modules
    * functions
    */
   esensor_module->init = fake_init;
   esensor_module->shutdown = fake_shutdown;
   esensor_module->read = fake_read;
   esensor_module->async_read = fake_async_read;

   if (!eeze_sensor_module_register("fake", esensor_module))
     {
        ERR("Failed to register fake module.");
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

/* Cleanup when the module gets unloaded. Unregister ourself from the core to
 * avoid calls into a not loaded module.
 */
void
sensor_fake_shutdown(void)
{
   Eeze_Sensor_Obj *sens;

   eeze_sensor_module_unregister("fake");
   EINA_LIST_FREE(esensor_module->sensor_list, sens)
      free(sens);

   free(esensor_module);
   esensor_module = NULL;

   eina_log_domain_unregister(_eeze_sensor_fake_log_dom);
   _eeze_sensor_fake_log_dom = -1;
}

EINA_MODULE_INIT(sensor_fake_init);
EINA_MODULE_SHUTDOWN(sensor_fake_shutdown);